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