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
),
46 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
) {
101 scrollOrientationChanged(orientation
, previous
);
105 void KItemListGroupHeader::setItemIndex(int index
)
107 if (m_itemIndex
!= index
) {
108 const int previous
= m_itemIndex
;
111 itemIndexChanged(m_itemIndex
, previous
);
115 int KItemListGroupHeader::itemIndex() const
120 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
122 return m_scrollOrientation
;
125 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
134 if (m_scrollOrientation
== Qt::Horizontal
) {
135 painter
->setPen(m_lineColor
);
136 const qreal x
= m_roleBounds
.x() - 2 * m_styleOption
.padding
;
137 painter
->drawLine(x
, 0, x
, size().height() - 1);
139 } else if (m_itemIndex
> 0) {
140 painter
->setPen(m_lineColor
);
141 const qreal y
= m_roleBounds
.y() - m_styleOption
.padding
;
142 painter
->drawLine(0, y
, size().width() - 1, y
);
146 QRectF
KItemListGroupHeader::roleBounds() const
151 QColor
KItemListGroupHeader::roleColor() const
156 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
162 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
168 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
174 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
180 void KItemListGroupHeader::itemIndexChanged(int current
, int previous
)
186 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
188 QGraphicsWidget::resizeEvent(event
);
189 if (event
->oldSize().height() != event
->newSize().height()) {
194 void KItemListGroupHeader::updateCache()
196 Q_ASSERT(m_dirtyCache
);
198 // Calculate the role- and line-color. No alphablending is used for
199 // performance reasons.
200 const QColor c1
= m_styleOption
.palette
.text().color();
201 const QColor c2
= m_styleOption
.palette
.base().color();
202 m_lineColor
= mixedColor(c1
, c2
, 10);
203 m_roleColor
= mixedColor(c1
, c2
, 70);
205 int padding
= m_styleOption
.padding
;
206 int horizontalMargin
= 0;
207 if (m_scrollOrientation
== Qt::Vertical
) {
208 // The x-position of the group-header-widget will always be 0,
209 // Add a minimum margin.
210 horizontalMargin
= qMax(2, m_styleOption
.horizontalMargin
);
215 const QFontMetrics
fontMetrics(m_styleOption
.font
);
216 const qreal roleHeight
= fontMetrics
.height();
218 m_roleBounds
= QRectF(horizontalMargin
+ padding
,
219 size().height() - roleHeight
- padding
,
220 size().width() - 2 * (horizontalMargin
+ padding
),
223 m_dirtyCache
= false;
226 QColor
KItemListGroupHeader::mixedColor(const QColor
& c1
, const QColor
& c2
, int c1Percent
)
228 Q_ASSERT(c1Percent
>= 0 && c1Percent
<= 100);
230 const int c2Percent
= 100 - c1Percent
;
231 return QColor((c1
.red() * c1Percent
+ c2
.red() * c2Percent
) / 100,
232 (c1
.green() * c1Percent
+ c2
.green() * c2Percent
) / 100,
233 (c1
.blue() * c1Percent
+ c2
.blue() * c2Percent
) / 100);
236 #include "kitemlistgroupheader.moc"