]>
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_contentdisplaysettings.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
, ForUsageAs forUsageAs
) 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
, forUsageAs
](const QDateTime
&time
) {
59 if (ContentDisplaySettings::useShortRelativeDates()) {
60 return formatter
.formatRelativeDateTime(time
,
61 forUsageAs
== KStandardItemListWidgetInformant::ForUsageAs::DisplayedText
? QLocale::ShortFormat
62 : QLocale::LongFormat
);
64 return local
.toString(time
, forUsageAs
== KStandardItemListWidgetInformant::ForUsageAs::DisplayedText
? QLocale::ShortFormat
: QLocale::LongFormat
);
69 if (values
.value("isDir").toBool()) {
70 if (!roleValue
.isNull() && roleValue
!= -1) {
71 // The item represents a directory.
72 if (ContentDisplaySettings::directorySizeMode() == ContentDisplaySettings::EnumDirectorySizeMode::ContentCount
73 || roleValue
== -2 /* size is invalid */) {
74 // Show the number of sub directories instead of the file size of the directory.
75 const int count
= values
.value("count").toInt();
76 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
78 // if we have directory size available
79 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
80 text
= formatter
.formatByteSize(size
);
84 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
85 text
= formatter
.formatByteSize(size
);
87 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
89 const long long time
= roleValue
.toLongLong(&ok
);
90 if (ok
&& time
!= -1) {
91 const QDateTime dateTime
= QDateTime::fromSecsSinceEpoch(time
);
92 text
= formatDate(dateTime
);
94 } else if (role
== "deletiontime" || role
== "imageDateTime") {
95 const QDateTime dateTime
= roleValue
.toDateTime();
96 if (dateTime
.isValid()) {
97 text
= formatDate(dateTime
);
99 } else if (role
== "dimensions") {
100 const auto dimensions
= roleValue
.toSize();
101 if (dimensions
.isValid()) {
102 text
= i18nc("width × height", "%1 × %2", dimensions
.width(), dimensions
.height());
104 } else if (role
== "permissions") {
105 const auto permissions
= roleValue
.value
<QVariantList
>();
107 switch (ContentDisplaySettings::usePermissionsFormat()) {
108 case ContentDisplaySettings::EnumUsePermissionsFormat::SymbolicFormat
:
109 text
= permissions
.at(0).toString();
111 case ContentDisplaySettings::EnumUsePermissionsFormat::NumericFormat
:
112 text
= QString::number(permissions
.at(1).toInt(), 8);
114 case ContentDisplaySettings::EnumUsePermissionsFormat::CombinedFormat
:
115 text
= QLatin1String("%1 (%2)").arg(permissions
.at(0).toString()).arg(permissions
.at(1).toInt(), 0, 8);
119 text
= KStandardItemListWidgetInformant::roleText(role
, values
, forUsageAs
);
125 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
&baseFont
) const
127 // The customized font should be italic if the file is a symbolic link.
128 QFont
font(baseFont
);
129 font
.setItalic(true);
133 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
*informant
, QGraphicsItem
*parent
)
134 : KStandardItemListWidget(informant
, parent
)
138 KFileItemListWidget::~KFileItemListWidget()
142 KItemListWidgetInformant
*KFileItemListWidget::createInformant()
144 return new KFileItemListWidgetInformant();
147 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
&role
) const
149 return role
== "size" || role
== "permissions";
152 bool KFileItemListWidget::isHidden() const
154 return data().value("isHidden").toBool();
157 QFont
KFileItemListWidget::customizedFont(const QFont
&baseFont
) const
159 // The customized font should be italic if the file is a symbolic link.
160 QFont
font(baseFont
);
161 font
.setItalic(data().value("isLink").toBool());
165 int KFileItemListWidget::selectionLength(const QString
&text
) const
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 numberOfUnicodeCharactersIn(text
);
173 int indexOfExtension
= text
.length();
176 const QString extension
= db
.suffixForFileName(text
);
177 if (extension
.isEmpty()) {
178 // For an unknown extension just exclude the extension after
179 // the last dot. This does not work for multiple extensions like
180 // *.tar.gz but usually this is anyhow a known extension.
181 indexOfExtension
= text
.lastIndexOf(QLatin1Char('.'));
183 // if there either is no dot, or the last dot is the first or last char, treat as no extension.
184 if (indexOfExtension
< 1 || indexOfExtension
== text
.length() - 1) {
185 indexOfExtension
= text
.length();
188 indexOfExtension
-= extension
.length() + 1;
191 return numberOfUnicodeCharactersIn(text
.left(indexOfExtension
));
194 void KFileItemListWidget::hoverSequenceStarted()
196 KFileItemListView
*view
= listView();
202 const QUrl itemUrl
= data().value("url").toUrl();
204 view
->setHoverSequenceState(itemUrl
, 0);
207 void KFileItemListWidget::forceUpdate()
209 updateAdditionalInfoTextColor();
210 // icon layout does not include the icons in the item selection rectangle
211 // so its icon does not need updating
212 if (listView()->itemLayout() != KStandardItemListView::ItemLayout::IconsLayout
) {
213 invalidateIconCache();
218 void KFileItemListWidget::hoverSequenceIndexChanged(int sequenceIndex
)
220 KFileItemListView
*view
= listView();
226 const QUrl itemUrl
= data().value("url").toUrl();
228 view
->setHoverSequenceState(itemUrl
, sequenceIndex
);
230 // Force-update the displayed icon
231 invalidateIconCache();
235 void KFileItemListWidget::hoverSequenceEnded()
237 KFileItemListView
*view
= listView();
243 view
->setHoverSequenceState(QUrl(), 0);
246 KFileItemListView
*KFileItemListWidget::listView()
248 return dynamic_cast<KFileItemListView
*>(parentItem());
251 #include "moc_kfileitemlistwidget.cpp"