KTextEditor

katecategorydrawer.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Rafael Fernández López <ereslibre@kde.org>
3 SPDX-FileCopyrightText: 2013 Dominik Haumann <dhaumann@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "katecategorydrawer.h"
9
10#include <QApplication>
11#include <QPainter>
12#include <QPainterPath>
13#include <QStyleOption>
14
15KateCategoryDrawer::KateCategoryDrawer()
16 : KCategoryDrawer(nullptr)
17{
18}
19
20void KateCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole, const QStyleOption &option, QPainter *painter) const
21{
22 Q_UNUSED(sortRole)
23
25
26 const QRect optRect = option.rect;
28 font.setBold(true);
29 const int height = categoryHeight(index, option);
30 const bool leftToRight = painter->layoutDirection() == Qt::LeftToRight;
31
32 {
33 QRect newOptRect(optRect);
34
35 if (leftToRight) {
36 newOptRect.translate(1, 1);
37 } else {
38 newOptRect.translate(-1, 1);
39 }
40
41 // BEGIN: inner top left corner
42 {
43 painter->save();
44 painter->setPen(option.palette.base().color());
45 QRectF arc;
46 if (leftToRight) {
47 const QPointF topLeft(newOptRect.topLeft());
48 arc = QRectF(topLeft, QSizeF(4, 4));
49 arc.translate(0.5, 0.5);
50 painter->drawArc(arc, 1440, 1440);
51 } else {
52 QPointF topRight(newOptRect.topRight());
53 topRight.rx() -= 4;
54 arc = QRectF(topRight, QSizeF(4, 4));
55 arc.translate(-0.5, 0.5);
56 painter->drawArc(arc, 0, 1440);
57 }
58 painter->restore();
59 }
60 // END: inner top left corner
61
62 // BEGIN: inner left vertical line
63 {
65 QPoint verticalGradBottom;
66 if (leftToRight) {
67 start = newOptRect.topLeft();
68 verticalGradBottom = newOptRect.topLeft();
69 } else {
70 start = newOptRect.topRight();
71 verticalGradBottom = newOptRect.topRight();
72 }
73 start.ry() += 3;
74 verticalGradBottom.ry() += newOptRect.height() - 3;
75 QLinearGradient gradient(start, verticalGradBottom);
76 gradient.setColorAt(0, option.palette.base().color());
77 gradient.setColorAt(1, Qt::transparent);
78 painter->fillRect(QRect(start, QSize(1, newOptRect.height() - 3)), gradient);
79 }
80 // END: inner left vertical line
81
82 // BEGIN: inner horizontal line
83 {
85 QPoint horizontalGradTop;
86 if (leftToRight) {
87 start = newOptRect.topLeft();
88 horizontalGradTop = newOptRect.topLeft();
89 start.rx() += 3;
90 horizontalGradTop.rx() += newOptRect.width() - 3;
91 } else {
92 start = newOptRect.topRight();
93 horizontalGradTop = newOptRect.topRight();
94 start.rx() -= 3;
95 horizontalGradTop.rx() -= newOptRect.width() - 3;
96 }
97 QLinearGradient gradient(start, horizontalGradTop);
98 gradient.setColorAt(0, option.palette.base().color());
99 gradient.setColorAt(1, Qt::transparent);
100 QSize rectSize;
101 if (leftToRight) {
102 rectSize = QSize(newOptRect.width() - 3, 1);
103 } else {
104 rectSize = QSize(-newOptRect.width() + 3, 1);
105 }
106 painter->fillRect(QRect(start, rectSize), gradient);
107 }
108 // END: inner horizontal line
109 }
110
111 QColor outlineColor = option.palette.text().color();
112 outlineColor.setAlphaF(0.35);
113
114 // BEGIN: top left corner
115 {
116 painter->save();
117 painter->setPen(outlineColor);
118 QRectF arc;
119 if (leftToRight) {
120 const QPointF topLeft(optRect.topLeft());
121 arc = QRectF(topLeft, QSizeF(4, 4));
122 arc.translate(0.5, 0.5);
123 painter->drawArc(arc, 1440, 1440);
124 } else {
125 QPointF topRight(optRect.topRight());
126 topRight.rx() -= 4;
127 arc = QRectF(topRight, QSizeF(4, 4));
128 arc.translate(-0.5, 0.5);
129 painter->drawArc(arc, 0, 1440);
130 }
131 painter->restore();
132 }
133 // END: top left corner
134
135 // BEGIN: left vertical line
136 {
138 QPoint verticalGradBottom;
139 if (leftToRight) {
140 start = optRect.topLeft();
141 verticalGradBottom = optRect.topLeft();
142 } else {
143 start = optRect.topRight();
144 verticalGradBottom = optRect.topRight();
145 }
146 start.ry() += 3;
147 verticalGradBottom.ry() += optRect.height() - 3;
148 QLinearGradient gradient(start, verticalGradBottom);
149 gradient.setColorAt(0, outlineColor);
150 gradient.setColorAt(1, option.palette.base().color());
151 painter->fillRect(QRect(start, QSize(1, optRect.height() - 3)), gradient);
152 }
153 // END: left vertical line
154
155 // BEGIN: horizontal line
156 {
158 QPoint horizontalGradTop;
159 if (leftToRight) {
160 start = optRect.topLeft();
161 horizontalGradTop = optRect.topLeft();
162 start.rx() += 3;
163 horizontalGradTop.rx() += optRect.width() - 3;
164 } else {
165 start = optRect.topRight();
166 horizontalGradTop = optRect.topRight();
167 start.rx() -= 3;
168 horizontalGradTop.rx() -= optRect.width() - 3;
169 }
170 QLinearGradient gradient(start, horizontalGradTop);
171 gradient.setColorAt(0, outlineColor);
172 gradient.setColorAt(1, option.palette.base().color());
173 QSize rectSize;
174 if (leftToRight) {
175 rectSize = QSize(optRect.width() - 3, 1);
176 } else {
177 rectSize = QSize(-optRect.width() + 3, 1);
178 }
179 painter->fillRect(QRect(start, rectSize), gradient);
180 }
181 // END: horizontal line
182
183 // BEGIN: draw text
184 {
185 const QString category = index.model()->data(index, Qt::DisplayRole).toString(); // KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
186 QRect textRect = QRect(option.rect.topLeft(), QSize(option.rect.width() - 2 - 3 - 3, height));
187 textRect.setTop(textRect.top() + 2 + 3 /* corner */);
188 textRect.setLeft(textRect.left() + 2 + 3 /* corner */ + 3 /* a bit of margin */);
189 painter->save();
190 painter->setFont(font);
191 QColor penColor(option.palette.text().color());
192 penColor.setAlphaF(0.6);
193 painter->setPen(penColor);
194 painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, category);
195 painter->restore();
196 }
197 // END: draw text
198}
199
200int KateCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
201{
202 Q_UNUSED(index);
203 Q_UNUSED(option);
204
206 font.setBold(true);
207 const QFontMetrics fontMetrics = QFontMetrics(font);
208
209 return fontMetrics.height() + 2 + 12 /* vertical spacing */;
210}
211
212int KateCategoryDrawer::leftMargin() const
213{
214 return 7;
215}
216
217int KateCategoryDrawer::rightMargin() const
218{
219 return 7;
220}
Q_SCRIPTABLE Q_NOREPLY void start()
Category category(StandardShortcut id)
virtual QVariant data(const QModelIndex &index, int role) const const=0
QFont font()
void setAlphaF(float alpha)
int height() const const
const QAbstractItemModel * model() const const
void drawArc(const QRect &rectangle, int startAngle, int spanAngle)
void drawText(const QPoint &position, const QString &text)
void fillRect(const QRect &rectangle, QGradient::Preset preset)
Qt::LayoutDirection layoutDirection() const const
void restore()
void save()
void setFont(const QFont &font)
void setPen(Qt::PenStyle style)
void setRenderHint(RenderHint hint, bool on)
int & rx()
int & ry()
int height() const const
int left() const const
void setLeft(int x)
void setTop(int y)
int top() const const
QPoint topLeft() const const
QPoint topRight() const const
int width() const const
void translate(const QPointF &offset)
AlignLeft
transparent
DisplayRole
LeftToRight
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.