]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistgroupheader.cpp
Fix visibility issues of group-headers
[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_leftBorderCache(0),
40 m_rightBorderCache(0),
41 m_outlineColor()
42 {
43 }
44
45 KItemListGroupHeader::~KItemListGroupHeader()
46 {
47 delete m_leftBorderCache;
48 delete m_rightBorderCache;
49 }
50
51 void KItemListGroupHeader::setRole(const QByteArray& role)
52 {
53 if (m_role != role) {
54 const QByteArray previous = m_role;
55 m_role = role;
56 update();
57 roleChanged(role, previous);
58 }
59 }
60
61 QByteArray KItemListGroupHeader::role() const
62 {
63 return m_role;
64 }
65
66 void KItemListGroupHeader::setData(const QVariant& data)
67 {
68 if (m_data != data) {
69 const QVariant previous = m_data;
70 m_data = data;
71 update();
72 dataChanged(data, previous);
73 }
74 }
75
76 QVariant KItemListGroupHeader::data() const
77 {
78 return m_data;
79 }
80
81 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption& option)
82 {
83 const KItemListStyleOption previous = m_styleOption;
84 m_styleOption = option;
85 m_dirtyCache = true;
86 styleOptionChanged(option, previous);
87 }
88
89 const KItemListStyleOption& KItemListGroupHeader::styleOption() const
90 {
91 return m_styleOption;
92 }
93
94 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation)
95 {
96 if (m_scrollOrientation != orientation) {
97 const Qt::Orientation previous = m_scrollOrientation;
98 m_scrollOrientation = orientation;
99 m_dirtyCache = true;
100 scrollOrientationChanged(orientation, previous);
101 }
102 }
103
104 Qt::Orientation KItemListGroupHeader::scrollOrientation() const
105 {
106 return m_scrollOrientation;
107 }
108
109 void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
110 {
111 if (m_dirtyCache) {
112 updateCache();
113 }
114
115 Q_UNUSED(option);
116 Q_UNUSED(widget);
117
118 const int leftBorderX = m_leftBorderCache->width() + 1;
119 const int rightBorderX = size().width() - m_rightBorderCache->width() - 2;
120
121 painter->setPen(m_outlineColor);
122 painter->drawLine(leftBorderX, 1, rightBorderX, 1);
123
124 painter->drawPixmap(1, 1, *m_leftBorderCache);
125 painter->drawPixmap(rightBorderX, 1, *m_rightBorderCache);
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::resizeEvent(QGraphicsSceneResizeEvent* event)
153 {
154 QGraphicsWidget::resizeEvent(event);
155 if (event->oldSize().height() != event->newSize().height()) {
156 m_dirtyCache = true;
157 }
158 }
159
160 void KItemListGroupHeader::updateCache()
161 {
162 Q_ASSERT(m_dirtyCache);
163
164 delete m_leftBorderCache;
165 delete m_rightBorderCache;
166
167 const int length = qMax(int(size().height() - 1), 1);
168 m_leftBorderCache = new QPixmap(length, length);
169 m_leftBorderCache->fill(Qt::transparent);
170
171 m_rightBorderCache = new QPixmap(length, length);
172 m_rightBorderCache->fill(Qt::transparent);
173
174 // Calculate the outline color. No alphablending is used for
175 // performance reasons.
176 const QColor c1 = m_styleOption.palette.text().color();
177 const QColor c2 = m_styleOption.palette.background().color();
178 const int p1 = 35;
179 const int p2 = 100 - p1;
180 m_outlineColor = QColor((c1.red() * p1 + c2.red() * p2) / 100,
181 (c1.green() * p1 + c2.green() * p2) / 100,
182 (c1.blue() * p1 + c2.blue() * p2) / 100);
183
184 // The drawing code is based on the code of DolphinCategoryDrawer from Dolphin 1.7
185 // Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
186 {
187 // Cache the left border as pixmap
188 QPainter painter(m_leftBorderCache);
189 painter.setPen(m_outlineColor);
190
191 // 1. Draw top horizontal line
192 painter.drawLine(3, 0, length, 0);
193
194 // 2. Draw vertical line with gradient
195 const QPoint start(0, 3);
196 QLinearGradient gradient(start, QPoint(0, length));
197 gradient.setColorAt(0, m_outlineColor);
198 gradient.setColorAt(1, Qt::transparent);
199 painter.fillRect(QRect(start, QSize(1, length - start.y())), gradient);
200
201 // 3. Draw arc
202 painter.setRenderHint(QPainter::Antialiasing);
203 QRectF arc(QPointF(0, 0), QSizeF(4, 4));
204 arc.translate(0.5, 0.5);
205 painter.drawArc(arc, 1440, 1440);
206 }
207
208 {
209 // Cache the right border as pixmap
210 QPainter painter(m_rightBorderCache);
211 painter.setPen(m_outlineColor);
212
213 const int right = length - 1;
214 if (m_scrollOrientation == Qt::Vertical) {
215 // 1. Draw top horizontal line
216 painter.drawLine(0, 0, length - 3, 0);
217
218 // 2. Draw vertical line with gradient
219 const QPoint start(right, 3);
220 QLinearGradient gradient(start, QPoint(right, length));
221 gradient.setColorAt(0, m_outlineColor);
222 gradient.setColorAt(1, Qt::transparent);
223 painter.fillRect(QRect(start, QSize(1, length - start.y())), gradient);
224
225 // 3. Draw arc
226 painter.setRenderHint(QPainter::Antialiasing);
227 QRectF arc(QPointF(length - 5, 0), QSizeF(4, 4));
228 arc.translate(0.5, 0.5);
229 painter.drawArc(arc, 0, 1440);
230 } else {
231 // Draw a horizontal gradiented line
232 QLinearGradient gradient(QPoint(0, 0), QPoint(length, 0));
233 gradient.setColorAt(0, m_outlineColor);
234 gradient.setColorAt(1, Qt::transparent);
235 painter.fillRect(QRect(QPoint(0, 0), QSize(length, 1)), gradient);
236 }
237 }
238
239 m_dirtyCache = false;
240 }
241
242 #include "kitemlistgroupheader.moc"