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