]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
"Group by" exists, group sorting rule is separate from sorting rule. Very WIP and...
[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_contentdisplaysettings.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 (ContentDisplaySettings::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 (ContentDisplaySettings::directorySizeMode() == ContentDisplaySettings::EnumDirectorySizeMode::ContentCount
71 || roleValue == -2 /* size is invalid */) {
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 if (role == "permissions") {
103 const auto permissions = roleValue.value<QVariantList>();
104
105 switch (ContentDisplaySettings::usePermissionsFormat()) {
106 case ContentDisplaySettings::EnumUsePermissionsFormat::SymbolicFormat:
107 text = permissions.at(0).toString();
108 break;
109 case ContentDisplaySettings::EnumUsePermissionsFormat::NumericFormat:
110 text = QString::number(permissions.at(1).toInt(), 8);
111 break;
112 case ContentDisplaySettings::EnumUsePermissionsFormat::CombinedFormat:
113 text = QLatin1String("%1 (%2)").arg(permissions.at(0).toString()).arg(permissions.at(1).toInt(), 0, 8);
114 break;
115 }
116 } else {
117 text = KStandardItemListWidgetInformant::roleText(role, values);
118 }
119
120 return text;
121 }
122
123 QFont KFileItemListWidgetInformant::customizedFontForLinks(const QFont &baseFont) const
124 {
125 // The customized font should be italic if the file is a symbolic link.
126 QFont font(baseFont);
127 font.setItalic(true);
128 return font;
129 }
130
131 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant *informant, QGraphicsItem *parent)
132 : KStandardItemListWidget(informant, parent)
133 {
134 }
135
136 KFileItemListWidget::~KFileItemListWidget()
137 {
138 }
139
140 KItemListWidgetInformant *KFileItemListWidget::createInformant()
141 {
142 return new KFileItemListWidgetInformant();
143 }
144
145 bool KFileItemListWidget::isRoleRightAligned(const QByteArray &role) const
146 {
147 return role == "size" || role == "permissions";
148 }
149
150 bool KFileItemListWidget::isHidden() const
151 {
152 return data().value("isHidden").toBool();
153 }
154
155 QFont KFileItemListWidget::customizedFont(const QFont &baseFont) const
156 {
157 // The customized font should be italic if the file is a symbolic link.
158 QFont font(baseFont);
159 font.setItalic(data().value("isLink").toBool());
160 return font;
161 }
162
163 int KFileItemListWidget::selectionLength(const QString &text) const
164 {
165 // Select the text without MIME-type extension
166 int selectionLength = text.length();
167
168 // If item is a directory, use the whole text length for
169 // selection (ignore all points)
170 if (data().value("isDir").toBool()) {
171 return selectionLength;
172 }
173
174 QMimeDatabase db;
175 const QString extension = db.suffixForFileName(text);
176 if (extension.isEmpty()) {
177 // For an unknown extension just exclude the extension after
178 // the last point. This does not work for multiple extensions like
179 // *.tar.gz but usually this is anyhow a known extension.
180 selectionLength = text.lastIndexOf(QLatin1Char('.'));
181
182 // If no point could be found, use whole text length for selection.
183 if (selectionLength < 1) {
184 selectionLength = text.length();
185 }
186
187 } else {
188 selectionLength -= extension.length() + 1;
189 }
190
191 return selectionLength;
192 }
193
194 void KFileItemListWidget::hoverSequenceStarted()
195 {
196 KFileItemListView *view = listView();
197
198 if (!view) {
199 return;
200 }
201
202 const QUrl itemUrl = data().value("url").toUrl();
203
204 view->setHoverSequenceState(itemUrl, 0);
205 }
206
207 void KFileItemListWidget::forceUpdate()
208 {
209 updateAdditionalInfoTextColor();
210 // icon layout does not include the icons in the item selection rectangle
211 // so its icon does not need updating
212 if (listView()->itemLayout() != KStandardItemListView::ItemLayout::IconsLayout) {
213 invalidateIconCache();
214 }
215 update();
216 }
217
218 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex)
219 {
220 KFileItemListView *view = listView();
221
222 if (!view) {
223 return;
224 }
225
226 const QUrl itemUrl = data().value("url").toUrl();
227
228 view->setHoverSequenceState(itemUrl, sequenceIndex);
229
230 // Force-update the displayed icon
231 invalidateIconCache();
232 update();
233 }
234
235 void KFileItemListWidget::hoverSequenceEnded()
236 {
237 KFileItemListView *view = listView();
238
239 if (!view) {
240 return;
241 }
242
243 view->setHoverSequenceState(QUrl(), 0);
244 }
245
246 KFileItemListView *KFileItemListWidget::listView()
247 {
248 return dynamic_cast<KFileItemListView *>(parentItem());
249 }
250
251 #include "moc_kfileitemlistwidget.cpp"