]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistgroupheader.cpp
Fix wrong text color in Places Group Header. Use QPalette::Window for base color...
[dolphin.git] / src / kitemviews / kitemlistgroupheader.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kstandarditemlistgroupheader.h"
24
25 #include "kitemlistview.h"
26
27 #include <QGraphicsSceneResizeEvent>
28 #include <QPainter>
29 #include <QStyleOptionGraphicsItem>
30 #include <KDebug>
31
32 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget* parent) :
33 QGraphicsWidget(parent, 0),
34 m_dirtyCache(true),
35 m_role(),
36 m_data(),
37 m_styleOption(),
38 m_scrollOrientation(Qt::Vertical),
39 m_itemIndex(-1),
40 m_separatorColor(),
41 m_roleColor(),
42 m_roleBounds()
43 {
44 }
45
46 KItemListGroupHeader::~KItemListGroupHeader()
47 {
48 }
49
50 void KItemListGroupHeader::setRole(const QByteArray& role)
51 {
52 if (m_role != role) {
53 const QByteArray previous = m_role;
54 m_role = role;
55 update();
56 roleChanged(role, previous);
57 }
58 }
59
60 QByteArray KItemListGroupHeader::role() const
61 {
62 return m_role;
63 }
64
65 void KItemListGroupHeader::setData(const QVariant& data)
66 {
67 if (m_data != data) {
68 const QVariant previous = m_data;
69 m_data = data;
70 update();
71 dataChanged(data, previous);
72 }
73 }
74
75 QVariant KItemListGroupHeader::data() const
76 {
77 return m_data;
78 }
79
80 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption& option)
81 {
82 const KItemListStyleOption previous = m_styleOption;
83 m_styleOption = option;
84 m_dirtyCache = true;
85 styleOptionChanged(option, previous);
86 }
87
88 const KItemListStyleOption& KItemListGroupHeader::styleOption() const
89 {
90 return m_styleOption;
91 }
92
93 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation)
94 {
95 if (m_scrollOrientation != orientation) {
96 const Qt::Orientation previous = m_scrollOrientation;
97 m_scrollOrientation = orientation;
98 if (orientation == Qt::Vertical) {
99 m_dirtyCache = true;
100 }
101 scrollOrientationChanged(orientation, previous);
102 }
103 }
104
105 void KItemListGroupHeader::setItemIndex(int index)
106 {
107 if (m_itemIndex != index) {
108 const int previous = m_itemIndex;
109 m_itemIndex = index;
110 m_dirtyCache = true;
111 itemIndexChanged(m_itemIndex, previous);
112 }
113 }
114
115 int KItemListGroupHeader::itemIndex() const
116 {
117 return m_itemIndex;
118 }
119
120 Qt::Orientation KItemListGroupHeader::scrollOrientation() const
121 {
122 return m_scrollOrientation;
123 }
124
125 void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
126 {
127 Q_UNUSED(painter);
128 Q_UNUSED(option);
129 Q_UNUSED(widget);
130
131 if (m_dirtyCache) {
132 updateCache();
133 }
134
135 paintSeparator(painter, m_separatorColor);
136 paintRole(painter, m_roleBounds, m_roleColor);
137 }
138
139 void KItemListGroupHeader::roleChanged(const QByteArray& current, const QByteArray& previous)
140 {
141 Q_UNUSED(current);
142 Q_UNUSED(previous);
143 }
144
145 void KItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous)
146 {
147 Q_UNUSED(current);
148 Q_UNUSED(previous);
149 }
150
151 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
152 {
153 Q_UNUSED(current);
154 Q_UNUSED(previous);
155 }
156
157 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
158 {
159 Q_UNUSED(current);
160 Q_UNUSED(previous);
161 }
162
163 void KItemListGroupHeader::itemIndexChanged(int current, int previous)
164 {
165 Q_UNUSED(current);
166 Q_UNUSED(previous);
167 }
168
169 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event)
170 {
171 QGraphicsWidget::resizeEvent(event);
172 if (event->oldSize().height() != event->newSize().height()) {
173 m_dirtyCache = true;
174 }
175 }
176
177 void KItemListGroupHeader::updateCache()
178 {
179 Q_ASSERT(m_dirtyCache);
180
181 // Calculate the role- and line-color. No alphablending is used for
182 // performance reasons.
183 const QColor c1 = textColor();
184 const QColor c2 = baseColor();
185 m_separatorColor = mixedColor(c1, c2, 10);
186 m_roleColor = mixedColor(c1, c2, 60);
187
188 const int padding = qMax(1, m_styleOption.padding);
189 const int horizontalMargin = qMax(2, m_styleOption.horizontalMargin);
190
191 const QFontMetrics fontMetrics(m_styleOption.font);
192 const qreal roleHeight = fontMetrics.height();
193
194 const int y = (m_scrollOrientation == Qt::Vertical) ? padding : horizontalMargin;
195
196 m_roleBounds = QRectF(horizontalMargin + padding,
197 y,
198 size().width() - 2 * padding - horizontalMargin,
199 roleHeight);
200
201 m_dirtyCache = false;
202 }
203
204 QColor KItemListGroupHeader::mixedColor(const QColor& c1, const QColor& c2, int c1Percent)
205 {
206 Q_ASSERT(c1Percent >= 0 && c1Percent <= 100);
207
208 const int c2Percent = 100 - c1Percent;
209 return QColor((c1.red() * c1Percent + c2.red() * c2Percent) / 100,
210 (c1.green() * c1Percent + c2.green() * c2Percent) / 100,
211 (c1.blue() * c1Percent + c2.blue() * c2Percent) / 100);
212 }
213
214 QPalette::ColorRole KItemListGroupHeader::normalTextColorRole() const
215 {
216 return QPalette::Text;
217 }
218
219 QPalette::ColorRole KItemListGroupHeader::normalBaseColorRole() const
220 {
221 return QPalette::Window;
222 }
223
224 QColor KItemListGroupHeader::textColor() const
225 {
226 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
227 return styleOption().palette.color(group, normalTextColorRole());
228 }
229
230 QColor KItemListGroupHeader::baseColor() const
231 {
232 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
233 return styleOption().palette.color(group, normalBaseColorRole());
234 }
235
236 #include "kitemlistgroupheader.moc"