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 "config-nepomuk.h"
23 #include "dolphincategorydrawer.h"
27 #include <QApplication>
28 #include <QStyleOption>
31 #include <nepomuk/kratingpainter.h>
34 #include <kiconloader.h>
35 #include <kcategorizedsortfilterproxymodel.h>
36 #include <qimageblitz.h>
39 #include "dolphinview.h"
40 #include "dolphinmodel.h"
42 #define HORIZONTAL_HINT 3
44 DolphinCategoryDrawer::DolphinCategoryDrawer()
48 DolphinCategoryDrawer::~DolphinCategoryDrawer()
52 void DolphinCategoryDrawer::drawCategory(const QModelIndex
&index
, int sortRole
,
53 const QStyleOption
&option
, QPainter
*painter
) const
57 QRect starRect
= option
.rect
;
59 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
60 QVariant categoryVariant
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
);
62 if (!categoryVariant
.isValid())
67 const QString category
= categoryVariant
.toString();
71 if (option
.state
& QStyle::State_Selected
)
73 color
= option
.palette
.color(QPalette::HighlightedText
);
77 color
= option
.palette
.color(QPalette::Text
);
81 painter
->setRenderHint(QPainter::Antialiasing
);
83 QStyleOptionButton opt
;
85 opt
.rect
= option
.rect
;
86 opt
.rect
.setLeft(opt
.rect
.left() + HORIZONTAL_HINT
);
87 opt
.rect
.setRight(opt
.rect
.right() - HORIZONTAL_HINT
);
88 opt
.palette
= option
.palette
;
89 opt
.direction
= option
.direction
;
92 QStyleOptionViewItemV4 viewOptions
;
93 viewOptions
.rect
= option
.rect
;
94 viewOptions
.palette
= option
.palette
;
95 viewOptions
.direction
= option
.direction
;
96 viewOptions
.state
= option
.state
;
97 viewOptions
.viewItemPosition
= QStyleOptionViewItemV4::OnlyOne
;
98 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &viewOptions
, painter
, 0);
100 QFont painterFont
= painter
->font();
101 painterFont
.setWeight(QFont::Bold
);
102 QFontMetrics
metrics(painterFont
);
103 painter
->setFont(painterFont
);
106 path
.addRect(option
.rect
.left(),
107 option
.rect
.bottom() - 1,
111 QLinearGradient
gradient(option
.rect
.topLeft(),
112 option
.rect
.bottomRight());
113 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 0
115 gradient
.setColorAt(option
.direction
== Qt::LeftToRight
? 1
116 : 0, Qt::transparent
);
118 painter
->setBrush(gradient
);
119 painter
->fillPath(path
, gradient
);
121 if (option
.direction
== Qt::LeftToRight
)
123 opt
.rect
.setLeft(opt
.rect
.left());
124 starRect
.setLeft(starRect
.left());
125 starRect
.setRight(starRect
.right());
129 opt
.rect
.setRight(opt
.rect
.width());
130 starRect
.setLeft(starRect
.width());
131 starRect
.setRight(starRect
.width());
134 bool paintIcon
= true;
135 bool paintText
= true;
138 switch (index
.column()) {
139 case KDirModel::Name
:
143 case KDirModel::Size
:
147 case KDirModel::ModifiedTime
:
151 case KDirModel::Permissions
:
152 paintIcon
= false; // TODO: let's think about how to represent permissions
155 case KDirModel::Owner
: {
156 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
157 KUser
user(category
);
158 QString faceIconPath
= user
.faceIconPath();
160 if (!faceIconPath
.isEmpty())
162 icon
= QPixmap::fromImage(QImage(faceIconPath
).scaledToHeight(option
.fontMetrics
.height(), Qt::SmoothTransformation
));
166 icon
= KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup
, option
.fontMetrics
.height());
169 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
174 case KDirModel::Group
:
178 case KDirModel::Type
: {
179 opt
.rect
.setTop(option
.rect
.bottom() - (iconSize
/ 4));
180 const KCategorizedSortFilterProxyModel
*proxyModel
= static_cast<const KCategorizedSortFilterProxyModel
*>(index
.model());
181 const DolphinModel
*model
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
182 KFileItem item
= model
->itemForIndex(proxyModel
->mapToSource(index
));
183 // This is the only way of getting the icon right. Others will fail on corner
184 // cases like the item representing this group has been set a different icon,
185 // so the group icon drawn is that one particularly. This way assures the drawn
186 // icon is the one of the mimetype of the group itself. (ereslibre)
187 icon
= KIconLoader::global()->loadMimeTypeIcon(item
.mimeTypePtr()->iconName(),
188 KIconLoader::NoGroup
, option
.fontMetrics
.height());
190 opt
.rect
.setTop(opt
.rect
.top() - icon
.height());
196 case DolphinModel::Rating
: {
200 painter
->setLayoutDirection( option
.direction
);
201 QRect
ratingRect( option
.rect
);
202 ratingRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
203 ratingRect
.setHeight( iconSize
);
204 KRatingPainter::paintRating( painter
, ratingRect
, Qt::AlignLeft
, category
.toInt() );
208 case DolphinModel::Tags
:
215 painter
->drawPixmap(QRect(option
.direction
== Qt::LeftToRight
? opt
.rect
.left()
216 : opt
.rect
.right() - icon
.width() + (iconSize
/ 4), opt
.rect
.top(), icon
.width(), icon
.height()), icon
);
218 if (option
.direction
== Qt::LeftToRight
)
220 opt
.rect
.setLeft(opt
.rect
.left() + icon
.width() + (iconSize
/ 4));
224 opt
.rect
.setRight(opt
.rect
.right() + (iconSize
/ 4));
229 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
230 opt
.rect
.setBottom(opt
.rect
.bottom() - 1);
231 painter
->setPen(color
);
233 QRect textRect
= opt
.rect
;
235 if (option
.direction
== Qt::RightToLeft
)
237 textRect
.setWidth(textRect
.width() - (paintIcon
? icon
.width() + (iconSize
/ 4)
241 painter
->drawText(textRect
, Qt::AlignVCenter
| Qt::AlignLeft
,
242 metrics
.elidedText(category
, Qt::ElideRight
, textRect
.width()));
248 int DolphinCategoryDrawer::categoryHeight(const QModelIndex
&index
, const QStyleOption
&option
) const
250 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
251 int heightWithoutIcon
= option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 1; /* 1 pixel-width gradient */
254 switch (index
.column()) {
255 case KDirModel::Owner
:
256 case KDirModel::Type
:
264 return qMax(heightWithoutIcon
, iconSize
+ (iconSize
/ 4) * 2 + 1) /* 1 pixel-width gradient */;
266 return heightWithoutIcon
;