2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
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 <kpixmapeffect.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(K3Icon::Small
);
56 QVariant categoryVariant
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryRole
);
58 if (!categoryVariant
.isValid())
63 const QString category
= categoryVariant
.toString();
65 QColor color
= option
.palette
.color(QPalette::Text
);
68 painter
->setRenderHint(QPainter::Antialiasing
);
70 QStyleOptionButton opt
;
72 opt
.rect
= option
.rect
;
73 opt
.palette
= option
.palette
;
74 opt
.direction
= option
.direction
;
77 if (option
.state
& QStyle::State_MouseOver
)
79 QColor hover
= option
.palette
.color(QPalette::Highlight
).light();
82 QLinearGradient
gradient(option
.rect
.topLeft(),
83 option
.rect
.bottomRight());
84 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
86 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
87 : 0, Qt::transparent
);
89 painter
->fillRect(option
.rect
, gradient
);
92 QFont painterFont
= painter
->font();
93 painterFont
.setWeight(QFont::Bold
);
94 QFontMetrics
metrics(painterFont
);
95 painter
->setFont(painterFont
);
98 path
.addRect(option
.rect
.left(),
99 option
.rect
.bottom() - 2,
103 QLinearGradient
gradient(option
.rect
.topLeft(),
104 option
.rect
.bottomRight());
105 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
107 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
108 : 0, Qt::transparent
);
110 painter
->setBrush(gradient
);
111 painter
->fillPath(path
, gradient
);
113 if (option
.direction
== Qt::LeftToRight
)
115 opt
.rect
.setLeft(opt
.rect
.left() + (iconSize
/ 4));
116 starRect
.setLeft(starRect
.left() + (iconSize
/ 4));
117 starRect
.setRight(starRect
.right() + (iconSize
/ 4));
121 opt
.rect
.setRight(opt
.rect
.width() - (iconSize
/ 4));
122 starRect
.setLeft(starRect
.width() - iconSize
);
123 starRect
.setRight(starRect
.width() - (iconSize
/ 4));
126 bool paintIcon
= true;
127 bool paintText
= true;
130 switch (index
.column()) {
131 case KDirModel::Name
:
135 case KDirModel::Size
:
139 case KDirModel::ModifiedTime
:
143 case KDirModel::Permissions
:
144 paintIcon
= false; // TODO: let's think about how to represent permissions
147 case KDirModel::Owner
: {
148 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
149 KUser
user(category
);
150 if (QFile::exists(user
.homeDir() + QDir::separator() + ".face.icon"))
152 icon
= QPixmap::fromImage(QImage(user
.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize
, iconSize
);
156 icon
= KIconLoader::global()->loadIcon("user", K3Icon::Small
);
161 case KDirModel::Group
:
165 case KDirModel::Type
: {
166 opt
.rect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
167 const KCategorizedSortFilterProxyModel
*proxyModel
= static_cast<const KCategorizedSortFilterProxyModel
*>(index
.model());
168 const DolphinModel
*model
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
169 KFileItem item
= model
->itemForIndex(proxyModel
->mapToSource(index
));
170 // This is the only way of getting the icon right. Others will fail on corner
171 // cases like the item representing this group has been set a different icon,
172 // so the group icon drawn is that one particularly. This way assures the drawn
173 // icon is the one of the mimetype of the group itself. (ereslibre)
174 icon
= KIconLoader::global()->loadMimeTypeIcon(item
.mimeTypePtr()->iconName(),
180 case DolphinModel::Rating
: {
184 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
185 starRect
.setSize(QSize(iconSize
, iconSize
));
187 QPixmap pixmap
= KIconLoader::global()->loadIcon("rating", K3Icon::Small
);
188 QPixmap smallPixmap
= KIconLoader::global()->loadIcon("rating", K3Icon::NoGroup
, iconSize
/ 2);
189 QPixmap disabledPixmap
= KIconLoader::global()->loadIcon("rating", K3Icon::Small
);
191 KPixmapEffect::toGray(disabledPixmap
, false);
193 int rating
= category
.toInt();
195 for (int i
= 0; i
< rating
- (rating
% 2); i
+= 2) {
196 painter
->drawPixmap(starRect
, pixmap
);
198 if (option
.direction
== Qt::LeftToRight
)
200 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4) /* separator between stars */);
201 starRect
.setRight(starRect
.right() + iconSize
+ (iconSize
/ 4) /* separator between stars */);
205 starRect
.setLeft(starRect
.left() - iconSize
- (iconSize
/ 4) /* separator between stars */);
206 starRect
.setRight(starRect
.right() - iconSize
- (iconSize
/ 4) /* separator between stars */);
210 if (rating
&& rating
% 2) {
211 if (option
.direction
== Qt::RightToLeft
)
213 starRect
.setLeft(starRect
.left() + (iconSize
/ 2) /* separator between stars */);
214 //starRect.setRight(starRect.right() + (iconSize / 2) /* separator between stars */);
217 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 4));
218 starRect
.setSize(QSize(iconSize
/ 2, iconSize
/ 2));
219 painter
->drawPixmap(starRect
, smallPixmap
);
220 starRect
.setTop(opt
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
221 //starRect.setSize(QSize(iconSize / 2, iconSize / 2));
223 if (option
.direction
== Qt::LeftToRight
)
225 starRect
.setLeft(starRect
.left() + (iconSize
/ 2) + (iconSize
/ 4));
226 starRect
.setRight(starRect
.right() + (iconSize
/ 2) + (iconSize
/ 4));
230 starRect
.setLeft(starRect
.left() - (iconSize
/ 2) - (iconSize
/ 4));
231 starRect
.setRight(starRect
.right() - (iconSize
/ 2) - (iconSize
/ 4));
234 if (option
.direction
== Qt::RightToLeft
)
236 starRect
.setLeft(starRect
.left() - (iconSize
/ 2));
237 starRect
.setRight(starRect
.right() - (iconSize
/ 2));
240 starRect
.setSize(QSize(iconSize
, iconSize
));
243 for (int i
= rating
; i
< 9; i
+= 2) {
244 painter
->drawPixmap(starRect
, disabledPixmap
);
246 if (option
.direction
== Qt::LeftToRight
)
248 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4));
249 starRect
.setRight(starRect
.right() + iconSize
+ (iconSize
/ 4));
253 starRect
.setLeft(starRect
.left() - iconSize
- (iconSize
/ 4));
254 starRect
.setRight(starRect
.right() - iconSize
- (iconSize
/ 4));
261 case DolphinModel::Tags
:
268 painter
->drawPixmap(QRect(option
.direction
== Qt::LeftToRight
? opt
.rect
.left()
269 : opt
.rect
.right() - iconSize
, opt
.rect
.top(), iconSize
, iconSize
), icon
);
271 if (option
.direction
== Qt::LeftToRight
)
273 opt
.rect
.setLeft(opt
.rect
.left() + iconSize
+ (iconSize
/ 4));
278 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
279 opt
.rect
.setBottom(opt
.rect
.bottom() - 2);
280 painter
->setPen(color
);
282 QRect textRect
= opt
.rect
;
284 if (option
.direction
== Qt::RightToLeft
)
286 textRect
.setWidth(textRect
.width() - (paintIcon
? iconSize
+ (iconSize
/ 4)
290 painter
->drawText(textRect
, Qt::AlignVCenter
| Qt::AlignLeft
,
291 metrics
.elidedText(category
, Qt::ElideRight
, textRect
.width()));
297 int DolphinCategoryDrawer::categoryHeight(const QStyleOption
&option
) const
299 int iconSize
= KIconLoader::global()->currentSize(K3Icon::Small
);
301 return qMax(option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 2, iconSize
+ (iconSize
/ 4) * 2 + 2) /* 2 gradient */;