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
, nullptr),
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 if (m_styleOption
== option
) {
85 const KItemListStyleOption previous
= m_styleOption
;
86 m_styleOption
= option
;
88 styleOptionChanged(option
, previous
);
91 const KItemListStyleOption
& KItemListGroupHeader::styleOption() const
96 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation
)
98 if (m_scrollOrientation
!= orientation
) {
99 const Qt::Orientation previous
= m_scrollOrientation
;
100 m_scrollOrientation
= orientation
;
101 if (orientation
== Qt::Vertical
) {
104 scrollOrientationChanged(orientation
, previous
);
108 void KItemListGroupHeader::setItemIndex(int index
)
110 if (m_itemIndex
!= index
) {
111 const int previous
= m_itemIndex
;
114 itemIndexChanged(m_itemIndex
, previous
);
118 int KItemListGroupHeader::itemIndex() const
123 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
125 return m_scrollOrientation
;
128 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
138 paintSeparator(painter
, m_separatorColor
);
139 paintRole(painter
, m_roleBounds
, m_roleColor
);
142 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
148 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
154 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
160 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
166 void KItemListGroupHeader::itemIndexChanged(int current
, int previous
)
172 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
174 QGraphicsWidget::resizeEvent(event
);
175 if (event
->oldSize().height() != event
->newSize().height()) {
180 void KItemListGroupHeader::updateCache()
182 Q_ASSERT(m_dirtyCache
);
184 // Calculate the role- and line-color. No alphablending is used for
185 // performance reasons.
186 const QColor c1
= textColor();
187 const QColor c2
= baseColor();
188 m_separatorColor
= mixedColor(c1
, c2
, 10);
189 m_roleColor
= mixedColor(c1
, c2
, 60);
191 const int padding
= qMax(1, m_styleOption
.padding
);
192 const int horizontalMargin
= qMax(2, m_styleOption
.horizontalMargin
);
194 const QFontMetrics
fontMetrics(m_styleOption
.font
);
195 const qreal roleHeight
= fontMetrics
.height();
197 const int y
= (m_scrollOrientation
== Qt::Vertical
) ? padding
: horizontalMargin
;
199 m_roleBounds
= QRectF(horizontalMargin
+ padding
,
201 size().width() - 2 * padding
- horizontalMargin
,
204 m_dirtyCache
= false;
207 QColor
KItemListGroupHeader::mixedColor(const QColor
& c1
, const QColor
& c2
, int c1Percent
)
209 Q_ASSERT(c1Percent
>= 0 && c1Percent
<= 100);
211 const int c2Percent
= 100 - c1Percent
;
212 return QColor((c1
.red() * c1Percent
+ c2
.red() * c2Percent
) / 100,
213 (c1
.green() * c1Percent
+ c2
.green() * c2Percent
) / 100,
214 (c1
.blue() * c1Percent
+ c2
.blue() * c2Percent
) / 100);
217 QPalette::ColorRole
KItemListGroupHeader::normalTextColorRole() const
219 return QPalette::Text
;
222 QPalette::ColorRole
KItemListGroupHeader::normalBaseColorRole() const
224 return QPalette::Window
;
227 QColor
KItemListGroupHeader::textColor() const
229 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
230 return styleOption().palette
.color(group
, normalTextColorRole());
233 QColor
KItemListGroupHeader::baseColor() const
235 const QPalette::ColorGroup group
= isActiveWindow() ? QPalette::Active
: QPalette::Inactive
;
236 return styleOption().palette
.color(group
, normalBaseColorRole());