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