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