~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to demos/shared/arthurstyle.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the demonstration applications of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "arthurstyle.h"
 
30
 
 
31
#include "arthurwidgets.h"
 
32
#include <qdebug.h>
 
33
#include <qlayout.h>
 
34
#include <qpainterpath.h>
 
35
#include <qpixmapcache.h>
 
36
#include <qradiobutton.h>
 
37
#include <qstring.h>
 
38
 
 
39
QPixmap cached(const QString &img)
 
40
{
 
41
    if (QPixmap *p = QPixmapCache::find(img))
 
42
        return *p;
 
43
 
 
44
    QPixmap pm;
 
45
    pm = QPixmap::fromImage(QImage(img), Qt::OrderedDither | Qt::OrderedAlphaDither);
 
46
    if (pm.isNull())
 
47
        return QPixmap();
 
48
 
 
49
    QPixmapCache::insert(img, pm);
 
50
    return pm;
 
51
}
 
52
 
 
53
 
 
54
ArthurStyle::ArthurStyle()
 
55
    : QWindowsStyle()
 
56
{
 
57
    Q_INIT_RESOURCE(shared);
 
58
}
 
59
 
 
60
 
 
61
void ArthurStyle::drawHoverRect(QPainter *painter, const QRect &r) const
 
62
{
 
63
    double h = r.height();
 
64
    double h2 = r.height() / 2.0;
 
65
    QPainterPath path;
 
66
    path.addRect(r.x() + h2, r.y() + 0, r.width() - h2 * 2, r.height());
 
67
    path.addEllipse(r.x(), r.y(), h, h);
 
68
    path.addEllipse(r.x() + r.width() - h, r.y(), h, h);
 
69
    path.setFillRule(Qt::WindingFill);
 
70
    painter->setPen(Qt::NoPen);
 
71
    painter->setBrush(QColor(191, 215, 191));
 
72
    painter->setRenderHint(QPainter::Antialiasing);
 
73
    painter->drawPath(path);
 
74
}
 
75
 
 
76
 
 
77
void ArthurStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
 
78
                                QPainter *painter, const QWidget *widget) const
 
79
{
 
80
 
 
81
    Q_ASSERT(option);
 
82
    switch (element) {
 
83
    case PE_FrameFocusRect:
 
84
        break;
 
85
 
 
86
    case PE_IndicatorRadioButton:
 
87
        if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) {
 
88
            bool hover = (button->state & State_Enabled) && (button->state & State_MouseOver);
 
89
            painter->save();
 
90
            QPixmap radio;
 
91
            if (hover)
 
92
                drawHoverRect(painter, widget->rect());
 
93
 
 
94
            if (button->state & State_Sunken)
 
95
                radio = cached(":res/images/radiobutton-on.png");
 
96
            else if (button->state & State_On)
 
97
                radio = cached(":res/images/radiobutton_on.png");
 
98
            else
 
99
                radio = cached(":res/images/radiobutton_off.png");
 
100
            painter->drawPixmap(button->rect.topLeft(), radio);
 
101
 
 
102
            painter->restore();
 
103
        }
 
104
        break;
 
105
 
 
106
    case PE_PanelButtonCommand:
 
107
        if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) {
 
108
            bool hover = (button->state & State_Enabled) && (button->state & State_MouseOver);
 
109
 
 
110
            painter->save();
 
111
            const QPushButton *pushButton = qobject_cast<const QPushButton *>(widget);
 
112
            QWidget *parent = pushButton->parentWidget();
 
113
            if (parent && qobject_cast<QGroupBox *>(parent)) {
 
114
                QLinearGradient lg(0, 0, 0, parent->height());
 
115
                lg.setColorAt(0, QColor(224,224,224));
 
116
                lg.setColorAt(1, QColor(255,255,255));
 
117
                painter->setPen(Qt::NoPen);
 
118
                painter->setBrush(lg);
 
119
                painter->setBrushOrigin(-widget->mapToParent(QPoint(0,0)));
 
120
                painter->drawRect(button->rect);
 
121
                painter->setBrushOrigin(0,0);
 
122
            }
 
123
 
 
124
            bool down = pushButton && ((button->state & State_Sunken) || (button->state & State_On));
 
125
 
 
126
            QPixmap left, right, mid;
 
127
            if (down) {
 
128
                left = cached(":res/images/button_pressed_cap_left.png");
 
129
                right = cached(":res/images/button_pressed_cap_right.png");
 
130
                mid = cached(":res/images/button_pressed_stretch.png");
 
131
            } else {
 
132
                left = cached(":res/images/button_normal_cap_left.png");
 
133
                right = cached(":res/images/button_normal_cap_right.png");
 
134
                mid = cached(":res/images/button_normal_stretch.png");
 
135
            }
 
136
            painter->drawPixmap(button->rect.topLeft(), left);
 
137
            painter->drawTiledPixmap(QRect(button->rect.x() + left.width(),
 
138
                                           button->rect.y(),
 
139
                                           button->rect.width() - left.width() - right.width(),
 
140
                                           left.height()),
 
141
                                     mid);
 
142
            painter->drawPixmap(button->rect.x() + button->rect.width() - right.width(),
 
143
                                button->rect.y(),
 
144
                                right);
 
145
            if (hover)
 
146
                painter->fillRect(widget->rect().adjusted(3,5,-3,-5), QColor(31,127,31,63));
 
147
            painter->restore();
 
148
        }
 
