]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
a62b75824c268ec4442d7ef61692af569aac03eb
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "kfileitemlistwidget.h"
8 #include "kfileitemlistview.h"
9 #include "kfileitemmodel.h"
10 #include "kitemlistview.h"
11
12 #include "dolphin_detailsmodesettings.h"
13
14 #include <KFormat>
15 #include <KLocalizedString>
16
17 #include <QGraphicsScene>
18 #include <QGraphicsView>
19 #include <QMimeDatabase>
20
21 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
22 KStandardItemListWidgetInformant()
23 {
24 }
25
26 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
27 {
28 }
29
30 QString KFileItemListWidgetInformant::itemText(int index, const KItemListView* view) const
31 {
32 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
33 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
34
35 const KFileItem item = fileItemModel->fileItem(index);
36 return item.text();
37 }
38
39 bool KFileItemListWidgetInformant::itemIsLink(int index, const KItemListView* view) const
40 {
41 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
42 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
43
44 const KFileItem item = fileItemModel->fileItem(index);
45 return item.isLink();
46 }
47
48 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
49 const QHash<QByteArray, QVariant>& values) const
50 {
51 QString text;
52 const QVariant roleValue = values.value(role);
53 QLocale local;
54 KFormat formatter(local);
55
56 // Implementation note: In case if more roles require a custom handling
57 // use a hash + switch for a linear runtime.
58
59 auto formatDate = [formatter, local](const QDateTime& time) {
60 if (DetailsModeSettings::useShortRelativeDates()) {
61 return formatter.formatRelativeDateTime(time, QLocale::ShortFormat);
62 } else {
63 return local.toString(time, QLocale::ShortFormat);
64 }
65 };
66
67 if (role == "size") {
68 if (values.value("isDir").toBool()) {
69 if (!roleValue.isNull() && roleValue != -1) {
70 // The item represents a directory.
71 if (DetailsModeSettings::directorySizeCount()) {
72 // Show the number of sub directories instead of the file size of the directory.
73 const int count = values.value("count").toInt();
74 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
75 } else {
76 // if we have directory size available
77 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
78 text = formatter.formatByteSize(size);
79 }
80 }
81 } else {
82 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
83 text = formatter.formatByteSize(size);
84 }
85 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
86 bool ok;
87 const long long time = roleValue.toLongLong(&ok);
88 if (ok && time != -1) {
89 const QDateTime dateTime = QDateTime::fromSecsSinceEpoch(time);
90 text = formatDate(dateTime);
91 }
92 } else if (role == "deletiontime" || role == "imageDateTime") {
93 const QDateTime dateTime = roleValue.toDateTime();
94 if (dateTime.isValid()) {
95 text = formatDate(dateTime);
96 }
97 } else if (role == "dimensions") {
98 const auto dimensions = roleValue.toSize();
99 if (dimensions.isValid()) {
100 text = i18nc("width × height", "%1 × %2", dimensions.width(), dimensions.height());
101 }
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 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
118 KStandardItemListWidget(informant, parent)
119 {
120 }
121
122 KFileItemListWidget::~KFileItemListWidget()
123 {
124 }
125
126 KItemListWidgetInformant* KFileItemListWidget::createInformant()
127 {
128 return new KFileItemListWidgetInformant();
129 }
130
131 bool KFileItemListWidget::isRoleRightAligned(const QByteArray& role) const
132 {
133 return role == "size";
134 }
135
136 bool KFileItemListWidget::isHidden() const
137 {
138 return data().value("isHidden").toBool();
139 }
140
141 QFont KFileItemListWidget::customizedFont(const QFont& baseFont) const
142 {
143 // The customized font should be italic if the file is a symbolic link.
144 QFont font(baseFont);
145 font.setItalic(data().value("isLink").toBool());
146 return font;
147 }
148
149 int KFileItemListWidget::selectionLength(const QString& text) const
150 {
151 // Select the text without MIME-type extension
152 int selectionLength = text.length();
153
154 // If item is a directory, use the whole text length for
155 // selection (ignore all points)
156 if(data().value("isDir").toBool()) {
157 return selectionLength;
158 }
159
160 QMimeDatabase db;
161 const QString extension = db.suffixForFileName(text);
162 if (extension.isEmpty()) {
163 // For an unknown extension just exclude the extension after
164 // the last point. This does not work for multiple extensions like
165 // *.tar.gz but usually this is anyhow a known extension.
166 selectionLength = text.lastIndexOf(QLatin1Char('.'));
167
168 // If no point could be found, use whole text length for selection.
169 if (selectionLength < 1) {
170 selectionLength = text.length();
171 }
172
173 } else {
174 selectionLength -= extension.length() + 1;
175 }
176
177 return selectionLength;
178 }
179
180 void KFileItemListWidget::hoverSequenceStarted()
181 {
182 KFileItemListView* view = listView();
183
184 if (!view) {
185 return;
186 }
187
188 const QUrl itemUrl = data().value("url").toUrl();
189
190 view->setHoverSequenceState(itemUrl, 0);
191 }
192
193 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex)
194 {
195 KFileItemListView* view = listView();
196
197 if (!view) {
198 return;
199 }
200
201 const QUrl itemUrl = data().value("url").toUrl();
202
203 view->setHoverSequenceState(itemUrl, sequenceIndex);
204
205 // Force-update the displayed icon
206 invalidateIconCache();
207 update();
208 }
209
210 void KFileItemListWidget::hoverSequenceEnded()
211 {
212 KFileItemListView* view = listView();
213
214 if (!view) {
215 return;
216 }
217
218 view->setHoverSequenceState(QUrl(), 0);
219 }
220
221 KFileItemListView* KFileItemListWidget::listView()
222 {
223 return dynamic_cast<KFileItemListView*>(parentItem());
224 }
225