]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
get rid of unused warnings
[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 <QDebug>
26 #include <KLocale>
27 #include <KIO/MetaData>
28 #include <QDateTime>
29 #include <KFormat>
30 #include <QMimeDatabase>
31
32 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
33 KStandardItemListWidgetInformant()
34 {
35 }
36
37 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
38 {
39 }
40
41 QString KFileItemListWidgetInformant::itemText(int index, const KItemListView* view) const
42 {
43 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
44 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
45
46 const KFileItem item = fileItemModel->fileItem(index);
47 return item.text();
48 }
49
50 bool KFileItemListWidgetInformant::itemIsLink(int index, const KItemListView* view) const
51 {
52 Q_ASSERT(qobject_cast<KFileItemModel*>(view->model()));
53 KFileItemModel* fileItemModel = static_cast<KFileItemModel*>(view->model());
54
55 const KFileItem item = fileItemModel->fileItem(index);
56 return item.isLink();
57 }
58
59 QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
60 const QHash<QByteArray, QVariant>& values) const
61 {
62 QString text;
63 const QVariant roleValue = values.value(role);
64
65 // Implementation note: In case if more roles require a custom handling
66 // use a hash + switch for a linear runtime.
67
68 if (role == "size") {
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();
74 if (count < 0) {
75 text = i18nc("@item:intable", "Unknown");
76 } else {
77 text = i18ncp("@item:intable", "%1 item", "%1 items", count);
78 }
79 }
80 } else {
81 const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
82 text = KFormat().formatByteSize(size);
83 }
84 } else if (role == "date") {
85 const QDateTime dateTime = roleValue.toDateTime();
86 text = KLocale::global()->formatDateTime(dateTime);
87 } else {
88 text = KStandardItemListWidgetInformant::roleText(role, values);
89 }
90
91 return text;
92 }
93
94 QFont KFileItemListWidgetInformant::customizedFontForLinks(const QFont& baseFont) const
95 {
96 // The customized font should be italic if the file is a symbolic link.
97 QFont font(baseFont);
98 font.setItalic(true);
99 return font;
100 }
101
102
103 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant* informant, QGraphicsItem* parent) :
104 KStandardItemListWidget(informant, parent)
105 {
106 }
107
108 KFileItemListWidget::~KFileItemListWidget()
109 {
110 }
111
112 KItemListWidgetInformant* KFileItemListWidget::createInformant()
113 {
114 return new KFileItemListWidgetInformant();
115 }
116
117 bool KFileItemListWidget::isRoleRightAligned(const QByteArray& role) const
118 {
119 return role == "size";
120 }
121
122 bool KFileItemListWidget::isHidden() const
123 {
124 return data().value("text").toString().startsWith(QLatin1Char('.'));
125 }
126
127 QFont KFileItemListWidget::customizedFont(const QFont& baseFont) const
128 {
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());
132 return font;
133 }
134
135 int KFileItemListWidget::selectionLength(const QString& text) const
136 {
137 // Select the text without MIME-type extension
138 int selectionLength = text.length();
139
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;
144 }
145
146 QMimeDatabase db;
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('.'));
153
154 // If no point could be found, use whole text length for selection.
155 if (selectionLength < 1) {
156 selectionLength = text.length();
157 }
158
159 } else {
160 selectionLength -= extension.length() + 1;
161 }
162
163 return selectionLength;
164 }
165