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