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