2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "dolphincategorydrawer.h"
27 #include <kiconloader.h>
28 #include <kcategorizedsortfilterproxymodel.h>
29 #include <qimageblitz.h>
32 #include <config-nepomuk.h>
34 #include <nepomuk/global.h>
35 #include <nepomuk/resource.h>
36 #include <nepomuk/tag.h>
39 #include "dolphinview.h"
40 #include "dolphinmodel.h"
42 DolphinCategoryDrawer::DolphinCategoryDrawer()
46 DolphinCategoryDrawer::~DolphinCategoryDrawer()
50 void DolphinCategoryDrawer::drawCategory(const QModelIndex
&index
, int sortRole
,
51 const QStyleOption
&option
, QPainter
*painter
) const
53 QRect starRect
= option
.rect
;
55 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
56 QVariant categoryVariant
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
);
58 if (!categoryVariant
.isValid())
63 const QString category
= categoryVariant
.toString();
67 if (option
.state
& QStyle::State_Selected
)
69 color
= option
.palette
.color(QPalette::HighlightedText
);
73 color
= option
.palette
.color(QPalette::Text
);
77 painter
->setRenderHint(QPainter::Antialiasing
);
79 QStyleOptionButton opt
;
81 opt
.rect
= option
.rect
;
82 opt
.palette
= option
.palette
;
83 opt
.direction
= option
.direction
;
86 if (option
.state
& QStyle::State_Selected
)
88 QColor selected
= option
.palette
.color(QPalette::Highlight
);
90 QLinearGradient
gradient(option
.rect
.topLeft(),
91 option
.rect
.bottomRight());
92 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
94 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
95 : 0, Qt::transparent
);
97 painter
->fillRect(option
.rect
, gradient
);
99 else if (option
.state
& QStyle::State_MouseOver
)
101 QColor hover
= option
.palette
.color(QPalette::Highlight
).light();
104 QLinearGradient
gradient(option
.rect
.topLeft(),
105 option
.rect
.bottomRight());
106 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
108 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
109 : 0, Qt::transparent
);
111 painter
->fillRect(option
.rect
, gradient
);
114 QFont painterFont
= painter
->font();
115 painterFont
.setWeight(QFont::Bold
);
116 QFontMetrics
metrics(painterFont
);
117 painter
->setFont(painterFont
);
120 path
.addRect(option
.rect
.left(),
121 option
.rect
.bottom() - 2,
125 QLinearGradient
gradient(option
.rect
.topLeft(),
126 option
.rect
.bottomRight());
127 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
129 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
130 : 0, Qt::transparent
);
132 painter
->setBrush(gradient
);
133 painter
->fillPath(path
, gradient
);
135 if (option
.direction
== Qt::LeftToRight
)
137 opt
.rect
.setLeft(opt
.rect
.left() + (iconSize
/ 4));
138 starRect
.setLeft(starRect
.left() + (iconSize
/ 4));
139 starRect
.setRight(starRect
.right() + (iconSize
/ 4));
143 opt
.rect
.setRight(opt
.rect
.width() - (iconSize
/ 4));
144 starRect
.setLeft(starRect
.width() - iconSize
);
145 starRect
.setRight(starRect
.width() - (iconSize
/ 4));
148 bool paintIcon
= true;
149 bool paintText
= true;
152 switch (index
.column()) {
153 case KDirModel::Name
:
157 case KDirModel::Size
:
161 case KDirModel::ModifiedTime
:
165 case KDirModel::Permissions
:
166 paintIcon
= false; // TODO: let's think about how to represent permissions
169 case KDirModel::Owner
: {
170 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
171 KUser
user(category
);
172 QString faceIconPath
= user
.faceIconPath();
174 if (!faceIconPath
.isEmpty())
176 icon
= QPixmap::fromImage(QImage(faceIconPath
).scaledToHeight(option
.fontMetrics
.height(), Qt::SmoothTransformation
));
180 icon
= KIconLoader::global()->loadIcon("user", KIconLoader::NoGroup
, option
.fontMetrics
.height());
183 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
188 case KDirModel::Group
:
192 case KDirModel::Type
: {
193 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
194 const KCategorizedSortFilterProxyModel
*proxyModel
= static_cast<const KCategorizedSortFilterProxyModel
*>(index
.model());
195 const DolphinModel
*model
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
196 KFileItem item
= model
->itemForIndex(proxyModel
->mapToSource(index
));
197 // This is the only way of getting the icon right. Others will fail on corner
198 // cases like the item representing this group has been set a different icon,
199 // so the group icon drawn is that one particularly. This way assures the drawn
200 // icon is the one of the mimetype of the group itself. (ereslibre)
201 icon
= KIconLoader::global()->loadMimeTypeIcon(item
.mimeTypePtr()->iconName(),
202 KIconLoader::NoGroup
, option
.fontMetrics
.height());
204 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
210 case DolphinModel::Rating
: {
214 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
215 starRect
.setSize(QSize(iconSize
, iconSize
));
217 QPixmap pixmap
= KIconLoader::global()->loadIcon("rating", KIconLoader::Small
);
218 QPixmap smallPixmap
= KIconLoader::global()->loadIcon("rating", KIconLoader::NoGroup
, iconSize
/ 2);
219 QPixmap disabledPixmap
= KIconLoader::global()->loadIcon("rating", KIconLoader::Small
);
221 QImage img
= disabledPixmap
.toImage();
222 Blitz::grayscale(img
);
223 disabledPixmap
= QPixmap::fromImage(img
);
225 int rating
= category
.toInt();
227 for (int i
= 0; i
< rating
- (rating
% 2); i
+= 2) {
228 painter
->drawPixmap(starRect
, pixmap
);
230 if (option
.direction
== Qt::LeftToRight
)
232 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4) /* separator between stars */);
233 starRect
.setRight(starRect
.right() + iconSize
+ (iconSize
/ 4) /* separator between stars */);
237 starRect
.setLeft(starRect
.left() - iconSize
- (iconSize
/ 4) /* separator between stars */);
238 starRect
.setRight(starRect
.right() - iconSize
- (iconSize
/ 4) /* separator between stars */);
242 if (rating
&& rating
% 2) {
243 if (option
.direction
== Qt::RightToLeft
)
245 starRect
.setLeft(starRect
.left() + (iconSize
/ 2) /* separator between stars */);
248 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 4));
249 starRect
.setSize(QSize(iconSize
/ 2, iconSize
/ 2));
250 painter
->drawPixmap(starRect
, smallPixmap
);
251 starRect
.setTop(opt
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
253 if (option
.direction
== Qt::LeftToRight
)
255 starRect
.setLeft(starRect
.left() + (iconSize
/ 2) + (iconSize
/ 4));
256 starRect
.setRight(starRect
.right() + (iconSize
/ 2) + (iconSize
/ 4));
260 starRect
.setLeft(starRect
.left() - (iconSize
/ 2) - (iconSize
/ 4));
261 starRect
.setRight(starRect
.right() - (iconSize
/ 2) - (iconSize
/ 4));
264 if (option
.direction
== Qt::RightToLeft
)
266 starRect
.setLeft(starRect
.left() - (iconSize
/ 2));
267 starRect
.setRight(starRect
.right() - (iconSize
/ 2));
270 starRect
.setSize(QSize(iconSize
, iconSize
));
273 for (int i
= rating
; i
< 9; i
+= 2) {
274 painter
->drawPixmap(starRect
, disabledPixmap
);
276 if (option
.direction
== Qt::LeftToRight
)
278 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4));
279 starRect
.setRight(starRect
.right() + iconSize
+ (iconSize
/ 4));
283 starRect
.setLeft(starRect
.left() - iconSize
- (iconSize
/ 4));
284 starRect
.setRight(starRect
.right() - iconSize
- (iconSize
/ 4));
291 case DolphinModel::Tags
:
298 painter
->drawPixmap(QRect(option
.direction
== Qt::LeftToRight
? opt
.rect
.left()
299 : opt
.rect
.right() - icon
.width() + (iconSize
/ 4), opt
.rect
.top(), icon
.width(), icon
.height()), icon
);
301 if (option
.direction
== Qt::LeftToRight
)
303 opt
.rect
.setLeft(opt
.rect
.left() + icon
.width() + (iconSize
/ 4));
307 opt
.rect
.setRight(opt
.rect
.right() + (iconSize
/ 4));
312 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
313 opt
.rect
.setBottom(opt
.rect
.bottom() - 2);
314 painter
->setPen(color
);
316 QRect textRect
= opt
.rect
;
318 if (option
.direction
== Qt::RightToLeft
)
320 textRect
.setWidth(textRect
.width() - (paintIcon
? icon
.width() + (iconSize
/ 2)
324 painter
->drawText(textRect
, Qt::AlignVCenter
| Qt::AlignLeft
,
325 metrics
.elidedText(category
, Qt::ElideRight
, textRect
.width()));
331 int DolphinCategoryDrawer::categoryHeight(const QStyleOption
&option
) const
333 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
335 return qMax(option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 2, iconSize
+ (iconSize
/ 4) * 2 + 2) /* 2 gradient */;