]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistgroupheader.cpp
f8576b08cfcd5da7157c0e9c0b9e99e9ef6469eb
[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
31 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget* parent) :
32 QGraphicsWidget(parent, 0),
33 m_dirtyCache(true),
34 m_role(),
35 m_data(),
36 m_styleOption(),
37 m_scrollOrientation(Qt::Vertical),
38 m_itemIndex(-1),
39 m_separatorColor(),
40 m_roleColor(),
41 m_roleBounds()
42 {
43 }
44
45 KItemListGroupHeader::~KItemListGroupHeader()
46 {
47 }
48
49 void KItemListGroupHeader::setRole(const QByteArray& role)
50 {
51 if (m_role != role) {
52 const QByteArray previous = m_role;
53 m_role = role;
54 update();
55 roleChanged(role, previous);
56 }
57 }
58
59 QByteArray KItemListGroupHeader::role() const
60 {
61 return m_role;
62 }
63
64 void KItemListGroupHeader::setData(const QVariant& data)
65 {
66 if (m_data != data) {
67 const QVariant previous = m_data;
68 m_data = data;
69 update();
70 dataChanged(data, previous);
71 }
72 }
73
74 QVariant KItemListGroupHeader::data() const
75 {
76 return m_data;
77 }
78
79 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption& option)
80 {
81 const KItemListStyleOption previous = m_styleOption;
82 m_styleOption = option;
83 m_dirtyCache = true;
84 styleOptionChanged(option, previous);
85 }
86
87 const KItemListStyleOption& KItemListGroupHeader::styleOption() const
88 {
89 return m_styleOption;
90 }
91
92 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation)
93 {
94 if (m_scrollOrientation != orientation) {
95 const Qt::Orientation previous = m_scrollOrientation;
96 m_scrollOrientation = orientation;
97 if (orientation == Qt::Vertical) {
98 m_dirtyCache = true;
99 }
100 scrollOrientationChanged(orientation, previous);
101 }
102 }
103
104 void KItemListGroupHeader::setItemIndex(int index)
105 {
106 if (m_itemIndex != index) {
107 const int previous = m_itemIndex;
108 m_itemIndex = index;
109 m_dirtyCache = true;
110 itemIndexChanged(m_itemIndex, previous);
111 }
112 }
113
114 int KItemListGroupHeader::itemIndex() const
115 {
116 return m_itemIndex;
117 }
118
119 Qt::Orientation KItemListGroupHeader::scrollOrientation() const
120 {
121 return m_scrollOrientation;
122 }
123
124 void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
125 {
126 Q_UNUSED(painter);
127 Q_UNUSED(option);
128 Q_UNUSED(widget);
129
130 if (m_dirtyCache) {
131 updateCache();
132 }
133
134 paintSeparator(painter, m_separatorColor);
135 paintRole(painter, m_roleBounds, m_roleColor);
136 }
137
138 void KItemListGroupHeader::roleChanged(const QByteArray& current, const QByteArray& previous)
139 {
140 Q_UNUSED(current);
141 Q_UNUSED(previous);
142 }
143
144 void KItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous)
145 {
146 Q_UNUSED(current);
147 Q_UNUSED(previous);
148 }
149
150 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
151 {
152 Q_UNUSED(current);
153 Q_UNUSED(previous);
154 }
155
156 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
157 {
158 Q_UNUSED(current);
159 Q_UNUSED(previous);
160 }
161
162 void KItemListGroupHeader::itemIndexChanged(int current, int previous)
163 {
164 Q_UNUSED(current);
165 Q_UNUSED(previous);
166 }
167
168 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event)
169 {
170 QGraphicsWidget::resizeEvent(event);
171 if (event->oldSize().height() != event->newSize().height()) {
172 m_dirtyCache = true;
173 }
174 }
175
176 void KItemListGroupHeader::updateCache()
177 {
178 Q_ASSERT(m_dirtyCache);
179
180 // Calculate the role- and line-color. No alphablending is used for
181 // performance reasons.
182 const QColor c1 = textColor();
183 const QColor c2 = baseColor();
184 m_separatorColor = mixedColor(c1, c2, 10);
185 m_roleColor = mixedColor(c1, c2, 60);
186
187 const int padding = qMax(1, m_styleOption.padding);
188 const int horizontalMargin = qMax(2, m_styleOption.horizontalMargin);
189
190 const QFontMetrics fontMetrics(m_styleOption.font);
191 const qreal roleHeight = fontMetrics.height();
192
193 const int y = (m_scrollOrientation == Qt::Vertical) ? padding : horizontalMargin;
194
195 m_roleBounds = QRectF(horizontalMargin + padding,
196 y,
197 size().width() - 2 * padding - horizontalMargin,
198 roleHeight);
199
200 m_dirtyCache = false;
201 }
202
203 QColor KItemListGroupHeader::mixedColor(const QColor& c1, const QColor& c2, int c1Percent)
204 {
205 Q_ASSERT(c1Percent >= 0 && c1Percent <= 100);
206
207 const int c2Percent = 100 - c1Percent;
208 return QColor((c1.red() * c1Percent + c2.red() * c2Percent) / 100,
209 (c1.green() * c1Percent + c2.green() * c2Percent) / 100,
210 (c1.blue() * c1Percent + c2.blue() * c2Percent) / 100);
211 }
212
213 QPalette::ColorRole KItemListGroupHeader::normalTextColorRole() const
214 {
215 return QPalette::Text;
216 }
217
218 QPalette::ColorRole KItemListGroupHeader::normalBaseColorRole() const
219 {
220 return QPalette::Window;
221 }
222
223 QColor KItemListGroupHeader::textColor() const
224 {
225 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
226 return styleOption().palette.color(group, normalTextColorRole());
227 }
228
229 QColor KItemListGroupHeader::baseColor() const
230 {
231 const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
232 return styleOption().palette.color(group, normalBaseColorRole());
233 }
234