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
53 QRect starRect
= option
.rect
;
55 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
56 QVariant categoryVariant
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
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
);
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() - 1,
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());
138 starRect
.setLeft(starRect
.left());
139 starRect
.setRight(starRect
.right());
143 opt
.rect
.setRight(opt
.rect
.width());
144 starRect
.setLeft(starRect
.width());
145 starRect
.setRight(starRect
.width());
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-identity", 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 painter
->setLayoutDirection( option
.direction
);
215 QRect
ratingRect( option
.rect
);
216 ratingRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
217 ratingRect
.setHeight( iconSize
);
218 Nepomuk::RatingPainter::drawRating( painter
, ratingRect
, Qt::AlignLeft
, category
.toInt() );
222 case DolphinModel::Tags
:
229 painter
->drawPixmap(QRect(option
.direction
== Qt::LeftToRight
? opt
.rect
.left()
230 : opt
.rect
.right() - icon
.width() + (iconSize
/ 4), opt
.rect
.top(), icon
.width(), icon
.height()), icon
);
232 if (option
.direction
== Qt::LeftToRight
)
234 opt
.rect
.setLeft(opt
.rect
.left() + icon
.width() + (iconSize
/ 4));
238 opt
.rect
.setRight(opt
.rect
.right() + (iconSize
/ 4));
243 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
244 opt
.rect
.setBottom(opt
.rect
.bottom() - 1);
245 painter
->setPen(color
);
247 QRect textRect
= opt
.rect
;
249 if (option
.direction
== Qt::RightToLeft
)
251 textRect
.setWidth(textRect
.width() - (paintIcon
? icon
.width() + (iconSize
/ 4)
255 painter
->drawText(textRect
, Qt::AlignVCenter
| Qt::AlignLeft
,
256 metrics
.elidedText(category
, Qt::ElideRight
, textRect
.width()));
262 int DolphinCategoryDrawer::categoryHeight(const QModelIndex
&index
, const QStyleOption
&option
) const
264 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
265 int heightWithoutIcon
= option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 1; /* 1 pixel-width gradient */
268 switch (index
.column()) {
269 case KDirModel::Owner
:
270 case KDirModel::Type
:
278 return qMax(heightWithoutIcon
, iconSize
+ (iconSize
/ 4) * 2 + 1) /* 1 pixel-width gradient */;
280 return heightWithoutIcon
;