149
        break;
 
150
 
 
151
    case PE_FrameGroupBox:
 
152
        if (const ArthurGroupBoxStyleOption *group = qstyleoption_cast<const ArthurGroupBoxStyleOption *>(option)) {
 
153
            QPixmap titleLeft = cached(":res/images/title_cap_left.png");
 
154
            QPixmap titleRight = cached(":res/images/title_cap_right.png");
 
155
            QPixmap titleStretch = cached(":res/images/title_stretch.png");
 
156
 
 
157
            QPixmap topLeft = cached(":res/images/groupframe_topleft.png");
 
158
            QPixmap topRight = cached(":res/images/groupframe_topright.png");
 
159
            QPixmap bottomLeft = cached(":res/images/groupframe_bottom_left.png");
 
160
            QPixmap bottomRight = cached(":res/images/groupframe_bottom_right.png");
 
161
            QPixmap leftStretch = cached(":res/images/groupframe_left_stretch.png");
 
162
            QPixmap topStretch = cached(":res/images/groupframe_top_stretch.png");
 
163
            QPixmap rightStretch = cached(":res/images/groupframe_right_stretch.png");
 
164
            QPixmap bottomStretch = cached(":res/images/groupframe_bottom_stretch.png");
 
165
 
 
166
            const QRect r = group->rect;
 
167
            painter->save();
 
168
 
 
169
            // first, get background from parent
 
170
            QWidget *parent = widget->parentWidget();
 
171
            if (parent && qobject_cast<QGroupBox *>(parent)) {
 
172
                QLinearGradient lg(0, 0, 0, parent->height());
 
173
                lg.setColorAt(0, QColor(224,224,224));
 
174
                lg.setColorAt(1, QColor(255,255,255));
 
175
                painter->setPen(Qt::NoPen);
 
176
                painter->setBrush(lg);
 
177
                painter->setBrushOrigin(-widget->mapToParent(QPoint(0,0)));
 
178
                painter->drawRect(r);
 
179
                painter->setBrushOrigin(0,0);
 
180
            }
 
181
 
 
182
            int radius = 14;
 
183
            int radius2 = radius*2;
 
184
            QPainterPath clipPath;
 
185
            clipPath.moveTo(radius, 0);
 
186
            clipPath.arcTo(r.right() - radius2, 0, radius2, radius2, 90, -90);
 
187
            clipPath.arcTo(r.right() - radius2, r.bottom() - radius2, radius2, radius2, 0, -90);
 
188
            clipPath.arcTo(r.left(), r.bottom() - radius2, radius2, radius2, 270, -90);
 
189
            clipPath.arcTo(r.left(), r.top(), radius2, radius2, 180, -90);
 
190
            painter->setClipPath(clipPath);
 
191
 
 
192
            // edges and fill
 
193
            QLinearGradient lg(0, 0, 0, r.height());
 
194
            lg.setColorAt(0, QColor(224,224,224));
 
195
            lg.setColorAt(1, QColor(255,255,255));
 
196
            painter->setPen(Qt::NoPen);
 
197
            painter->setBrush(lg);
 
198
            painter->drawRect(r.adjusted(0, titleStretch.height()/2, 0, 0));
 
199
            painter->setClipping(false);
 
200
 
 
201
            int topFrameOffset = titleStretch.height()/2 - 2;
 
202
            painter->drawPixmap(r.topLeft() + QPoint(0, topFrameOffset), topLeft);
 
203
            painter->drawPixmap(r.topRight() - QPoint(topRight.width()-1, 0)  + QPoint(0, topFrameOffset), topRight);
 
204
            painter->drawPixmap(r.bottomLeft() - QPoint(0, bottomLeft.height()-1), bottomLeft);
 
205
            painter->drawPixmap(r.bottomRight() - QPoint(bottomRight.width()-1, bottomRight.height()-1), bottomRight);
 
206
 
 
207
            QRect left = r;
 
208
            left.setY(r.y() + topLeft.height() + topFrameOffset);
 
209
            left.setWidth(leftStretch.width());
 
210
            left.setHeight(r.height() - topLeft.height() - bottomLeft.height() - topFrameOffset);
 
211
            painter->drawTiledPixmap(left, leftStretch);
 
212
 
 
213
            QRect top = r;
 
214
            top.setX(r.x() + topLeft.width());
 
215
            top.setY(r.y() + topFrameOffset);
 
216
            top.setWidth(r.width() - topLeft.width() - topRight.width());
 
217
            top.setHeight(topLeft.height());
 
218
            painter->drawTiledPixmap(top, topStretch);
 
219
 
 
220
            QRect right = r;
 
221
            right.setX(r.right() - rightStretch.width()+1);
 
222
            right.setY(r.y() + topRight.height() + topFrameOffset);
 
223
            right.setWidth(rightStretch.width());
 
224
            right.setHeight(r.height() - topRight.height() - bottomRight.height() - topFrameOffset);
 
225
            painter->drawTiledPixmap(right, rightStretch);
 
226
 
 
227
            QRect bottom = r;
 
228
            bottom.setX(r.x() + bottomLeft.width());
 
229
            bottom.setY(r.bottom() - bottomStretch.height()+1);
 
230
            bottom.setWidth(r.width() - bottomLeft.width() - bottomRight.width());
 
231
            bottom.setHeight(bottomLeft.height());
 
232
            painter->drawTiledPixmap(bottom, bottomStretch);
 
233
 
 
234
            // labels
 
235
 
 
236
            int txt_width = group->fontMetrics.width(group->title) + 20;
 
237
            painter->drawPixmap(r.center().x() - txt_width/2, 0, titleLeft);
 
238
            QRect tileRect(r.center().x() - txt_width/2 + titleLeft.width(),
 
239
                           0,
 
240
                           txt_width - titleLeft.width() - titleRight.width(),
 
241
                           titleStretch.height());
 
242
            painter->drawTiledPixmap(tileRect, titleStretch);
 
243
            painter->drawPixmap(tileRect.x() + tileRect.width(), 0, titleRight);
 
244
 
 
245
            {
 
246
                int opacity = 31;
 
247
                painter->setPen(QColor(0, 0, 0, opacity));
 
248
                painter->drawText(tileRect.translated(0, 1),
 
249
                                  Qt::AlignVCenter | Qt::AlignHCenter, group->title);
 
250
                painter->drawText(tileRect.translated(2, 1),
 
251
                                  Qt::AlignVCenter | Qt::AlignHCenter, group->title);
 
252
                painter->setPen(QColor(0, 0, 0, opacity * 2));
 
253
                painter->drawText(tileRect.translated(1, 1),
 
254
                                  Qt::AlignVCenter | Qt::AlignHCenter, group->title);
 
255
            }
 
256
            painter->setPen(Qt::white);
 
257
            painter->drawText(tileRect, Qt::AlignVCenter | Qt::AlignHCenter, group->title);
 
258
            painter->restore();
 
259
        }
 
