1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the demonstration applications of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "arthurstyle.h"
31
#include "arthurwidgets.h"
34
#include <qpainterpath.h>
35
#include <qpixmapcache.h>
36
#include <qradiobutton.h>
39
QPixmap cached(const QString &img)
41
if (QPixmap *p = QPixmapCache::find(img))
45
pm = QPixmap::fromImage(QImage(img), Qt::OrderedDither | Qt::OrderedAlphaDither);
49
QPixmapCache::insert(img, pm);
54
ArthurStyle::ArthurStyle()
57
Q_INIT_RESOURCE(shared);
61
void ArthurStyle::drawHoverRect(QPainter *painter, const QRect &r) const
63
double h = r.height();
64
double h2 = r.height() / 2.0;
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);
77
void ArthurStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
78
QPainter *painter, const QWidget *widget) const
83
case PE_FrameFocusRect:
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);
92
drawHoverRect(painter, widget->rect());
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");
99
radio = cached(":res/images/radiobutton_off.png");
100
painter->drawPixmap(button->rect.topLeft(), radio);
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);
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);
124
bool down = pushButton && ((button->state & State_Sunken) || (button->state & State_On));
126
QPixmap left, right, mid;
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");
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");
136
painter->drawPixmap(button->rect.topLeft(), left);
137
painter->drawTiledPixmap(QRect(button->rect.x() + left.width(),
139
button->rect.width() - left.width() - right.width(),
142
painter->drawPixmap(button->rect.x() + button->rect.width() - right.width(),
146
painter->fillRect(widget->rect().adjusted(3,5,-3,-5), QColor(31,127,31,63));
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");
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");
166
const QRect r = group->rect;
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);
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);
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);
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);
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);
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);
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);
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);
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(),
240
txt_width - titleLeft.width() - titleRight.width(),
241
titleStretch.height());
242
painter->drawTiledPixmap(tileRect, titleStretch);
243
painter->drawPixmap(tileRect.x() + tileRect.width(), 0, titleRight);
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);
256
painter->setPen(Qt::white);
257
painter->drawText(tileRect, Qt::AlignVCenter | Qt::AlignHCenter, group->title);
263
QWindowsStyle::drawPrimitive(element, option, painter, widget);
270
void ArthurStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option,
271
QPainter *painter, const QWidget *widget) const
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);
281
bool hover = (slider->state & State_Enabled) && (slider->state & State_MouseOver);
283
QRect moderated = widget->rect().adjusted(0, 4, 0, -4);
284
drawHoverRect(painter, moderated);
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()),
293
if ((option->subControls & SC_SliderHandle) && handle.isValid()) {
294
QPixmap hndl = cached(":res/images/slider_thumb_on.png");
295
painter->drawPixmap(handle.topLeft(), hndl);
302
QWindowsStyle::drawComplexControl(control, option, painter, widget);
308
QRect ArthurStyle::subControlRect(ComplexControl control, const QStyleOptionComplex *option,
309
SubControl subControl, const QWidget *widget) const
311
QRect rect = QWindowsStyle::subControlRect(control, option, subControl, widget);
312
// const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(option);
314
if (control == CC_Slider && subControl == SC_SliderHandle) {
317
} else if (control == CC_Slider && subControl == SC_SliderGroove) {
319
rect.moveTop(27/2 - 9/2);
324
QSize ArthurStyle::sizeFromContents(ContentsType type, const QStyleOption *option,
325
const QSize &size, const QWidget *widget) const
327
QSize newSize = QWindowsStyle::sizeFromContents(type, option, size, widget);
333
newSize.setHeight(26);
337
newSize.setHeight(27);
347
int ArthurStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QWidget *widget) const
349
if (pm == PM_SliderLength)
351
return QWindowsStyle::pixelMetric(pm, opt, widget);
354
void ArthurStyle::polish(QWidget *widget)
356
if (widget->layout() && qobject_cast<ArthurGroupBox *>(widget)) {
357
if (qFindChildren<ArthurGroupBox *>(widget).size() == 0)
358
widget->layout()->setSpacing(0);
360
widget->layout()->setMargin(10);
363
if (qobject_cast<QPushButton *>(widget)
364
|| qobject_cast<QRadioButton *>(widget)
365
|| qobject_cast<QSlider *>(widget)) {
366
widget->setAttribute(Qt::WA_Hover);
369
QPalette pal = widget->palette();
370
if (widget->isWindow()) {
371
pal.setColor(QPalette::Background, QColor(241, 241, 241));
372
widget->setPalette(pal);
377
void ArthurStyle::unpolish(QWidget *widget)
379
if (qobject_cast<QPushButton *>(widget)
380
|| qobject_cast<QRadioButton *>(widget)
381
|| qobject_cast<QSlider *>(widget)) {
382
widget->setAttribute(Qt::WA_Hover, false);
386
void ArthurStyle::polish(QPalette &palette)
388
palette.setColor(QPalette::Background, QColor(241, 241, 241));
391
QRect ArthurStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
395
case SE_RadioButtonClickRect:
398
case SE_RadioButtonContents:
399
r = widget->rect().adjusted(20, 0, 0, 0);
402
r = QWindowsStyle::subElementRect(element, option, widget);
406
if (qobject_cast<const QRadioButton*>(widget))
407
r = r.adjusted(5, 0, -5, 0);