]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
fix: forgot to invoke base implementation
[dolphin.git] / src / dolphinitemcategorizer.cpp
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
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 "dolphinitemcategorizer.h"
22
23 #include "dolphinview.h"
24 #include "dolphinsortfilterproxymodel.h"
25
26 #ifdef HAVE_NEPOMUK
27 #include <config-nepomuk.h>
28 #include <nepomuk/global.h>
29 #include <nepomuk/resource.h>
30 #endif
31
32 #include <kdatetime.h>
33 #include <kdirmodel.h>
34 #include <kfileitem.h>
35 #include <kiconloader.h>
36 #include <klocale.h>
37 #include <kurl.h>
38 #include <kuser.h>
39 #include <kmimetype.h>
40 #include <kstandarddirs.h>
41 #include <kiconeffect.h>
42
43 #include <QList>
44 #include <QSortFilterProxyModel>
45 #include <QPainter>
46 #include <QDir>
47
48 DolphinItemCategorizer::DolphinItemCategorizer() :
49 KItemCategorizer()
50 {
51 }
52
53 DolphinItemCategorizer::~DolphinItemCategorizer()
54 {
55 }
56
57 QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
58 int sortRole) const
59 {
60 QString retString;
61
62 if (!index.isValid())
63 {
64 return retString;
65 }
66
67 const KDirModel *dirModel = qobject_cast<const KDirModel*>(index.model());
68 KFileItem item = dirModel->itemForIndex(index);
69
70 switch (sortRole)
71 {
72 case DolphinView::SortByName:
73 {
74 // KDirModel checks columns to know to which role are
75 // we talking about
76 QModelIndex theIndex = index.model()->index(index.row(),
77 KDirModel::Name,
78 index.parent());
79
80 if (!theIndex.isValid()) {
81 return retString;
82 }
83
84 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
85 if (data.toString().size())
86 {
87 if (!item.isHidden() && data.toString().at(0).isLetter())
88 retString = data.toString().toUpper().at(0);
89 else if (item.isHidden() && data.toString().at(0) == '.' &&
90 data.toString().at(1).isLetter())
91 retString = data.toString().toUpper().at(1);
92 else if (item.isHidden() && data.toString().at(0) == '.' &&
93 !data.toString().at(1).isLetter())
94 retString = i18nc("@title:group Name", "Others");
95 else if (item.isHidden() && data.toString().at(0) != '.')
96 retString = data.toString().toUpper().at(0);
97 else if (item.isHidden())
98 retString = data.toString().toUpper().at(0);
99 else
100 {
101 bool validCategory = false;
102
103 const QString str(data.toString().toUpper());
104 const QChar* currA = str.unicode();
105 while (!currA->isNull() && !validCategory) {
106 if (currA->isLetter())
107 validCategory = true;
108 else if (currA->isDigit())
109 return i18nc("@title:group", "Others");
110 else
111 ++currA;
112 }
113
114 if (!validCategory)
115 retString = i18nc("@title:group Name", "Others");
116 else
117 retString = *currA;
118 }
119 }
120 break;
121 }
122
123 case DolphinView::SortByDate:
124 {
125 KDateTime modifiedTime = item.time(KFileItem::ModificationTime);
126 modifiedTime = modifiedTime.toLocalZone();
127
128 if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 0)
129 retString = i18nc("@title:group Date", "Today");
130 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) == 1)
131 retString = i18nc("@title:group Date", "Yesterday");
132 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 7)
133 retString = i18nc("@title:group Date", "Less than a week");
134 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 31)
135 retString = i18nc("@title:group Date", "Less than a month");
136 else if (modifiedTime.daysTo(KDateTime::currentLocalDateTime()) < 365)
137 retString = i18nc("@title:group Date", "Less than a year");
138 else
139 retString = i18nc("@title:group Date", "More than a year");
140 break;
141 }
142
143 case DolphinView::SortByPermissions:
144 retString = item.permissionsString();
145 break;
146
147 case DolphinView::SortByOwner:
148 retString = item.user();
149 break;
150
151 case DolphinView::SortByGroup:
152 retString = item.group();
153 break;
154
155 case DolphinView::SortBySize: {
156 const int fileSize = !item.isNull() ? item.size() : -1;
157 if (!item.isNull() && item.isDir()) {
158 retString = i18nc("@title:group Size", "Folders");
159 } else if (fileSize < 5242880) {
160 retString = i18nc("@title:group Size", "Small");
161 } else if (fileSize < 10485760) {
162 retString = i18nc("@title:group Size", "Medium");
163 } else {
164 retString = i18nc("@title:group Size", "Big");
165 }
166 break;
167 }
168
169 case DolphinView::SortByType:
170 retString = item.mimeComment();
171 break;
172
173 #ifdef HAVE_NEPOMUK
174 case DolphinView::SortByRating: {
175 const quint32 rating = DolphinSortFilterProxyModel::ratingForIndex(index);
176
177 retString = QString::number(rating);
178 break;
179 }
180
181 case DolphinView::SortByTags: {
182 retString = DolphinSortFilterProxyModel::tagsForIndex(index);
183
184 if (retString.isEmpty())
185 retString = i18nc("@title:group Tags", "Not yet tagged");
186
187 break;
188 }
189 #endif
190 }
191
192 return retString;
193 }
194
195 void DolphinItemCategorizer::drawCategory(const QModelIndex &index,
196 int sortRole,
197 const QStyleOption &option,
198 QPainter *painter) const
199 {
200 QRect starRect = option.rect;
201
202 int iconSize = KIconLoader::global()->currentSize(K3Icon::Small);
203 const QString category = categoryForItem(index, sortRole);
204
205 QColor color = option.palette.color(QPalette::Text);
206
207 painter->save();
208 painter->setRenderHint(QPainter::Antialiasing);
209
210 QStyleOptionButton opt;
211
212 opt.rect = option.rect;
213 opt.palette = option.palette;
214 opt.direction = option.direction;
215 opt.text = category;
216
217 if (option.state & QStyle::State_MouseOver)
218 {
219 const QPalette::ColorGroup group =
220 option.state & QStyle::State_Enabled ?
221 QPalette::Normal : QPalette::Disabled;
222
223 QLinearGradient gradient(option.rect.topLeft(),
224 option.rect.bottomRight());
225 gradient.setColorAt(0,
226 option.palette.color(group,
227 QPalette::Highlight).light());
228 gradient.setColorAt(1, Qt::transparent);
229
230 painter->fillRect(option.rect, gradient);
231 }
232
233 QFont painterFont = painter->font();
234 painterFont.setWeight(QFont::Bold);
235 QFontMetrics metrics(painterFont);
236 painter->setFont(painterFont);
237
238 QPainterPath path;
239 path.addRect(option.rect.left(),
240 option.rect.bottom() - 2,
241 option.rect.width(),
242 2);
243
244 QLinearGradient gradient(option.rect.topLeft(),
245 option.rect.bottomRight());
246 gradient.setColorAt(0, color);
247 gradient.setColorAt(1, Qt::transparent);
248
249 painter->setBrush(gradient);
250 painter->fillPath(path, gradient);
251
252 opt.rect.setLeft(opt.rect.left() + (iconSize / 4));
253 starRect.setLeft(starRect.left() + (iconSize / 4));
254 starRect.setRight(starRect.right() + (iconSize / 4));
255
256 bool paintIcon = true;
257 bool paintText = true;
258
259 QPixmap icon;
260 switch (sortRole) {
261 case DolphinView::SortByName:
262 paintIcon = false;
263 break;
264
265 case DolphinView::SortByDate:
266 paintIcon = false;
267 break;
268
269 case DolphinView::SortByPermissions:
270 paintIcon = false; // FIXME: let's think about how to represent permissions
271 break;
272
273 case DolphinView::SortByOwner: {
274 opt.rect.setTop(option.rect.top() + (iconSize / 4));
275 KUser user(category);
276 if (QFile::exists(user.homeDir() + QDir::separator() + ".face.icon"))
277 {
278 icon = QPixmap::fromImage(QImage(user.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize, iconSize);
279 }
280 else
281 {
282 icon = KIconLoader::global()->loadIcon("user", K3Icon::Small);
283 }
284 break;
285 }
286
287 case DolphinView::SortByGroup:
288 paintIcon = false;
289 break;
290
291 case DolphinView::SortBySize:
292 paintIcon = false;
293 break;
294
295 case DolphinView::SortByType: {
296 opt.rect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
297 const KDirModel *model = static_cast<const KDirModel*>(index.model());
298 KFileItem item = model->itemForIndex(index);
299 icon = KIconLoader::global()->loadIcon(KMimeType::iconNameForUrl(item.url()),
300 K3Icon::Small);
301 break;
302 }
303
304 #ifdef HAVE_NEPOMUK
305 case DolphinView::SortByRating: {
306 paintText = false;
307 paintIcon = false;
308
309 starRect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
310 starRect.setSize(QSize(iconSize, iconSize));
311
312 QPixmap pixmap = KIconLoader::global()->loadIcon("rating", K3Icon::Small);
313 QPixmap smallPixmap = KIconLoader::global()->loadIcon("rating", K3Icon::NoGroup, iconSize / 2);
314 QPixmap disabledIcon = KIconLoader::global()->loadIcon("rating", K3Icon::Small);
315
316 QImage disabledImage = disabledIcon.toImage();
317 KIconEffect::toGray(disabledImage, 1.0);
318 QPixmap disabledPixmap = QPixmap::fromImage(disabledImage);
319
320 int rating = category.toInt();
321
322 for (int i = 0; i < rating - (rating % 2); i += 2) {
323 painter->drawPixmap(starRect, pixmap);
324 starRect.setLeft(starRect.left() + iconSize + (iconSize / 4) /* separator between stars */);
325 }
326
327 if (rating && rating % 2) {
328 starRect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 4));
329 starRect.setSize(QSize(iconSize / 2, iconSize / 2));
330 painter->drawPixmap(starRect, smallPixmap);
331 starRect.setTop(opt.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
332 starRect.setSize(QSize(iconSize / 2, iconSize / 2));
333 starRect.setLeft(starRect.left() + (iconSize / 2) + (iconSize / 4));
334 starRect.setSize(QSize(iconSize, iconSize));
335 }
336
337 for (int i = rating; i < 9; i += 2) {
338 painter->drawPixmap(starRect, disabledPixmap);
339 starRect.setLeft(starRect.left() + iconSize + (iconSize / 4));
340 }
341
342 break;
343 }
344
345 case DolphinView::SortByTags:
346 paintIcon = false;
347 break;
348 #endif
349 }
350
351 if (paintIcon) {
352 painter->drawPixmap(QRect(opt.rect.left(), opt.rect.top(), iconSize, iconSize), icon);
353 opt.rect.setLeft(opt.rect.left() + iconSize + (iconSize / 4));
354 }
355
356 if (paintText) {
357 opt.rect.setTop(option.rect.top() + (iconSize / 4));
358 opt.rect.setBottom(opt.rect.bottom() - 2);
359 painter->setPen(color);
360
361 painter->drawText(opt.rect, Qt::AlignVCenter | Qt::AlignLeft,
362 metrics.elidedText(category, Qt::ElideRight, opt.rect.width()));
363 }
364
365 painter->restore();
366 }
367
368 int DolphinItemCategorizer::categoryHeight(const QStyleOption &option) const
369 {
370 int iconSize = KIconLoader::global()->currentSize(K3Icon::Small);
371
372 return qMax(option.fontMetrics.height() + (iconSize / 4) * 2 + 2, iconSize + (iconSize / 4) * 2 + 2) /* 2 gradient */;
373 }