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 <KRatingPainter>
27 KStandardItemListGroupHeader::KStandardItemListGroupHeader(QGraphicsWidget
* parent
) :
28 KItemListGroupHeader(parent
),
33 m_text
.setTextFormat(Qt::PlainText
);
34 m_text
.setPerformanceHint(QStaticText::AggressiveCaching
);
37 KStandardItemListGroupHeader::~KStandardItemListGroupHeader()
41 void KStandardItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
46 KItemListGroupHeader::paint(painter
, option
, widget
);
49 void KStandardItemListGroupHeader::paintRole(QPainter
* painter
, const QRectF
& roleBounds
, const QColor
& color
)
51 if (m_pixmap
.isNull()) {
52 painter
->setPen(color
);
53 painter
->drawStaticText(roleBounds
.topLeft(), m_text
);
55 painter
->drawPixmap(roleBounds
.topLeft(), m_pixmap
);
59 void KStandardItemListGroupHeader::paintSeparator(QPainter
* painter
, const QColor
& color
)
61 if (itemIndex() == 0) {
62 // No top- or left-line should be drawn for the first group-header
66 painter
->setPen(color
);
68 if (scrollOrientation() == Qt::Horizontal
) {
69 painter
->drawLine(0, 0, 0, size().height() - 1);
71 painter
->drawLine(0, 0, size().width() - 1, 0);
75 void KStandardItemListGroupHeader::roleChanged(const QByteArray
¤t
, const QByteArray
&previous
)
82 void KStandardItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
89 void KStandardItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
91 QGraphicsWidget::resizeEvent(event
);
95 void KStandardItemListGroupHeader::updateCache()
97 Q_ASSERT(m_dirtyCache
);
100 const qreal maxWidth
= size().width() - 4 * styleOption().padding
;
102 if (role() == "rating") {
103 m_text
.setText(QString());
105 const qreal height
= styleOption().fontMetrics
.ascent();
106 const QSizeF
pixmapSize(qMin(height
* 5, maxWidth
), height
);
108 m_pixmap
= QPixmap(pixmapSize
.toSize());
109 m_pixmap
.fill(Qt::transparent
);
111 QPainter
painter(&m_pixmap
);
112 const QRect
rect(0, 0, m_pixmap
.width(), m_pixmap
.height());
113 const int rating
= data().toInt();
114 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
116 m_pixmap
= QPixmap();
118 QFontMetricsF
fontMetrics(font());
119 const QString text
= fontMetrics
.elidedText(data().toString(), Qt::ElideRight
, maxWidth
);
120 m_text
.setText(text
);