File indexing completed on 2024-04-28 05:45:07

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 "kitemlistview.h"
0012 
0013 #include <QGraphicsSceneResizeEvent>
0014 #include <QPainter>
0015 #include <QStyleOptionGraphicsItem>
0016 
0017 KItemListGroupHeader::KItemListGroupHeader(QGraphicsWidget *parent)
0018     : QGraphicsWidget(parent)
0019     , m_dirtyCache(true)
0020     , m_role()
0021     , m_data()
0022     , m_styleOption()
0023     , m_scrollOrientation(Qt::Vertical)
0024     , m_itemIndex(-1)
0025     , m_separatorColor()
0026     , m_roleColor()
0027     , m_roleBounds()
0028 {
0029 }
0030 
0031 KItemListGroupHeader::~KItemListGroupHeader()
0032 {
0033 }
0034 
0035 void KItemListGroupHeader::setRole(const QByteArray &role)
0036 {
0037     if (m_role != role) {
0038         const QByteArray previous = m_role;
0039         m_role = role;
0040         update();
0041         roleChanged(role, previous);
0042     }
0043 }
0044 
0045 QByteArray KItemListGroupHeader::role() const
0046 {
0047     return m_role;
0048 }
0049 
0050 void KItemListGroupHeader::setData(const QVariant &data)
0051 {
0052     if (m_data != data) {
0053         const QVariant previous = m_data;
0054         m_data = data;
0055         update();
0056         dataChanged(data, previous);
0057     }
0058 }
0059 
0060 QVariant KItemListGroupHeader::data() const
0061 {
0062     return m_data;
0063 }
0064 
0065 void KItemListGroupHeader::setStyleOption(const KItemListStyleOption &option)
0066 {
0067     if (m_styleOption == option) {
0068         return;
0069     }
0070 
0071     const KItemListStyleOption previous = m_styleOption;
0072     m_styleOption = option;
0073     m_dirtyCache = true;
0074     styleOptionChanged(option, previous);
0075 }
0076 
0077 const KItemListStyleOption &KItemListGroupHeader::styleOption() const
0078 {
0079     return m_styleOption;
0080 }
0081 
0082 void KItemListGroupHeader::setScrollOrientation(Qt::Orientation orientation)
0083 {
0084     if (m_scrollOrientation != orientation) {
0085         const Qt::Orientation previous = m_scrollOrientation;
0086         m_scrollOrientation = orientation;
0087         if (orientation == Qt::Vertical) {
0088             m_dirtyCache = true;
0089         }
0090         scrollOrientationChanged(orientation, previous);
0091     }
0092 }
0093 
0094 void KItemListGroupHeader::setItemIndex(int index)
0095 {
0096     if (m_itemIndex != index) {
0097         const int previous = m_itemIndex;
0098         m_itemIndex = index;
0099         m_dirtyCache = true;
0100         itemIndexChanged(m_itemIndex, previous);
0101     }
0102 }
0103 
0104 int KItemListGroupHeader::itemIndex() const
0105 {
0106     return m_itemIndex;
0107 }
0108 
0109 Qt::Orientation KItemListGroupHeader::scrollOrientation() const
0110 {
0111     return m_scrollOrientation;
0112 }
0113 
0114 void KItemListGroupHeader::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
0115 {
0116     Q_UNUSED(painter)
0117     Q_UNUSED(option)
0118     Q_UNUSED(widget)
0119 
0120     if (m_dirtyCache) {
0121         updateCache();
0122     }
0123 
0124     paintSeparator(painter, m_separatorColor);
0125     paintRole(painter, m_roleBounds, m_roleColor);
0126 }
0127 
0128 void KItemListGroupHeader::roleChanged(const QByteArray &current, const QByteArray &previous)
0129 {
0130     Q_UNUSED(current)
0131     Q_UNUSED(previous)
0132 }
0133 
0134 void KItemListGroupHeader::dataChanged(const QVariant &current, const QVariant &previous)
0135 {
0136     Q_UNUSED(current)
0137     Q_UNUSED(previous)
0138 }
0139 
0140 void KItemListGroupHeader::styleOptionChanged(const KItemListStyleOption &current, const KItemListStyleOption &previous)
0141 {
0142     Q_UNUSED(current)
0143     Q_UNUSED(previous)
0144 }
0145 
0146 void KItemListGroupHeader::scrollOrientationChanged(Qt::Orientation current, Qt::Orientation previous)
0147 {
0148     Q_UNUSED(current)
0149     Q_UNUSED(previous)
0150 }
0151 
0152 void KItemListGroupHeader::itemIndexChanged(int current, int previous)
0153 {
0154     Q_UNUSED(current)
0155     Q_UNUSED(previous)
0156 }
0157 
0158 void KItemListGroupHeader::resizeEvent(QGraphicsSceneResizeEvent *event)
0159 {
0160     QGraphicsWidget::resizeEvent(event);
0161     if (event->oldSize().height() != event->newSize().height()) {
0162         m_dirtyCache = true;
0163     }
0164     updateSize();
0165 }
0166 
0167 void KItemListGroupHeader::updateCache()
0168 {
0169     Q_ASSERT(m_dirtyCache);
0170 
0171     // Calculate the role- and line-color. No alphablending is used for
0172     // performance reasons.
0173     const QColor c1 = textColor();
0174     const QColor c2 = baseColor();
0175     m_separatorColor = mixedColor(c1, c2, 10);
0176     m_roleColor = mixedColor(c1, c2, 60);
0177 
0178     updateSize();
0179 
0180     m_dirtyCache = false;
0181 }
0182 
0183 void KItemListGroupHeader::updateSize()
0184 {
0185     const int padding = qMax(1, m_styleOption.padding);
0186     const int horizontalMargin = qMax(2, m_styleOption.horizontalMargin);
0187 
0188     const QFontMetrics fontMetrics(m_styleOption.font);
0189     const qreal roleHeight = fontMetrics.height();
0190 
0191     const int y = (m_scrollOrientation == Qt::Vertical) ? padding : horizontalMargin;
0192 
0193     m_roleBounds = QRectF(horizontalMargin + padding, y, size().width() - 2 * padding - horizontalMargin, roleHeight);
0194 
0195     update();
0196 }
0197 
0198 QColor KItemListGroupHeader::mixedColor(const QColor &c1, const QColor &c2, int c1Percent)
0199 {
0200     Q_ASSERT(c1Percent >= 0 && c1Percent <= 100);
0201 
0202     const int c2Percent = 100 - c1Percent;
0203     return QColor((c1.red() * c1Percent + c2.red() * c2Percent) / 100,
0204                   (c1.green() * c1Percent + c2.green() * c2Percent) / 100,
0205                   (c1.blue() * c1Percent + c2.blue() * c2Percent) / 100);
0206 }
0207 
0208 QPalette::ColorRole KItemListGroupHeader::normalTextColorRole() const
0209 {
0210     return QPalette::Text;
0211 }
0212 
0213 QPalette::ColorRole KItemListGroupHeader::normalBaseColorRole() const
0214 {
0215     return QPalette::Window;
0216 }
0217 
0218 QColor KItemListGroupHeader::textColor() const
0219 {
0220     const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
0221     return styleOption().palette.color(group, normalTextColorRole());
0222 }
0223 
0224 QColor KItemListGroupHeader::baseColor() const
0225 {
0226     const QPalette::ColorGroup group = isActiveWindow() ? QPalette::Active : QPalette::Inactive;
0227     return styleOption().palette.color(group, normalBaseColorRole());
0228 }
0229 
0230 #include "moc_kitemlistgroupheader.cpp"