260
        break;
 
261
 
 
262
    default:
 
263
        QWindowsStyle::drawPrimitive(element, option, painter, widget);
 
264
        break;
 
265
    }
 
266
    return;
 
267
}
 
268
 
 
269
 
 
270
void ArthurStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option,
 
271
                                     QPainter *painter, const QWidget *widget) const
 
272
{
 
273
    switch (control) {
 
274
    case CC_Slider:
 
275
        if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option)) {
 
276
            QRect groove = subControlRect(CC_Slider, option, SC_SliderGroove, widget);
 
277
            QRect handle = subControlRect(CC_Slider, option, SC_SliderHandle, widget);
 
278
 
 
279
            painter->save();
 
280
 
 
281
            bool hover = (slider->state & State_Enabled) && (slider->state & State_MouseOver);
 
282
            if (hover) {
 
283
                QRect moderated = widget->rect().adjusted(0, 4, 0, -4);
 
284
                drawHoverRect(painter, moderated);
 
285
            }
 
286
 
 
287
            if ((option->subControls & SC_SliderGroove) && groove.isValid()) {
 
288
                QPixmap grv = cached(":res/images/slider_bar.png");
 
289
                painter->drawPixmap(QRect(groove.x() + 5, groove.y(),
 
290
                                          groove.width() - 10, grv.height()),
 
291
                                    grv);
 
292
            }
 
293
            if ((option->subControls & SC_SliderHandle) && handle.isValid()) {
 
294
                QPixmap hndl = cached(":res/images/slider_thumb_on.png");
 
295
                painter->drawPixmap(handle.topLeft(), hndl);
 
296
            }
 
297
 
 
298
            painter->restore();
 
299
        }
 
