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>
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
)
135 paintSeparator(painter
, m_separatorColor
);
136 paintRole(painter
, m_roleBounds
, m_roleColor
);
139 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
145 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
151 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
157 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
163 void KItemListGroupHeader::itemIndexChanged(int current
, int previous
)
169 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
171 QGraphicsWidget::resizeEvent(event
);
172 if (event
->oldSize().height() != event
->newSize().height()) {
177 void KItemListGroupHeader::updateCache()
179 Q_ASSERT(m_dirtyCache
);
181 // Calculate the role- and line-color. No alphablending is used for
182 // performance reasons.
183 const QColor c1
= textColor();
184 const QColor c2
= baseColor();
185 m_separatorColor
= mixedColor(c1
, c2
, 10);
186 m_roleColor
= mixedColor(c1
, c2
, 60);
188 const int padding
= qMax(1, m_styleOption
.padding
);
189 const int horizontalMargin
= qMax(2, m_styleOption
.horizontalMargin
);
191 const QFontMetrics
fontMetrics(m_styleOption
.font
);
192 const qreal roleHeight
= fontMetrics
.height();
194 const int y
= (m_scrollOrientation
== Qt::Vertical
) ? padding
: horizontalMargin
;
196 m_roleBounds
= QRectF(horizontalMargin
+ padding
,
198 size().width() - 2 * padding
- horizontalMargin
,
201 m_dirtyCache
= false;
204 QColor
KItemListGroupHeader::mixedColor(const QColor
& c1
, const QColor
& c2
, int c1Percent
)
206 Q_ASSERT(c1Percent
>= 0 && c1Percent
<= 100);
208 const int c2Percent
= 100 - c1Percent
;
209 return QColor((c1
.red() * c1Percent
+ c2
.red() * c2Percent
) / 100,
210 (c1
.green() * c1Percent
+ c2
.green() * c2Percent
) / 100,
211 (c1
.blue() * c1Percent
+ c2
.blue() * c2Percent
) / 100);
214 QPalette::ColorRole
KItemListGroupHeader::normalTextColorRole() const
216 return QPalette::Text
;
219 QPalette::ColorRole
KItemListGroupHeader::normalBaseColorRole() const
221 return QPalette::Window
;
224 QColor
KItemListGroupHeader::textColor() const
226 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
227 return styleOption().palette
.color(group
, normalTextColorRole());
230 QColor
KItemListGroupHeader::baseColor() const
232 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
233 return styleOption().palette
.color(group
, normalBaseColorRole());