]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinfileitemdelegate.cpp
Assure that non-default messages don't get hidden after a very short time by default...
[dolphin.git] / src / dolphinfileitemdelegate.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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 "dolphinfileitemdelegate.h"
21
22 #include <dolphinmodel.h>
23 #include <kfileitem.h>
24 #include <kicon.h>
25 #include <kiconloader.h>
26
27 #include <QAbstractItemModel>
28 #include <QAbstractProxyModel>
29 #include <QFontMetrics>
30 #include <QPalette>
31 #include <QPainter>
32 #include <QStyleOptionViewItemV4>
33
34 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
35 KFileItemDelegate(parent),
36 m_hasMinimizedNameColumn(false),
37 m_cachedSize(),
38 m_cachedEmblems()
39 {
40 }
41
42 DolphinFileItemDelegate::~DolphinFileItemDelegate()
43 {
44 }
45
46 void DolphinFileItemDelegate::paint(QPainter* painter,
47 const QStyleOptionViewItem& option,
48 const QModelIndex& index) const
49 {
50 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(index.model());
51 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(proxyModel->sourceModel());
52 const bool isNameColumn = (index.column() == KDirModel::Name);
53
54 if (m_hasMinimizedNameColumn && isNameColumn) {
55 QStyleOptionViewItemV4 opt(option);
56 adjustOptionWidth(opt, proxyModel, dolphinModel, index);
57 KFileItemDelegate::paint(painter, opt, index);
58 } else {
59 KFileItemDelegate::paint(painter, option, index);
60 }
61
62 if (dolphinModel->hasRevisionData() && isNameColumn) {
63 // The currently shown items are under revision control. Show the current revision
64 // state by adding an emblem.
65 const QModelIndex dirIndex = proxyModel->mapToSource(index);
66 const QModelIndex revisionIndex = dolphinModel->index(dirIndex.row(), DolphinModel::Revision, dirIndex.parent());
67 const QVariant data = dolphinModel->data(revisionIndex, Qt::DecorationRole);
68 const RevisionControlPlugin::RevisionState state = static_cast<RevisionControlPlugin::RevisionState>(data.toInt());
69
70 if (state != RevisionControlPlugin::UnversionedRevision) {
71 const QRect rect = iconRect(option, index);
72 const QPixmap emblem = emblemForState(state, rect.size());
73 painter->drawPixmap(rect.x(), rect.y() + rect.height() - emblem.height(), emblem);
74 }
75 }
76 }
77
78 int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
79 {
80 QFontMetrics fontMetrics(option.font);
81 int width = option.decorationSize.width() + fontMetrics.width(name) + 16;
82
83 const int defaultWidth = option.rect.width();
84 if ((defaultWidth > 0) && (defaultWidth < width)) {
85 width = defaultWidth;
86 }
87 return width;
88 }
89
90 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4& option,
91 const QAbstractProxyModel* proxyModel,
92 const DolphinModel* dolphinModel,
93 const QModelIndex& index)
94 {
95 const QModelIndex dirIndex = proxyModel->mapToSource(index);
96 const KFileItem item = dolphinModel->itemForIndex(dirIndex);
97 if (!item.isNull()) {
98 // symbolic links are displayed in an italic font
99 if (item.isLink()) {
100 option.font.setItalic(true);
101 }
102
103 const int width = nameColumnWidth(item.text(), option);
104 option.rect.setWidth(width);
105 }
106 }
107
108 QPixmap DolphinFileItemDelegate::emblemForState(RevisionControlPlugin::RevisionState state, const QSize& size) const
109 {
110 // TODO: all icons that are use here will be replaced by revision control emblems provided by the
111 // Oxygen team before KDE 4.4
112 Q_ASSERT(state <= RevisionControlPlugin::ConflictingRevision);
113 if ((m_cachedSize != size) || !m_cachedEmblems[state].isNull()) {
114 m_cachedSize = size;
115
116 const int iconHeight = size.height();
117 int emblemHeight = KIconLoader::SizeSmall;
118 if (iconHeight >= KIconLoader::SizeEnormous) {
119 emblemHeight = KIconLoader::SizeMedium;
120 } else if (iconHeight >= KIconLoader::SizeLarge) {
121 emblemHeight = KIconLoader::SizeSmallMedium;
122 } else if (iconHeight >= KIconLoader::SizeMedium) {
123 emblemHeight = KIconLoader::SizeSmall;
124 } else {
125 // TODO: it depends on the final icons whether a smaller size works
126 emblemHeight = KIconLoader::SizeSmall /* / 2 */;
127 }
128
129 const QSize emblemSize(emblemHeight, emblemHeight);
130 for (int i = 0; i <= RevisionControlPlugin::ConflictingRevision; ++i) {
131 QString iconName;
132 switch (state) {
133 case RevisionControlPlugin::NormalRevision: iconName = "dialog-ok-apply"; break;
134 case RevisionControlPlugin::UpdateRequiredRevision: iconName = "rating"; break;
135 case RevisionControlPlugin::LocallyModifiedRevision: iconName = "emblem-important"; break;
136 case RevisionControlPlugin::AddedRevision: iconName = "list-add"; break;
137 case RevisionControlPlugin::ConflictingRevision: iconName = "application-exit"; break;
138 default: Q_ASSERT(false); break;
139 }
140
141 m_cachedEmblems[i] = KIcon(iconName).pixmap(emblemSize);
142 }
143 }
144 return m_cachedEmblems[state];
145 }
146