~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to systemsettings/icons/CategoryDrawer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2009 by Rafael Fernández López <ereslibre@kde.org>      *
 
3
 *                                                                         *
 
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.                                   *
 
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                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 
18
 ***************************************************************************/
 
19
 
 
20
#include "CategoryDrawer.h"
 
21
 
 
22
#include "MenuProxyModel.h"
 
23
 
 
24
#include <QPainter>
 
25
#include <QApplication>
 
26
#include <QStyleOption>
 
27
 
 
28
CategoryDrawer::CategoryDrawer()
 
29
{
 
30
    setLeftMargin( 7 );
 
31
    setRightMargin( 7 );
 
32
}
 
33
 
 
34
void CategoryDrawer::drawCategory(const QModelIndex &index,
 
35
                                            int sortRole,
 
36
                                            const QStyleOption &option,
 
37
                                            QPainter *painter) const
 
38
{
 
39
    Q_UNUSED( option )
 
40
    Q_UNUSED( painter )
 
41
    Q_UNUSED( sortRole )
 
42
 
 
43
    painter->setRenderHint(QPainter::Antialiasing);
 
44
 
 
45
    const QRect optRect = option.rect;
 
46
    QFont font(QApplication::font());
 
47
    font.setBold(true);
 
48
    const QFontMetrics fontMetrics = QFontMetrics(font);
 
49
    const int height = categoryHeight(index, option);
 
50
    const bool leftToRight = painter->layoutDirection() == Qt::LeftToRight;
 
51
 
 
52
    //BEGIN: decoration gradient
 
53
    {
 
54
        QPainterPath path(optRect.bottomLeft());
 
55
 
 
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());
 
63
 
 
64
        QColor window(option.palette.window().color());
 
65
        const QColor base(option.palette.base().color());
 
66
 
 
67
        window.setAlphaF(0.4);
 
68
 
 
69
        QLinearGradient decoGradient1;
 
70
        if (leftToRight) {
 
71
            decoGradient1.setStart(optRect.topLeft());
 
72
            decoGradient1.setFinalStop(optRect.bottomLeft());
 
73
        } else {
 
74
            decoGradient1.setStart(optRect.topRight());
 
75
            decoGradient1.setFinalStop(optRect.bottomRight());
 
76
        }
 
77
        decoGradient1.setColorAt(0, window);
 
78
        decoGradient1.setColorAt(1, Qt::transparent);
 
79
 
 
80
        QLinearGradient decoGradient2;
 
81
        if (leftToRight) {
 
82
            decoGradient2.setStart(optRect.topLeft());
 
83
            decoGradient2.setFinalStop(optRect.topRight());
 
84
        } else {
 
85
            decoGradient2.setStart(optRect.topRight());
 
86
            decoGradient2.setFinalStop(optRect.topLeft());
 
87
        }
 
88
        decoGradient2.setColorAt(0, Qt::transparent);
 
89
        decoGradient2.setColorAt(1, base);
 
90
 
 
91
        painter->fillPath(path, decoGradient1);
 
92
        painter->fillPath(path, decoGradient2);
 
93
    }
 
94
    //END: decoration gradient
 
95
 
 
96
    {
 
97
        QRect newOptRect(optRect);
 
98
 
 
99
        if (leftToRight) {
 
100
            newOptRect.translate(1, 1);
 
101
        } else {
 
102
            newOptRect.translate(-1, 1);
 
103
        }
 
104
 
 
105
        //BEGIN: inner top left corner
 
106
        {
 
107
            painter->save();
 
108
            painter->setPen(option.palette.base().color());
 
109
            QRectF arc;
 
110
            if (leftToRight) {
 
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);
 
115
            } else {
 
116
                QPointF topRight(newOptRect.topRight());
 
117
                topRight.rx() -= 4;
 
118
                arc = QRectF(topRight, QSizeF(4, 4));
 
119
                arc.translate(-0.5, 0.5);
 
120
                painter->drawArc(arc, 0, 1440);
 
121
            }
 
122
            painter->restore();
 
123
        }
 
124
        //END: inner top left corner
 
125
 
 
126
        //BEGIN: inner left vertical line
 
127
        {
 
128
            QPoint start;
 
129
            QPoint verticalGradBottom;
 
130
            if (leftToRight) {
 
131
                start = newOptRect.topLeft();
 
132
                verticalGradBottom = newOptRect.topLeft();
 
133
            } else {
 
134
                start = newOptRect.topRight();
 
135
                verticalGradBottom = newOptRect.topRight();
 
136
            }
 
137
            start.ry() += 3;
 
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);
 
