]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinitemcategorizer.cpp
Added Rafael López's item categorizer into Dolphin (it's currently deactivated in...
[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
25 #include <klocale.h>
26 #include <kdirmodel.h>
27
28 #include <QSortFilterProxyModel>
29
30 DolphinItemCategorizer::DolphinItemCategorizer() :
31 KItemCategorizer()
32 {}
33
34 DolphinItemCategorizer::~DolphinItemCategorizer()
35 {}
36
37 QString DolphinItemCategorizer::categoryForItem(const QModelIndex& index,
38 int sortRole)
39 {
40 QString retString;
41
42 if (!index.isValid()) {
43 return retString;
44 }
45
46 // KDirModel checks columns to know to which role are
47 // we talking about
48 QModelIndex theIndex = index.model()->index(index.row(),
49 sortRole,
50 index.parent());
51
52 const QSortFilterProxyModel* proxyModel = static_cast<const QSortFilterProxyModel*>(index.model());
53 const KDirModel* dirModel = static_cast<const KDirModel*>(proxyModel->sourceModel());
54
55 QVariant data = theIndex.model()->data(theIndex, Qt::DisplayRole);
56
57 QModelIndex mappedIndex = proxyModel->mapToSource(theIndex);
58 KFileItem* item = dirModel->itemForIndex(mappedIndex);
59
60 switch (sortRole) {
61 case DolphinView::SortByName:
62 retString = data.toString().toUpper().at(0);
63 break;
64 case DolphinView::SortBySize:
65 int fileSize = (item) ? item->size() : -1;
66 if (item && item->isDir()) {
67 retString = i18n("Unknown");
68 } else if (fileSize < 5242880) {
69 retString = i18n("Small");
70 } else if (fileSize < 10485760) {
71 retString = i18n("Medium");
72 } else {
73 retString = i18n("Big");
74 }
75 break;
76 }
77
78 return retString;
79 }