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 "kstandarditemlistgroupheader.h"
25 #include "kitemlistview.h"
27 #include <QGraphicsSceneResizeEvent>
29 #include <QStyleOptionGraphicsItem>
31 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget
* parent
) :
32 QGraphicsWidget(parent
, 0),
37 m_scrollOrientation(Qt::Vertical
),
45 KItemListGroupHeader::~KItemListGroupHeader()
49 void KItemListGroupHeader::setRole(const QByteArray
& role
)
52 const QByteArray previous
= m_role
;
55 roleChanged(role
, previous
);
59 QByteArray
KItemListGroupHeader::role() const
64 void KItemListGroupHeader::setData(const QVariant
& data
)
67 const QVariant previous
= m_data
;
70 dataChanged(data
, previous
);
74 QVariant
KItemListGroupHeader::data() const
79 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption
& option
)
81 const KItemListStyleOption previous
= m_styleOption
;
82 m_styleOption
= option
;
84 styleOptionChanged(option
, previous
);
87 const KItemListStyleOption
& KItemListGroupHeader::styleOption() const
92 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation
)
94 if (m_scrollOrientation
!= orientation
) {
95 const Qt::Orientation previous
= m_scrollOrientation
;
96 m_scrollOrientation
= orientation
;
97 if (orientation
== Qt::Vertical
) {
100 scrollOrientationChanged(orientation
, previous
);
104 void KItemListGroupHeader::setItemIndex(int index
)
106 if (m_itemIndex
!= index
) {
107 const int previous
= m_itemIndex
;
110 itemIndexChanged(m_itemIndex
, previous
);
114 int KItemListGroupHeader::itemIndex() const
119 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
121 return m_scrollOrientation
;
124 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
134 paintSeparator(painter
, m_separatorColor
);
135 paintRole(painter
, m_roleBounds
, m_roleColor
);
138 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
144 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
150 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
156 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
162 void KItemListGroupHeader::itemIndexChanged(int current
, int previous
)
168 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
170 QGraphicsWidget::resizeEvent(event
);
171 if (event
->oldSize().height() != event
->newSize().height()) {
176 void KItemListGroupHeader::updateCache()
178 Q_ASSERT(m_dirtyCache
);
180 // Calculate the role- and line-color. No alphablending is used for
181 // performance reasons.
182 const QColor c1
= textColor();
183 const QColor c2
= baseColor();
184 m_separatorColor
= mixedColor(c1
, c2
, 10);
185 m_roleColor
= mixedColor(c1
, c2
, 60);
187 const int padding
= qMax(1, m_styleOption
.padding
);
188 const int horizontalMargin
= qMax(2, m_styleOption
.horizontalMargin
);
190 const QFontMetrics
fontMetrics(m_styleOption
.font
);
191 const qreal roleHeight
= fontMetrics
.height();
193 const int y
= (m_scrollOrientation
== Qt::Vertical
) ? padding
: horizontalMargin
;
195 m_roleBounds
= QRectF(horizontalMargin
+ padding
,
197 size().width() - 2 * padding
- horizontalMargin
,
200 m_dirtyCache
= false;
203 QColor
KItemListGroupHeader::mixedColor(const QColor
& c1
, const QColor
& c2
, int c1Percent
)
205 Q_ASSERT(c1Percent
>= 0 && c1Percent
<= 100);
207 const int c2Percent
= 100 - c1Percent
;
208 return QColor((c1
.red() * c1Percent
+ c2
.red() * c2Percent
) / 100,
209 (c1
.green() * c1Percent
+ c2
.green() * c2Percent
) / 100,
210 (c1
.blue() * c1Percent
+ c2
.blue() * c2Percent
) / 100);
213 QPalette::ColorRole
KItemListGroupHeader::normalTextColorRole() const
215 return QPalette::Text
;
218 QPalette::ColorRole
KItemListGroupHeader::normalBaseColorRole() const
220 return QPalette::Window
;
223 QColor
KItemListGroupHeader::textColor() const
225 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
226 return styleOption().palette
.color(group
, normalTextColorRole());
229 QColor
KItemListGroupHeader::baseColor() const
231 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
232 return styleOption().palette
.color(group
, normalBaseColorRole());