]>
cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #include "kfileitemlistwidget.h"
21 #include "kfileitemmodel.h"
22 #include "kitemlistview.h"
25 #include <KIO/MetaData>
28 #include <QMimeDatabase>
30 KFileItemListWidgetInformant::KFileItemListWidgetInformant() :
31 KStandardItemListWidgetInformant()
35 KFileItemListWidgetInformant::~KFileItemListWidgetInformant()
39 QString
KFileItemListWidgetInformant::itemText(int index
, const KItemListView
* view
) const
41 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
42 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
44 const KFileItem item
= fileItemModel
->fileItem(index
);
48 bool KFileItemListWidgetInformant::itemIsLink(int index
, const KItemListView
* view
) const
50 Q_ASSERT(qobject_cast
<KFileItemModel
*>(view
->model()));
51 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(view
->model());
53 const KFileItem item
= fileItemModel
->fileItem(index
);
57 QString
KFileItemListWidgetInformant::roleText(const QByteArray
& role
,
58 const QHash
<QByteArray
, QVariant
>& values
) const
61 const QVariant roleValue
= values
.value(role
);
63 // Implementation note: In case if more roles require a custom handling
64 // use a hash + switch for a linear runtime.
67 if (values
.value("isDir").toBool()) {
68 // The item represents a directory. Show the number of sub directories
69 // instead of the file size of the directory.
70 if (!roleValue
.isNull()) {
71 const int count
= roleValue
.toInt();
73 text
= i18nc("@item:intable", "Unknown");
75 text
= i18ncp("@item:intable", "%1 item", "%1 items", count
);
79 const KIO::filesize_t size
= roleValue
.value
<KIO::filesize_t
>();
80 text
= KFormat().formatByteSize(size
);
82 } else if (role
== "date") {
83 const QDateTime dateTime
= roleValue
.toDateTime();
84 text
= KLocale::global()->formatDateTime(dateTime
);
86 text
= KStandardItemListWidgetInformant::roleText(role
, values
);
92 QFont
KFileItemListWidgetInformant::customizedFontForLinks(const QFont
& baseFont
) const
94 // The customized font should be italic if the file is a symbolic link.
101 KFileItemListWidget::KFileItemListWidget(KItemListWidgetInformant
* informant
, QGraphicsItem
* parent
) :
102 KStandardItemListWidget(informant
, parent
)
106 KFileItemListWidget::~KFileItemListWidget()
110 KItemListWidgetInformant
* KFileItemListWidget::createInformant()
112 return new KFileItemListWidgetInformant();
115 bool KFileItemListWidget::isRoleRightAligned(const QByteArray
& role
) const
117 return role
== "size";
120 bool KFileItemListWidget::isHidden() const
122 return data().value("text").toString().startsWith(QLatin1Char('.'));
125 QFont
KFileItemListWidget::customizedFont(const QFont
& baseFont
) const
127 // The customized font should be italic if the file is a symbolic link.
128 QFont
font(baseFont
);
129 font
.setItalic(data().value("isLink").toBool());
133 int KFileItemListWidget::selectionLength(const QString
& text
) const
135 // Select the text without MIME-type extension
136 int selectionLength
= text
.length();
138 // If item is a directory, use the whole text length for
139 // selection (ignore all points)
140 if(data().value("isDir").toBool()) {
141 return selectionLength
;
145 const QString extension
= db
.suffixForFileName(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('.'));
152 // If no point could be found, use whole text length for selection.
153 if (selectionLength
< 1) {
154 selectionLength
= text
.length();
158 selectionLength
-= extension
.length() + 1;
161 return selectionLength
;