~blue-shell/blue-shell/kde-workspace-systemsettings-qml

« back to all changes in this revision

Viewing changes to systemsettings/qml/CategoryBackground.cpp

  • Committer: Eike Hein
  • Date: 2013-01-23 10:25:54 UTC
  • Revision ID: git-v1:cd6b36487ddc7ef8ff77defcc4ae40bb0c2e89ab
Basic QML Testbed view mode plugin.

Missing wip feature branches for:
- keyboard nav and associated scroll behavior
- item rect animations
- search filtering
- CJK-optimized label wordwrapping
- rtl layout experiments

To be finished/merged one by one soon.

Known bugs:
- scrollbar doesn't update on first show (minor)
- doesn't follow the single vs. doubleclick global setting
- whole bunch of hardcoded margins making for ugly code (mostly
  addressed in the rtl branch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /**************************************************************************
 
2
 * Copyright (C) 2012 Eike Hein <hein@kde.org>                            *
 
3
 *                                                                        *
 
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.                 *
 
8
 *                                                                        *
 
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.                           *
 
13
 *                                                                        *
 
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          *
 
17
 * 02110-1301, USA.                                                       *
 
18
***************************************************************************/
 
19
 
 
20
#include "CategoryBackground.h"
 
21
 
 
22
#include <QApplication>
 
23
#include <QPainter>
 
24
#include <QStyle>
 
25
#include <QStyleOptionViewItemV4>
 
26
 
 
27
CategoryBackground::CategoryBackground( QDeclarativeItem *parent ) : QDeclarativeItem( parent )
 
28
{
 
29
    setFlag( QGraphicsItem::ItemHasNoContents, false );
 
30
}
 
31
 
 
32
void CategoryBackground::paint( QPainter * painter, const QStyleOptionGraphicsItem *, QWidget * )
 
33
{
 
34
    painter->setRenderHint(QPainter::Antialiasing);
 
35
 
 
36
    QStyleOptionViewItemV4 option;
 
37
    option.rect = boundingRect().toRect().adjusted( 5, 5, 0, 0);
 
38
 
 
39
    const QRect optRect = option.rect;
 
40
    QFont font(QApplication::font());
 
41
    font.setBold(true);
 
42
    const QFontMetrics fontMetrics = QFontMetrics(font);
 
43
    const bool leftToRight = painter->layoutDirection() == Qt::LeftToRight;
 
44
 
 
45
    //BEGIN: decoration gradient
 
46
    {
 
47
        QPainterPath path(optRect.bottomLeft());
 
48
 
 
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());
 
56
 
 
57
        QColor window(option.palette.window().color());
 
58
        const QColor base(option.palette.base().color());
 
59
 
 
60
        window.setAlphaF(0.4);
 
61
 
 
62
        QLinearGradient decoGradient1;
 
63
        if (leftToRight) {
 
64
            decoGradient1.setStart(optRect.topLeft());
 
65
            decoGradient1.setFinalStop(optRect.bottomLeft());
 
66
        } else {
 
67
            decoGradient1.setStart(optRect.topRight());
 
68
            decoGradient1.setFinalStop(optRect.bottomRight());
 
69
        }
 
70
        decoGradient1.setColorAt(0, window);
 
71
        decoGradient1.setColorAt(1, Qt::transparent);
 
72
 
 
73
        QLinearGradient decoGradient2;
 
74
        if (leftToRight) {
 
75
            decoGradient2.setStart(optRect.topLeft());
 
76
            decoGradient2.setFinalStop(optRect.topRight());
 
77
        } else {
 
78
            decoGradient2.setStart(optRect.topRight());
 
79
            decoGradient2.setFinalStop(optRect.topLeft());
 
80
        }
 
81
        decoGradient2.setColorAt(0, Qt::transparent);
 
82
        decoGradient2.setColorAt(1, base);
 
83
 
 
84
        painter->fillPath(path, decoGradient1);
 
85
        painter->fillPath(path, decoGradient2);
 
86
    }
 
87
    //END: decoration gradient
 
88
 
 
89
    {
 
90
        QRect newOptRect(optRect);
 
91
 
 
92
        if (leftToRight) {
 
93
            newOptRect.translate(1, 1);
 
94
        } else {
 
95
            newOptRect.translate(-1, 1);
 
96
        }
 
97
 
 
98
        //BEGIN: inner top left corner
 
99
        {
 
100
            painter->save();
 
101
            painter->setPen(option.palette.base().color());
 
102
            QRectF arc;
 
103
            if (leftToRight) {
 
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);
 
108
            } else {
 
109
                QPointF topRight(newOptRect.topRight());
 
110
                topRight.rx() -= 4;
 
111
                arc = QRectF(topRight, QSizeF(4, 4));
 
112
                arc.translate(-0.5, 0.5);
 
113
                painter->drawArc(arc, 0, 1440);
 
114
            }
 
115
            painter->restore();
 
116
        }
 
117
        //END: inner top left corner
 
118
 
 
119
        //BEGIN: inner left vertical line
 
120
        {
 
121
            QPoint start;
 
122
            QPoint verticalGradBottom;
 
123
            if (leftToRight) {
 
124
                start = newOptRect.topLeft();
 
125
                verticalGradBottom = newOptRect.topLeft();
 
126
            } else {
 
127
                start = newOptRect.topRight();
 
128
                verticalGradBottom = newOptRect.topRight();
 
129
            }
 
130
            start.ry() += 3;
 
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);
 
136
        }
 
