]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistgroupheader.cpp
Group header improvements
[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_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 const KItemListStyleOption previous = m_styleOption;
81 m_styleOption = option;
82 m_dirtyCache = true;
83 styleOptionChanged(option, previous);
84 }
85
86 const KItemListStyleOption& KItemListGroupHeader::styleOption() const
87 {
88 return m_styleOption;
89 }
90
91 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation)
92 {
93 if (m_scrollOrientation != orientation) {
94 const Qt::Orientation previous = m_scrollOrientation;
95 m_scrollOrientation = orientation;
96 if (orientation == Qt::Vertical) {
97 m_dirtyCache = true;
98 }
99 scrollOrientationChanged(orientation, previous);
100 }
101 }
102
103 Qt::Orientation KItemListGroupHeader::scrollOrientation() const
104 {
105 return m_scrollOrientation;
106 }
107
108 void KItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
109 {
110 Q_UNUSED(option);
111 Q_UNUSED(widget);
112
113 if (m_dirtyCache) {
114 updateCache();
115 }
116
117 if (m_scrollOrientation != Qt::Horizontal) {
118 painter->setPen(m_roleColor);
119 const qreal y = m_roleBounds.y() - m_styleOption.margin;
120 painter->drawLine(0, y, size().width() - 1, y);
121 }
122 }
123
124 QRectF KItemListGroupHeader::roleBounds() const
125 {
126 return m_roleBounds;
127 }
128
129 QColor KItemListGroupHeader::roleColor() const
130 {
131 return m_roleColor;
132 }
133
134 void KItemListGroupHeader::roleChanged(const QByteArray& current, const QByteArray& previous)
135 {
136 Q_UNUSED(current);
137 Q_UNUSED(previous);
138 }
139
140 void KItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous)
141 {
142 Q_UNUSED(current);
143 Q_UNUSED(previous);
144 }
145
146 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption& current, const KItemListStyleOption& previous)
147 {
148 Q_UNUSED(current);
149 Q_UNUSED(previous);
150 }
151
152 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current, Qt::Orientation 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 outline color. No alphablending is used for
171 // performance reasons.
172 const QColor c1 = m_styleOption.palette.text().color();
173 const QColor c2 = m_styleOption.palette.background().color();
174 const int p1 = 35;
175 const int p2 = 100 - p1;
176 m_roleColor = QColor((c1.red() * p1 + c2.red() * p2) / 100,
177 (c1.green() * p1 + c2.green() * p2) / 100,
178 (c1.blue() * p1 + c2.blue() * p2) / 100);
179
180 const int margin = m_styleOption.margin;
181 const QFontMetrics fontMetrics(m_styleOption.font);
182 const qreal roleHeight = fontMetrics.height();
183
184 m_roleBounds = QRectF(margin,
185 size().height() - roleHeight - margin,
186 size().width() - 2 * margin,
187 roleHeight);
188
189 m_dirtyCache = false;
190 }
191
192 #include "kitemlistgroupheader.moc"