]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincategorydrawer.cpp
SVN_SILENT made messages (.desktop file)
[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 "config-nepomuk.h"
22
23 #include "dolphincategorydrawer.h"
24 #include <QPainter>
25 #include <QFile>
26 #include <QDir>
27 #include <QApplication>
28 #include <QStyleOption>
29
30 #ifdef HAVE_NEPOMUK
31 #include <nepomuk/kratingpainter.h>
32 #endif
33
34 #include <kiconloader.h>
35 #include <kcategorizedsortfilterproxymodel.h>
36 #include <qimageblitz.h>
37 #include <kuser.h>
38
39 #include "dolphinview.h"
40 #include "dolphinmodel.h"
41
42 #define HORIZONTAL_HINT 3
43
44 DolphinCategoryDrawer::DolphinCategoryDrawer()
45 {
46 }
47
48 DolphinCategoryDrawer::~DolphinCategoryDrawer()
49 {
50 }
51
52 void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
53 const QStyleOption &option, QPainter *painter) const
54 {
55 Q_UNUSED(sortRole);
56
57 QRect starRect = option.rect;
58
59 int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
60 QVariant categoryVariant = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
61
62 if (!categoryVariant.isValid())
63 {
64 return;
65 }
66
67 const QString category = categoryVariant.toString();
68
69 QColor color;
70
71 if (option.state & QStyle::State_Selected)
72 {
73 color = option.palette.color(QPalette::HighlightedText);
74 }
75 else
76 {
77 color = option.palette.color(QPalette::Text);
78 }
79
80 painter->save();
81 painter->setRenderHint(QPainter::Antialiasing);
82
83 QStyleOptionButton opt;
84
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;
90 opt.text = category;
91
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);
99
100 QFont painterFont = painter->font();
101 painterFont.setWeight(QFont::Bold);
102 QFontMetrics metrics(painterFont);
103 painter->setFont(painterFont);
104
105 QPainterPath path;
106 path.addRect(option.rect.left(),
107 option.rect.bottom() - 1,
108 option.rect.width(),
109 1);
110
111 QLinearGradient gradient(option.rect.topLeft(),
112 option.rect.bottomRight());
113 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
114 : 1, color);
115 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
116 : 0, Qt::transparent);
117
118 painter->setBrush(gradient);
119 painter->fillPath(path, gradient);
120
121 if (option.direction == Qt::LeftToRight)
122 {
123 opt.rect.setLeft(opt.rect.left());
124 starRect.setLeft(starRect.left());
125 starRect.setRight(starRect.right());
126 }
127 else
128 {
129 opt.rect.setRight(opt.rect.width());
130 starRect.setLeft(starRect.width());
131 starRect.setRight(starRect.width());
132 }
133
134 bool paintIcon = true;
135 bool paintText = true;
136
137 QPixmap icon;
138 switch (index.column()) {
139 case KDirModel::Name:
140 paintIcon = false;
141 break;
142
143 case KDirModel::Size:
144 paintIcon = false;
145 break;
146
147 case KDirModel::ModifiedTime:
148 paintIcon = false;
149 break;
150
151 case KDirModel::Permissions:
152 paintIcon = false; // TODO: let's think about how to represent permissions
153 break;
154
155 case KDirModel::Owner: {
156 opt.rect.setTop(option.rect.bottom() - (iconSize / 4));
157 KUser user(category);
158 QString faceIconPath = user.faceIconPath();
159
160 if (!faceIconPath.isEmpty())
161 {
162 icon = QPixmap::fromImage(QImage(faceIconPath).scaledToHeight(option.fontMetrics.height(), Qt::SmoothTransformation));
163 }
164 else
165 {
166 icon = KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup, option.fontMetrics.height());
167 }
168
169 opt.rect.setTop(opt.rect.top() - icon.height());
170
171 break;
172 }
173
174 case KDirModel::Group:
175 paintIcon = false;
176 break;
177
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());
189
190 opt.rect.setTop(opt.rect.top() - icon.height());
191
192 break;
193 }
194
195 #ifdef HAVE_NEPOMUK
196 case DolphinModel::Rating: {
197 paintText = false;
198 paintIcon = false;
199
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() );
205 break;
206 }
207
208 case DolphinModel::Tags:
209 paintIcon = false;
210 break;
211 #endif
212 }
213
214 if (paintIcon) {
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);
217
218 if (option.direction == Qt::LeftToRight)
219 {
220 opt.rect.setLeft(opt.rect.left() + icon.width() + (iconSize / 4));
221 }
222 else
223 {
224 opt.rect.setRight(opt.rect.right() + (iconSize / 4));
225 }
226 }
227
228 if (paintText) {
229 opt.rect.setTop(option.rect.top() + (iconSize / 4));
230 opt.rect.setBottom(opt.rect.bottom() - 1);
231 painter->setPen(color);
232
233 QRect textRect = opt.rect;
234
235 if (option.direction == Qt::RightToLeft)
236 {
237 textRect.setWidth(textRect.width() - (paintIcon ? icon.width() + (iconSize / 4)
238 : -(iconSize / 4)));
239 }
240
241 painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft,
242 metrics.elidedText(category, Qt::ElideRight, textRect.width()));
243 }
244
245 painter->restore();
246 }
247
248 int DolphinCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
249 {
250 int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
251 int heightWithoutIcon = option.fontMetrics.height() + (iconSize / 4) * 2 + 1; /* 1 pixel-width gradient */
252 bool paintIcon;
253
254 switch (index.column()) {
255 case KDirModel::Owner:
256 case KDirModel::Type:
257 paintIcon = true;
258 break;
259 default:
260 paintIcon = false;
261 }
262
263 if (paintIcon)
264 return qMax(heightWithoutIcon, iconSize + (iconSize / 4) * 2 + 1) /* 1 pixel-width gradient */;
265
266 return heightWithoutIcon;
267 }