]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
40b8ccf3717a4a10965cf8c361c8be7e743fb57a
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
20 #include "kfileitemlistwidget.h"
21 #include "kfileitemmodel.h"
22 #include "kitemlistview.h"
25 #include <KLocalizedString>
27 #include <QMimeDatabase>
29 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
30 KStandardItemListWidgetInformant()
34 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
38 QString
KFileItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
40 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
41 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
43 const KFileItem item
= fileItemModel
->fileItem(index
);
47 bool KFileItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
49 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
50 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
52 const KFileItem item
= fileItemModel
->fileItem(index
);
56 QString
KFileItemListWidgetInformant::roleText(const QByteArray
& role
,
57 const QHash
<QByteArray
, QVariant
>& values
) const
60 const QVariant roleValue
= values
.value(role
);
62 // Implementation note: In case if more roles require a custom handling
63 // use a hash + switch for a linear runtime.
66 if (values
.value("isDir").toBool()) {
67 // The item represents a directory. Show the number of sub directories
68 // instead of the file size of the directory.
69 if (!roleValue
.isNull()) {
70 const int count
= roleValue
.toInt();
72 text
= i18nc("@item:intable", "Unknown");
74 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
78 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
79 text
= KFormat().formatByteSize(size
);
81 } else if (role
== "modificationtime" || role
== "creationtime" || role
== "accesstime") {
83 const long long time
= roleValue
.toLongLong(&ok
);
84 if (ok
&& time
!= -1) {
85 return QLocale().toString(QDateTime::fromSecsSinceEpoch(time
), QLocale::ShortFormat
);
87 } else if (role
== "deletiontime" || role
== "imageDateTime") {
88 const QDateTime dateTime
= roleValue
.toDateTime();
89 text
= QLocale().toString(dateTime
, QLocale::ShortFormat
);
91 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
97 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
99 // The customized font should be italic if the file is a symbolic link.
100 QFont
font(baseFont
);
101 font
.setItalic(true);
106 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
107 KStandardItemListWidget(informant
, parent
)
111 KFileItemListWidget::~KFileItemListWidget()
115 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
117 return new KFileItemListWidgetInformant();
120 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
122 return role
== "size";
125 bool KFileItemListWidget::isHidden() const
127 return data().value("isHidden").toBool();
130 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
132 // The customized font should be italic if the file is a symbolic link.
133 QFont
font(baseFont
);
134 font
.setItalic(data().value("isLink").toBool());
138 int KFileItemListWidget::selectionLength(const QString
& text
) const
140 // Select the text without MIME-type extension
141 int selectionLength
= text
.length();
143 // If item is a directory, use the whole text length for
144 // selection (ignore all points)
145 if(data().value("isDir").toBool()) {
146 return selectionLength
;
150 const QString extension
= db
.suffixForFileName(text
);
151 if (extension
.isEmpty()) {
152 // For an unknown extension just exclude the extension after
153 // the last point. This does not work for multiple extensions like
154 // *.tar.gz but usually this is anyhow a known extension.
155 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
157 // If no point could be found, use whole text length for selection.
158 if (selectionLength
< 1) {
159 selectionLength
= text
.length();
163 selectionLength
-= extension
.length() + 1;
166 return selectionLength
;