]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
587603ab32d291f88f382bc3b43e3d8e6e2f7f55
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
,
49 const QHash
<QByteArray
, QVariant
>& values
) const
52 const QVariant roleValue
= values
.value(role
);
54 KFormat
formatter(local
);
56 // Implementation note: In case if more roles require a custom handling
57 // use a hash + switch for a linear runtime.
59 auto formatDate
= [formatter
, local
](const QDateTime
& time
) {
60 if (DetailsModeSettings::useShortRelativeDates()) {
61 return formatter
.formatRelativeDateTime(time
, QLocale::ShortFormat
);
63 return local
.toString(time
, QLocale::ShortFormat
);
68 if (values
.value("isDir").toBool()) {
69 if (!roleValue
.isNull() && roleValue
!= -1) {
70 // The item represents a directory.
71 if (DetailsModeSettings::directorySizeCount()) {
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
);
76 // if we have directory size available
77 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
78 text
= formatter
.formatByteSize(size
);
82 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
83 text
= formatter
.formatByteSize(size
);
85 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
87 const long long time
= roleValue
.toLongLong(&ok
);
88 if (ok
&& time
!= -1) {
89 const QDateTime dateTime
= QDateTime::fromSecsSinceEpoch(time
);
90 text
= formatDate(dateTime
);
92 } else if (role
== "deletiontime" || role
== "imageDateTime") {
93 const QDateTime dateTime
= roleValue
.toDateTime();
94 if (dateTime
.isValid()) {
95 text
= formatDate(dateTime
);
98 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
104 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
106 // The customized font should be italic if the file is a symbolic link.
107 QFont
font(baseFont
);
108 font
.setItalic(true);
113 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
114 KStandardItemListWidget(informant
, parent
)
118 KFileItemListWidget::~KFileItemListWidget()
122 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
124 return new KFileItemListWidgetInformant();
127 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
129 return role
== "size";
132 bool KFileItemListWidget::isHidden() const
134 return data().value("isHidden").toBool();
137 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
139 // The customized font should be italic if the file is a symbolic link.
140 QFont
font(baseFont
);
141 font
.setItalic(data().value("isLink").toBool());
145 int KFileItemListWidget::selectionLength(const QString
& text
) const
147 // Select the text without MIME-type extension
148 int selectionLength
= text
.length();
150 // If item is a directory, use the whole text length for
151 // selection (ignore all points)
152 if(data().value("isDir").toBool()) {
153 return selectionLength
;
157 const QString extension
= db
.suffixForFileName(text
);
158 if (extension
.isEmpty()) {
159 // For an unknown extension just exclude the extension after
160 // the last point. This does not work for multiple extensions like
161 // *.tar.gz but usually this is anyhow a known extension.
162 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
164 // If no point could be found, use whole text length for selection.
165 if (selectionLength
< 1) {
166 selectionLength
= text
.length();
170 selectionLength
-= extension
.length() + 1;
173 return selectionLength
;
176 void KFileItemListWidget::hoverSequenceStarted()
178 KFileItemListView
* view
= listView();
184 const QUrl itemUrl
= data().value("url").toUrl();
186 view
->setHoverSequenceState(itemUrl
, 0);
189 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex
)
191 KFileItemListView
* view
= listView();
197 const QUrl itemUrl
= data().value("url").toUrl();
199 view
->setHoverSequenceState(itemUrl
, sequenceIndex
);
201 // Force-update the displayed icon
202 invalidateIconCache();
206 void KFileItemListWidget::hoverSequenceEnded()
208 KFileItemListView
* view
= listView();
214 view
->setHoverSequenceState(QUrl(), 0);
217 KFileItemListView
* KFileItemListWidget::listView()
219 return dynamic_cast<KFileItemListView
*>(parentItem());