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
),
40 m_rightBorderCache(0),
45 KItemListGroupHeader::~KItemListGroupHeader()
47 delete m_leftBorderCache
;
48 delete m_rightBorderCache
;
51 void KItemListGroupHeader::setRole(const QByteArray
& role
)
54 const QByteArray previous
= m_role
;
57 roleChanged(role
, previous
);
61 QByteArray
KItemListGroupHeader::role() const
66 void KItemListGroupHeader::setData(const QVariant
& data
)
69 const QVariant previous
= m_data
;
72 dataChanged(data
, previous
);
76 QVariant
KItemListGroupHeader::data() const
81 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption
& option
)
83 const KItemListStyleOption previous
= m_styleOption
;
84 m_styleOption
= option
;
86 styleOptionChanged(option
, previous
);
89 const KItemListStyleOption
& KItemListGroupHeader::styleOption() const
94 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation
)
96 if (m_scrollOrientation
!= orientation
) {
97 const Qt::Orientation previous
= m_scrollOrientation
;
98 m_scrollOrientation
= orientation
;
100 scrollOrientationChanged(orientation
, previous
);
104 Qt::Orientation
KItemListGroupHeader::scrollOrientation() const
106 return m_scrollOrientation
;
109 void KItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
118 const int leftBorderX
= m_leftBorderCache
->width() + 1;
119 const int rightBorderX
= size().width() - m_rightBorderCache
->width() - 2;
121 painter
->setPen(m_outlineColor
);
122 painter
->drawLine(leftBorderX
, 1, rightBorderX
, 1);
124 painter
->drawPixmap(1, 1, *m_leftBorderCache
);
125 painter
->drawPixmap(rightBorderX
, 1, *m_rightBorderCache
);
128 void KItemListGroupHeader::roleChanged(const QByteArray
& current
, const QByteArray
& previous
)
134 void KItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
140 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption
& current
, const KItemListStyleOption
& previous
)
146 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current
, Qt::Orientation previous
)
152 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
154 QGraphicsWidget::resizeEvent(event
);
155 if (event
->oldSize().height() != event
->newSize().height()) {
160 void KItemListGroupHeader::updateCache()
162 Q_ASSERT(m_dirtyCache
);
164 delete m_leftBorderCache
;
165 delete m_rightBorderCache
;
167 const int length
= qMax(int(size().height() - 1), 1);
168 m_leftBorderCache
= new QPixmap(length
, length
);
169 m_leftBorderCache
->fill(Qt::transparent
);
171 m_rightBorderCache
= new QPixmap(length
, length
);
172 m_rightBorderCache
->fill(Qt::transparent
);
174 // Calculate the outline color. No alphablending is used for
175 // performance reasons.
176 const QColor c1
= m_styleOption
.palette
.text().color();
177 const QColor c2
= m_styleOption
.palette
.background().color();
179 const int p2
= 100 - p1
;
180 m_outlineColor
= QColor((c1
.red() * p1
+ c2
.red() * p2
) / 100,
181 (c1
.green() * p1
+ c2
.green() * p2
) / 100,
182 (c1
.blue() * p1
+ c2
.blue() * p2
) / 100);
184 // The drawing code is based on the code of DolphinCategoryDrawer from Dolphin 1.7
185 // Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
187 // Cache the left border as pixmap
188 QPainter
painter(m_leftBorderCache
);
189 painter
.setPen(m_outlineColor
);
191 // 1. Draw top horizontal line
192 painter
.drawLine(3, 0, length
, 0);
194 // 2. Draw vertical line with gradient
195 const QPoint
start(0, 3);
196 QLinearGradient
gradient(start
, QPoint(0, length
));
197 gradient
.setColorAt(0, m_outlineColor
);
198 gradient
.setColorAt(1, Qt::transparent
);
199 painter
.fillRect(QRect(start
, QSize(1, length
- start
.y())), gradient
);
202 painter
.setRenderHint(QPainter::Antialiasing
);
203 QRectF
arc(QPointF(0, 0), QSizeF(4, 4));
204 arc
.translate(0.5, 0.5);
205 painter
.drawArc(arc
, 1440, 1440);
209 // Cache the right border as pixmap
210 QPainter
painter(m_rightBorderCache
);
211 painter
.setPen(m_outlineColor
);
213 const int right
= length
- 1;
214 if (m_scrollOrientation
== Qt::Vertical
) {
215 // 1. Draw top horizontal line
216 painter
.drawLine(0, 0, length
- 3, 0);
218 // 2. Draw vertical line with gradient
219 const QPoint
start(right
, 3);
220 QLinearGradient
gradient(start
, QPoint(right
, length
));
221 gradient
.setColorAt(0, m_outlineColor
);
222 gradient
.setColorAt(1, Qt::transparent
);
223 painter
.fillRect(QRect(start
, QSize(1, length
- start
.y())), gradient
);
226 painter
.setRenderHint(QPainter::Antialiasing
);
227 QRectF
arc(QPointF(length
- 5, 0), QSizeF(4, 4));
228 arc
.translate(0.5, 0.5);
229 painter
.drawArc(arc
, 0, 1440);
231 // Draw a horizontal gradiented line
232 QLinearGradient
gradient(QPoint(0, 0), QPoint(length
, 0));
233 gradient
.setColorAt(0, m_outlineColor
);
234 gradient
.setColorAt(1, Qt::transparent
);
235 painter
.fillRect(QRect(QPoint(0, 0), QSize(length
, 1)), gradient
);
239 m_dirtyCache
= false;
242 #include "kitemlistgroupheader.moc"