]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
Prepare view-engine for non-KFileItem usecase
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kfileitemlistwidget.h"
21
22 #include <KDebug>
23 #include <KGlobal>
24 #include <KLocale>
25 #include <KIO/MetaData>
26 #include <QDateTime>
27
28 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
29 KStandardItemListWidgetInformant()
30 {
31 }
32
33 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
34 {
35 }
36
37 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
38 const QHash<QByteArray, QVariant>& values) const
39 {
40 QString text;
41 const QVariant roleValue = values.value(role);
42
43 // Implementation note: In case if more roles require a custom handling
44 // use a hash + switch for a linear runtime.
45
46 if (role == "size") {
47 if (values.value("isDir").toBool()) {
48 // The item represents a directory. Show the number of sub directories
49 // instead of the file size of the directory.
50 if (!roleValue.isNull()) {
51 const int count = roleValue.toInt();
52 if (count < 0) {
53 text = i18nc("@item:intable", "Unknown");
54 } else {
55 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
56 }
57 }
58 } else {
59 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
60 text = KGlobal::locale()->formatByteSize(size);
61 }
62 } else if (role == "date") {
63 const QDateTime dateTime = roleValue.toDateTime();
64 text = KGlobal::locale()->formatDateTime(dateTime);
65 } else {
66 text = KStandardItemListWidgetInformant::roleText(role, values);
67 }
68
69 return text;
70 }
71
72 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
73 KStandardItemListWidget(informant, parent)
74 {
75 }
76
77 KFileItemListWidget::~KFileItemListWidget()
78 {
79 }
80
81 KItemListWidgetInformant* KFileItemListWidget::createInformant()
82 {
83 return new KFileItemListWidgetInformant();
84 }
85
86 bool KFileItemListWidget::isRoleRightAligned(const QByteArray& role) const
87 {
88 return role == "size";
89 }
90
91 #include "kfileitemlistwidget.moc"