1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickfusiondial_p.h"
38
39#include <QtGui/qpainter.h>
40#include <QtGui/private/qmath_p.h>
41
42QT_BEGIN_NAMESPACE
43
44QQuickFusionDial::QQuickFusionDial(QQuickItem *parent)
45 : QQuickPaintedItem(parent)
46{
47}
48
49bool QQuickFusionDial::highlight() const
50{
51 return m_highlight;
52}
53
54void QQuickFusionDial::setHighlight(bool highlight)
55{
56 if (m_highlight == highlight)
57 return;
58
59 m_highlight = highlight;
60 update();
61}
62
63QPalette QQuickFusionDial::palette() const
64{
65 return m_palette;
66}
67
68void QQuickFusionDial::setPalette(const QPalette &palette)
69{
70 if (palette == m_palette)
71 return;
72
73 m_palette = palette;
74 update();
75}
76
77// based on QStyleHelper::drawDial()
78void QQuickFusionDial::paint(QPainter *painter)
79{
80 const int width = QQuickItem::width();
81 const int height = QQuickItem::height();
82 if (width <= 0 || height <= 0 || !isVisible())
83 return;
84
85 QColor buttonColor = m_palette.button().color();
86 const bool enabled = isEnabled();
87 qreal r = qMin(a: width, b: height) / 2.0;
88 r -= r/50;
89 const qreal penSize = r/20.0;
90
91 painter->setRenderHint(hint: QPainter::Antialiasing);
92
93 const qreal d_ = r / 6;
94 const qreal dx = d_ + (width - 2 * r) / 2 + 1;
95 const qreal dy = d_ + (height - 2 * r) / 2 + 1;
96
97 QRectF br = QRectF(dx + 0.5, dy + 0.5,
98 int(r * 2 - 2 * d_ - 2),
99 int(r * 2 - 2 * d_ - 2));
100 buttonColor.setHsv(h: buttonColor .hue(),
101 s: qMin(a: 140, b: buttonColor .saturation()),
102 v: qMax(a: 180, b: buttonColor.value()));
103
104 if (enabled) {
105 // Drop shadow
106 qreal shadowSize = qMax(a: 1.0, b: penSize/2.0);
107 QRectF shadowRect= br.adjusted(xp1: -2*shadowSize, yp1: -2*shadowSize,
108 xp2: 2*shadowSize, yp2: 2*shadowSize);
109 QRadialGradient shadowGradient(shadowRect.center().x(),
110 shadowRect.center().y(), shadowRect.width()/2.0,
111 shadowRect.center().x(), shadowRect.center().y());
112 shadowGradient.setColorAt(pos: qreal(0.91), color: QColor(0, 0, 0, 40));
113 shadowGradient.setColorAt(pos: qreal(1.0), color: Qt::transparent);
114 painter->setBrush(shadowGradient);
115 painter->setPen(Qt::NoPen);
116 painter->translate(dx: shadowSize, dy: shadowSize);
117 painter->drawEllipse(r: shadowRect);
118 painter->translate(dx: -shadowSize, dy: -shadowSize);
119
120 // Main gradient
121 QRadialGradient gradient(br.center().x() - br.width()/3, dy,
122 br.width()*1.3, br.center().x(),
123 br.center().y() - br.height()/2);
124 gradient.setColorAt(pos: 0, color: buttonColor.lighter(f: 110));
125 gradient.setColorAt(pos: qreal(0.5), color: buttonColor);
126 gradient.setColorAt(pos: qreal(0.501), color: buttonColor.darker(f: 102));
127 gradient.setColorAt(pos: 1, color: buttonColor.darker(f: 115));
128 painter->setBrush(gradient);
129 } else {
130 painter->setBrush(Qt::NoBrush);
131 }
132
133 painter->setPen(QPen(buttonColor.darker(f: 280)));
134 painter->drawEllipse(r: br);
135 painter->setBrush(Qt::NoBrush);
136 painter->setPen(buttonColor.lighter(f: 110));
137 painter->drawEllipse(r: br.adjusted(xp1: 1, yp1: 1, xp2: -1, yp2: -1));
138
139 if (m_highlight) {
140 QColor highlight = m_palette.highlight().color();
141 highlight.setHsv(h: highlight.hue(),
142 s: qMin(a: 160, b: highlight.saturation()),
143 v: qMax(a: 230, b: highlight.value()));
144 highlight.setAlpha(127);
145 painter->setPen(QPen(highlight, 2.0));
146 painter->setBrush(Qt::NoBrush);
147 painter->drawEllipse(r: br.adjusted(xp1: -1, yp1: -1, xp2: 1, yp2: 1));
148 }
149}
150
151QT_END_NAMESPACE
152

source code of qtquickcontrols2/src/imports/controls/fusion/qquickfusiondial.cpp