]>
cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@gmail.com>
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 "dolphinitemcategorizer.h"
23 #include "dolphinview.h"
24 #include "dolphinsortfilterproxymodel.h"
27 #include <config-nepomuk.h>
28 #include <nepomuk/global.h>
29 #include <nepomuk/resource.h>
32 #include <kdatetime.h>
33 #include <kdirmodel.h>
34 #include <kfileitem.h>
35 #include <kiconloader.h>
39 #include <kmimetype.h>
40 #include <kstandarddirs.h>
41 #include <kpixmapeffect.h>
44 #include <QSortFilterProxyModel>
48 DolphinItemCategorizer::DolphinItemCategorizer() :
53 DolphinItemCategorizer::~DolphinItemCategorizer()
57 QString
DolphinItemCategorizer::categoryForItem(const QModelIndex
& index
,
67 const KDirModel
*dirModel
= qobject_cast
<const KDirModel
*>(index
.model());
68 KFileItem
*item
= dirModel
->itemForIndex(index
);
72 case DolphinView::SortByName
:
74 // KDirModel checks columns to know to which role are
76 QModelIndex theIndex
= index
.model()->index(index
.row(),
80 if (!theIndex
.isValid()) {
84 QVariant data
= theIndex
.model()->data(theIndex
, Qt::DisplayRole
);
85 if (data
.toString().size())
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);
101 bool validCategory
= false;
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");
115 retString
= i18nc("@title:group Name", "Others");
123 case DolphinView::SortByDate
:
125 KDateTime modifiedTime
;
126 modifiedTime
.setTime_t(item
->time(KIO::UDS_MODIFICATION_TIME
));
127 modifiedTime
= modifiedTime
.toLocalZone();
129 if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) == 0)
130 retString
= i18nc("@title:group Date", "Today");
131 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) == 1)
132 retString
= i18nc("@title:group Date", "Yesterday");
133 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) < 7)
134 retString
= i18nc("@title:group Date", "Less than a week");
135 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) < 31)
136 retString
= i18nc("@title:group Date", "Less than a month");
137 else if (modifiedTime
.daysTo(KDateTime::currentLocalDateTime()) < 365)
138 retString
= i18nc("@title:group Date", "Less than a year");
140 retString
= i18nc("@title:group Date", "More than a year");
144 case DolphinView::SortByPermissions
:
145 retString
= item
->permissionsString();
148 case DolphinView::SortByOwner
:
149 retString
= item
->user();
152 case DolphinView::SortByGroup
:
153 retString
= item
->group();
156 case DolphinView::SortBySize
: {
157 const int fileSize
= item
? item
->size() : -1;
158 if (item
&& item
->isDir()) {
159 retString
= i18nc("@title:group Size", "Folders");
160 } else if (fileSize
< 5242880) {
161 retString
= i18nc("@title:group Size", "Small");
162 } else if (fileSize
< 10485760) {
163 retString
= i18nc("@title:group Size", "Medium");
165 retString
= i18nc("@title:group Size", "Big");
170 case DolphinView::SortByType
:
171 retString
= item
->mimeComment();
175 case DolphinView::SortByRating
: {
176 const quint32 rating
= DolphinSortFilterProxyModel::ratingForIndex(index
);
178 retString
= QString::number(rating
);
182 case DolphinView::SortByTags
: {
183 retString
= DolphinSortFilterProxyModel::tagsForIndex(index
);
185 if (retString
.isEmpty())
186 retString
= i18nc("@title:group Tags", "Not yet tagged");
196 void DolphinItemCategorizer::drawCategory(const QModelIndex
&index
,
198 const QStyleOption
&option
,
199 QPainter
*painter
) const
201 QRect starRect
= option
.rect
;
202 int iconSize
= KIconLoader::global()->theme()->defaultSize(K3Icon::Small
);
204 const QString category
= categoryForItem(index
, sortRole
);
206 QColor color
= option
.palette
.color(QPalette::Text
);
209 painter
->setRenderHint(QPainter::Antialiasing
);
211 QStyleOptionButton opt
;
213 opt
.rect
= option
.rect
;
214 opt
.palette
= option
.palette
;
215 opt
.direction
= option
.direction
;
218 if (option
.state
& QStyle::State_MouseOver
)
220 const QPalette::ColorGroup group
=
221 option
.state
& QStyle::State_Enabled
?
222 QPalette::Normal
: QPalette::Disabled
;
224 QLinearGradient
gradient(option
.rect
.topLeft(),
225 option
.rect
.bottomRight());
226 gradient
.setColorAt(0,
227 option
.palette
.color(group
,
228 QPalette::Highlight
).light());
229 gradient
.setColorAt(1, Qt::transparent
);
231 painter
->fillRect(option
.rect
, gradient
);
234 QFont painterFont
= painter
->font();
235 painterFont
.setWeight(QFont::Bold
);
236 QFontMetrics
metrics(painterFont
);
237 painter
->setFont(painterFont
);
240 path
.addRect(option
.rect
.left(),
241 option
.rect
.bottom() - 2,
245 QLinearGradient
gradient(option
.rect
.topLeft(),
246 option
.rect
.bottomRight());
247 gradient
.setColorAt(0, color
);
248 gradient
.setColorAt(1, Qt::transparent
);
250 painter
->setBrush(gradient
);
251 painter
->fillPath(path
, gradient
);
253 opt
.rect
.setLeft(opt
.rect
.left() + (iconSize
/ 4));
254 starRect
.setLeft(starRect
.left() + (iconSize
/ 4));
255 starRect
.setRight(starRect
.right() + (iconSize
/ 4));
257 bool paintIcon
= true;
258 bool paintText
= true;
262 case DolphinView::SortByName
:
266 case DolphinView::SortByDate
:
270 case DolphinView::SortByPermissions
:
271 paintIcon
= false; // FIXME: let's think about how to represent permissions
274 case DolphinView::SortByOwner
: {
275 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
276 KUser
user(category
);
277 if (QFile::exists(user
.homeDir() + QDir::separator() + ".face.icon"))
279 icon
= QPixmap::fromImage(QImage(user
.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize
, iconSize
);
283 icon
= KIconLoader::global()->loadIcon("user", K3Icon::Small
);
288 case DolphinView::SortByGroup
:
292 case DolphinView::SortBySize
:
296 case DolphinView::SortByType
: {
297 opt
.rect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
298 const KDirModel
*model
= static_cast<const KDirModel
*>(index
.model());
299 KFileItem
*item
= model
->itemForIndex(index
);
300 icon
= KIconLoader::global()->loadIcon(KMimeType::iconNameForUrl(item
->url()),
306 case DolphinView::SortByRating
: {
310 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
311 starRect
.setSize(QSize(iconSize
, iconSize
));
313 QPixmap pixmap
= KIconLoader::global()->loadIcon("rating", K3Icon::Small
);
314 QPixmap smallPixmap
= KIconLoader::global()->loadIcon("rating", K3Icon::NoGroup
, iconSize
/ 2);
315 QPixmap disabledPixmap
= KIconLoader::global()->loadIcon("rating", K3Icon::Small
);
317 KPixmapEffect::toGray(disabledPixmap
, false);
319 int rating
= category
.toInt();
321 for (int i
= 0; i
< rating
- (rating
% 2); i
+= 2) {
322 painter
->drawPixmap(starRect
, pixmap
);
323 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4) /* separator between stars */);
326 if (rating
&& rating
% 2) {
327 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 4));
328 starRect
.setSize(QSize(iconSize
/ 2, iconSize
/ 2));
329 painter
->drawPixmap(starRect
, smallPixmap
);
330 starRect
.setTop(opt
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
331 starRect
.setSize(QSize(iconSize
/ 2, iconSize
/ 2));
332 starRect
.setLeft(starRect
.left() + (iconSize
/ 2) + (iconSize
/ 4));
333 starRect
.setSize(QSize(iconSize
, iconSize
));
336 for (int i
= rating
; i
< 9; i
+= 2) {
337 painter
->drawPixmap(starRect
, disabledPixmap
);
338 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4));
344 case DolphinView::SortByTags
:
351 painter
->drawPixmap(QRect(opt
.rect
.left(), opt
.rect
.top(), iconSize
, iconSize
), icon
);
352 opt
.rect
.setLeft(opt
.rect
.left() + iconSize
+ (iconSize
/ 4));
356 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
357 opt
.rect
.setBottom(opt
.rect
.bottom() - 2);
358 painter
->setPen(color
);
360 painter
->drawText(opt
.rect
, Qt::AlignVCenter
| Qt::AlignLeft
,
361 metrics
.elidedText(category
, Qt::ElideRight
, opt
.rect
.width()));
367 int DolphinItemCategorizer::categoryHeight(const QStyleOption
&option
) const
369 int iconSize
= KIconLoader::global()->theme()->defaultSize(K3Icon::Small
);
371 return qMax(option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 2, iconSize
+ (iconSize
/ 4) * 2 + 2) /* 2 gradient */;