300
        break;
 
301
    default:
 
302
        QWindowsStyle::drawComplexControl(control, option, painter, widget);
 
303
        break;
 
304
    }
 
305
    return;
 
306
}
 
307
 
 
308
QRect ArthurStyle::subControlRect(ComplexControl control, const QStyleOptionComplex *option,
 
309
                                  SubControl subControl, const QWidget *widget) const
 
310
{
 
311
    QRect rect = QWindowsStyle::subControlRect(control, option, subControl, widget);
 
312
//     const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option);
 
313
 
 
314
    if (control == CC_Slider && subControl == SC_SliderHandle) {
 
315
        rect.setWidth(13);
 
316
        rect.setHeight(27);
 
317
    } else if (control == CC_Slider && subControl == SC_SliderGroove) {
 
318
        rect.setHeight(9);
 
319
        rect.moveTop(27/2 - 9/2);
 
320
    }
 
321
    return rect;
 
322
}
 
323
 
 
324
QSize ArthurStyle::sizeFromContents(ContentsType type, const QStyleOption *option,
 
325
                                    const QSize &size, const QWidget *widget) const
 
326
{
 
327
    QSize newSize = QWindowsStyle::sizeFromContents(type, option, size, widget);
 
328
 
 
329
 
 
330
    switch (type) {
 
331
 
 
332
    case CT_PushButton:
 
333
        newSize.setHeight(26);
 
334
        break;
 
335
 
 
336
    case CT_Slider:
 
337
        newSize.setHeight(27);
 
338
        break;
 
339
 
 
340
    default:
 
341
        break;
 
342
    }
 
343
 
 
344
    return newSize;
 
345
}
 
346
 
 
347
int ArthurStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QWidget *widget) const
 
348
{
 
349
    if (pm == PM_SliderLength)
 
350
        return 13;
 
351
    return QWindowsStyle::pixelMetric(pm, opt, widget);
 
352
}
 
353
 
 
354
void ArthurStyle::polish(QWidget *widget)
 
355
{
 
356
    if (widget->layout() && qobject_cast<ArthurGroupBox *>(widget)) {
 
357
        if (qFindChildren<ArthurGroupBox *>(widget).size() == 0)
 
358
            widget->layout()->setSpacing(0);
 
359
        else
 
360
            widget->layout()->setMargin(10);
 
361
    }
 
362
 
 
363
    if (qobject_cast<QPushButton *>(widget)
 
364
        || qobject_cast<QRadioButton *>(widget)
 
365
        || qobject_cast<QSlider *>(widget)) {
 
366
        widget->setAttribute(Qt::WA_Hover);
 
367
    }
 
368
 
 
369
    QPalette pal = widget->palette();
 
370
    if (widget->isWindow()) {
 
371
        pal.setColor(QPalette::Background, QColor(241, 241, 241));
 
372
        widget->setPalette(pal);
 
373
    }
 
374
 
 
375
}
 
376
 
 
377
void ArthurStyle::unpolish(QWidget *widget)
 
378
{
 
379
    if (qobject_cast<QPushButton *>(widget)
 
380
        || qobject_cast<QRadioButton *>(widget)
 
381
        || qobject_cast<QSlider *>(widget)) {
 
382
        widget->setAttribute(Qt::WA_Hover, false);
 
383
    }
 
384
}
 
385
 
 
386
void ArthurStyle::polish(QPalette &palette)
 
387
{
 
388
    palette.setColor(QPalette::Background, QColor(241, 241, 241));
 
389
}
 
390
 
 
391
QRect ArthurStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
 
392
{
 
393
    QRect r;
 
394
    switch(element) {
 
395
    case SE_RadioButtonClickRect:
 
396
        r = widget->rect();
 
397
        break;
 
398
    case SE_RadioButtonContents:
 
399
        r = widget->rect().adjusted(20, 0, 0, 0);
 
400
        break;
 
401
    default:
 
402
        r = QWindowsStyle::subElementRect(element, option, widget);
 
403
        break;
 
404
    }
 
405
 
 
406
    if (qobject_cast<const QRadioButton*>(widget))
 
407
        r = r.adjusted(5, 0, -5, 0);
 
408
 
 
409
    return r;
 
410
}