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 <KRatingPainter>
28 KStandardItemListGroupHeader::KStandardItemListGroupHeader(QGraphicsWidget
* parent
) :
29 KItemListGroupHeader(parent
),
34 m_text
.setTextFormat(Qt::PlainText
);
35 m_text
.setPerformanceHint(QStaticText::AggressiveCaching
);
38 KStandardItemListGroupHeader::~KStandardItemListGroupHeader()
42 void KStandardItemListGroupHeader::paint(QPainter
* painter
, const QStyleOptionGraphicsItem
* option
, QWidget
* widget
)
47 KItemListGroupHeader::paint(painter
, option
, widget
);
50 void KStandardItemListGroupHeader::paintRole(QPainter
* painter
, const QRectF
& roleBounds
, const QColor
& color
)
52 if (m_pixmap
.isNull()) {
53 painter
->setPen(color
);
54 painter
->drawStaticText(roleBounds
.topLeft(), m_text
);
56 painter
->drawPixmap(roleBounds
.topLeft(), m_pixmap
);
60 void KStandardItemListGroupHeader::paintSeparator(QPainter
* painter
, const QColor
& color
)
62 if (itemIndex() == 0) {
63 // No top- or left-line should be drawn for the first group-header
67 painter
->setPen(color
);
69 if (scrollOrientation() == Qt::Horizontal
) {
70 painter
->drawLine(0, 0, 0, size().height() - 1);
72 painter
->drawLine(0, 0, size().width() - 1, 0);
76 void KStandardItemListGroupHeader::roleChanged(const QByteArray
¤t
, const QByteArray
&previous
)
83 void KStandardItemListGroupHeader::dataChanged(const QVariant
& current
, const QVariant
& previous
)
90 void KStandardItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent
* event
)
92 QGraphicsWidget::resizeEvent(event
);
96 void KStandardItemListGroupHeader::updateCache()
98 Q_ASSERT(m_dirtyCache
);
101 const qreal maxWidth
= size().width() - 4 * styleOption().padding
;
103 if (role() == "rating") {
104 m_text
= QString(); // krazy:exlude=nullstrassign
106 const qreal height
= styleOption().fontMetrics
.ascent();
107 const QSizeF
pixmapSize(qMin(height
* 5, maxWidth
), height
);
109 m_pixmap
= QPixmap(pixmapSize
.toSize());
110 m_pixmap
.fill(Qt::transparent
);
112 QPainter
painter(&m_pixmap
);
113 const QRect
rect(0, 0, m_pixmap
.width(), m_pixmap
.height());
114 const int rating
= data().toInt();
115 KRatingPainter::paintRating(&painter
, rect
, Qt::AlignJustify
| Qt::AlignVCenter
, rating
);
117 m_pixmap
= QPixmap();
119 QFontMetricsF
fontMetrics(font());
120 const QString text
= fontMetrics
.elidedText(data().toString(), Qt::ElideRight
, maxWidth
);
121 m_text
.setText(text
);