]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistgroupheader.cpp
bc68cd562c2f8f2836bcb64f27ca66a756637075
[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 "kitemlistgroupheader.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_lineColor(),
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(option);
128 Q_UNUSED(widget);
129
130 if (m_dirtyCache) {
131 updateCache();
132 }
133
134 if (m_itemIndex == 0) {
135 // No top- or left-line should be drawn for the first group-header
136 return;
137 }
138
139 painter->setPen(m_lineColor);
140
141 if (m_scrollOrientation == Qt::Horizontal) {
142 painter->drawLine(0, 0, 0, size().height() - 1);
143 } else {
144 painter->drawLine(0, 0, size().width() - 1, 0);
145 }
146 }
147
148 QRectF KItemListGroupHeader::roleBounds() const
149 {
150 return m_roleBounds;
151 }
152
153 QColor KItemListGroupHeader::roleColor() const
154 {
155 return m_roleColor;
156 }
157
158 void KItemListGroupHeader::roleChanged(const QByteArray& current, const QByteArray& previous)
159 {
160 Q_UNUSED(current);
161 Q_UNUSED(previous);
162 }
163
164 void KItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous)
165 {
166 Q_UNUSED(current);
167 Q_UNUSED(previous);
168 }
169
170 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
171 {
172 Q_UNUSED(current);
173 Q_UNUSED(previous);
174 }
175
176 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
177 {
178 Q_UNUSED(current);
179 Q_UNUSED(previous);
180 }
181
182 void KItemListGroupHeader::itemIndexChanged(int current, int previous)
183 {
184 Q_UNUSED(current);
185 Q_UNUSED(previous);
186 }
187
188 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event)
189 {
190 QGraphicsWidget::resizeEvent(event);
191 if (event->oldSize().height() != event->newSize().height()) {
192 m_dirtyCache = true;
193 }
194 }
195
196 void KItemListGroupHeader::updateCache()
197 {
198 Q_ASSERT(m_dirtyCache);
199
200 // Calculate the role- and line-color. No alphablending is used for
201 // performance reasons.
202 const QColor c1 = m_styleOption.palette.text().color();
203 const QColor c2 = m_styleOption.palette.base().color();
204 m_lineColor = mixedColor(c1, c2, 10);
205 m_roleColor = mixedColor(c1, c2, 70);
206
207 const int padding = qMax(1, m_styleOption.padding);
208 const int horizontalMargin = qMax(2, m_styleOption.horizontalMargin);
209
210 const QFontMetrics fontMetrics(m_styleOption.font);
211 const qreal roleHeight = fontMetrics.height();
212
213 const int y = (m_scrollOrientation == Qt::Vertical) ? padding : horizontalMargin;
214
215 m_roleBounds = QRectF(horizontalMargin + padding,
216 y,
217 size().width() - 2 * padding - horizontalMargin,
218 roleHeight);
219
220 m_dirtyCache = false;
221 }
222
223 QColor KItemListGroupHeader::mixedColor(const QColor& c1, const QColor& c2, int c1Percent)
224 {
225 Q_ASSERT(c1Percent >= 0 && c1Percent <= 100);
226
227 const int c2Percent = 100 - c1Percent;
228 return QColor((c1.red() * c1Percent + c2.red() * c2Percent) / 100,
229 (c1.green() * c1Percent + c2.green() * c2Percent) / 100,
230 (c1.blue() * c1Percent + c2.blue() * c2Percent) / 100);
231 }
232
233 #include "kitemlistgroupheader.moc"