1
/***************************************************************************
2
* Copyright (C) 2009 by Rafael Fernández López <ereslibre@kde.org> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
18
***************************************************************************/
20
#include "CategoryDrawer.h"
22
#include "MenuProxyModel.h"
25
#include <QApplication>
26
#include <QStyleOption>
28
CategoryDrawer::CategoryDrawer()
34
void CategoryDrawer::drawCategory(const QModelIndex &index,
36
const QStyleOption &option,
37
QPainter *painter) const
43
painter->setRenderHint(QPainter::Antialiasing);
45
const QRect optRect = option.rect;
46
QFont font(QApplication::font());
48
const QFontMetrics fontMetrics = QFontMetrics(font);
49
const int height = categoryHeight(index, option);
50
const bool leftToRight = painter->layoutDirection() == Qt::LeftToRight;
52
//BEGIN: decoration gradient
54
QPainterPath path(optRect.bottomLeft());
56
path.lineTo(QPoint(optRect.topLeft().x(), optRect.topLeft().y() - 3));
57
const QPointF topLeft(optRect.topLeft());
58
QRectF arc(topLeft, QSizeF(4, 4));
59
path.arcTo(arc, 180, -90);
60
path.lineTo(optRect.topRight());
61
path.lineTo(optRect.bottomRight());
62
path.lineTo(optRect.bottomLeft());
64
QColor window(option.palette.window().color());
65
const QColor base(option.palette.base().color());
67
window.setAlphaF(0.4);
69
QLinearGradient decoGradient1;
71
decoGradient1.setStart(optRect.topLeft());
72
decoGradient1.setFinalStop(optRect.bottomLeft());
74
decoGradient1.setStart(optRect.topRight());
75
decoGradient1.setFinalStop(optRect.bottomRight());
77
decoGradient1.setColorAt(0, window);
78
decoGradient1.setColorAt(1, Qt::transparent);
80
QLinearGradient decoGradient2;
82
decoGradient2.setStart(optRect.topLeft());
83
decoGradient2.setFinalStop(optRect.topRight());
85
decoGradient2.setStart(optRect.topRight());
86
decoGradient2.setFinalStop(optRect.topLeft());
88
decoGradient2.setColorAt(0, Qt::transparent);
89
decoGradient2.setColorAt(1, base);
91
painter->fillPath(path, decoGradient1);
92
painter->fillPath(path, decoGradient2);
94
//END: decoration gradient
97
QRect newOptRect(optRect);
100
newOptRect.translate(1, 1);
102
newOptRect.translate(-1, 1);
105
//BEGIN: inner top left corner
108
painter->setPen(option.palette.base().color());
111
const QPointF topLeft(newOptRect.topLeft());
112
arc = QRectF(topLeft, QSizeF(4, 4));
113
arc.translate(0.5, 0.5);
114
painter->drawArc(arc, 1440, 1440);
116
QPointF topRight(newOptRect.topRight());
118
arc = QRectF(topRight, QSizeF(4, 4));
119
arc.translate(-0.5, 0.5);
120
painter->drawArc(arc, 0, 1440);
124
//END: inner top left corner
126
//BEGIN: inner left vertical line
129
QPoint verticalGradBottom;
131
start = newOptRect.topLeft();
132
verticalGradBottom = newOptRect.topLeft();
134
start = newOptRect.topRight();
135
verticalGradBottom = newOptRect.topRight();
138
verticalGradBottom.ry() += newOptRect.height() - 3;
139
QLinearGradient gradient(start, verticalGradBottom);
140
gradient.setColorAt(0, option.palette.base().color());
141
gradient.setColorAt(1, Qt::transparent);
142
painter->fillRect(QRect(start, QSize(1, newOptRect.height() - 3)), gradient);
144
//END: inner left vertical line
146
//BEGIN: inner horizontal line
149
QPoint horizontalGradTop;
151
start = newOptRect.topLeft();
152
horizontalGradTop = newOptRect.topLeft();
154
horizontalGradTop.rx() += newOptRect.width() - 3;
156
start = newOptRect.topRight();
157
horizontalGradTop = newOptRect.topRight();
159
horizontalGradTop.rx() -= newOptRect.width() - 3;
161
QLinearGradient gradient(start, horizontalGradTop);
162
gradient.setColorAt(0, option.palette.base().color());
163
gradient.setColorAt(1, Qt::transparent);
166
rectSize = QSize(newOptRect.width() - 3, 1);
168
rectSize = QSize(-newOptRect.width() + 3, 1);
170
painter->fillRect(QRect(start, rectSize), gradient);
172
//END: inner horizontal line
175
QColor outlineColor = option.palette.text().color();
176
outlineColor.setAlphaF(0.35);
178
//BEGIN: top left corner
181
painter->setPen(outlineColor);
184
const QPointF topLeft(optRect.topLeft());
185
arc = QRectF(topLeft, QSizeF(4, 4));
186
arc.translate(0.5, 0.5);
187
painter->drawArc(arc, 1440, 1440);
189
QPointF topRight(optRect.topRight());
191
arc = QRectF(topRight, QSizeF(4, 4));
192
arc.translate(-0.5, 0.5);
193
painter->drawArc(arc, 0, 1440);
197
//END: top left corner
199
//BEGIN: left vertical line
202
QPoint verticalGradBottom;
204
start = optRect.topLeft();
205
verticalGradBottom = optRect.topLeft();
207
start = optRect.topRight();
208
verticalGradBottom = optRect.topRight();
211
verticalGradBottom.ry() += optRect.height() - 3;
212
QLinearGradient gradient(start, verticalGradBottom);
213
gradient.setColorAt(0, outlineColor);
214
gradient.setColorAt(1, option.palette.base().color());
215
painter->fillRect(QRect(start, QSize(1, optRect.height() - 3)), gradient);
217
//END: left vertical line
219
//BEGIN: horizontal line
222
QPoint horizontalGradTop;
224
start = optRect.topLeft();
225
horizontalGradTop = optRect.topLeft();
227
horizontalGradTop.rx() += optRect.width() - 3;
229
start = optRect.topRight();
230
horizontalGradTop = optRect.topRight();
232
horizontalGradTop.rx() -= optRect.width() - 3;
234
QLinearGradient gradient(start, horizontalGradTop);
235
gradient.setColorAt(0, outlineColor);
236
gradient.setColorAt(1, option.palette.base().color());
239
rectSize = QSize(optRect.width() - 3, 1);
241
rectSize = QSize(-optRect.width() + 3, 1);
243
painter->fillRect(QRect(start, rectSize), gradient);
245
//END: horizontal line
249
const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
250
QRect textRect = QRect(option.rect.topLeft(), QSize(option.rect.width() - 2 - 3 - 3, height));
251
textRect.setTop(textRect.top() + 2 + 3 /* corner */);
252
textRect.setLeft(textRect.left() + 2 + 3 /* corner */ + 3 /* a bit of margin */);
254
painter->setFont(font);
255
QColor penColor(option.palette.text().color());
256
penColor.setAlphaF(0.6);
257
painter->setPen(penColor);
258
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, category);
264
int CategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
269
QFont font(QApplication::font());
271
const QFontMetrics fontMetrics = QFontMetrics(font);
273
return fontMetrics.height() + 2 + 12 /* vertical spacing */;