]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #include "kfileitemlistwidget.h"
8 #include "kfileitemlistview.h"
9 #include "kfileitemmodel.h"
10 #include "kitemlistview.h"
12 #include "dolphin_detailsmodesettings.h"
15 #include <KLocalizedString>
17 #include <QGraphicsScene>
18 #include <QGraphicsView>
19 #include <QMimeDatabase>
21 KFileItemListWidgetInformant::KFileItemListWidgetInformant()
22 : KStandardItemListWidgetInformant()
26 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
30 QString
KFileItemListWidgetInformant::itemText(int index
, const KItemListView
*view
) const
32 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
33 KFileItemModel
*fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
35 const KFileItem item
= fileItemModel
->fileItem(index
);
39 bool KFileItemListWidgetInformant::itemIsLink(int index
, const KItemListView
*view
) const
41 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
42 KFileItemModel
*fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
44 const KFileItem item
= fileItemModel
->fileItem(index
);
48 QString
KFileItemListWidgetInformant::roleText(const QByteArray
&role
, const QHash
<QByteArray
, QVariant
> &values
) const
51 const QVariant roleValue
= values
.value(role
);
53 KFormat
formatter(local
);
55 // Implementation note: In case if more roles require a custom handling
56 // use a hash + switch for a linear runtime.
58 auto formatDate
= [formatter
, local
](const QDateTime
&time
) {
59 if (DetailsModeSettings::useShortRelativeDates()) {
60 return formatter
.formatRelativeDateTime(time
, QLocale::ShortFormat
);
62 return local
.toString(time
, QLocale::ShortFormat
);
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
);
75 // if we have directory size available
76 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
77 text
= formatter
.formatByteSize(size
);
81 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
82 text
= formatter
.formatByteSize(size
);
84 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
86 const long long time
= roleValue
.toLongLong(&ok
);
87 if (ok
&& time
!= -1) {
88 const QDateTime dateTime
= QDateTime::fromSecsSinceEpoch(time
);
89 text
= formatDate(dateTime
);
91 } else if (role
== "deletiontime" || role
== "imageDateTime") {
92 const QDateTime dateTime
= roleValue
.toDateTime();
93 if (dateTime
.isValid()) {
94 text
= formatDate(dateTime
);
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());
101 } else if (role
== "permissions") {
102 const auto permissions
= roleValue
.value
<QVariantList
>();
104 switch (DetailsModeSettings::usePermissionsFormat()) {
105 case DetailsModeSettings::EnumUsePermissionsFormat::SymbolicFormat
:
106 text
= permissions
.at(0).toString();
108 case DetailsModeSettings::EnumUsePermissionsFormat::NumericFormat
:
109 text
= QString::number(permissions
.at(1).toInt(), 8);
111 case DetailsModeSettings::EnumUsePermissionsFormat::CombinedFormat
:
112 text
= QString("%1 (%2)").arg(permissions
.at(0).toString()).arg(permissions
.at(1).toInt(), 0, 8);
116 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
122 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
&baseFont
) const
124 // The customized font should be italic if the file is a symbolic link.
125 QFont
font(baseFont
);
126 font
.setItalic(true);
130 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
)
131 : KStandardItemListWidget(informant
, parent
)
135 KFileItemListWidget::~KFileItemListWidget()
139 KItemListWidgetInformant
*KFileItemListWidget::createInformant()
141 return new KFileItemListWidgetInformant();
144 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
&role
) const
146 return role
== "size" || role
== "permissions";
149 bool KFileItemListWidget::isHidden() const
151 return data().value("isHidden").toBool();
154 QFont
KFileItemListWidget::customizedFont(const QFont
&baseFont
) const
156 // The customized font should be italic if the file is a symbolic link.
157 QFont
font(baseFont
);
158 font
.setItalic(data().value("isLink").toBool());
162 int KFileItemListWidget::selectionLength(const QString
&text
) const
164 // Select the text without MIME-type extension
165 int selectionLength
= text
.length();
167 // If item is a directory, use the whole text length for
168 // selection (ignore all points)
169 if (data().value("isDir").toBool()) {
170 return selectionLength
;
174 const QString extension
= db
.suffixForFileName(text
);
175 if (extension
.isEmpty()) {
176 // For an unknown extension just exclude the extension after
177 // the last point. This does not work for multiple extensions like
178 // *.tar.gz but usually this is anyhow a known extension.
179 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
181 // If no point could be found, use whole text length for selection.
182 if (selectionLength
< 1) {
183 selectionLength
= text
.length();
187 selectionLength
-= extension
.length() + 1;
190 return selectionLength
;
193 void KFileItemListWidget::hoverSequenceStarted()
195 KFileItemListView
*view
= listView();
201 const QUrl itemUrl
= data().value("url").toUrl();
203 view
->setHoverSequenceState(itemUrl
, 0);
206 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex
)
208 KFileItemListView
*view
= listView();
214 const QUrl itemUrl
= data().value("url").toUrl();
216 view
->setHoverSequenceState(itemUrl
, sequenceIndex
);
218 // Force-update the displayed icon
219 invalidateIconCache();
223 void KFileItemListWidget::hoverSequenceEnded()
225 KFileItemListView
*view
= listView();
231 view
->setHoverSequenceState(QUrl(), 0);
234 KFileItemListView
*KFileItemListWidget::listView()
236 return dynamic_cast<KFileItemListView
*>(parentItem());