1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "arthurstyle.h"
52#include "arthurwidgets.h"
53#include <QLayout>
54#include <QPainter>
55#include <QPainterPath>
56#include <QPixmapCache>
57#include <QRadioButton>
58#include <QString>
59#include <QStyleOption>
60#include <QtDebug>
61
62QPixmap cached(const QString &img)
63{
64 QPixmap pm;
65 if (QPixmapCache::find(key: img, pixmap: &pm))
66 return pm;
67
68 pm = QPixmap::fromImage(image: QImage(img), flags: Qt::OrderedDither | Qt::OrderedAlphaDither);
69 if (pm.isNull())
70 return QPixmap();
71
72 QPixmapCache::insert(key: img, pixmap: pm);
73 return pm;
74}
75
76
77ArthurStyle::ArthurStyle()
78 : QCommonStyle()
79{
80 Q_INIT_RESOURCE(shared);
81}
82
83
84void ArthurStyle::drawHoverRect(QPainter *painter, const QRect &r) const
85{
86 qreal h = r.height();
87 qreal h2 = r.height() / qreal(2);
88 QPainterPath path;
89 path.addRect(x: r.x() + h2, y: r.y() + 0, w: r.width() - h2 * 2, h: r.height());
90 path.addEllipse(x: r.x(), y: r.y(), w: h, h);
91 path.addEllipse(x: r.x() + r.width() - h, y: r.y(), w: h, h);
92 path.setFillRule(Qt::WindingFill);
93 painter->setPen(Qt::NoPen);
94 painter->setBrush(QColor(191, 215, 191));
95 painter->setRenderHint(hint: QPainter::Antialiasing);
96 painter->drawPath(path);
97}
98
99
100void ArthurStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,
101 QPainter *painter, const QWidget *widget) const
102{
103
104 Q_ASSERT(option);
105 switch (element) {
106 case PE_FrameFocusRect:
107 break;
108
109 case PE_IndicatorRadioButton:
110 if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(opt: option)) {
111 bool hover = (button->state & State_Enabled) && (button->state & State_MouseOver);
112 painter->save();
113 QPixmap radio;
114 if (hover)
115 drawHoverRect(painter, r: widget->rect());
116
117 if (button->state & State_Sunken)
118 radio = cached(img: ":res/images/radiobutton-on.png");
119 else if (button->state & State_On)
120 radio = cached(img: ":res/images/radiobutton_on.png");
121 else
122 radio = cached(img: ":res/images/radiobutton_off.png");
123 painter->drawPixmap(p: button->rect.topLeft(), pm: radio);
124
125 painter->restore();
126 }
127 break;
128
129 case PE_PanelButtonCommand:
130 if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(opt: option)) {
131 bool hover = (button->state & State_Enabled) && (button->state & State_MouseOver);
132
133 painter->save();
134 const QPushButton *pushButton = qobject_cast<const QPushButton *>(object: widget);
135 Q_ASSERT(pushButton);
136 QWidget *parent = pushButton->parentWidget();
137 if (parent && qobject_cast<QGroupBox *>(object: parent)) {
138 QLinearGradient lg(0, 0, 0, parent->height());
139 lg.setColorAt(pos: 0, color: QColor(224,224,224));
140 lg.setColorAt(pos: 1, color: QColor(255,255,255));
141 painter->setPen(Qt::NoPen);
142 painter->setBrush(lg);
143 painter->setBrushOrigin(-widget->mapToParent(QPoint(0,0)));
144 painter->drawRect(r: button->rect);
145 painter->setBrushOrigin(x: 0,y: 0);
146 }
147
148 bool down = (button->state & State_Sunken) || (button->state & State_On);
149
150 QPixmap left, right, mid;
151 if (down) {
152 left = cached(img: ":res/images/button_pressed_cap_left.png");
153 right = cached(img: ":res/images/button_pressed_cap_right.png");
154 mid = cached(img: ":res/images/button_pressed_stretch.png");
155 } else {
156 left = cached(img: ":res/images/button_normal_cap_left.png");
157 right = cached(img: ":res/images/button_normal_cap_right.png");
158 mid = cached(img: ":res/images/button_normal_stretch.png");
159 }
160 painter->drawPixmap(p: button->rect.topLeft(), pm: left);
161 painter->drawTiledPixmap(rect: QRect(button->rect.x() + left.width(),
162 button->rect.y(),
163 button->rect.width() - left.width() - right.width(),
164 left.height()),
165 pm: mid);
166 painter->drawPixmap(x: button->rect.x() + button->rect.width() - right.width(),
167 y: button->rect.y(),
168 pm: right);
169 if (hover)
170 painter->fillRect(widget->rect().adjusted(xp1: 3,yp1: 5,xp2: -3,yp2: -5), color: QColor(31,127,31,63));
171 painter->restore();
172 }
173 break;
174
175 case PE_FrameGroupBox:
176 if (const QStyleOptionFrame *group
177 = qstyleoption_cast<const QStyleOptionFrame *>(opt: option)) {
178 const QRect &r = group->rect;
179
180 painter->save();
181 int radius = 14;
182 int radius2 = radius*2;
183 QPainterPath clipPath;
184 clipPath.moveTo(x: radius, y: 0);
185 clipPath.arcTo(x: r.right() - radius2, y: 0, w: radius2, h: radius2, startAngle: 90, arcLength: -90);
186 clipPath.arcTo(x: r.right() - radius2, y: r.bottom() - radius2, w: radius2, h: radius2, startAngle: 0, arcLength: -90);
187 clipPath.arcTo(x: r.left(), y: r.bottom() - radius2, w: radius2, h: radius2, startAngle: 270, arcLength: -90);
188 clipPath.arcTo(x: r.left(), y: r.top(), w: radius2, h: radius2, startAngle: 180, arcLength: -90);
189 painter->setClipPath(path: clipPath);
190 QPixmap titleStretch = cached(img: ":res/images/title_stretch.png");
191 QPixmap topLeft = cached(img: ":res/images/groupframe_topleft.png");
192 QPixmap topRight = cached(img: ":res/images/groupframe_topright.png");
193 QPixmap bottomLeft = cached(img: ":res/images/groupframe_bottom_left.png");
194 QPixmap bottomRight = cached(img: ":res/images/groupframe_bottom_right.png");
195 QPixmap leftStretch = cached(img: ":res/images/groupframe_left_stretch.png");
196 QPixmap topStretch = cached(img: ":res/images/groupframe_top_stretch.png");
197 QPixmap rightStretch = cached(img: ":res/images/groupframe_right_stretch.png");
198 QPixmap bottomStretch = cached(img: ":res/images/groupframe_bottom_stretch.png");
199 QLinearGradient lg(0, 0, 0, r.height());
200 lg.setColorAt(pos: 0, color: QColor(224,224,224));
201 lg.setColorAt(pos: 1, color: QColor(255,255,255));
202 painter->setPen(Qt::NoPen);
203 painter->setBrush(lg);
204 painter->drawRect(r: r.adjusted(xp1: 0, yp1: titleStretch.height()/2, xp2: 0, yp2: 0));
205 painter->setClipping(false);
206
207 int topFrameOffset = titleStretch.height()/2 - 2;
208 painter->drawPixmap(p: r.topLeft() + QPoint(0, topFrameOffset), pm: topLeft);
209 painter->drawPixmap(p: r.topRight() - QPoint(topRight.width()-1, 0)
210 + QPoint(0, topFrameOffset), pm: topRight);
211 painter->drawPixmap(p: r.bottomLeft() - QPoint(0, bottomLeft.height()-1), pm: bottomLeft);
212 painter->drawPixmap(p: r.bottomRight() - QPoint(bottomRight.width()-1,
213 bottomRight.height()-1), pm: bottomRight);
214
215 QRect left = r;
216 left.setY(r.y() + topLeft.height() + topFrameOffset);
217 left.setWidth(leftStretch.width());
218 left.setHeight(r.height() - topLeft.height() - bottomLeft.height() - topFrameOffset);
219 painter->drawTiledPixmap(rect: left, pm: leftStretch);
220
221 QRect top = r;
222 top.setX(r.x() + topLeft.width());
223 top.setY(r.y() + topFrameOffset);
224 top.setWidth(r.width() - topLeft.width() - topRight.width());
225 top.setHeight(topLeft.height());
226 painter->drawTiledPixmap(rect: top, pm: topStretch);
227
228 QRect right = r;
229 right.setX(r.right() - rightStretch.width()+1);
230 right.setY(r.y() + topRight.height() + topFrameOffset);
231 right.setWidth(rightStretch.width());
232 right.setHeight(r.height() - topRight.height()
233 - bottomRight.height() - topFrameOffset);
234 painter->drawTiledPixmap(rect: right, pm: rightStretch);
235
236 QRect bottom = r;
237 bottom.setX(r.x() + bottomLeft.width());
238 bottom.setY(r.bottom() - bottomStretch.height()+1);
239 bottom.setWidth(r.width() - bottomLeft.width() - bottomRight.width());
240 bottom.setHeight(bottomLeft.height());
241 painter->drawTiledPixmap(rect: bottom, pm: bottomStretch);
242 painter->restore();
243 }
244 break;
245
246 default:
247 QCommonStyle::drawPrimitive(pe: element, opt: option, p: painter, w: widget);
248 break;
249 }
250 return;
251}
252
253
254void ArthurStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex *option,
255 QPainter *painter, const QWidget *widget) const
256{
257 switch (control) {
258 case CC_Slider:
259 if (const QStyleOptionSlider *slider = qstyleoption_cast<const QStyleOptionSlider *>(opt: option)) {
260 QRect groove = subControlRect(cc: CC_Slider, opt: option, sc: SC_SliderGroove, widget);
261 QRect handle = subControlRect(cc: CC_Slider, opt: option, sc: SC_SliderHandle, widget);
262
263 painter->save();
264
265 bool hover = (slider->state & State_Enabled) && (slider->state & State_MouseOver);
266 if (hover) {
267 QRect moderated = widget->rect().adjusted(xp1: 0, yp1: 4, xp2: 0, yp2: -4);
268 drawHoverRect(painter, r: moderated);
269 }
270
271 if ((option->subControls & SC_SliderGroove) && groove.isValid()) {
272 QPixmap grv = cached(img: ":res/images/slider_bar.png");
273 painter->drawPixmap(r: QRect(groove.x() + 5, groove.y(),
274 groove.width() - 10, grv.height()),
275 pm: grv);
276 }
277 if ((option->subControls & SC_SliderHandle) && handle.isValid()) {
278 QPixmap hndl = cached(img: ":res/images/slider_thumb_on.png");
279 painter->drawPixmap(p: handle.topLeft(), pm: hndl);
280 }
281
282 painter->restore();
283 }
284 break;
285 case CC_GroupBox:
286 if (const QStyleOptionGroupBox *groupBox
287 = qstyleoption_cast<const QStyleOptionGroupBox *>(opt: option)) {
288 QStyleOptionGroupBox groupBoxCopy(*groupBox);
289 groupBoxCopy.subControls &= ~SC_GroupBoxLabel;
290 QCommonStyle::drawComplexControl(cc: control, opt: &groupBoxCopy, p: painter, w: widget);
291
292 if (groupBox->subControls & SC_GroupBoxLabel) {
293 const QRect &r = groupBox->rect;
294 QPixmap titleLeft = cached(img: ":res/images/title_cap_left.png");
295 QPixmap titleRight = cached(img: ":res/images/title_cap_right.png");
296 QPixmap titleStretch = cached(img: ":res/images/title_stretch.png");
297 int txt_width = groupBox->fontMetrics.horizontalAdvance(groupBox->text) + 20;
298 painter->drawPixmap(x: r.center().x() - txt_width/2, y: 0, pm: titleLeft);
299 QRect tileRect = subControlRect(cc: control, opt: groupBox, sc: SC_GroupBoxLabel, widget);
300 painter->drawTiledPixmap(rect: tileRect, pm: titleStretch);
301 painter->drawPixmap(x: tileRect.x() + tileRect.width(), y: 0, pm: titleRight);
302 int opacity = 31;
303 painter->setPen(QColor(0, 0, 0, opacity));
304 painter->drawText(r: tileRect.translated(dx: 0, dy: 1),
305 flags: Qt::AlignVCenter | Qt::AlignHCenter, text: groupBox->text);
306 painter->drawText(r: tileRect.translated(dx: 2, dy: 1),
307 flags: Qt::AlignVCenter | Qt::AlignHCenter, text: groupBox->text);
308 painter->setPen(QColor(0, 0, 0, opacity * 2));
309 painter->drawText(r: tileRect.translated(dx: 1, dy: 1),
310 flags: Qt::AlignVCenter | Qt::AlignHCenter, text: groupBox->text);
311 painter->setPen(Qt::white);
312 painter->drawText(r: tileRect, flags: Qt::AlignVCenter | Qt::AlignHCenter, text: groupBox->text);
313 }
314 }
315 break;
316 default:
317 QCommonStyle::drawComplexControl(cc: control, opt: option, p: painter, w: widget);
318 break;
319 }
320 return;
321}
322
323void ArthurStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option,
324 QPainter *painter, const QWidget *widget) const
325{
326 switch (element) {
327 case CE_RadioButtonLabel:
328 if (const QStyleOptionButton *button
329 = qstyleoption_cast<const QStyleOptionButton *>(opt: option)) {
330
331 if (button->text.isEmpty()) {
332 QCommonStyle::drawControl(element, opt: option, p: painter, w: widget);
333 } else {
334 painter->save();
335 painter->setPen(Qt::black);
336 painter->drawText(r: button->rect, flags: Qt::AlignVCenter, text: button->text);
337 painter->restore();
338 }
339 }
340 break;
341 case CE_PushButtonLabel:
342 if (const QStyleOptionButton *button
343 = qstyleoption_cast<const QStyleOptionButton *>(opt: option)) {
344
345 if (button->text.isEmpty()) {
346 QCommonStyle::drawControl(element, opt: option, p: painter, w: widget);
347 } else {
348 painter->save();
349 painter->setPen(Qt::black);
350 painter->drawText(r: button->rect, flags: Qt::AlignVCenter | Qt::AlignHCenter, text: button->text);
351 painter->restore();
352 }
353 }
354 break;
355 default:
356 QCommonStyle::drawControl(element, opt: option, p: painter, w: widget);
357 break;
358 }
359}
360
361QRect ArthurStyle::subControlRect(ComplexControl control, const QStyleOptionComplex *option,
362 SubControl subControl, const QWidget *widget) const
363{
364 QRect rect;
365
366 switch (control) {
367 default:
368 rect = QCommonStyle::subControlRect(cc: control, opt: option, sc: subControl, w: widget);
369 break;
370 case CC_GroupBox:
371 if (const QStyleOptionGroupBox *group
372 = qstyleoption_cast<const QStyleOptionGroupBox *>(opt: option)) {
373 switch (subControl) {
374 default:
375 rect = QCommonStyle::subControlRect(cc: control, opt: option, sc: subControl, w: widget);
376 break;
377 case SC_GroupBoxContents:
378 rect = QCommonStyle::subControlRect(cc: control, opt: option, sc: subControl, w: widget);
379 rect.adjust(dx1: 0, dy1: -8, dx2: 0, dy2: 0);
380 break;
381 case SC_GroupBoxFrame:
382 rect = group->rect;
383 break;
384 case SC_GroupBoxLabel:
385 QPixmap titleLeft = cached(img: ":res/images/title_cap_left.png");
386 QPixmap titleRight = cached(img: ":res/images/title_cap_right.png");
387 QPixmap titleStretch = cached(img: ":res/images/title_stretch.png");
388 int txt_width = group->fontMetrics.horizontalAdvance(group->text) + 20;
389 rect = QRect(group->rect.center().x() - txt_width/2 + titleLeft.width(), 0,
390 txt_width - titleLeft.width() - titleRight.width(),
391 titleStretch.height());
392 break;
393 }
394 }
395 break;
396 }
397
398 if (control == CC_Slider && subControl == SC_SliderHandle) {
399 rect.setWidth(13);
400 rect.setHeight(27);
401 } else if (control == CC_Slider && subControl == SC_SliderGroove) {
402 rect.setHeight(9);
403 rect.moveTop(pos: 27/2 - 9/2);
404 }
405 return rect;
406}
407
408QSize ArthurStyle::sizeFromContents(ContentsType type, const QStyleOption *option,
409 const QSize &size, const QWidget *widget) const
410{
411 QSize newSize = QCommonStyle::sizeFromContents(ct: type, opt: option, contentsSize: size, widget);
412
413
414 switch (type) {
415 case CT_RadioButton:
416 newSize += QSize(20, 0);
417 break;
418
419 case CT_PushButton:
420 newSize.setHeight(26);
421 break;
422
423 case CT_Slider:
424 newSize.setHeight(27);
425 break;
426
427 default:
428 break;
429 }
430
431 return newSize;
432}
433
434int ArthurStyle::pixelMetric(PixelMetric pm, const QStyleOption *opt, const QWidget *widget) const
435{
436 if (pm == PM_SliderLength)
437 return 13;
438 return QCommonStyle::pixelMetric(m: pm, opt, widget);
439}
440
441void ArthurStyle::polish(QWidget *widget)
442{
443 if (widget->layout() && qobject_cast<QGroupBox *>(object: widget)) {
444 if (widget->findChildren<QGroupBox *>().size() == 0) {
445 widget->layout()->setSpacing(0);
446 widget->layout()->setContentsMargins(left: 12, top: 12, right: 12, bottom: 12);
447 } else {
448 widget->layout()->setContentsMargins(left: 13, top: 13, right: 13, bottom: 13);
449 }
450 }
451
452 if (qobject_cast<QPushButton *>(object: widget)
453 || qobject_cast<QRadioButton *>(object: widget)
454 || qobject_cast<QSlider *>(object: widget)) {
455 widget->setAttribute(Qt::WA_Hover);
456 }
457
458 QPalette pal = widget->palette();
459 if (widget->isWindow()) {
460 pal.setColor(acr: QPalette::Window, acolor: QColor(241, 241, 241));
461 widget->setPalette(pal);
462 }
463
464}
465
466void ArthurStyle::unpolish(QWidget *widget)
467{
468 if (qobject_cast<QPushButton *>(object: widget)
469 || qobject_cast<QRadioButton *>(object: widget)
470 || qobject_cast<QSlider *>(object: widget)) {
471 widget->setAttribute(Qt::WA_Hover, on: false);
472 }
473}
474
475void ArthurStyle::polish(QPalette &palette)
476{
477 palette.setColor(acr: QPalette::Window, acolor: QColor(241, 241, 241));
478}
479
480QRect ArthurStyle::subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const
481{
482 QRect r;
483 switch(element) {
484 case SE_RadioButtonClickRect:
485 r = widget->rect();
486 break;
487 case SE_RadioButtonContents:
488 r = widget->rect().adjusted(xp1: 20, yp1: 0, xp2: 0, yp2: 0);
489 break;
490 default:
491 r = QCommonStyle::subElementRect(r: element, opt: option, widget);
492 break;
493 }
494
495 if (qobject_cast<const QRadioButton*>(object: widget))
496 r = r.adjusted(xp1: 5, yp1: 0, xp2: -5, yp2: 0);
497
498 return r;
499}
500

source code of qtbase/examples/widgets/painting/shared/arthurstyle.cpp