143
        }
 
144
        //END: inner left vertical line
 
145
 
 
146
        //BEGIN: inner horizontal line
 
147
        {
 
148
            QPoint start;
 
149
            QPoint horizontalGradTop;
 
150
            if (leftToRight) {
 
151
                start = newOptRect.topLeft();
 
152
                horizontalGradTop = newOptRect.topLeft();
 
153
                start.rx() += 3;
 
154
                horizontalGradTop.rx() += newOptRect.width() - 3;
 
155
            } else {
 
156
                start = newOptRect.topRight();
 
157
                horizontalGradTop = newOptRect.topRight();
 
158
                start.rx() -= 3;
 
159
                horizontalGradTop.rx() -= newOptRect.width() - 3;
 
160
            }
 
161
            QLinearGradient gradient(start, horizontalGradTop);
 
162
            gradient.setColorAt(0, option.palette.base().color());
 
163
            gradient.setColorAt(1, Qt::transparent);
 
164
            QSize rectSize;
 
165
            if (leftToRight) {
 
166
                rectSize = QSize(newOptRect.width() - 3, 1);
 
167
            } else {
 
168
                rectSize = QSize(-newOptRect.width() + 3, 1);
 
169
            }
 
170
            painter->fillRect(QRect(start, rectSize), gradient);
 
171
        }
 
172
        //END: inner horizontal line
 
173
    }
 
174
 
 
175
    QColor outlineColor = option.palette.text().color();
 
176
    outlineColor.setAlphaF(0.35);
 
177
 
 
178
    //BEGIN: top left corner
 
179
    {
 
180
        painter->save();
 
181
        painter->setPen(outlineColor);
 
182
        QRectF arc;
 
183
        if (leftToRight) {
 
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);
 
188
        } else {
 
189
            QPointF topRight(optRect.topRight());
 
190
            topRight.rx() -= 4;
 
191
            arc = QRectF(topRight, QSizeF(4, 4));
 
192
            arc.translate(-0.5, 0.5);
 
193
            painter->drawArc(arc, 0, 1440);
 
194
        }
 
195
        painter->restore();
 
196
    }
 
197
    //END: top left corner
 
198
 
 
199
    //BEGIN: left vertical line
 
200
    {
 
201
        QPoint start;
 
202
        QPoint verticalGradBottom;
 
203
        if (leftToRight) {
 
204
            start = optRect.topLeft();
 
205
            verticalGradBottom = optRect.topLeft();
 
206
        } else {
 
207
            start = optRect.topRight();
 
208
            verticalGradBottom = optRect.topRight();
 
209
        }
 
210
        start.ry() += 3;
 
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);
 
216
    }
 
217
    //END: left vertical line
 
218
 
 
219
    //BEGIN: horizontal line
 
220
    {
 
221
        QPoint start;
 
222
        QPoint horizontalGradTop;
 
223
        if (leftToRight) {
 
224
            start = optRect.topLeft();
 
225
            horizontalGradTop = optRect.topLeft();
 
226
            start.rx() += 3;
 
227
            horizontalGradTop.rx() += optRect.width() - 3;
 
228
        } else {
 
229
            start = optRect.topRight();
 
230
            horizontalGradTop = optRect.topRight();
 
231
            start.rx() -= 3;
 
232
            horizontalGradTop.rx() -= optRect.width() - 3;
 
233
        }
 
234
        QLinearGradient gradient(start, horizontalGradTop);
 
235
        gradient.setColorAt(0, outlineColor);
 
236
        gradient.setColorAt(1, option.palette.base().color());
 
237
        QSize rectSize;
 
238
        if (leftToRight) {
 
239
            rectSize = QSize(optRect.width() - 3, 1);
 
240
        } else {
 
241
            rectSize = QSize(-optRect.width() + 3, 1);
 
242
        }
 
243
        painter->fillRect(QRect(start, rectSize), gradient);
 
244
    }
 
245
    //END: horizontal line
 
246
 
 
247
    //BEGIN: draw text
 
248
    {
 
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 */);
 
253
        painter->save();
 
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);
 
259
        painter->restore();
 
260
    }
 
261
    //END: draw text
 
262
}
 
263
 
 
264
int CategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
 
265
{
 
266
    Q_UNUSED( index );
 
267
    Q_UNUSED( option );
 
268
 
 
269
    QFont font(QApplication::font());
 
270
    font.setBold(true);
 
271
    const QFontMetrics fontMetrics = QFontMetrics(font);
 
272
 
 
273
    return fontMetrics.height() + 2 + 12 /* vertical spacing */;
 
274
}