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