]>
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 "kfileitemmodel.h"
9 #include "kitemlistview.h"
11 #include "dolphin_detailsmodesettings.h"
14 #include <KLocalizedString>
16 #include <QMimeDatabase>
18 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
19 KStandardItemListWidgetInformant()
23 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
27 QString
KFileItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
29 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
30 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
32 const KFileItem item
= fileItemModel
->fileItem(index
);
36 bool KFileItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
38 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
39 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
41 const KFileItem item
= fileItemModel
->fileItem(index
);
45 QString
KFileItemListWidgetInformant::roleText(const QByteArray
& role
,
46 const QHash
<QByteArray
, QVariant
>& values
) const
49 const QVariant roleValue
= values
.value(role
);
51 // Implementation note: In case if more roles require a custom handling
52 // use a hash + switch for a linear runtime.
55 if (values
.value("isDir").toBool()) {
56 // The item represents a directory.
57 if (!roleValue
.isNull()) {
58 const int count
= values
.value("count").toInt();
60 text
= i18nc("@item:intable", "Unknown");
62 if (DetailsModeSettings::directorySizeCount()) {
63 // Show the number of sub directories instead of the file size of the directory.
64 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
66 // if we have directory size available
67 if (roleValue
== -1) {
68 text
= i18nc("@item:intable", "Unknown");
70 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
71 text
= KFormat().formatByteSize(size
);
77 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
78 text
= KFormat().formatByteSize(size
);
80 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
82 const long long time
= roleValue
.toLongLong(&ok
);
83 if (ok
&& time
!= -1) {
84 return QLocale().toString(QDateTime::fromSecsSinceEpoch(time
), QLocale::ShortFormat
);
86 } else if (role
== "deletiontime" || role
== "imageDateTime") {
87 const QDateTime dateTime
= roleValue
.toDateTime();
88 text
= QLocale().toString(dateTime
, QLocale::ShortFormat
);
90 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
96 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
98 // The customized font should be italic if the file is a symbolic link.
100 font
.setItalic(true);
105 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
106 KStandardItemListWidget(informant
, parent
)
110 KFileItemListWidget::~KFileItemListWidget()
114 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
116 return new KFileItemListWidgetInformant();
119 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
121 return role
== "size";
124 bool KFileItemListWidget::isHidden() const
126 return data().value("isHidden").toBool();
129 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
131 // The customized font should be italic if the file is a symbolic link.
132 QFont
font(baseFont
);
133 font
.setItalic(data().value("isLink").toBool());
137 int KFileItemListWidget::selectionLength(const QString
& text
) const
139 // Select the text without MIME-type extension
140 int selectionLength
= text
.length();
142 // If item is a directory, use the whole text length for
143 // selection (ignore all points)
144 if(data().value("isDir").toBool()) {
145 return selectionLength
;
149 const QString extension
= db
.suffixForFileName(text
);
150 if (extension
.isEmpty()) {
151 // For an unknown extension just exclude the extension after
152 // the last point. This does not work for multiple extensions like
153 // *.tar.gz but usually this is anyhow a known extension.
154 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
156 // If no point could be found, use whole text length for selection.
157 if (selectionLength
< 1) {
158 selectionLength
= text
.length();
162 selectionLength
-= extension
.length() + 1;
165 return selectionLength
;