]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
[Details mode] Allow to fill the column size of directories with actual size
[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 #include "kfileitemmodel.h"
22 #include "kitemlistview.h"
23
24 #include "dolphin_detailsmodesettings.h"
25
26 #include <KFormat>
27 #include <KLocalizedString>
28
29 #include <QMimeDatabase>
30
31 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
32 KStandardItemListWidgetInformant()
33 {
34 }
35
36 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
37 {
38 }
39
40 QString KFileItemListWidgetInformant::itemText(int index, const KItemListView* view) const
41 {
42 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
43 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
44
45 const KFileItem item = fileItemModel->fileItem(index);
46 return item.text();
47 }
48
49 bool KFileItemListWidgetInformant::itemIsLink(int index, const KItemListView* view) const
50 {
51 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
52 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
53
54 const KFileItem item = fileItemModel->fileItem(index);
55 return item.isLink();
56 }
57
58 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
59 const QHash<QByteArray, QVariant>& values) const
60 {
61 QString text;
62 const QVariant roleValue = values.value(role);
63
64 // Implementation note: In case if more roles require a custom handling
65 // use a hash + switch for a linear runtime.
66
67 if (role == "size") {
68 if (values.value("isDir").toBool()) {
69 // The item represents a directory.
70 if (!roleValue.isNull()) {
71 const int count = values.value("count").toInt();
72 if (count < 0) {
73 text = i18nc("@item:intable", "Unknown");
74 } else {
75 if (DetailsModeSettings::directorySizeCount()) {
76 // Show the number of sub directories instead of the file size of the directory.
77 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
78 } else {
79 // if we have directory size available
80 if (roleValue == -1) {
81 text = i18nc("@item:intable", "Unknown");
82 } else {
83 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
84 text = KFormat().formatByteSize(size);
85 }
86 }
87 }
88 }
89 } else {
90 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
91 text = KFormat().formatByteSize(size);
92 }
93 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
94 bool ok;
95 const long long time = roleValue.toLongLong(&ok);
96 if (ok && time != -1) {
97 return QLocale().toString(QDateTime::fromSecsSinceEpoch(time), QLocale::ShortFormat);
98 }
99 } else if (role == "deletiontime" || role == "imageDateTime") {
100 const QDateTime dateTime = roleValue.toDateTime();
101 text = QLocale().toString(dateTime, QLocale::ShortFormat);
102 } else {
103 text = KStandardItemListWidgetInformant::roleText(role, values);
104 }
105
106 return text;
107 }
108
109 QFont KFileItemListWidgetInformant::customizedFontForLinks(const QFont& baseFont) const
110 {
111 // The customized font should be italic if the file is a symbolic link.
112 QFont font(baseFont);
113 font.setItalic(true);
114 return font;
115 }
116
117
118 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
119 KStandardItemListWidget(informant, parent)
120 {
121 }
122
123 KFileItemListWidget::~KFileItemListWidget()
124 {
125 }
126
127 KItemListWidgetInformant* KFileItemListWidget::createInformant()
128 {
129 return new KFileItemListWidgetInformant();
130 }
131
132 bool KFileItemListWidget::isRoleRightAligned(const QByteArray& role) const
133 {
134 return role == "size";
135 }
136
137 bool KFileItemListWidget::isHidden() const
138 {
139 return data().value("isHidden").toBool();
140 }
141
142 QFont KFileItemListWidget::customizedFont(const QFont& baseFont) const
143 {
144 // The customized font should be italic if the file is a symbolic link.
145 QFont font(baseFont);
146 font.setItalic(data().value("isLink").toBool());
147 return font;
148 }
149
150 int KFileItemListWidget::selectionLength(const QString& text) const
151 {
152 // Select the text without MIME-type extension
153 int selectionLength = text.length();
154
155 // If item is a directory, use the whole text length for
156 // selection (ignore all points)
157 if(data().value("isDir").toBool()) {
158 return selectionLength;
159 }
160
161 QMimeDatabase db;
162 const QString extension = db.suffixForFileName(text);
163 if (extension.isEmpty()) {
164 // For an unknown extension just exclude the extension after
165 // the last point. This does not work for multiple extensions like
166 // *.tar.gz but usually this is anyhow a known extension.
167 selectionLength = text.lastIndexOf(QLatin1Char('.'));
168
169 // If no point could be found, use whole text length for selection.
170 if (selectionLength < 1) {
171 selectionLength = text.length();
172 }
173
174 } else {
175 selectionLength -= extension.length() + 1;
176 }
177
178 return selectionLength;
179 }
180