File indexing completed on 2024-10-13 12:16:01

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2019 David Redondo <kde@david-redondo.de>
0004     SPDX-FileCopyrightText: 2007, 2009 Rafael Fernández López <ereslibre@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kcategorydrawer.h"
0010 
0011 #include <QApplication>
0012 #include <QPainter>
0013 #include <QStyleOption>
0014 
0015 #include <kcategorizedsortfilterproxymodel.h>
0016 #include <kcategorizedview.h>
0017 
0018 #include <cmath>
0019 
0020 class KCategoryDrawerPrivate
0021 {
0022 public:
0023     KCategoryDrawerPrivate(KCategorizedView *view)
0024         : view(view)
0025     {
0026     }
0027 
0028     ~KCategoryDrawerPrivate()
0029     {
0030     }
0031 
0032     KCategorizedView *const view;
0033 };
0034 
0035 KCategoryDrawer::KCategoryDrawer(KCategorizedView *view)
0036     : QObject(view)
0037     , d(new KCategoryDrawerPrivate(view))
0038 {
0039 }
0040 
0041 KCategoryDrawer::~KCategoryDrawer() = default;
0042 
0043 void KCategoryDrawer::drawCategory(const QModelIndex &index, int /*sortRole*/, const QStyleOption &option, QPainter *painter) const
0044 {
0045     // Keep this in sync with Kirigami.ListSectionHeader
0046     painter->setRenderHint(QPainter::Antialiasing);
0047 
0048     const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
0049     QFont font(QApplication::font());
0050     font.setBold(true);
0051     const QFontMetrics fontMetrics = QFontMetrics(font);
0052 
0053     const int topPadding = 8 + 4; // Kirigami.Units.largeSpacing + smallSpacing
0054     const int sidePadding = 8; // Kirigami.Units.largeSpacing
0055 
0056     // BEGIN: text
0057     {
0058         QRect textRect(option.rect);
0059         textRect.setTop(textRect.top() + topPadding);
0060         textRect.setLeft(textRect.left() + sidePadding);
0061         textRect.setRight(textRect.right() - sidePadding);
0062         textRect.setHeight(fontMetrics.height());
0063 
0064         painter->save();
0065         painter->setFont(font);
0066         QColor penColor(option.palette.text().color());
0067         penColor.setAlphaF(0.7);
0068         painter->setPen(penColor);
0069         painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, category);
0070         painter->restore();
0071     }
0072     // END: text
0073 
0074     // BEGIN: horizontal line
0075     {
0076         QColor backgroundColor = option.palette.text().color();
0077         backgroundColor.setAlphaF(0.7 * 0.15); // replicate Kirigami.Separator color
0078         QRect backgroundRect(option.rect);
0079         backgroundRect.setLeft(fontMetrics.horizontalAdvance(category) + sidePadding * 2);
0080         backgroundRect.setRight(backgroundRect.right() - sidePadding);
0081         backgroundRect.setTop(backgroundRect.top() + topPadding + ceil(fontMetrics.height() / 2));
0082         backgroundRect.setHeight(1);
0083         painter->save();
0084         painter->setBrush(backgroundColor);
0085         painter->setPen(Qt::NoPen);
0086         painter->drawRect(backgroundRect);
0087         painter->restore();
0088     }
0089     // END: horizontal line
0090 }
0091 
0092 int KCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
0093 {
0094     Q_UNUSED(index);
0095     Q_UNUSED(option)
0096 
0097     QFont font(QApplication::font());
0098     QFontMetrics fontMetrics(font);
0099 
0100     const int height = fontMetrics.height() + 8 + 8; // Kirigami.Units.largeSpacing + smallSpacing * 2
0101     return height;
0102 }
0103 
0104 int KCategoryDrawer::leftMargin() const
0105 {
0106     return 0;
0107 }
0108 
0109 int KCategoryDrawer::rightMargin() const
0110 {
0111     return 0;
0112 }
0113 
0114 KCategorizedView *KCategoryDrawer::view() const
0115 {
0116     return d->view;
0117 }
0118 
0119 void KCategoryDrawer::mouseButtonPressed(const QModelIndex &, const QRect &, QMouseEvent *event)
0120 {
0121     event->ignore();
0122 }
0123 
0124 void KCategoryDrawer::mouseButtonReleased(const QModelIndex &, const QRect &, QMouseEvent *event)
0125 {
0126     event->ignore();
0127 }
0128 
0129 void KCategoryDrawer::mouseMoved(const QModelIndex &, const QRect &, QMouseEvent *event)
0130 {
0131     event->ignore();
0132 }
0133 
0134 void KCategoryDrawer::mouseButtonDoubleClicked(const QModelIndex &, const QRect &, QMouseEvent *event)
0135 {
0136     event->ignore();
0137 }
0138 
0139 void KCategoryDrawer::mouseLeft(const QModelIndex &, const QRect &)
0140 {
0141 }
0142 
0143 #include "moc_kcategorydrawer.cpp"