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