]>
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 <kiconeffect.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
= item
.time(KFileItem::ModificationTime
);
126 modifiedTime
= modifiedTime
.toLocalZone();
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");
139 retString
= i18nc("@title:group Date", "More than a year");
143 case DolphinView::SortByPermissions
:
144 retString
= item
.permissionsString();
147 case DolphinView::SortByOwner
:
148 retString
= item
.user();
151 case DolphinView::SortByGroup
:
152 retString
= item
.group();
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");
164 retString
= i18nc("@title:group Size", "Big");
169 case DolphinView::SortByType
:
170 retString
= item
.mimeComment();
174 case DolphinView::SortByRating
: {
175 const quint32 rating
= DolphinSortFilterProxyModel::ratingForIndex(index
);
177 retString
= QString::number(rating
);
181 case DolphinView::SortByTags
: {
182 retString
= DolphinSortFilterProxyModel::tagsForIndex(index
);
184 if (retString
.isEmpty())
185 retString
= i18nc("@title:group Tags", "Not yet tagged");
195 void DolphinItemCategorizer::drawCategory(const QModelIndex
&index
,
197 const QStyleOption
&option
,
198 QPainter
*painter
) const
200 QRect starRect
= option
.rect
;
202 int iconSize
= KIconLoader::global()->currentSize(K3Icon::Small
);
203 const QString category
= categoryForItem(index
, sortRole
);
205 QColor color
= option
.palette
.color(QPalette::Text
);
208 painter
->setRenderHint(QPainter::Antialiasing
);
210 QStyleOptionButton opt
;
212 opt
.rect
= option
.rect
;
213 opt
.palette
= option
.palette
;
214 opt
.direction
= option
.direction
;
217 if (option
.state
& QStyle::State_MouseOver
)
219 const QPalette::ColorGroup group
=
220 option
.state
& QStyle::State_Enabled
?
221 QPalette::Normal
: QPalette::Disabled
;
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
);
230 painter
->fillRect(option
.rect
, gradient
);
233 QFont painterFont
= painter
->font();
234 painterFont
.setWeight(QFont::Bold
);
235 QFontMetrics
metrics(painterFont
);
236 painter
->setFont(painterFont
);
239 path
.addRect(option
.rect
.left(),
240 option
.rect
.bottom() - 2,
244 QLinearGradient
gradient(option
.rect
.topLeft(),
245 option
.rect
.bottomRight());
246 gradient
.setColorAt(0, color
);
247 gradient
.setColorAt(1, Qt::transparent
);
249 painter
->setBrush(gradient
);
250 painter
->fillPath(path
, gradient
);
252 opt
.rect
.setLeft(opt
.rect
.left() + (iconSize
/ 4));
253 starRect
.setLeft(starRect
.left() + (iconSize
/ 4));
254 starRect
.setRight(starRect
.right() + (iconSize
/ 4));
256 bool paintIcon
= true;
257 bool paintText
= true;
261 case DolphinView::SortByName
:
265 case DolphinView::SortByDate
:
269 case DolphinView::SortByPermissions
:
270 paintIcon
= false; // FIXME: let's think about how to represent permissions
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"))
278 icon
= QPixmap::fromImage(QImage(user
.homeDir() + QDir::separator() + ".face.icon")).scaled(iconSize
, iconSize
);
282 icon
= KIconLoader::global()->loadIcon("user", K3Icon::Small
);
287 case DolphinView::SortByGroup
:
291 case DolphinView::SortBySize
:
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()),
305 case DolphinView::SortByRating
: {
309 starRect
.setTop(option
.rect
.top() + (option
.rect
.height() / 2) - (iconSize
/ 2));
310 starRect
.setSize(QSize(iconSize
, iconSize
));
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
);
316 QImage disabledImage
= disabledIcon
.toImage();
317 KIconEffect::toGray(disabledImage
, 1.0);
318 QPixmap disabledPixmap
= QPixmap::fromImage(disabledImage
);
320 int rating
= category
.toInt();
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 */);
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
));
337 for (int i
= rating
; i
< 9; i
+= 2) {
338 painter
->drawPixmap(starRect
, disabledPixmap
);
339 starRect
.setLeft(starRect
.left() + iconSize
+ (iconSize
/ 4));
345 case DolphinView::SortByTags
:
352 painter
->drawPixmap(QRect(opt
.rect
.left(), opt
.rect
.top(), iconSize
, iconSize
), icon
);
353 opt
.rect
.setLeft(opt
.rect
.left() + iconSize
+ (iconSize
/ 4));
357 opt
.rect
.setTop(option
.rect
.top() + (iconSize
/ 4));
358 opt
.rect
.setBottom(opt
.rect
.bottom() - 2);
359 painter
->setPen(color
);
361 painter
->drawText(opt
.rect
, Qt::AlignVCenter
| Qt::AlignLeft
,
362 metrics
.elidedText(category
, Qt::ElideRight
, opt
.rect
.width()));
368 int DolphinItemCategorizer::categoryHeight(const QStyleOption
&option
) const
370 int iconSize
= KIconLoader::global()->currentSize(K3Icon::Small
);
372 return qMax(option
.fontMetrics
.height() + (iconSize
/ 4) * 2 + 2, iconSize
+ (iconSize
/ 4) * 2 + 2) /* 2 gradient */;