]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
8b2c761551f36757ce790d63b40305983bfed56c
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"
24 #include <kmimetype.h>
27 #include <KIO/MetaData>
30 #include <QMimeDatabase>
32 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
33 KStandardItemListWidgetInformant()
37 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
41 QString
KFileItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
43 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
44 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
46 const KFileItem item
= fileItemModel
->fileItem(index
);
50 bool KFileItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
52 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
53 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
55 const KFileItem item
= fileItemModel
->fileItem(index
);
59 QString
KFileItemListWidgetInformant::roleText(const QByteArray
& role
,
60 const QHash
<QByteArray
, QVariant
>& values
) const
63 const QVariant roleValue
= values
.value(role
);
65 // Implementation note: In case if more roles require a custom handling
66 // use a hash + switch for a linear runtime.
69 if (values
.value("isDir").toBool()) {
70 // The item represents a directory. Show the number of sub directories
71 // instead of the file size of the directory.
72 if (!roleValue
.isNull()) {
73 const int count
= roleValue
.toInt();
75 text
= i18nc("@item:intable", "Unknown");
77 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
81 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
82 text
= KFormat().formatByteSize(size
);
84 } else if (role
== "date") {
85 const QDateTime dateTime
= roleValue
.toDateTime();
86 text
= KLocale::global()->formatDateTime(dateTime
);
88 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
94 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
96 // The customized font should be italic if the file is a symbolic link.
103 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
104 KStandardItemListWidget(informant
, parent
)
108 KFileItemListWidget::~KFileItemListWidget()
112 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
114 return new KFileItemListWidgetInformant();
117 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
119 return role
== "size";
122 bool KFileItemListWidget::isHidden() const
124 return data().value("text").toString().startsWith(QLatin1Char('.'));
127 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
129 // The customized font should be italic if the file is a symbolic link.
130 QFont
font(baseFont
);
131 font
.setItalic(data().value("isLink").toBool());
135 int KFileItemListWidget::selectionLength(const QString
& text
) const
137 // Select the text without MIME-type extension
138 int selectionLength
= text
.length();
140 // If item is a directory, use the whole text length for
141 // selection (ignore all points)
142 if(data().value("isDir").toBool()) {
143 return selectionLength
;
147 const QString extension
= db
.suffixForFileName(text
);
148 if (extension
.isEmpty()) {
149 // For an unknown extension just exclude the extension after
150 // the last point. This does not work for multiple extensions like
151 // *.tar.gz but usually this is anyhow a known extension.
152 selectionLength
= text
.lastIndexOf(QLatin1Char('.'));
154 // If no point could be found, use whole text length for selection.
155 if (selectionLength
< 1) {
156 selectionLength
= text
.length();
160 selectionLength
-= extension
.length() + 1;
163 return selectionLength
;