]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistgroupheader.cpp
Removed unused KGlobal includes and use KIO/Job instead of KIO/JobClasses
[dolphin.git] / src / kitemviews / kstandarditemlistgroupheader.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
22
23 #include "kstandarditemlistgroupheader.h"
24
25 #include <KRatingPainter>
26 #include <QPainter>
27
28 KStandardItemListGroupHeader::KStandardItemListGroupHeader(QGraphicsWidget* parent) :
29 KItemListGroupHeader(parent),
30 m_dirtyCache(true),
31 m_text(),
32 m_pixmap()
33 {
34 m_text.setTextFormat(Qt::PlainText);
35 m_text.setPerformanceHint(QStaticText::AggressiveCaching);
36 }
37
38 KStandardItemListGroupHeader::~KStandardItemListGroupHeader()
39 {
40 }
41
42 void KStandardItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
43 {
44 if (m_dirtyCache) {
45 updateCache();
46 }
47 KItemListGroupHeader::paint(painter, option, widget);
48 }
49
50 void KStandardItemListGroupHeader::paintRole(QPainter* painter, const QRectF& roleBounds, const QColor& color)
51 {
52 if (m_pixmap.isNull()) {
53 painter->setPen(color);
54 painter->drawStaticText(roleBounds.topLeft(), m_text);
55 } else {
56 painter->drawPixmap(roleBounds.topLeft(), m_pixmap);
57 }
58 }
59
60 void KStandardItemListGroupHeader::paintSeparator(QPainter* painter, const QColor& color)
61 {
62 if (itemIndex() == 0) {
63 // No top- or left-line should be drawn for the first group-header
64 return;
65 }
66
67 painter->setPen(color);
68
69 if (scrollOrientation() == Qt::Horizontal) {
70 painter->drawLine(0, 0, 0, size().height() - 1);
71 } else {
72 painter->drawLine(0, 0, size().width() - 1, 0);
73 }
74 }
75
76 void KStandardItemListGroupHeader::roleChanged(const QByteArray &current, const QByteArray &previous)
77 {
78 Q_UNUSED(current);
79 Q_UNUSED(previous);
80 m_dirtyCache = true;
81 }
82
83 void KStandardItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous)
84 {
85 Q_UNUSED(current);
86 Q_UNUSED(previous);
87 m_dirtyCache = true;
88 }
89
90 void KStandardItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event)
91 {
92 QGraphicsWidget::resizeEvent(event);
93 m_dirtyCache = true;
94 }
95
96 void KStandardItemListGroupHeader::updateCache()
97 {
98 Q_ASSERT(m_dirtyCache);
99 m_dirtyCache = false;
100
101 const qreal maxWidth = size().width() - 4 * styleOption().padding;
102
103 if (role() == "rating") {
104 m_text = QString(); // krazy:exlude=nullstrassign
105
106 const qreal height = styleOption().fontMetrics.ascent();
107 const QSizeF pixmapSize(qMin(height * 5, maxWidth), height);
108
109 m_pixmap = QPixmap(pixmapSize.toSize());
110 m_pixmap.fill(Qt::transparent);
111
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);
116 } else {
117 m_pixmap = QPixmap();
118
119 QFontMetricsF fontMetrics(font());
120 const QString text = fontMetrics.elidedText(data().toString(), Qt::ElideRight, maxWidth);
121 m_text.setText(text);
122 }
123 }
124