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