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()
50 DolphinCategoryDrawer::~DolphinCategoryDrawer()
54 void DolphinCategoryDrawer::drawCategory(const QModelIndex
&index
, int sortRole
,
55 const QStyleOption
&option
, QPainter
*painter
) const
58 painter
->setRenderHint(QPainter::Antialiasing
);
60 const QString category
= index
.model()->data(index
, KCategorizedSortFilterProxyModel::CategoryDisplayRole
).toString();
61 const QRect optRect
= option
.rect
;
62 QFont
font(QApplication::font());
64 const QFontMetrics fontMetrics
= QFontMetrics(font
);
66 QColor outlineColor
= option
.palette
.text().color();
67 outlineColor
.setAlphaF(0.35);
69 //BEGIN: top left corner
72 painter
->setPen(outlineColor
);
73 const QPointF
topLeft(optRect
.topLeft());
74 QRectF
arc(topLeft
, QSizeF(4, 4));
75 arc
.translate(0.5, 0.5);
76 painter
->drawArc(arc
, 1440, 1440);
79 //END: top left corner
81 //BEGIN: left vertical line
83 QPoint
start(optRect
.topLeft());
85 QPoint
verticalGradBottom(optRect
.topLeft());
86 verticalGradBottom
.ry() += fontMetrics
.height() + 5;
87 QLinearGradient
gradient(start
, verticalGradBottom
);
88 gradient
.setColorAt(0, outlineColor
);
89 gradient
.setColorAt(1, Qt::transparent
);
90 painter
->fillRect(QRect(start
, QSize(1, fontMetrics
.height() + 5)), gradient
);
92 //END: left vertical line
94 //BEGIN: horizontal line
96 QPoint
start(optRect
.topLeft());
98 QPoint
horizontalGradTop(optRect
.topLeft());
99 horizontalGradTop
.rx() += optRect
.width() - 6;
100 painter
->fillRect(QRect(start
, QSize(optRect
.width() - 6, 1)), outlineColor
);
102 //END: horizontal line
104 //BEGIN: top right corner
107 painter
->setPen(outlineColor
);
108 QPointF
topRight(optRect
.topRight());
110 QRectF
arc(topRight
, QSizeF(4, 4));
111 arc
.translate(0.5, 0.5);
112 painter
->drawArc(arc
, 0, 1440);
115 //END: top right corner
117 //BEGIN: right vertical line
119 QPoint
start(optRect
.topRight());
121 QPoint
verticalGradBottom(optRect
.topRight());
122 verticalGradBottom
.ry() += fontMetrics
.height() + 5;
123 QLinearGradient
gradient(start
, verticalGradBottom
);
124 gradient
.setColorAt(0, outlineColor
);
125 gradient
.setColorAt(1, Qt::transparent
);
126 painter
->fillRect(QRect(start
, QSize(1, fontMetrics
.height() + 5)), gradient
);
128 //END: right vertical line
130 //BEGIN: category information
132 const int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
136 switch (index
.column()) {
137 case KDirModel::Owner
: {
139 KUser
user(category
);
140 const QString faceIconPath
= user
.faceIconPath();
141 if (faceIconPath
.isEmpty()) {
142 icon
= KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup
, iconSize
);
144 icon
= QPixmap::fromImage(QImage(faceIconPath
).scaledToHeight(iconSize
, Qt::SmoothTransformation
));
148 case KDirModel::Type
: {
150 const KCategorizedSortFilterProxyModel
*proxyModel
= static_cast<const KCategorizedSortFilterProxyModel
*>(index
.model());
151 const DolphinModel
*model
= static_cast<const DolphinModel
*>(proxyModel
->sourceModel());
152 KFileItem item
= model
->itemForIndex(proxyModel
->mapToSource(index
));
153 // This is the only way of getting the icon right. Others will fail on corner
154 // cases like the item representing this group has been set a different icon,
155 // so the group icon drawn is that one particularly. This way assures the drawn
156 // icon is the one of the mimetype of the group itself. (ereslibre)
157 icon
= KIconLoader::global()->loadMimeTypeIcon(item
.mimeTypePtr()->iconName(), KIconLoader::NoGroup
, iconSize
);
165 QRect
iconRect(option
.rect
);
166 iconRect
.setTop(iconRect
.top() + 4);
167 iconRect
.setLeft(iconRect
.left() + 7);
168 iconRect
.setSize(QSize(iconSize
, iconSize
));
170 painter
->drawPixmap(iconRect
, icon
);
175 QRect
textRect(option
.rect
);
176 textRect
.setTop(textRect
.top() + 7);
177 textRect
.setLeft(textRect
.left() + 7 + (paintIcon
? (iconSize
+ 6) : 0));
178 textRect
.setHeight(qMax(fontMetrics
.height(), iconSize
));
179 textRect
.setRight(textRect
.right() - 7);
180 textRect
.setBottom(textRect
.bottom() - 5); // only one pixel separation here (no gradient)
183 painter
->setFont(font
);
184 QColor
penColor(option
.palette
.text().color());
185 penColor
.setAlphaF(0.6);
186 painter
->setPen(penColor
);
187 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, category
);
192 //BEGIN: category information
195 int DolphinCategoryDrawer::categoryHeight(const QModelIndex
&index
, const QStyleOption
&option
) const
197 int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Small
);
198 int heightWithoutIcon
= option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 1; /* 1 pixel-width gradient */
201 switch (index
.column()) {
202 case KDirModel::Owner
:
203 case KDirModel::Type
:
211 return qMax(heightWithoutIcon
+ 5, iconSize
+ 1 /* 1 pixel-width gradient */
212 + 5 /* top and bottom separation */);
215 return heightWithoutIcon
+ 5;