KItemViews

kcategorydrawer.cpp
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
47
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(textRect, Qt::AlignLeft | Qt::AlignVCenter, 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(fontMetrics.height() / 2));
82 backgroundRect.setHeight(1);
83 painter->save();
84 painter->setBrush(backgroundColor);
85 painter->setPen(Qt::NoPen);
86 painter->drawRect(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
98 QFontMetrics fontMetrics(font);
99
100 const int height = fontMetrics.height() + 8 + 8; // Kirigami.Units.largeSpacing + smallSpacing * 2
101 return height;
102}
103
105{
106 return 0;
107}
108
110{
111 return 0;
112}
113
115{
116 return d->view;
117}
118
120{
121 event->ignore();
122}
123
125{
126 event->ignore();
127}
128
130{
131 event->ignore();
132}
133
135{
136 event->ignore();
137}
138
140{
141}
142
143#include "moc_kcategorydrawer.cpp"
@ CategoryDisplayRole
This role is used for asking the category to a given index.
Item view for listing items in a categorized fashion optionally.
virtual void mouseButtonPressed(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
Method called when the mouse button has been pressed.
virtual void mouseLeft(const QModelIndex &index, const QRect &blockRect)
Method called when the mouse button has left this block.
virtual void mouseButtonReleased(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
Method called when the mouse button has been released.
KCategorizedView * view() const
virtual int categoryHeight(const QModelIndex &index, const QStyleOption &option) const
virtual void mouseButtonDoubleClicked(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
Method called when the mouse button has been double clicked.
virtual int leftMargin() const
virtual int rightMargin() const
virtual void mouseMoved(const QModelIndex &index, const QRect &blockRect, QMouseEvent *event)
Method called when the mouse has been moved.
virtual void drawCategory(const QModelIndex &index, int sortRole, const QStyleOption &option, QPainter *painter) const
This method purpose is to draw a category represented by the given.
virtual QVariant data(const QModelIndex &index, int role) const const=0
QFont font()
void setAlphaF(float alpha)
void setBold(bool enable)
int height() const const
int horizontalAdvance(QChar ch) const const
const QAbstractItemModel * model() const const
void drawRect(const QRect &rectangle)
void drawText(const QPoint &position, const QString &text)
void restore()
void save()
void setBrush(Qt::BrushStyle style)
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
void setRenderHint(RenderHint hint, bool on)
int left() const const
int right() const const
void setHeight(int height)
void setLeft(int x)
void setRight(int x)
void setTop(int y)
int top() const const
AlignLeft
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.