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