File indexing completed on 2024-04-28 05:45:10
0001 /* 0002 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com> 0003 * 0004 * Based on the Itemviews NG project from Trolltech Labs 0005 * 0006 * SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "kstandarditemlistgroupheader.h" 0010 0011 #include <KRatingPainter> 0012 #include <QPainter> 0013 0014 KStandardItemListGroupHeader::KStandardItemListGroupHeader(QGraphicsWidget *parent) 0015 : KItemListGroupHeader(parent) 0016 , m_dirtyCache(true) 0017 , m_text() 0018 , m_pixmap() 0019 { 0020 } 0021 0022 KStandardItemListGroupHeader::~KStandardItemListGroupHeader() 0023 { 0024 } 0025 0026 void KStandardItemListGroupHeader::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 0027 { 0028 if (m_dirtyCache) { 0029 updateCache(); 0030 } 0031 KItemListGroupHeader::paint(painter, option, widget); 0032 } 0033 0034 void KStandardItemListGroupHeader::paintRole(QPainter *painter, const QRectF &roleBounds, const QColor &color) 0035 { 0036 if (m_pixmap.isNull()) { 0037 painter->setPen(color); 0038 painter->drawText(roleBounds, 0, m_text); 0039 } else { 0040 painter->drawPixmap(roleBounds.topLeft(), m_pixmap); 0041 } 0042 } 0043 0044 void KStandardItemListGroupHeader::paintSeparator(QPainter *painter, const QColor &color) 0045 { 0046 if (itemIndex() == 0) { 0047 // No top- or left-line should be drawn for the first group-header 0048 return; 0049 } 0050 0051 painter->setPen(color); 0052 0053 if (scrollOrientation() == Qt::Horizontal) { 0054 painter->drawLine(0, 0, 0, size().height() - 1); 0055 } else { 0056 if (layoutDirection() == Qt::LeftToRight) { 0057 painter->drawLine(0, 0, size().width() - 1, 0); 0058 } else { 0059 painter->drawLine(1, 0, size().width(), 0); 0060 } 0061 } 0062 } 0063 0064 void KStandardItemListGroupHeader::roleChanged(const QByteArray ¤t, const QByteArray &previous) 0065 { 0066 Q_UNUSED(current) 0067 Q_UNUSED(previous) 0068 m_dirtyCache = true; 0069 } 0070 0071 void KStandardItemListGroupHeader::dataChanged(const QVariant ¤t, const QVariant &previous) 0072 { 0073 Q_UNUSED(current) 0074 Q_UNUSED(previous) 0075 m_dirtyCache = true; 0076 } 0077 0078 void KStandardItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent *event) 0079 { 0080 KItemListGroupHeader::resizeEvent(event); 0081 m_dirtyCache = true; 0082 } 0083 0084 void KStandardItemListGroupHeader::updateCache() 0085 { 0086 Q_ASSERT(m_dirtyCache); 0087 m_dirtyCache = false; 0088 0089 const qreal maxWidth = size().width() - 4 * styleOption().padding; 0090 0091 if (role() == "rating") { 0092 m_text = QString(); 0093 0094 const qreal height = styleOption().fontMetrics.ascent(); 0095 const QSizeF pixmapSize(qMin(height * 5, maxWidth), height); 0096 0097 m_pixmap = QPixmap(pixmapSize.toSize()); 0098 m_pixmap.fill(Qt::transparent); 0099 0100 QPainter painter(&m_pixmap); 0101 const QRect rect(0, 0, m_pixmap.width(), m_pixmap.height()); 0102 const int rating = data().toInt(); 0103 KRatingPainter::paintRating(&painter, rect, Qt::AlignJustify | Qt::AlignVCenter, rating); 0104 } else { 0105 m_pixmap = QPixmap(); 0106 0107 QFontMetricsF fontMetrics(font()); 0108 const QString text = fontMetrics.elidedText(data().toString(), Qt::ElideRight, maxWidth); 0109 m_text = text; 0110 } 0111 } 0112 0113 #include "moc_kstandarditemlistgroupheader.cpp"