1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
4 * Based on the Itemviews NG project from Trolltech Labs *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
22 #include "kstandarditemlistgroupheader.h"
24 #include "kitemlistview.h"
26 #include <QGraphicsSceneResizeEvent>
28 #include <QStyleOptionGraphicsItem>
30 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget
* parent
) :
31 QGraphicsWidget(parent
),
36 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 if (m_styleOption
== option
) {
84 const KItemListStyleOption previous
= m_styleOption
;
85 m_styleOption
= option
;
87 styleOptionChanged(option
, previous
);
90 const KItemListStyleOption
& KItemListGroupHeader::styleOption() const
95 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation
)
97 if (m_scrollOrientation
!= orientation
) {
98 const Qt::Orientation previous
= m_scrollOrientation
;
99 m_scrollOrientation
= orientation
;
100 if (orientation
== Qt::Vertical
) {
103 scrollOrientationChanged(orientation
, previous
);
107 void KItemListGroupHeader::setItemIndex(int index
)
109 if (m_itemIndex
!= index
) {
110 const int previous
= m_itemIndex
;
113 itemIndexChanged(m_itemIndex
, previous
);
117 int KItemListGroupHeader::itemIndex() const
122 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
124 return m_scrollOrientation
;
127 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
137 paintSeparator(painter
, m_separatorColor
);
138 paintRole(painter
, m_roleBounds
, m_roleColor
);
141 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
147 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
153 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
159 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
165 void KItemListGroupHeader::itemIndexChanged(int current
, int previous
)
171 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
173 QGraphicsWidget::resizeEvent(event
);
174 if (event
->oldSize().height() != event
->newSize().height()) {
179 void KItemListGroupHeader::updateCache()
181 Q_ASSERT(m_dirtyCache
);
183 // Calculate the role- and line-color. No alphablending is used for
184 // performance reasons.
185 const QColor c1
= textColor();
186 const QColor c2
= baseColor();
187 m_separatorColor
= mixedColor(c1
, c2
, 10);
188 m_roleColor
= mixedColor(c1
, c2
, 60);
190 const int padding
= qMax(1, m_styleOption
.padding
);
191 const int horizontalMargin
= qMax(2, m_styleOption
.horizontalMargin
);
193 const QFontMetrics
fontMetrics(m_styleOption
.font
);
194 const qreal roleHeight
= fontMetrics
.height();
196 const int y
= (m_scrollOrientation
== Qt::Vertical
) ? padding
: horizontalMargin
;
198 m_roleBounds
= QRectF(horizontalMargin
+ padding
,
200 size().width() - 2 * padding
- horizontalMargin
,
203 m_dirtyCache
= false;
206 QColor
KItemListGroupHeader::mixedColor(const QColor
& c1
, const QColor
& c2
, int c1Percent
)
208 Q_ASSERT(c1Percent
>= 0 && c1Percent
<= 100);
210 const int c2Percent
= 100 - c1Percent
;
211 return QColor((c1
.red() * c1Percent
+ c2
.red() * c2Percent
) / 100,
212 (c1
.green() * c1Percent
+ c2
.green() * c2Percent
) / 100,
213 (c1
.blue() * c1Percent
+ c2
.blue() * c2Percent
) / 100);
216 QPalette::ColorRole
KItemListGroupHeader::normalTextColorRole() const
218 return QPalette::Text
;
221 QPalette::ColorRole
KItemListGroupHeader::normalBaseColorRole() const
223 return QPalette::Window
;
226 QColor
KItemListGroupHeader::textColor() const
228 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
229 return styleOption().palette
.color(group
, normalTextColorRole());
232 QColor
KItemListGroupHeader::baseColor() const
234 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
235 return styleOption().palette
.color(group
, normalBaseColorRole());