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"
22 #include "ratingpainter.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
55 QRect starRect
= option
.rect
;
57 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
58 QVariant categoryVariant
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
60 if (!categoryVariant
.isValid())
65 const QString category
= categoryVariant
.toString();
69 if (option
.state
& QStyle::State_Selected
)
71 color
= option
.palette
.color(QPalette::HighlightedText
);
75 color
= option
.palette
.color(QPalette::Text
);
79 painter
->setRenderHint(QPainter::Antialiasing
);
81 QStyleOptionButton opt
;
83 opt
.rect
= option
.rect
;
84 opt
.palette
= option
.palette
;
85 opt
.direction
= option
.direction
;
88 if (option
.state
& QStyle::State_Selected
)
90 QColor selected
= option
.palette
.color(QPalette::Highlight
);
92 QLinearGradient
gradient(option
.rect
.topLeft(),
93 option
.rect
.bottomRight());
94 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
96 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
97 : 0, Qt::transparent
);
99 painter
->fillRect(option
.rect
, gradient
);
101 else if (option
.state
& QStyle::State_MouseOver
)
103 QColor hover
= option
.palette
.color(QPalette::Highlight
);
106 QLinearGradient
gradient(option
.rect
.topLeft(),
107 option
.rect
.bottomRight());
108 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
110 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
111 : 0, Qt::transparent
);
113 painter
->fillRect(option
.rect
, gradient
);
116 QFont painterFont
= painter
->font();
117 painterFont
.setWeight(QFont::Bold
);
118 QFontMetrics
metrics(painterFont
);
119 painter
->setFont(painterFont
);
122 path
.addRect(option
.rect
.left(),
123 option
.rect
.bottom() - 1,
127 QLinearGradient
gradient(option
.rect
.topLeft(),
128 option
.rect
.bottomRight());
129 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
131 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
132 : 0, Qt::transparent
);
134 painter
->setBrush(gradient
);
135 painter
->fillPath(path
, gradient
);
137 if (option
.direction
== Qt::LeftToRight
)
139 opt
.rect
.setLeft(opt
.rect
.left());
140 starRect
.setLeft(starRect
.left());
141 starRect
.setRight(starRect
.right());
145 opt
.rect
.setRight(opt
.rect
.width());
146 starRect
.setLeft(starRect
.width());
147 starRect
.setRight(starRect
.width());
150 bool paintIcon
= true;
151 bool paintText
= true;
154 switch (index
.column()) {
155 case KDirModel::Name
:
159 case KDirModel::Size
:
163 case KDirModel::ModifiedTime
:
167 case KDirModel::Permissions
:
168 paintIcon
= false; // TODO: let's think about how to represent permissions
171 case KDirModel::Owner
: {
172 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
173 KUser
user(category
);
174 QString faceIconPath
= user
.faceIconPath();
176 if (!faceIconPath
.isEmpty())
178 icon
= QPixmap::fromImage(QImage(faceIconPath
).scaledToHeight(option
.fontMetrics
.height(), Qt::SmoothTransformation
));
182 icon
= KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup
, option
.fontMetrics
.height());
185 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
190 case KDirModel::Group
:
194 case KDirModel::Type
: {
195 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
196 const KCategorizedSortFilterProxyModel
*proxyModel
= static_cast<const KCategorizedSortFilterProxyModel
*>(index
.model());
197 const DolphinModel
*model
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
198 KFileItem item
= model
->itemForIndex(proxyModel
->mapToSource(index
));
199 // This is the only way of getting the icon right. Others will fail on corner
200 // cases like the item representing this group has been set a different icon,
201 // so the group icon drawn is that one particularly. This way assures the drawn
202 // icon is the one of the mimetype of the group itself. (ereslibre)
203 icon
= KIconLoader::global()->loadMimeTypeIcon(item
.mimeTypePtr()->iconName(),
204 KIconLoader::NoGroup
, option
.fontMetrics
.height());
206 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
212 case DolphinModel::Rating
: {
216 painter
->setLayoutDirection( option
.direction
);
217 QRect
ratingRect( option
.rect
);
218 ratingRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
219 ratingRect
.setHeight( iconSize
);
220 Nepomuk::RatingPainter::drawRating( painter
, ratingRect
, Qt::AlignLeft
, category
.toInt() );
224 case DolphinModel::Tags
:
231 painter
->drawPixmap(QRect(option
.direction
== Qt::LeftToRight
? opt
.rect
.left()
232 : opt
.rect
.right() - icon
.width() + (iconSize
/ 4), opt
.rect
.top(), icon
.width(), icon
.height()), icon
);
234 if (option
.direction
== Qt::LeftToRight
)
236 opt
.rect
.setLeft(opt
.rect
.left() + icon
.width() + (iconSize
/ 4));
240 opt
.rect
.setRight(opt
.rect
.right() + (iconSize
/ 4));
245 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
246 opt
.rect
.setBottom(opt
.rect
.bottom() - 1);
247 painter
->setPen(color
);
249 QRect textRect
= opt
.rect
;
251 if (option
.direction
== Qt::RightToLeft
)
253 textRect
.setWidth(textRect
.width() - (paintIcon
? icon
.width() + (iconSize
/ 4)
257 painter
->drawText(textRect
, Qt::AlignVCenter
| Qt::AlignLeft
,
258 metrics
.elidedText(category
, Qt::ElideRight
, textRect
.width()));
264 int DolphinCategoryDrawer::categoryHeight(const QModelIndex
&index
, const QStyleOption
&option
) const
266 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
267 int heightWithoutIcon
= option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 1; /* 1 pixel-width gradient */
270 switch (index
.column()) {
271 case KDirModel::Owner
:
272 case KDirModel::Type
:
280 return qMax(heightWithoutIcon
, iconSize
+ (iconSize
/ 4) * 2 + 1) /* 1 pixel-width gradient */;
282 return heightWithoutIcon
;