]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincategorydrawer.cpp
get_servicemenu added to dolphinpart
[dolphin.git] / src / dolphincategorydrawer.cpp
1 /*
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #include "dolphincategorydrawer.h"
22
23 #include <config-nepomuk.h>
24
25 #include <QPainter>
26 #include <QFile>
27 #include <QDir>
28 #include <QApplication>
29 #include <QStyleOption>
30
31 #ifdef HAVE_NEPOMUK
32 #include <nepomuk/kratingpainter.h>
33 #endif
34
35 #include <kiconloader.h>
36 #include <kcategorizedsortfilterproxymodel.h>
37 #include <qimageblitz.h>
38 #include <kuser.h>
39
40 #include "dolphinview.h"
41 #include "dolphinmodel.h"
42
43 #define HORIZONTAL_HINT 3
44
45 DolphinCategoryDrawer::DolphinCategoryDrawer()
46 : KCategoryDrawer()
47 {
48 }
49
50 DolphinCategoryDrawer::~DolphinCategoryDrawer()
51 {
52 }
53
54 void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
55 const QStyleOption &option, QPainter *painter) const
56 {
57 Q_UNUSED(sortRole);
58 painter->setRenderHint(QPainter::Antialiasing);
59
60 const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
61 const QRect optRect = option.rect;
62 QFont font(QApplication::font());
63 font.setBold(true);
64 const QFontMetrics fontMetrics = QFontMetrics(font);
65
66 QColor outlineColor = option.palette.text().color();
67 outlineColor.setAlphaF(0.35);
68
69 //BEGIN: top left corner
70 {
71 painter->save();
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);
77 painter->restore();
78 }
79 //END: top left corner
80
81 //BEGIN: left vertical line
82 {
83 QPoint start(optRect.topLeft());
84 start.ry() += 3;
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);
91 }
92 //END: left vertical line
93
94 //BEGIN: horizontal line
95 {
96 QPoint start(optRect.topLeft());
97 start.rx() += 3;
98 QPoint horizontalGradTop(optRect.topLeft());
99 horizontalGradTop.rx() += optRect.width() - 6;
100 painter->fillRect(QRect(start, QSize(optRect.width() - 6, 1)), outlineColor);
101 }
102 //END: horizontal line
103
104 //BEGIN: top right corner
105 {
106 painter->save();
107 painter->setPen(outlineColor);
108 QPointF topRight(optRect.topRight());
109 topRight.rx() -= 4;
110 QRectF arc(topRight, QSizeF(4, 4));
111 arc.translate(0.5, 0.5);
112 painter->drawArc(arc, 0, 1440);
113 painter->restore();
114 }
115 //END: top right corner
116
117 //BEGIN: right vertical line
118 {
119 QPoint start(optRect.topRight());
120 start.ry() += 3;
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);
127 }
128 //END: right vertical line
129
130 //BEGIN: category information
131 {
132 const int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
133
134 bool paintIcon;
135 QPixmap icon;
136 switch (index.column()) {
137 case KDirModel::Owner: {
138 paintIcon = true;
139 KUser user(category);
140 const QString faceIconPath = user.faceIconPath();
141 if (faceIconPath.isEmpty()) {
142 icon = KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup, iconSize);
143 } else {
144 icon = QPixmap::fromImage(QImage(faceIconPath).scaledToHeight(iconSize, Qt::SmoothTransformation));
145 }
146 }
147 break;
148 case KDirModel::Type: {
149 paintIcon = true;
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);
158 }
159 break;
160 default:
161 paintIcon = false;
162 }
163
164 if (paintIcon) {
165 QRect iconRect(option.rect);
166 iconRect.setTop(iconRect.top() + 4);
167 iconRect.setLeft(iconRect.left() + 7);
168 iconRect.setSize(QSize(iconSize, iconSize));
169
170 painter->drawPixmap(iconRect, icon);
171 }
172
173 //BEGIN: text
174 {
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)
181
182 painter->save();
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);
188 painter->restore();
189 }
190 //END: text
191 }
192 //BEGIN: category information
193 }
194
195 int DolphinCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
196 {
197 int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
198 int heightWithoutIcon = option.fontMetrics.height() + (iconSize / 4) * 2 + 1; /* 1 pixel-width gradient */
199 bool paintIcon;
200
201 switch (index.column()) {
202 case KDirModel::Owner:
203 case KDirModel::Type:
204 paintIcon = true;
205 break;
206 default:
207 paintIcon = false;
208 }
209
210 if (paintIcon) {
211 return qMax(heightWithoutIcon + 5, iconSize + 1 /* 1 pixel-width gradient */
212 + 5 /* top and bottom separation */);
213 }
214
215 return heightWithoutIcon + 5;
216 }