]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinfileitemdelegate.cpp
4d66c73f10ef782d93c3664b45ea482906802a57
[dolphin.git] / src / views / dolphinfileitemdelegate.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 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 "dolphinfileitemdelegate.h"
21
22 #include "dolphinmodel.h"
23 #include <KColorScheme>
24 #include <KFileItem>
25 #include <KGlobalSettings>
26 #include <KIcon>
27 #include <KIconLoader>
28 #include <KStringHandler>
29
30 #include <QAbstractItemModel>
31 #include <QAbstractProxyModel>
32 #include <QFontMetrics>
33 #include <QPalette>
34 #include <QPainter>
35 #include <QStyleOptionViewItemV4>
36
37 DolphinFileItemDelegate::DolphinFileItemDelegate(QObject* parent) :
38 KFileItemDelegate(parent),
39 m_hasMinimizedNameColumn(false),
40 m_cachedSize(),
41 m_cachedEmblems(),
42 m_cachedInactiveTextColorDirty(true)
43 {
44 setJobTransfersVisible(true);
45 connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), this, SLOT(handleDisplayPaletteChange()));
46 }
47
48 DolphinFileItemDelegate::~DolphinFileItemDelegate()
49 {
50 }
51
52 void DolphinFileItemDelegate::paint(QPainter* painter,
53 const QStyleOptionViewItem& option,
54 const QModelIndex& index) const
55 {
56 const QAbstractProxyModel* proxyModel = static_cast<const QAbstractProxyModel*>(index.model());
57 const DolphinModel* dolphinModel = static_cast<const DolphinModel*>(proxyModel->sourceModel());
58 const bool isNameColumn = (index.column() == KDirModel::Name);
59
60 QStyleOptionViewItemV4 opt(option);
61 if (m_hasMinimizedNameColumn && isNameColumn) {
62 adjustOptionWidth(opt, proxyModel, dolphinModel, index);
63 }
64
65 if (!isNameColumn) {
66 // Use the inactive text color for all columns except the name column. This indicates for the user that
67 // hovering other columns does not change the actions context.
68 QPalette palette = opt.palette;
69 if (m_cachedInactiveTextColorDirty) {
70 m_cachedInactiveTextColor = KColorScheme(QPalette::Active).foreground(KColorScheme::InactiveText).color();
71 m_cachedInactiveTextColorDirty = false;
72 }
73 palette.setColor(QPalette::Text, m_cachedInactiveTextColor);
74 opt.palette = palette;
75 }
76
77 if (dolphinModel->hasVersionData() && isNameColumn) {
78 // The currently shown items are under revision control. Show the current revision
79 // state by adding an emblem and changing the text tintColor.
80 const QModelIndex dirIndex = proxyModel->mapToSource(index);
81 const QModelIndex revisionIndex = dolphinModel->index(dirIndex.row(), DolphinModel::Version, dirIndex.parent());
82 const QVariant data = dolphinModel->data(revisionIndex, Qt::DecorationRole);
83 const KVersionControlPlugin::VersionState state = static_cast<KVersionControlPlugin::VersionState>(data.toInt());
84
85 adjustOptionTextColor(opt, state);
86
87 KFileItemDelegate::paint(painter, opt, index);
88
89 if (state != KVersionControlPlugin::UnversionedVersion) {
90 const QRect rect = iconRect(option, index);
91 const QPixmap emblem = emblemForState(state, rect.size());
92 painter->drawPixmap(rect.x(), rect.y() + rect.height() - emblem.height(), emblem);
93 }
94 } else {
95 KFileItemDelegate::paint(painter, opt, index);
96 }
97 }
98
99 int DolphinFileItemDelegate::nameColumnWidth(const QString& name, const QStyleOptionViewItem& option)
100 {
101 QFontMetrics fontMetrics(option.font);
102 int width = option.decorationSize.width() + fontMetrics.width(KStringHandler::preProcessWrap(name)) + 16;
103
104 const int defaultWidth = option.rect.width();
105 if ((defaultWidth > 0) && (defaultWidth < width)) {
106 width = defaultWidth;
107 }
108 return width;
109 }
110
111 void DolphinFileItemDelegate::handleDisplayPaletteChange()
112 {
113 m_cachedInactiveTextColorDirty = true;
114 }
115
116 void DolphinFileItemDelegate::adjustOptionWidth(QStyleOptionViewItemV4& option,
117 const QAbstractProxyModel* proxyModel,
118 const DolphinModel* dolphinModel,
119 const QModelIndex& index)
120 {
121 const QModelIndex dirIndex = proxyModel->mapToSource(index);
122 const KFileItem item = dolphinModel->itemForIndex(dirIndex);
123 if (!item.isNull()) {
124 // symbolic links are displayed in an italic font
125 if (item.isLink()) {
126 option.font.setItalic(true);
127 }
128
129 const int width = nameColumnWidth(item.text(), option);
130 option.rect.setWidth(width);
131 }
132 }
133
134 void DolphinFileItemDelegate::adjustOptionTextColor(QStyleOptionViewItemV4& option,
135 KVersionControlPlugin::VersionState state)
136 {
137 QColor tintColor;
138
139 // Using hardcoded colors is generally a bad idea. In this case the colors just act
140 // as tint colors and are mixed with the current set text color. The tint colors
141 // have been optimized for the base colors of the corresponding Oxygen emblems.
142 switch (state) {
143 case KVersionControlPlugin::UpdateRequiredVersion: tintColor = Qt::yellow; break;
144 case KVersionControlPlugin::LocallyModifiedUnstagedVersion: tintColor = Qt::darkGreen; break;
145 case KVersionControlPlugin::LocallyModifiedVersion: tintColor = Qt::green; break;
146 case KVersionControlPlugin::AddedVersion: tintColor = Qt::green; break;
147 case KVersionControlPlugin::RemovedVersion: tintColor = Qt::darkRed; break;
148 case KVersionControlPlugin::ConflictingVersion: tintColor = Qt::red; break;
149 case KVersionControlPlugin::UnversionedVersion:
150 case KVersionControlPlugin::NormalVersion:
151 default:
152 // use the default text color
153 return;
154 }
155
156 QPalette palette = option.palette;
157 const QColor textColor = palette.color(QPalette::Text);
158 tintColor = QColor((tintColor.red() + textColor.red()) / 2,
159 (tintColor.green() + textColor.green()) / 2,
160 (tintColor.blue() + textColor.blue()) / 2,
161 (tintColor.alpha() + textColor.alpha()) / 2);
162 palette.setColor(QPalette::Text, tintColor);
163 option.palette = palette;
164 }
165
166 QPixmap DolphinFileItemDelegate::emblemForState(KVersionControlPlugin::VersionState state, const QSize& size) const
167 {
168 Q_ASSERT(state <= KVersionControlPlugin::LocallyModifiedUnstagedVersion);
169 if (m_cachedSize != size) {
170 m_cachedSize = size;
171
172 const int iconHeight = size.height();
173 int emblemHeight = KIconLoader::SizeSmall;
174 if (iconHeight >= KIconLoader::SizeEnormous) {
175 emblemHeight = KIconLoader::SizeMedium;
176 } else if (iconHeight >= KIconLoader::SizeLarge) {
177 emblemHeight = KIconLoader::SizeSmallMedium;
178 } else if (iconHeight >= KIconLoader::SizeMedium) {
179 emblemHeight = KIconLoader::SizeSmall;
180 } else {
181 emblemHeight = KIconLoader::SizeSmall / 2;
182 }
183
184 const QSize emblemSize(emblemHeight, emblemHeight);
185 for (int i = KVersionControlPlugin::NormalVersion; i <= KVersionControlPlugin::LocallyModifiedUnstagedVersion; ++i) {
186 QString iconName;
187 switch (i) {
188 case KVersionControlPlugin::NormalVersion:
189 iconName = "vcs-normal";
190 break;
191 case KVersionControlPlugin::UpdateRequiredVersion:
192 iconName = "vcs-update-required";
193 break;
194 case KVersionControlPlugin::LocallyModifiedVersion:
195 iconName = "vcs-locally-modified";
196 break;
197 case KVersionControlPlugin::LocallyModifiedUnstagedVersion:
198 iconName = "vcs-locally-modified-unstaged";
199 break;
200 case KVersionControlPlugin::AddedVersion:
201 iconName = "vcs-added";
202 break;
203 case KVersionControlPlugin::RemovedVersion:
204 iconName = "vcs-removed";
205 break;
206 case KVersionControlPlugin::ConflictingVersion:
207 iconName = "vcs-conflicting";
208 break;
209 case KVersionControlPlugin::UnversionedVersion:
210 break;
211 default:
212 Q_ASSERT(false);
213 break;
214 }
215
216 m_cachedEmblems[i] = KIcon(iconName).pixmap(emblemSize);
217 }
218 }
219 return m_cachedEmblems[state];
220 }
221