]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemlistgroupheader.cpp
0c940ed285c8cc7b6e9dfb73ffd11fea5d545dde
[dolphin.git] / src / kitemviews / kfileitemlistgroupheader.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 "kfileitemlistgroupheader.h"
24
25 #include <kratingpainter.h>
26 #include <QPainter>
27
28 KFileItemListGroupHeader::KFileItemListGroupHeader(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 KFileItemListGroupHeader::~KFileItemListGroupHeader()
39 {
40 }
41
42 void KFileItemListGroupHeader::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
43 {
44 KItemListGroupHeader::paint(painter, option, widget);
45
46 if (m_dirtyCache) {
47 updateCache();
48 }
49
50 if (m_pixmap.isNull()) {
51 painter->setPen(roleColor());
52 painter->drawStaticText(roleBounds().topLeft(), m_text);
53 } else {
54 painter->drawPixmap(roleBounds().topLeft(), m_pixmap);
55 }
56 }
57
58 void KFileItemListGroupHeader::roleChanged(const QByteArray &current, const QByteArray &previous)
59 {
60 Q_UNUSED(current);
61 Q_UNUSED(previous);
62 m_dirtyCache = true;
63 }
64
65 void KFileItemListGroupHeader::dataChanged(const QVariant& current, const QVariant& previous)
66 {
67 Q_UNUSED(current);
68 Q_UNUSED(previous);
69 m_dirtyCache = true;
70 }
71
72 void KFileItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent* event)
73 {
74 QGraphicsWidget::resizeEvent(event);
75 m_dirtyCache = true;
76 }
77
78 void KFileItemListGroupHeader::updateCache()
79 {
80 Q_ASSERT(m_dirtyCache);
81 m_dirtyCache = false;
82
83 const qreal maxWidth = size().width() - 4 * styleOption().padding;
84
85 if (role() == "rating") {
86 m_text = QString();
87
88 const qreal height = styleOption().fontMetrics.ascent();
89 const QSizeF pixmapSize(qMin(height * 5, maxWidth), height);
90
91 m_pixmap = QPixmap(pixmapSize.toSize());
92 m_pixmap.fill(Qt::transparent);
93
94 QPainter painter(&m_pixmap);
95 const QRect rect(0, 0, m_pixmap.width(), m_pixmap.height());
96 const int rating = data().toInt();
97 KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating);
98 } else {
99 m_pixmap = QPixmap();
100
101 QFontMetricsF fontMetrics(font());
102 const QString text = fontMetrics.elidedText(data().toString(), Qt::ElideRight, maxWidth);
103 m_text.setText(text);
104 }
105 }
106
107 #include "kfileitemlistgroupheader.moc"