]>
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
,
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);
112 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
113 KStandardItemListWidget(informant
, parent
)
117 KFileItemListWidget::~KFileItemListWidget()
121 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
123 return new KFileItemListWidgetInformant();
126 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
128 return role
== "size";
131 bool KFileItemListWidget::isHidden() const
133 return data().value("isHidden").toBool();
136 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
138 // The customized font should be italic if the file is a symbolic link.
139 QFont
font(baseFont
);
140 font
.setItalic(data().value("isLink").toBool());
144 int KFileItemListWidget::selectionLength(const QString
& text
) const
146 // Select the text without MIME-type extension
147 int selectionLength
= text
.length();
149 // If item is a directory, use the whole text length for
150 // selection (ignore all points)
151 if(data().value("isDir").toBool()) {
152 return selectionLength
;
156 const QString extension
= db
.suffixForFileName(text
);
157 if (extension
.isEmpty()) {
158 // For an unknown extension just exclude the extension after
159 // the last point. This does not work for multiple extensions like
160 // *.tar.gz but usually this is anyhow a known extension.
161 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
163 // If no point could be found, use whole text length for selection.
164 if (selectionLength
< 1) {
165 selectionLength
= text
.length();
169 selectionLength
-= extension
.length() + 1;
172 return selectionLength
;
175 void KFileItemListWidget::hoverSequenceStarted()
177 KFileItemListView
* view
= listView();
183 const QUrl itemUrl
= data().value("url").toUrl();
185 view
->setHoverSequenceState(itemUrl
, 0);
188 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex
)
190 KFileItemListView
* view
= listView();
196 const QUrl itemUrl
= data().value("url").toUrl();
198 view
->setHoverSequenceState(itemUrl
, sequenceIndex
);
200 // Force-update the displayed icon
201 invalidateIconCache();
205 void KFileItemListWidget::hoverSequenceEnded()
207 KFileItemListView
* view
= listView();
213 view
->setHoverSequenceState(QUrl(), 0);
216 KFileItemListView
* KFileItemListWidget::listView()
218 return dynamic_cast<KFileItemListView
*>(parentItem());