]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
Merge branch 'KDE/4.11' into KDE/4.12
[dolphin.git] / src / kitemviews / kfileitemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "kfileitemlistwidget.h"
21 #include "kfileitemmodel.h"
22 #include "kitemlistview.h"
23
24 #include <kmimetype.h>
25 #include <KDebug>
26 #include <KGlobal>
27 #include <KLocale>
28 #include <KIO/MetaData>
29 #include <QDateTime>
30
31 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
32 KStandardItemListWidgetInformant()
33 {
34 }
35
36 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
37 {
38 }
39
40 QString KFileItemListWidgetInformant::itemText(int index, const KItemListView* view) const
41 {
42 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
43 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
44
45 const KFileItem item = fileItemModel->fileItem(index);
46 return item.text();
47 }
48
49 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
50 const QHash<QByteArray, QVariant>& values) const
51 {
52 QString text;
53 const QVariant roleValue = values.value(role);
54
55 // Implementation note: In case if more roles require a custom handling
56 // use a hash + switch for a linear runtime.
57
58 if (role == "size") {
59 if (values.value("isDir").toBool()) {
60 // The item represents a directory. Show the number of sub directories
61 // instead of the file size of the directory.
62 if (!roleValue.isNull()) {
63 const int count = roleValue.toInt();
64 if (count < 0) {
65 text = i18nc("@item:intable", "Unknown");
66 } else {
67 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
68 }
69 }
70 } else {
71 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
72 text = KGlobal::locale()->formatByteSize(size);
73 }
74 } else if (role == "date") {
75 const QDateTime dateTime = roleValue.toDateTime();
76 text = KGlobal::locale()->formatDateTime(dateTime);
77 } else {
78 text = KStandardItemListWidgetInformant::roleText(role, values);
79 }
80
81 return text;
82 }
83
84 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
85 KStandardItemListWidget(informant, parent)
86 {
87 }
88
89 KFileItemListWidget::~KFileItemListWidget()
90 {
91 }
92
93 KItemListWidgetInformant* KFileItemListWidget::createInformant()
94 {
95 return new KFileItemListWidgetInformant();
96 }
97
98 bool KFileItemListWidget::isRoleRightAligned(const QByteArray& role) const
99 {
100 return role == "size";
101 }
102
103 bool KFileItemListWidget::isHidden() const
104 {
105 return data().value("text").toString().startsWith(QLatin1Char('.'));
106 }
107
108 QFont KFileItemListWidget::customizedFont(const QFont& baseFont) const
109 {
110 // The customized font should be italic if the file is a symbolic link.
111 QFont font(baseFont);
112 font.setItalic(data().value("isLink").toBool());
113 return font;
114 }
115
116 int KFileItemListWidget::selectionLength(const QString& text) const
117 {
118 // Select the text without MIME-type extension
119 int selectionLength = text.length();
120
121 // If item is a directory, use the whole text length for
122 // selection (ignore all points)
123 if(data().value("isDir").toBool()) {
124 return selectionLength;
125 }
126
127 const QString extension = KMimeType::extractKnownExtension(text);
128 if (extension.isEmpty()) {
129 // For an unknown extension just exclude the extension after
130 // the last point. This does not work for multiple extensions like
131 // *.tar.gz but usually this is anyhow a known extension.
132 selectionLength = text.lastIndexOf(QLatin1Char('.'));
133
134 // If no point could be found, use whole text length for selection.
135 if (selectionLength < 1) {
136 selectionLength = text.length();
137 }
138
139 } else {
140 selectionLength -= extension.length() + 1;
141 }
142
143 return selectionLength;
144 }
145
146 #include "kfileitemlistwidget.moc"