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