]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
Revert "Remove deprecated ConfigurePreviewPluginDialog"
[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, const QHash<QByteArray, QVariant> &values) const
49 {
50 QString text;
51 const QVariant roleValue = values.value(role);
52 QLocale local;
53 KFormat formatter(local);
54
55 // Implementation note: In case if more roles require a custom handling
56 // use a hash + switch for a linear runtime.
57
58 auto formatDate = [formatter, local](const QDateTime &time) {
59 if (DetailsModeSettings::useShortRelativeDates()) {
60 return formatter.formatRelativeDateTime(time, QLocale::ShortFormat);
61 } else {
62 return local.toString(time, QLocale::ShortFormat);
63 }
64 };
65
66 if (role == "size") {
67 if (values.value("isDir").toBool()) {
68 if (!roleValue.isNull() && roleValue != -1) {
69 // The item represents a directory.
70 if (DetailsModeSettings::directorySizeCount()) {
71 // Show the number of sub directories instead of the file size of the directory.
72 const int count = values.value("count").toInt();
73 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
74 } else {
75 // if we have directory size available
76 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
77 text = formatter.formatByteSize(size);
78 }
79 }
80 } else {
81 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
82 text = formatter.formatByteSize(size);
83 }
84 } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
85 bool ok;
86 const long long time = roleValue.toLongLong(&ok);
87 if (ok && time != -1) {
88 const QDateTime dateTime = QDateTime::fromSecsSinceEpoch(time);
89 text = formatDate(dateTime);
90 }
91 } else if (role == "deletiontime" || role == "imageDateTime") {
92 const QDateTime dateTime = roleValue.toDateTime();
93 if (dateTime.isValid()) {
94 text = formatDate(dateTime);
95 }
96 } else if (role == "dimensions") {
97 const auto dimensions = roleValue.toSize();
98 if (dimensions.isValid()) {
99 text = i18nc("width × height", "%1 × %2", dimensions.width(), dimensions.height());
100 }
101 } else {
102 text = KStandardItemListWidgetInformant::roleText(role, values);
103 }
104
105 return text;
106 }
107
108 QFont KFileItemListWidgetInformant::customizedFontForLinks(const QFont &baseFont) const
109 {
110 // The customized font should be italic if the file is a symbolic link.
111 QFont font(baseFont);
112 font.setItalic(true);
113 return font;
114 }
115
116 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent)
117 : KStandardItemListWidget(informant, parent)
118 {
119 }
120
121 KFileItemListWidget::~KFileItemListWidget()
122 {
123 }
124
125 KItemListWidgetInformant *KFileItemListWidget::createInformant()
126 {
127 return new KFileItemListWidgetInformant();
128 }
129
130 bool KFileItemListWidget::isRoleRightAligned(const QByteArray &role) const
131 {
132 return role == "size";
133 }
134
135 bool KFileItemListWidget::isHidden() const
136 {
137 return data().value("isHidden").toBool();
138 }
139
140 QFont KFileItemListWidget::customizedFont(const QFont &baseFont) const
141 {
142 // The customized font should be italic if the file is a symbolic link.
143 QFont font(baseFont);
144 font.setItalic(data().value("isLink").toBool());
145 return font;
146 }
147
148 int KFileItemListWidget::selectionLength(const QString &text) const
149 {
150 // Select the text without MIME-type extension
151 int selectionLength = text.length();
152
153 // If item is a directory, use the whole text length for
154 // selection (ignore all points)
155 if (data().value("isDir").toBool()) {
156 return selectionLength;
157 }
158
159 QMimeDatabase db;
160 const QString extension = db.suffixForFileName(text);
161 if (extension.isEmpty()) {
162 // For an unknown extension just exclude the extension after
163 // the last point. This does not work for multiple extensions like
164 // *.tar.gz but usually this is anyhow a known extension.
165 selectionLength = text.lastIndexOf(QLatin1Char('.'));
166
167 // If no point could be found, use whole text length for selection.
168 if (selectionLength < 1) {
169 selectionLength = text.length();
170 }
171
172 } else {
173 selectionLength -= extension.length() + 1;
174 }
175
176 return selectionLength;
177 }
178
179 void KFileItemListWidget::hoverSequenceStarted()
180 {
181 KFileItemListView *view = listView();
182
183 if (!view) {
184 return;
185 }
186
187 const QUrl itemUrl = data().value("url").toUrl();
188
189 view->setHoverSequenceState(itemUrl, 0);
190 }
191
192 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex)
193 {
194 KFileItemListView *view = listView();
195
196 if (!view) {
197 return;
198 }
199
200 const QUrl itemUrl = data().value("url").toUrl();
201
202 view->setHoverSequenceState(itemUrl, sequenceIndex);
203
204 // Force-update the displayed icon
205 invalidateIconCache();
206 update();
207 }
208
209 void KFileItemListWidget::hoverSequenceEnded()
210 {
211 KFileItemListView *view = listView();
212
213 if (!view) {
214 return;
215 }
216
217 view->setHoverSequenceState(QUrl(), 0);
218 }
219
220 KFileItemListView *KFileItemListWidget::listView()
221 {
222 return dynamic_cast<KFileItemListView *>(parentItem());
223 }