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