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