1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2019 David Redondo <kde@david-redondo.de>
4 SPDX-FileCopyrightText: 2007, 2009 Rafael Fernández López <ereslibre@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kcategorydrawer.h"
10
11#include <QApplication>
12#include <QPainter>
13#include <QStyleOption>
14
15#include <kcategorizedsortfilterproxymodel.h>
16#include <kcategorizedview.h>
17
18#include <cmath>
19
20class KCategoryDrawerPrivate
21{
22public:
23 KCategoryDrawerPrivate(KCategorizedView *view)
24 : view(view)
25 {
26 }
27
28 ~KCategoryDrawerPrivate()
29 {
30 }
31
32 KCategorizedView *const view;
33};
34
35KCategoryDrawer::KCategoryDrawer(KCategorizedView *view)
36 : QObject(view)
37 , d(new KCategoryDrawerPrivate(view))
38{
39}
40
41KCategoryDrawer::~KCategoryDrawer() = default;
42
43void KCategoryDrawer::drawCategory(const QModelIndex &index, int /*sortRole*/, const QStyleOption &option, QPainter *painter) const
44{
45 // Keep this in sync with Kirigami.ListSectionHeader
46 painter->setRenderHint(hint: QPainter::Antialiasing);
47
48 const QString category = index.model()->data(index, role: KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
49 QFont font(QApplication::font());
50 font.setBold(true);
51 const QFontMetrics fontMetrics = QFontMetrics(font);
52
53 const int topPadding = 8 + 4; // Kirigami.Units.largeSpacing + smallSpacing
54 const int sidePadding = 8; // Kirigami.Units.largeSpacing
55
56 // BEGIN: text
57 {
58 QRect textRect(option.rect);
59 textRect.setTop(textRect.top() + topPadding);
60 textRect.setLeft(textRect.left() + sidePadding);
61 textRect.setRight(textRect.right() - sidePadding);
62 textRect.setHeight(fontMetrics.height());
63
64 painter->save();
65 painter->setFont(font);
66 QColor penColor(option.palette.text().color());
67 penColor.setAlphaF(0.7);
68 painter->setPen(penColor);
69 painter->drawText(r: textRect, flags: Qt::AlignLeft | Qt::AlignVCenter, text: category);
70 painter->restore();
71 }
72 // END: text
73
74 // BEGIN: horizontal line
75 {
76 QColor backgroundColor = option.palette.text().color();
77 backgroundColor.setAlphaF(0.7 * 0.15); // replicate Kirigami.Separator color
78 QRect backgroundRect(option.rect);
79 backgroundRect.setLeft(fontMetrics.horizontalAdvance(category) + sidePadding * 2);
80 backgroundRect.setRight(backgroundRect.right() - sidePadding);
81 backgroundRect.setTop(backgroundRect.top() + topPadding + ceil(x: fontMetrics.height() / 2));
82 backgroundRect.setHeight(1);
83 painter->save();
84 painter->setBrush(backgroundColor);
85 painter->setPen(Qt::NoPen);
86 painter->drawRect(r: backgroundRect);
87 painter->restore();
88 }
89 // END: horizontal line
90}
91
92int KCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
93{
94 Q_UNUSED(index);
95 Q_UNUSED(option)
96
97 QFont font(QApplication::font());
98 QFontMetrics fontMetrics(font);
99
100 const int height = fontMetrics.height() + 8 + 8; // Kirigami.Units.largeSpacing + smallSpacing * 2
101 return height;
102}
103
104int KCategoryDrawer::leftMargin() const
105{
106 return 0;
107}
108
109int KCategoryDrawer::rightMargin() const
110{
111 return 0;
112}
113
114KCategorizedView *KCategoryDrawer::view() const
115{
116 return d->view;
117}
118
119void KCategoryDrawer::mouseButtonPressed(const QModelIndex &, const QRect &, QMouseEvent *event)
120{
121 event->ignore();
122}
123
124void KCategoryDrawer::mouseButtonReleased(const QModelIndex &, const QRect &, QMouseEvent *event)
125{
126 event->ignore();
127}
128
129void KCategoryDrawer::mouseMoved(const QModelIndex &, const QRect &, QMouseEvent *event)
130{
131 event->ignore();
132}
133
134void KCategoryDrawer::mouseButtonDoubleClicked(const QModelIndex &, const QRect &, QMouseEvent *event)
135{
136 event->ignore();
137}
138
139void KCategoryDrawer::mouseLeft(const QModelIndex &, const QRect &)
140{
141}
142
143#include "moc_kcategorydrawer.cpp"
144

source code of kitemviews/src/kcategorydrawer.cpp