2 * alarmlistdelegate.cpp - handles editing and display of alarm list
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.
22 #include "alarmlistdelegate.moc"
25 #include "akonadimodel.h"
26 #define ITEM_LIST_MODEL AlarmListModel
28 #include "eventlistmodel.h"
29 #define ITEM_LIST_MODEL EventListModel
31 #include "functions.h"
33 #include <kalarmcal/kacalendar.h>
35 #include <kcolorscheme.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
;
48 if (opt
.state
& QStyle::State_Selected
50 && !index
.data(AkonadiModel::EnabledRole
).toBool())
52 && !index
.data(EventListModel::EnabledRole
).toBool())
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
69 value
= index
.data(Qt::ForegroundRole
);
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
);
77 rect
.setLeft(rect
.left() + w
);
78 drawDisplay(painter
, opt
, rect
, str
.mid(i
+ 2));
83 case ITEM_LIST_MODEL::ColourColumn
:
86 const KAEvent event
= static_cast<const ItemListModel
*>(index
.model())->event(index
);
87 if (event
.isValid() && event
.commandError() != KAEvent::CMD_NO_ERROR
)
89 const KAEvent
* event
= static_cast<const EventListFilterModel
*>(index
.model())->event(index
);
90 if (event
&& event
->commandError() != KAEvent::CMD_NO_ERROR
)
93 opt
.font
.setBold(true);
94 opt
.font
.setStyleHint(QFont::Serif
);
95 opt
.font
.setPixelSize(opt
.rect
.height() - 2);
103 QItemDelegate::paint(painter
, opt
, index
);
106 QSize
AlarmListDelegate::sizeHint(const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const
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
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));;
127 w
+= option
.fontMetrics
.width(str
);
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)