Factor out the shared parts of the agent action manager setup.
[kdepim.git] / kalarm / alarmlistdelegate.cpp
blob03921aeef9173d89470eb2b32547acd585037c8a
1 /*
2 * alarmlistdelegate.cpp - handles editing and display of alarm list
3 * Program: kalarm
4 * Copyright © 2007-2011 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kalarm.h"
22 #include "alarmlistdelegate.moc"
24 #ifdef USE_AKONADI
25 #include "akonadimodel.h"
26 #define ITEM_LIST_MODEL AlarmListModel
27 #else
28 #include "eventlistmodel.h"
29 #define ITEM_LIST_MODEL EventListModel
30 #endif
31 #include "functions.h"
33 #include <kalarmcal/kacalendar.h>
35 #include <kcolorscheme.h>
36 #include <kdebug.h>
38 #include <QAbstractProxyModel>
39 #include <QMouseEvent>
40 #include <QApplication>
43 void AlarmListDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
45 QStyleOptionViewItem opt = option;
46 if (index.isValid())
48 if (opt.state & QStyle::State_Selected
49 #ifdef USE_AKONADI
50 && !index.data(AkonadiModel::EnabledRole).toBool())
51 #else
52 && !index.data(EventListModel::EnabledRole).toBool())
53 #endif
55 // Make the text colour for selected disabled alarms
56 // distinguishable from enabled alarms.
57 KColorScheme::adjustForeground(opt.palette, KColorScheme::InactiveText, QPalette::HighlightedText, KColorScheme::Selection);
59 switch (index.column())
61 case ITEM_LIST_MODEL::TimeColumn:
63 QString str = index.data(Qt::DisplayRole).toString();
64 // Need to pad out spacing to align times without leading zeroes
65 int i = str.indexOf(" ~"); // look for indicator that leading zeroes are omitted
66 if (i >= 0)
68 QVariant value;
69 value = index.data(Qt::ForegroundRole);
70 if (value.isValid())
71 opt.palette.setColor(QPalette::Text, value.value<QColor>());
72 int digitWidth = opt.fontMetrics.width("0");
73 QString date = str.left(i + 1);
74 int w = opt.fontMetrics.width(date) + digitWidth;
75 drawDisplay(painter, opt, opt.rect, date);
76 QRect rect(opt.rect);
77 rect.setLeft(rect.left() + w);
78 drawDisplay(painter, opt, rect, str.mid(i + 2));
79 return;
81 break;
83 case ITEM_LIST_MODEL::ColourColumn:
85 #ifdef USE_AKONADI
86 const KAEvent event = static_cast<const ItemListModel*>(index.model())->event(index);
87 if (event.isValid() && event.commandError() != KAEvent::CMD_NO_ERROR)
88 #else
89 const KAEvent* event = static_cast<const EventListFilterModel*>(index.model())->event(index);
90 if (event && event->commandError() != KAEvent::CMD_NO_ERROR)
91 #endif
93 opt.font.setBold(true);
94 opt.font.setStyleHint(QFont::Serif);
95 opt.font.setPixelSize(opt.rect.height() - 2);
97 break;
99 default:
100 break;
103 QItemDelegate::paint(painter, opt, index);
106 QSize AlarmListDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
108 if (index.isValid())
110 switch (index.column())
112 case ITEM_LIST_MODEL::TimeColumn:
114 int h = option.fontMetrics.lineSpacing();
115 const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1;
116 int w = 2 * textMargin;
117 QString str = index.data(Qt::DisplayRole).toString();
118 // Need to pad out spacing to align times without leading zeroes
119 int i = str.indexOf(" ~"); // look for indicator that leading zeroes are omitted
120 if (i >= 0)
122 int digitWidth = option.fontMetrics.width("0");
123 QString date = str.left(i + 1);
124 w += option.fontMetrics.width(date) + digitWidth + option.fontMetrics.width(str.mid(i + 2));;
126 else
127 w += option.fontMetrics.width(str);
128 return QSize(w, h);
130 case ITEM_LIST_MODEL::ColourColumn:
132 int h = option.fontMetrics.lineSpacing();
133 return QSize(h * 3 / 4, h);
137 return QItemDelegate::sizeHint(option, index);
140 void AlarmListDelegate::edit(KAEvent* event, EventListView* view)
142 KAlarm::editAlarm(event, static_cast<AlarmListView*>(view)); // edit alarm (view-only mode if archived or read-only)
145 // vim: et sw=4: