]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinfileitemdelegate.cpp
Fix issue that no content is shown in the Information Panel on startup
[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::LocallyModifiedVersion: tintColor = Qt::green; break;
124 case KVersionControlPlugin::AddedVersion: tintColor = Qt::darkGreen; break;
125 case KVersionControlPlugin::RemovedVersion: tintColor = Qt::darkRed; break;
126 case KVersionControlPlugin::ConflictingVersion: tintColor = Qt::red; break;
127 case KVersionControlPlugin::UnversionedVersion:
128 case KVersionControlPlugin::NormalVersion:
129 default:
130 // use the default text color
131 return;
132 }
133
134 QPalette palette = option.palette;
135 const QColor textColor = palette.color(QPalette::Text);
136 tintColor = QColor((tintColor.red() + textColor.red()) / 2,
137 (tintColor.green() + textColor.green()) / 2,
138 (tintColor.blue() + textColor.blue()) / 2,
139 (tintColor.alpha() + textColor.alpha()) / 2);
140 palette.setColor(QPalette::Text, tintColor);
141 option.palette = palette;
142 }
143
144 QPixmap DolphinFileItemDelegate::emblemForState(KVersionControlPlugin::VersionState state, const QSize& size) const
145 {
146 Q_ASSERT(state <= KVersionControlPlugin::ConflictingVersion);
147 if (m_cachedSize != size) {
148 m_cachedSize = size;
149
150 const int iconHeight = size.height();
151 int emblemHeight = KIconLoader::SizeSmall;
152 if (iconHeight >= KIconLoader::SizeEnormous) {
153 emblemHeight = KIconLoader::SizeMedium;
154 } else if (iconHeight >= KIconLoader::SizeLarge) {
155 emblemHeight = KIconLoader::SizeSmallMedium;
156 } else if (iconHeight >= KIconLoader::SizeMedium) {
157 emblemHeight = KIconLoader::SizeSmall;
158 } else {
159 emblemHeight = KIconLoader::SizeSmall / 2;
160 }
161
162 const QSize emblemSize(emblemHeight, emblemHeight);
163 for (int i = KVersionControlPlugin::NormalVersion; i <= KVersionControlPlugin::ConflictingVersion; ++i) {
164 QString iconName;
165 switch (i) {
166 case KVersionControlPlugin::NormalVersion: iconName = "vcs-normal"; break;
167 case KVersionControlPlugin::UpdateRequiredVersion: iconName = "vcs-update-required"; break;
168 case KVersionControlPlugin::LocallyModifiedVersion: iconName = "vcs-locally-modified"; break;
169 case KVersionControlPlugin::AddedVersion: iconName = "vcs-added"; break;
170 case KVersionControlPlugin::RemovedVersion: iconName = "vcs-removed"; break;
171 case KVersionControlPlugin::ConflictingVersion: iconName = "vcs-conflicting"; break;
172 case KVersionControlPlugin::UnversionedVersion:
173 default: Q_ASSERT(false); break;
174 }
175
176 m_cachedEmblems[i] = KIcon(iconName).pixmap(emblemSize);
177 }
178 }
179 return m_cachedEmblems[state];
180 }
181