]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kstandarditemlistgroupheader.cpp
GIT_SILENT Sync po/docbooks with svn
[dolphin.git] / src / kitemviews / kstandarditemlistgroupheader.cpp
1 /*
2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
3 *
4 * Based on the Itemviews NG project from Trolltech Labs
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "kstandarditemlistgroupheader.h"
10
11 #include <KRatingPainter>
12 #include <QPainter>
13
14 KStandardItemListGroupHeader::KStandardItemListGroupHeader(QGraphicsWidget *parent)
15 : KItemListGroupHeader(parent)
16 , m_dirtyCache(true)
17 , m_text()
18 , m_pixmap()
19 {
20 }
21
22 KStandardItemListGroupHeader::~KStandardItemListGroupHeader()
23 {
24 }
25
26 void KStandardItemListGroupHeader::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
27 {
28 if (m_dirtyCache) {
29 updateCache();
30 }
31 KItemListGroupHeader::paint(painter, option, widget);
32 }
33
34 void KStandardItemListGroupHeader::paintRole(QPainter *painter, const QRectF &roleBounds, const QColor &color)
35 {
36 if (m_pixmap.isNull()) {
37 painter->setPen(color);
38 painter->drawText(roleBounds, 0, m_text);
39 } else {
40 painter->drawPixmap(roleBounds.topLeft(), m_pixmap);
41 }
42 }
43
44 void KStandardItemListGroupHeader::paintSeparator(QPainter *painter, const QColor &color)
45 {
46 if (itemIndex() == 0) {
47 // No top- or left-line should be drawn for the first group-header
48 return;
49 }
50
51 painter->setPen(color);
52
53 if (scrollOrientation() == Qt::Horizontal) {
54 const qreal x = layoutDirection() == Qt::RightToLeft ? size().width() - 1 : 0;
55 painter->drawLine(x, 0, x, size().height() - 1);
56 } else {
57 if (layoutDirection() == Qt::LeftToRight) {
58 painter->drawLine(0, 0, size().width() - 1, 0);
59 } else {
60 painter->drawLine(1, 0, size().width(), 0);
61 }
62 }
63 }
64
65 void KStandardItemListGroupHeader::roleChanged(const QByteArray &current, const QByteArray &previous)
66 {
67 Q_UNUSED(current)
68 Q_UNUSED(previous)
69 m_dirtyCache = true;
70 }
71
72 void KStandardItemListGroupHeader::dataChanged(const QVariant &current, const QVariant &previous)
73 {
74 Q_UNUSED(current)
75 Q_UNUSED(previous)
76 m_dirtyCache = true;
77 }
78
79 void KStandardItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent *event)
80 {
81 KItemListGroupHeader::resizeEvent(event);
82 m_dirtyCache = true;
83 }
84
85 void KStandardItemListGroupHeader::updateCache()
86 {
87 Q_ASSERT(m_dirtyCache);
88 m_dirtyCache = false;
89
90 const qreal maxWidth = size().width() - 4 * styleOption().padding;
91
92 if (role() == "rating") {
93 m_text = QString();
94
95 const qreal height = styleOption().fontMetrics.ascent();
96 const QSizeF pixmapSize(qMin(height * 5, maxWidth), height);
97
98 m_pixmap = QPixmap(pixmapSize.toSize());
99 m_pixmap.fill(Qt::transparent);
100
101 QPainter painter(&m_pixmap);
102 const QRect rect(0, 0, m_pixmap.width(), m_pixmap.height());
103 const int rating = data().toInt();
104 KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating);
105 } else {
106 m_pixmap = QPixmap();
107
108 QFontMetricsF fontMetrics(font());
109 const QString text = fontMetrics.elidedText(data().toString(), Qt::ElideRight, maxWidth);
110 m_text = text;
111 }
112 }
113
114 #include "moc_kstandarditemlistgroupheader.cpp"