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"
23 #include <config-nepomuk.h>
28 #include <QApplication>
29 #include <QStyleOption>
32 #include <nepomuk/kratingpainter.h>
35 #include <kiconloader.h>
36 #include <kcategorizedsortfilterproxymodel.h>
37 #include <qimageblitz.h>
40 #include "dolphinview.h"
41 #include "dolphinmodel.h"
43 #define HORIZONTAL_HINT 3
45 DolphinCategoryDrawer::DolphinCategoryDrawer()
49 DolphinCategoryDrawer::~DolphinCategoryDrawer()
53 void DolphinCategoryDrawer::drawCategory(const QModelIndex
&index
, int sortRole
,
54 const QStyleOption
&option
, QPainter
*painter
) const
58 QRect starRect
= option
.rect
;
60 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
61 QVariant categoryVariant
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
63 if (!categoryVariant
.isValid())
68 const QString category
= categoryVariant
.toString();
72 if (option
.state
& QStyle::State_Selected
)
74 color
= option
.palette
.color(QPalette::HighlightedText
);
78 color
= option
.palette
.color(QPalette::Text
);
82 painter
->setRenderHint(QPainter::Antialiasing
);
84 QStyleOptionButton opt
;
86 opt
.rect
= option
.rect
;
87 opt
.rect
.setLeft(opt
.rect
.left() + HORIZONTAL_HINT
);
88 opt
.rect
.setRight(opt
.rect
.right() - HORIZONTAL_HINT
);
89 opt
.palette
= option
.palette
;
90 opt
.direction
= option
.direction
;
93 QStyleOptionViewItemV4 viewOptions
;
94 viewOptions
.rect
= option
.rect
;
95 viewOptions
.palette
= option
.palette
;
96 viewOptions
.direction
= option
.direction
;
97 viewOptions
.state
= option
.state
;
98 viewOptions
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
99 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewOptions
, painter
, 0);
101 QFont painterFont
= painter
->font();
102 painterFont
.setWeight(QFont::Bold
);
103 QFontMetrics
metrics(painterFont
);
104 painter
->setFont(painterFont
);
107 path
.addRect(option
.rect
.left(),
108 option
.rect
.bottom() - 1,
112 QLinearGradient
gradient(option
.rect
.topLeft(),
113 option
.rect
.bottomRight());
114 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
116 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
117 : 0, Qt::transparent
);
119 painter
->setBrush(gradient
);
120 painter
->fillPath(path
, gradient
);
122 if (option
.direction
== Qt::LeftToRight
)
124 opt
.rect
.setLeft(opt
.rect
.left());
125 starRect
.setLeft(starRect
.left());
126 starRect
.setRight(starRect
.right());
130 opt
.rect
.setRight(opt
.rect
.width());
131 starRect
.setLeft(starRect
.width());
132 starRect
.setRight(starRect
.width());
135 bool paintIcon
= true;
136 bool paintText
= true;
139 switch (index
.column()) {
140 case KDirModel::Name
:
144 case KDirModel::Size
:
148 case KDirModel::ModifiedTime
:
152 case KDirModel::Permissions
:
153 paintIcon
= false; // TODO: let's think about how to represent permissions
156 case KDirModel::Owner
: {
157 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
158 KUser
user(category
);
159 QString faceIconPath
= user
.faceIconPath();
161 if (!faceIconPath
.isEmpty())
163 icon
= QPixmap::fromImage(QImage(faceIconPath
).scaledToHeight(option
.fontMetrics
.height(), Qt::SmoothTransformation
));
167 icon
= KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup
, option
.fontMetrics
.height());
170 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
175 case KDirModel::Group
:
179 case KDirModel::Type
: {
180 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
181 const KCategorizedSortFilterProxyModel
*proxyModel
= static_cast<const KCategorizedSortFilterProxyModel
*>(index
.model());
182 const DolphinModel
*model
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
183 KFileItem item
= model
->itemForIndex(proxyModel
->mapToSource(index
));
184 // This is the only way of getting the icon right. Others will fail on corner
185 // cases like the item representing this group has been set a different icon,
186 // so the group icon drawn is that one particularly. This way assures the drawn
187 // icon is the one of the mimetype of the group itself. (ereslibre)
188 icon
= KIconLoader::global()->loadMimeTypeIcon(item
.mimeTypePtr()->iconName(),
189 KIconLoader::NoGroup
, option
.fontMetrics
.height());
191 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
197 case DolphinModel::Rating
: {
201 painter
->setLayoutDirection( option
.direction
);
202 QRect
ratingRect( option
.rect
);
203 ratingRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
204 ratingRect
.setHeight( iconSize
);
205 KRatingPainter::paintRating( painter
, ratingRect
, Qt::AlignLeft
, category
.toInt() );
209 case DolphinModel::Tags
:
216 painter
->drawPixmap(QRect(option
.direction
== Qt::LeftToRight
? opt
.rect
.left()
217 : opt
.rect
.right() - icon
.width() + (iconSize
/ 4), opt
.rect
.top(), icon
.width(), icon
.height()), icon
);
219 if (option
.direction
== Qt::LeftToRight
)
221 opt
.rect
.setLeft(opt
.rect
.left() + icon
.width() + (iconSize
/ 4));
225 opt
.rect
.setRight(opt
.rect
.right() + (iconSize
/ 4));
230 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
231 opt
.rect
.setBottom(opt
.rect
.bottom() - 1);
232 painter
->setPen(color
);
234 QRect textRect
= opt
.rect
;
236 if (option
.direction
== Qt::RightToLeft
)
238 textRect
.setWidth(textRect
.width() - (paintIcon
? icon
.width() + (iconSize
/ 4)
242 painter
->drawText(textRect
, Qt::AlignVCenter
| Qt::AlignLeft
,
243 metrics
.elidedText(category
, Qt::ElideRight
, textRect
.width()));
249 int DolphinCategoryDrawer::categoryHeight(const QModelIndex
&index
, const QStyleOption
&option
) const
251 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
252 int heightWithoutIcon
= option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 1; /* 1 pixel-width gradient */
255 switch (index
.column()) {
256 case KDirModel::Owner
:
257 case KDirModel::Type
:
265 return qMax(heightWithoutIcon
, iconSize
+ (iconSize
/ 4) * 2 + 1) /* 1 pixel-width gradient */;
267 return heightWithoutIcon
;