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
),
44 KItemListGroupHeader::~KItemListGroupHeader()
48 void KItemListGroupHeader::setRole(const QByteArray
& role
)
51 const QByteArray previous
= m_role
;
54 roleChanged(role
, previous
);
58 QByteArray
KItemListGroupHeader::role() const
63 void KItemListGroupHeader::setData(const QVariant
& data
)
66 const QVariant previous
= m_data
;
69 dataChanged(data
, previous
);
73 QVariant
KItemListGroupHeader::data() const
78 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption
& option
)
80 const KItemListStyleOption previous
= m_styleOption
;
81 m_styleOption
= option
;
83 styleOptionChanged(option
, previous
);
86 const KItemListStyleOption
& KItemListGroupHeader::styleOption() const
91 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation
)
93 if (m_scrollOrientation
!= orientation
) {
94 const Qt::Orientation previous
= m_scrollOrientation
;
95 m_scrollOrientation
= orientation
;
96 if (orientation
== Qt::Vertical
) {
99 scrollOrientationChanged(orientation
, previous
);
103 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
105 return m_scrollOrientation
;
108 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
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
);
124 QRectF
KItemListGroupHeader::roleBounds() const
129 QColor
KItemListGroupHeader::roleColor() const
134 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
140 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
146 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
152 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
158 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
160 QGraphicsWidget::resizeEvent(event
);
161 if (event
->oldSize().height() != event
->newSize().height()) {
166 void KItemListGroupHeader::updateCache()
168 Q_ASSERT(m_dirtyCache
);
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();
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);
180 const int margin
= m_styleOption
.margin
;
181 const QFontMetrics
fontMetrics(m_styleOption
.font
);
182 const qreal roleHeight
= fontMetrics
.height();
184 m_roleBounds
= QRectF(margin
,
185 size().height() - roleHeight
- margin
,
186 size().width() - 2 * margin
,
189 m_dirtyCache
= false;
192 #include "kitemlistgroupheader.moc"