137
        //END: inner left vertical line
 
138
 
 
139
        //BEGIN: inner horizontal line
 
140
        {
 
141
            QPoint start;
 
142
            QPoint horizontalGradTop;
 
143
            if (leftToRight) {
 
144
                start = newOptRect.topLeft();
 
145
                horizontalGradTop = newOptRect.topLeft();
 
146
                start.rx() += 3;
 
147
                horizontalGradTop.rx() += newOptRect.width() - 3;
 
148
            } else {
 
149
                start = newOptRect.topRight();
 
150
                horizontalGradTop = newOptRect.topRight();
 
151
                start.rx() -= 3;
 
152
                horizontalGradTop.rx() -= newOptRect.width() - 3;
 
153
            }
 
154
            QLinearGradient gradient(start, horizontalGradTop);
 
155
            gradient.setColorAt(0, option.palette.base().color());
 
156
            gradient.setColorAt(1, Qt::transparent);
 
157
            QSize rectSize;
 
158
            if (leftToRight) {
 
159
                rectSize = QSize(newOptRect.width() - 3, 1);
 
160
            } else {
 
161
                rectSize = QSize(-newOptRect.width() + 3, 1);
 
162
            }
 
163
            painter->fillRect(QRect(start, rectSize), gradient);
 
164
        }
 
165
        //END: inner horizontal line
 
166
    }
 
167
 
 
168
    QColor outlineColor = option.palette.text().color();
 
169
    outlineColor.setAlphaF(0.35);
 
170
 
 
171
    //BEGIN: top left corner
 
172
    {
 
173
        painter->save();
 
174
        painter->setPen(outlineColor);
 
175
        QRectF arc;
 
176
        if (leftToRight) {
 
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);
 
181
        } else {
 
182
            QPointF topRight(optRect.topRight());
 
183
            topRight.rx() -= 4;
 
184
            arc = QRectF(topRight, QSizeF(4, 4));
 
185
            arc.translate(-0.5, 0.5);
 
186
            painter->drawArc(arc, 0, 1440);
 
187
        }
 
188
        painter->restore();
 
189
    }
 
190
    //END: top left corner
 
191
 
 
192
    //BEGIN: left vertical line
 
193
    {
 
194
        QPoint start;
 
195
        QPoint verticalGradBottom;
 
196
        if (leftToRight) {
 
197
            start = optRect.topLeft();
 
198
            verticalGradBottom = optRect.topLeft();
 
199
        } else {
 
200
            start = optRect.topRight();
 
201
            verticalGradBottom = optRect.topRight();
 
202
        }
 
203
        start.ry() += 3;
 
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);
 
209
    }
 
210
    //END: left vertical line
 
211
 
 
212
    //BEGIN: horizontal line
 
213
    {
 
214
        QPoint start;
 
215
        QPoint horizontalGradTop;
 
216
        if (leftToRight) {
 
217
            start = optRect.topLeft();
 
218
            horizontalGradTop = optRect.topLeft();
 
219
            start.rx() += 3;
 
220
            horizontalGradTop.rx() += optRect.width() - 3;
 
221
        } else {
 
222
            start = optRect.topRight();
 
223
            horizontalGradTop = optRect.topRight();
 
224
            start.rx() -= 3;
 
225
            horizontalGradTop.rx() -= optRect.width() - 3;
 
226
        }
 
227
        QLinearGradient gradient(start, horizontalGradTop);
 
228
        gradient.setColorAt(0, outlineColor);
 
229
        gradient.setColorAt(1, option.palette.base().color());
 
230
        QSize rectSize;
 
231
        if (leftToRight) {
 
232
            rectSize = QSize(optRect.width() - 3, 1);
 
233
        } else {
 
234
            rectSize = QSize(-optRect.width() + 3, 1);
 
235
        }
 
236
        painter->fillRect(QRect(start, rectSize), gradient);
 
237
    }
 
238
    //END: horizontal line
 
239
}