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