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