]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
a62b75824c268ec4442d7ef61692af569aac03eb
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
);
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());
103 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
109 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
111 // The customized font should be italic if the file is a symbolic link.
112 QFont
font(baseFont
);
113 font
.setItalic(true);
117 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
118 KStandardItemListWidget(informant
, parent
)
122 KFileItemListWidget::~KFileItemListWidget()
126 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
128 return new KFileItemListWidgetInformant();
131 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
133 return role
== "size";
136 bool KFileItemListWidget::isHidden() const
138 return data().value("isHidden").toBool();
141 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
143 // The customized font should be italic if the file is a symbolic link.
144 QFont
font(baseFont
);
145 font
.setItalic(data().value("isLink").toBool());
149 int KFileItemListWidget::selectionLength(const QString
& text
) const
151 // Select the text without MIME-type extension
152 int selectionLength
= text
.length();
154 // If item is a directory, use the whole text length for
155 // selection (ignore all points)
156 if(data().value("isDir").toBool()) {
157 return selectionLength
;
161 const QString extension
= db
.suffixForFileName(text
);
162 if (extension
.isEmpty()) {
163 // For an unknown extension just exclude the extension after
164 // the last point. This does not work for multiple extensions like
165 // *.tar.gz but usually this is anyhow a known extension.
166 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
168 // If no point could be found, use whole text length for selection.
169 if (selectionLength
< 1) {
170 selectionLength
= text
.length();
174 selectionLength
-= extension
.length() + 1;
177 return selectionLength
;
180 void KFileItemListWidget::hoverSequenceStarted()
182 KFileItemListView
* view
= listView();
188 const QUrl itemUrl
= data().value("url").toUrl();
190 view
->setHoverSequenceState(itemUrl
, 0);
193 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex
)
195 KFileItemListView
* view
= listView();
201 const QUrl itemUrl
= data().value("url").toUrl();
203 view
->setHoverSequenceState(itemUrl
, sequenceIndex
);
205 // Force-update the displayed icon
206 invalidateIconCache();
210 void KFileItemListWidget::hoverSequenceEnded()
212 KFileItemListView
* view
= listView();
218 view
->setHoverSequenceState(QUrl(), 0);
221 KFileItemListView
* KFileItemListWidget::listView()
223 return dynamic_cast<KFileItemListView
*>(parentItem());