]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinfileitemlistwidget.cpp
Version control: show pixmap overlays for the version state
[dolphin.git] / src / views / dolphinfileitemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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 "dolphinfileitemlistwidget.h"
21
22 #include <KIcon>
23 #include <KIconLoader>
24 #include <QColor>
25
26 #include <KDebug>
27
28 DolphinFileItemListWidget::DolphinFileItemListWidget(QGraphicsItem* parent) :
29 KFileItemListWidget(parent)
30 {
31 }
32
33 DolphinFileItemListWidget::~DolphinFileItemListWidget()
34 {
35 }
36
37 void DolphinFileItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current, const QSet<QByteArray>& roles)
38 {
39 KFileItemListWidget::dataChanged(current, roles);
40
41 QColor color;
42 QPixmap overlay;
43 if (roles.contains("version")) {
44 // The item is under version control. Apply the text color corresponding
45 // to its version state.
46 const KVersionControlPlugin::VersionState version = static_cast<KVersionControlPlugin::VersionState>(current.value("version").toInt());
47 const QColor textColor = styleOption().palette.text().color();
48 QColor tintColor = textColor;
49
50 // Using hardcoded colors is generally a bad idea. In this case the colors just act
51 // as tint colors and are mixed with the current set text color. The tint colors
52 // have been optimized for the base colors of the corresponding Oxygen emblems.
53 switch (version) {
54 case KVersionControlPlugin::UpdateRequiredVersion: tintColor = Qt::yellow; break;
55 case KVersionControlPlugin::LocallyModifiedUnstagedVersion: tintColor = Qt::green; break;
56 case KVersionControlPlugin::LocallyModifiedVersion: tintColor = Qt::green; break;
57 case KVersionControlPlugin::AddedVersion: tintColor = Qt::green; break;
58 case KVersionControlPlugin::RemovedVersion: tintColor = Qt::darkRed; break;
59 case KVersionControlPlugin::ConflictingVersion: tintColor = Qt::red; break;
60 case KVersionControlPlugin::UnversionedVersion: tintColor = Qt::white; break;
61 case KVersionControlPlugin::NormalVersion:
62 default:
63 break;
64 }
65
66 color = QColor((tintColor.red() + textColor.red()) / 2,
67 (tintColor.green() + textColor.green()) / 2,
68 (tintColor.blue() + textColor.blue()) / 2,
69 (tintColor.alpha() + textColor.alpha()) / 2);
70
71 overlay = overlayForState(version, styleOption().iconSize);
72 }
73
74 setTextColor(color);
75 setOverlay(overlay);
76 }
77
78 void DolphinFileItemListWidget::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
79 {
80 KFileItemListWidget::styleOptionChanged(current, previous);
81
82 if (!overlay().isNull() && current.iconSize != previous.iconSize) {
83 const KVersionControlPlugin::VersionState version = static_cast<KVersionControlPlugin::VersionState>(data().value("version").toInt());
84 const QPixmap newOverlay = overlayForState(version, current.iconSize);
85 setOverlay(newOverlay);
86 }
87 }
88
89 QPixmap DolphinFileItemListWidget::overlayForState(KVersionControlPlugin::VersionState version, int size)
90 {
91 int overlayHeight = KIconLoader::SizeSmall;
92 if (size >= KIconLoader::SizeEnormous) {
93 overlayHeight = KIconLoader::SizeMedium;
94 } else if (size >= KIconLoader::SizeLarge) {
95 overlayHeight = KIconLoader::SizeSmallMedium;
96 } else if (size >= KIconLoader::SizeMedium) {
97 overlayHeight = KIconLoader::SizeSmall;
98 } else {
99 overlayHeight = KIconLoader::SizeSmall / 2;
100 }
101
102 QString iconName;
103 switch (version) {
104 case KVersionControlPlugin::NormalVersion:
105 iconName = "vcs-normal";
106 break;
107 case KVersionControlPlugin::UpdateRequiredVersion:
108 iconName = "vcs-update-required";
109 break;
110 case KVersionControlPlugin::LocallyModifiedVersion:
111 iconName = "vcs-locally-modified";
112 break;
113 case KVersionControlPlugin::LocallyModifiedUnstagedVersion:
114 iconName = "vcs-locally-modified-unstaged";
115 break;
116 case KVersionControlPlugin::AddedVersion:
117 iconName = "vcs-added";
118 break;
119 case KVersionControlPlugin::RemovedVersion:
120 iconName = "vcs-removed";
121 break;
122 case KVersionControlPlugin::ConflictingVersion:
123 iconName = "vcs-conflicting";
124 break;
125 case KVersionControlPlugin::UnversionedVersion:
126 break;
127 default:
128 Q_ASSERT(false);
129 break;
130 }
131
132 return KIcon(iconName).pixmap(QSize(overlayHeight, overlayHeight));
133 }
134
135 #include "dolphinfileitemlistwidget.moc"