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