]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
Merge branch 'ivan/dolphin-activities'
[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 <kmimetype.h>
23 #include <KDebug>
24 #include <KGlobal>
25 #include <KLocale>
26 #include <KIO/MetaData>
27 #include <QDateTime>
28
29 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
30 KStandardItemListWidgetInformant()
31 {
32 }
33
34 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
35 {
36 }
37
38 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
39 const QHash<QByteArray, QVariant>& values) const
40 {
41 QString text;
42 const QVariant roleValue = values.value(role);
43
44 // Implementation note: In case if more roles require a custom handling
45 // use a hash + switch for a linear runtime.
46
47 if (role == "size") {
48 if (values.value("isDir").toBool()) {
49 // The item represents a directory. Show the number of sub directories
50 // instead of the file size of the directory.
51 if (!roleValue.isNull()) {
52 const int count = roleValue.toInt();
53 if (count < 0) {
54 text = i18nc("@item:intable", "Unknown");
55 } else {
56 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
57 }
58 }
59 } else {
60 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
61 text = KGlobal::locale()->formatByteSize(size);
62 }
63 } else if (role == "date") {
64 const QDateTime dateTime = roleValue.toDateTime();
65 text = KGlobal::locale()->formatDateTime(dateTime);
66 } else {
67 text = KStandardItemListWidgetInformant::roleText(role, values);
68 }
69
70 return text;
71 }
72
73 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
74 KStandardItemListWidget(informant, parent)
75 {
76 }
77
78 KFileItemListWidget::~KFileItemListWidget()
79 {
80 }
81
82 KItemListWidgetInformant* KFileItemListWidget::createInformant()
83 {
84 return new KFileItemListWidgetInformant();
85 }
86
87 bool KFileItemListWidget::isRoleRightAligned(const QByteArray& role) const
88 {
89 return role == "size";
90 }
91
92 bool KFileItemListWidget::isHidden() const
93 {
94 return data().value("text").toString().startsWith(QLatin1Char('.'));
95 }
96
97 QFont KFileItemListWidget::customizedFont(const QFont& baseFont) const
98 {
99 // The customized font should be italic if the file is a symbolic link.
100 QFont font(baseFont);
101 font.setItalic(data().value("isLink").toBool());
102 return font;
103 }
104
105 int KFileItemListWidget::selectionLength(const QString& text) const
106 {
107 // Select the text without MIME-type extension
108 int selectionLength = text.length();
109
110 // If item is a directory, use the whole text length for
111 // selection (ignore all points)
112 if(data().value("isDir").toBool()) {
113 return selectionLength;
114 }
115
116 const QString extension = KMimeType::extractKnownExtension(text);
117 if (extension.isEmpty()) {
118 // For an unknown extension just exclude the extension after
119 // the last point. This does not work for multiple extensions like
120 // *.tar.gz but usually this is anyhow a known extension.
121 selectionLength = text.lastIndexOf(QLatin1Char('.'));
122
123 // If no point could be found, use whole text length for selection.
124 if (selectionLength < 1) {
125 selectionLength = text.length();
126 }
127
128 } else {
129 selectionLength -= extension.length() + 1;
130 }
131
132 return selectionLength;
133 }
134
135 #include "kfileitemlistwidget.moc"