2
* Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License version 2 (or,
6
* at your option, any later version) as published by the Free Software
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details
14
* You should have received a copy of the GNU Library General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
#include "kcategorizeditemsviewdelegate_p.h"
26
#include <KIconLoader>
28
#include "kcategorizeditemsview_p.h"
30
#define FAV_ICON_SIZE 24
31
#define EMBLEM_ICON_SIZE 16
32
#define UNIVERSAL_PADDING 6
33
#define FADE_LENGTH 32
34
#define MAIN_ICON_SIZE 48
35
#define DROPDOWN_PADDING 2
36
#define DROPDOWN_SEPARATOR_HEIGHT 32
38
KCategorizedItemsViewDelegate::KCategorizedItemsViewDelegate(QObject * parent)
39
: QItemDelegate(parent), m_favoriteIcon("bookmark"),
40
m_favoriteAddIcon("edit-add"), m_favoriteRemoveIcon("edit-delete"),
41
m_onFavoriteIconItem(NULL)
43
m_parent = (KCategorizedItemsView *) parent;
46
void KCategorizedItemsViewDelegate::paint(QPainter *painter,
47
const QStyleOptionViewItem &option, const QModelIndex &index) const
50
// Preparing needed data for painting
51
int left = option.rect.left();
52
int top = option.rect.top();
53
int width = option.rect.width();
54
int height = option.rect.height();
55
bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
57
QIcon::Mode iconMode = QIcon::Normal;
59
QColor backgroundColor = (option.state.testFlag(QStyle::State_Selected))?
60
option.palette.color(QPalette::Highlight):option.palette.color(QPalette::Base);
61
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
62
option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
64
KCategorizedItemsViewModels::AbstractItem * item =
65
getItemByProxyIndex(index);
69
painter->fillRect(option.rect, QBrush(backgroundColor));
71
if (index.column() == 0) {
72
// Painting main column
73
QStyleOptionViewItem local_option_title(option);
74
QStyleOptionViewItem local_option_normal(option);
76
local_option_title.font.setBold(true);
77
local_option_title.font.setPointSize(local_option_title.font.pointSize() + 2);
79
QLinearGradient gradient;
81
QString title = item->name();
82
QString description = item->description();
87
int textInner = 2 * UNIVERSAL_PADDING + MAIN_ICON_SIZE;
89
painter->setPen(foregroundColor);
90
painter->setFont(local_option_title.font);
92
left + (leftToRight ? textInner : 0),
93
top + UNIVERSAL_PADDING,
94
width - textInner, MAIN_ICON_SIZE / 2,
95
Qt::AlignBottom | Qt::AlignLeft, title);
96
painter->setFont(local_option_normal.font);
98
left + (leftToRight ? textInner : 0),
99
top + UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2,
100
width - textInner, MAIN_ICON_SIZE / 2,
101
Qt::AlignTop | Qt::AlignLeft, description);
104
item->icon().paint(painter,
105
leftToRight ? left + UNIVERSAL_PADDING : left + width - UNIVERSAL_PADDING - MAIN_ICON_SIZE,
106
top + UNIVERSAL_PADDING,
107
MAIN_ICON_SIZE, MAIN_ICON_SIZE, Qt::AlignCenter, iconMode);
109
// Counting the number of emblems for this item
111
QPair < Filter, QIcon * > emblem;
112
foreach (emblem, m_parent->m_emblems) {
113
if (item->passesFiltering(emblem.first)) ++emblemCount;
116
// Gradient part of the background - fading of the text at the end
118
gradient = QLinearGradient(left + width - UNIVERSAL_PADDING - FADE_LENGTH, 0,
119
left + width - UNIVERSAL_PADDING, 0);
120
gradient.setColorAt(1, backgroundColor);
121
backgroundColor.setAlpha(0);
122
gradient.setColorAt(0, backgroundColor);
124
gradient = QLinearGradient(left + UNIVERSAL_PADDING, 0,
125
left + UNIVERSAL_PADDING + FADE_LENGTH, 0);
126
gradient.setColorAt(0, backgroundColor);
127
backgroundColor.setAlpha(0);
128
gradient.setColorAt(1, backgroundColor);
132
QRect paintRect = option.rect;
133
painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
134
painter->fillRect(paintRect, gradient);
137
gradient.setStart(left + width
138
- emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) - FADE_LENGTH, 0);
139
gradient.setFinalStop(left + width
140
- emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE), 0);
142
gradient.setStart(left + UNIVERSAL_PADDING
143
+ emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE), 0);
144
gradient.setFinalStop(left + UNIVERSAL_PADDING
145
+ emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) + FADE_LENGTH, 0);
147
paintRect.setHeight(UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2);
148
painter->fillRect(paintRect, gradient);
151
int emblemLeft = leftToRight ? (left + width - EMBLEM_ICON_SIZE) : left; // - FAV_ICON_SIZE - 2 * UNIVERSAL_PADDING
152
foreach (emblem, m_parent->m_emblems) {
153
if (item->passesFiltering(emblem.first)) {
154
emblem.second->paint(painter,
155
emblemLeft, top + UNIVERSAL_PADDING,
156
EMBLEM_ICON_SIZE, EMBLEM_ICON_SIZE, Qt::AlignCenter, iconMode);
158
emblemLeft -= UNIVERSAL_PADDING + EMBLEM_ICON_SIZE;
160
emblemLeft += UNIVERSAL_PADDING + EMBLEM_ICON_SIZE;
166
// Painting favorite icon column
168
KCategorizedItemsViewModels::AbstractItem * item =
169
getItemByProxyIndex(index);
171
if (! (option.state & QStyle::State_MouseOver) && m_onFavoriteIconItem == item)
172
m_onFavoriteIconItem = NULL;
174
QIcon::Mode iconMode = QIcon::Normal;
175
if (!item->isFavorite()) {
176
iconMode = QIcon::Disabled;
177
} else if (option.state & QStyle::State_MouseOver) {
178
iconMode = QIcon::Active;
181
m_favoriteIcon.paint(painter,
182
left + width - FAV_ICON_SIZE - UNIVERSAL_PADDING, top + UNIVERSAL_PADDING,
183
FAV_ICON_SIZE, FAV_ICON_SIZE, Qt::AlignCenter, iconMode);
185
const KIcon * icon = (item->isFavorite())? & m_favoriteRemoveIcon : & m_favoriteAddIcon;
187
if ((option.state & QStyle::State_MouseOver) && (m_onFavoriteIconItem != item))
189
left + width - EMBLEM_ICON_SIZE - UNIVERSAL_PADDING, top + UNIVERSAL_PADDING,
190
EMBLEM_ICON_SIZE, EMBLEM_ICON_SIZE, Qt::AlignCenter, iconMode);
195
backgroundColor = option.palette.color(QPalette::Highlight);
196
backgroundColor.setAlpha(100);
197
painter->setPen(backgroundColor);
198
painter->drawLine(left, top + height - 1, left + width, top + height - 1);
202
bool KCategorizedItemsViewDelegate::editorEvent(QEvent *event,
203
QAbstractItemModel *model,
204
const QStyleOptionViewItem &option,
205
const QModelIndex &index)
207
if (index.column() == 1 && event->type() == QEvent::MouseButtonPress) {
208
(m_onFavoriteIconItem = getItemByProxyIndex(index))
209
->setFavorite(!getItemByProxyIndex(index)->isFavorite());
214
return QItemDelegate::editorEvent(event, model, option, index);
217
QSize KCategorizedItemsViewDelegate::sizeHint(const QStyleOptionViewItem &option,
218
const QModelIndex &index ) const
224
int width = (index.column() == 0) ? 0 : FAV_ICON_SIZE;
225
return QSize(width, MAIN_ICON_SIZE + 2 * UNIVERSAL_PADDING);
228
int KCategorizedItemsViewDelegate::columnWidth (int column, int viewWidth) const {
230
return FAV_ICON_SIZE + 2 * UNIVERSAL_PADDING;
231
} else return viewWidth - columnWidth(1, viewWidth);
235
KCategorizedItemsViewModels::AbstractItem * KCategorizedItemsViewDelegate::getItemByProxyIndex(const QModelIndex & index) const {
236
return (AbstractItem *) m_parent->m_modelItems->itemFromIndex(
237
m_parent->m_modelFilterItems->mapToSource(index)
241
// KCategorizedItemsViewFilterDelegate
244
KCategorizedItemsViewFilterDelegate::KCategorizedItemsViewFilterDelegate(QObject *parent)
245
: QItemDelegate(parent) {
246
kDebug() << "KCategorizedItemsViewFilterDelegate(QObject *parent)\n";
250
void KCategorizedItemsViewFilterDelegate::paint(QPainter *painter,
251
const QStyleOptionViewItem &option, const QModelIndex &index) const
253
if (index.flags() & Qt::ItemIsEnabled) {
254
QItemDelegate::paint(painter, option, index);
256
QStyleOptionViewItem separatorOption(option);
257
int height = QItemDelegate::sizeHint(option, index).height() + 2 * DROPDOWN_PADDING;
259
separatorOption.state &= ~(QStyle::State_Selected
260
| QStyle::State_MouseOver | QStyle::State_HasFocus);
261
separatorOption.rect.setTop(separatorOption.rect.top() + separatorOption.rect.height() - height);
262
separatorOption.rect.setHeight(height);
263
QItemDelegate::paint(painter, separatorOption, index);
266
option.rect.top() + 1,
267
option.rect.left() + option.rect.width(),
268
option.rect.top() + 1);*/
272
QSize KCategorizedItemsViewFilterDelegate::sizeHint(const QStyleOptionViewItem &option,
273
const QModelIndex &index ) const
275
QSize size = QItemDelegate::sizeHint(option, index);
276
if (index.flags() & Qt::ItemIsEnabled) {
277
size.setHeight(size.height() + 2 * DROPDOWN_PADDING);
279
size.setHeight(DROPDOWN_SEPARATOR_HEIGHT);
284
#include "kcategorizeditemsviewdelegate_p.moc"