1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
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. *
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. *
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 ***************************************************************************/
23 #include "kitemlistgroupheader.h"
25 #include "kitemlistview.h"
27 #include <QGraphicsSceneResizeEvent>
29 #include <QStyleOptionGraphicsItem>
32 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget
* parent
) :
33 QGraphicsWidget(parent
, 0),
38 m_scrollOrientation(Qt::Vertical
),
40 m_rightBorderCache(0),
45 KItemListGroupHeader::~KItemListGroupHeader()
50 void KItemListGroupHeader::setRole(const QByteArray
& role
)
53 const QByteArray previous
= m_role
;
56 roleChanged(role
, previous
);
60 QByteArray
KItemListGroupHeader::role() const
65 void KItemListGroupHeader::setData(const QVariant
& data
)
68 const QVariant previous
= m_data
;
71 dataChanged(data
, previous
);
75 QVariant
KItemListGroupHeader::data() const
80 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption
& option
)
82 const KItemListStyleOption previous
= m_styleOption
;
83 m_styleOption
= option
;
85 styleOptionChanged(option
, previous
);
88 const KItemListStyleOption
& KItemListGroupHeader::styleOption() const
93 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation
)
95 if (m_scrollOrientation
!= orientation
) {
96 const Qt::Orientation previous
= m_scrollOrientation
;
97 m_scrollOrientation
= orientation
;
98 if (orientation
== Qt::Vertical
) {
103 scrollOrientationChanged(orientation
, previous
);
107 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
109 return m_scrollOrientation
;
112 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
117 if (m_scrollOrientation
== Qt::Horizontal
) {
118 Q_ASSERT(!m_leftBorderCache
);
119 Q_ASSERT(!m_rightBorderCache
);
127 const int leftBorderX
= m_leftBorderCache
->width() + 1;
128 const int rightBorderX
= size().width() - m_rightBorderCache
->width() - 2;
130 painter
->setPen(m_outlineColor
);
131 painter
->drawLine(leftBorderX
, 1, rightBorderX
, 1);
133 painter
->drawPixmap(1, 1, *m_leftBorderCache
);
134 painter
->drawPixmap(rightBorderX
, 1, *m_rightBorderCache
);
137 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
143 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
149 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
155 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
161 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
163 QGraphicsWidget::resizeEvent(event
);
164 if (event
->oldSize().height() != event
->newSize().height()) {
169 void KItemListGroupHeader::updateCache()
171 Q_ASSERT(m_dirtyCache
);
175 const int length
= qMax(int(size().height() - 1), 1);
176 m_leftBorderCache
= new QPixmap(length
, length
);
177 m_leftBorderCache
->fill(Qt::transparent
);
179 m_rightBorderCache
= new QPixmap(length
, length
);
180 m_rightBorderCache
->fill(Qt::transparent
);
182 // Calculate the outline color. No alphablending is used for
183 // performance reasons.
184 const QColor c1
= m_styleOption
.palette
.text().color();
185 const QColor c2
= m_styleOption
.palette
.background().color();
187 const int p2
= 100 - p1
;
188 m_outlineColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
189 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
190 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
192 // The drawing code is based on the code of DolphinCategoryDrawer from Dolphin 1.7
193 // Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
195 // Cache the left border as pixmap
196 QPainter
painter(m_leftBorderCache
);
197 painter
.setPen(m_outlineColor
);
199 // 1. Draw top horizontal line
200 painter
.drawLine(3, 0, length
, 0);
202 // 2. Draw vertical line with gradient
203 const QPoint
start(0, 3);
204 QLinearGradient
gradient(start
, QPoint(0, length
));
205 gradient
.setColorAt(0, m_outlineColor
);
206 gradient
.setColorAt(1, Qt::transparent
);
207 painter
.fillRect(QRect(start
, QSize(1, length
- start
.y())), gradient
);
210 painter
.setRenderHint(QPainter::Antialiasing
);
211 QRectF
arc(QPointF(0, 0), QSizeF(4, 4));
212 arc
.translate(0.5, 0.5);
213 painter
.drawArc(arc
, 1440, 1440);
217 // Cache the right border as pixmap
218 QPainter
painter(m_rightBorderCache
);
219 painter
.setPen(m_outlineColor
);
221 // 1. Draw top horizontal line
222 painter
.drawLine(0, 0, length
- 3, 0);
224 // 2. Draw vertical line with gradient
225 const int right
= length
- 1;
226 const QPoint
start(right
, 3);
227 QLinearGradient
gradient(start
, QPoint(right
, length
));
228 gradient
.setColorAt(0, m_outlineColor
);
229 gradient
.setColorAt(1, Qt::transparent
);
230 painter
.fillRect(QRect(start
, QSize(1, length
- start
.y())), gradient
);
233 painter
.setRenderHint(QPainter::Antialiasing
);
234 QRectF
arc(QPointF(length
- 5, 0), QSizeF(4, 4));
235 arc
.translate(0.5, 0.5);
236 painter
.drawArc(arc
, 0, 1440);
239 m_dirtyCache
= false;
242 void KItemListGroupHeader::deleteCache()
244 delete m_leftBorderCache
;
245 m_leftBorderCache
= 0;
247 delete m_rightBorderCache
;
248 m_rightBorderCache
= 0;
251 #include "kitemlistgroupheader.moc"