1
/**************************************************************************
2
* Copyright (C) 2012 Eike Hein <hein@kde.org> *
4
* This program is free software; you can redistribute it and/or *
5
* modify it under the terms of the GNU General Public License *
6
* as published by the Free Software Foundation; either version 2 *
7
* of the License, or (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 Free Software *
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
18
***************************************************************************/
20
#include "CategoryBackground.h"
22
#include <QApplication>
25
#include <QStyleOptionViewItemV4>
27
CategoryBackground::CategoryBackground( QDeclarativeItem *parent ) : QDeclarativeItem( parent )
29
setFlag( QGraphicsItem::ItemHasNoContents, false );
32
void CategoryBackground::paint( QPainter * painter, const QStyleOptionGraphicsItem *, QWidget * )
34
painter->setRenderHint(QPainter::Antialiasing);
36
QStyleOptionViewItemV4 option;
37
option.rect = boundingRect().toRect().adjusted( 5, 5, 0, 0);
39
const QRect optRect = option.rect;
40
QFont font(QApplication::font());
42
const QFontMetrics fontMetrics = QFontMetrics(font);
43
const bool leftToRight = painter->layoutDirection() == Qt::LeftToRight;
45
//BEGIN: decoration gradient
47
QPainterPath path(optRect.bottomLeft());
49
path.lineTo(QPoint(optRect.topLeft().x(), optRect.topLeft().y() - 3));
50
const QPointF topLeft(optRect.topLeft());
51
QRectF arc(topLeft, QSizeF(4, 4));
52
path.arcTo(arc, 180, -90);
53
path.lineTo(optRect.topRight());
54
path.lineTo(optRect.bottomRight());
55
path.lineTo(optRect.bottomLeft());
57
QColor window(option.palette.window().color());
58
const QColor base(option.palette.base().color());
60
window.setAlphaF(0.4);
62
QLinearGradient decoGradient1;
64
decoGradient1.setStart(optRect.topLeft());
65
decoGradient1.setFinalStop(optRect.bottomLeft());
67
decoGradient1.setStart(optRect.topRight());
68
decoGradient1.setFinalStop(optRect.bottomRight());
70
decoGradient1.setColorAt(0, window);
71
decoGradient1.setColorAt(1, Qt::transparent);
73
QLinearGradient decoGradient2;
75
decoGradient2.setStart(optRect.topLeft());
76
decoGradient2.setFinalStop(optRect.topRight());
78
decoGradient2.setStart(optRect.topRight());
79
decoGradient2.setFinalStop(optRect.topLeft());
81
decoGradient2.setColorAt(0, Qt::transparent);
82
decoGradient2.setColorAt(1, base);
84
painter->fillPath(path, decoGradient1);
85
painter->fillPath(path, decoGradient2);
87
//END: decoration gradient
90
QRect newOptRect(optRect);
93
newOptRect.translate(1, 1);
95
newOptRect.translate(-1, 1);
98
//BEGIN: inner top left corner
101
painter->setPen(option.palette.base().color());
104
const QPointF topLeft(newOptRect.topLeft());
105
arc = QRectF(topLeft, QSizeF(4, 4));
106
arc.translate(0.5, 0.5);
107
painter->drawArc(arc, 1440, 1440);
109
QPointF topRight(newOptRect.topRight());
111
arc = QRectF(topRight, QSizeF(4, 4));
112
arc.translate(-0.5, 0.5);
113
painter->drawArc(arc, 0, 1440);
117
//END: inner top left corner
119
//BEGIN: inner left vertical line
122
QPoint verticalGradBottom;
124
start = newOptRect.topLeft();
125
verticalGradBottom = newOptRect.topLeft();
127
start = newOptRect.topRight();
128
verticalGradBottom = newOptRect.topRight();
131
verticalGradBottom.ry() += newOptRect.height() - 3;
132
QLinearGradient gradient(start, verticalGradBottom);
133
gradient.setColorAt(0, option.palette.base().color());
134
gradient.setColorAt(1, Qt::transparent);
135
painter->fillRect(QRect(start, QSize(1, newOptRect.height() - 3)), gradient);
137
//END: inner left vertical line
139
//BEGIN: inner horizontal line
142
QPoint horizontalGradTop;
144
start = newOptRect.topLeft();
145
horizontalGradTop = newOptRect.topLeft();
147
horizontalGradTop.rx() += newOptRect.width() - 3;
149
start = newOptRect.topRight();
150
horizontalGradTop = newOptRect.topRight();
152
horizontalGradTop.rx() -= newOptRect.width() - 3;
154
QLinearGradient gradient(start, horizontalGradTop);
155
gradient.setColorAt(0, option.palette.base().color());
156
gradient.setColorAt(1, Qt::transparent);
159
rectSize = QSize(newOptRect.width() - 3, 1);
161
rectSize = QSize(-newOptRect.width() + 3, 1);
163
painter->fillRect(QRect(start, rectSize), gradient);
165
//END: inner horizontal line
168
QColor outlineColor = option.palette.text().color();
169
outlineColor.setAlphaF(0.35);
171
//BEGIN: top left corner
174
painter->setPen(outlineColor);
177
const QPointF topLeft(optRect.topLeft());
178
arc = QRectF(topLeft, QSizeF(4, 4));
179
arc.translate(0.5, 0.5);
180
painter->drawArc(arc, 1440, 1440);
182
QPointF topRight(optRect.topRight());
184
arc = QRectF(topRight, QSizeF(4, 4));
185
arc.translate(-0.5, 0.5);
186
painter->drawArc(arc, 0, 1440);
190
//END: top left corner
192
//BEGIN: left vertical line
195
QPoint verticalGradBottom;
197
start = optRect.topLeft();
198
verticalGradBottom = optRect.topLeft();
200
start = optRect.topRight();
201
verticalGradBottom = optRect.topRight();
204
verticalGradBottom.ry() += optRect.height() - 3;
205
QLinearGradient gradient(start, verticalGradBottom);
206
gradient.setColorAt(0, outlineColor);
207
gradient.setColorAt(1, option.palette.base().color());
208
painter->fillRect(QRect(start, QSize(1, optRect.height() - 3)), gradient);
210
//END: left vertical line
212
//BEGIN: horizontal line
215
QPoint horizontalGradTop;
217
start = optRect.topLeft();
218
horizontalGradTop = optRect.topLeft();
220
horizontalGradTop.rx() += optRect.width() - 3;
222
start = optRect.topRight();
223
horizontalGradTop = optRect.topRight();
225
horizontalGradTop.rx() -= optRect.width() - 3;
227
QLinearGradient gradient(start, horizontalGradTop);
228
gradient.setColorAt(0, outlineColor);
229
gradient.setColorAt(1, option.palette.base().color());
232
rectSize = QSize(optRect.width() - 3, 1);
234
rectSize = QSize(-optRect.width() + 3, 1);
236
painter->fillRect(QRect(start, rectSize), gradient);
238
//END: horizontal line