1 | // Copyright (C) 2017 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickfusiondial_p.h" |
5 | |
6 | #include <QtGui/qpainter.h> |
7 | #include <QtGui/private/qmath_p.h> |
8 | #include <QtQuick/private/qquickpalette_p.h> |
9 | #include <QtQuick/private/qquickitem_p.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | QQuickFusionDial::QQuickFusionDial(QQuickItem *parent) |
14 | : QQuickPaintedItem(parent) |
15 | { |
16 | } |
17 | |
18 | bool QQuickFusionDial::highlight() const |
19 | { |
20 | return m_highlight; |
21 | } |
22 | |
23 | void QQuickFusionDial::setHighlight(bool highlight) |
24 | { |
25 | if (m_highlight == highlight) |
26 | return; |
27 | |
28 | m_highlight = highlight; |
29 | update(); |
30 | } |
31 | |
32 | // based on QStyleHelper::drawDial() |
33 | void QQuickFusionDial::paint(QPainter *painter) |
34 | { |
35 | const int width = QQuickItem::width(); |
36 | const int height = QQuickItem::height(); |
37 | if (width <= 0 || height <= 0 || !isVisible()) |
38 | return; |
39 | |
40 | const bool enabled = isEnabled(); |
41 | qreal r = qMin(a: width, b: height) / 2.0; |
42 | r -= r/50; |
43 | const qreal penSize = r/20.0; |
44 | |
45 | painter->setRenderHint(hint: QPainter::Antialiasing); |
46 | |
47 | const qreal d_ = r / 6; |
48 | const qreal dx = d_ + (width - 2 * r) / 2 + 1; |
49 | const qreal dy = d_ + (height - 2 * r) / 2 + 1; |
50 | |
51 | QRectF br = QRectF(dx + 0.5, dy + 0.5, |
52 | int(r * 2 - 2 * d_ - 2), |
53 | int(r * 2 - 2 * d_ - 2)); |
54 | QColor buttonColor = QQuickItemPrivate::get(item: this)->palette()->button().toHsv(); |
55 | buttonColor.setHsv(h: buttonColor .hue(), |
56 | s: qMin(a: 140, b: buttonColor .saturation()), |
57 | v: qMax(a: 180, b: buttonColor.value())); |
58 | |
59 | if (enabled) { |
60 | // Drop shadow |
61 | qreal shadowSize = qMax(a: 1.0, b: penSize/2.0); |
62 | QRectF shadowRect= br.adjusted(xp1: -2*shadowSize, yp1: -2*shadowSize, |
63 | xp2: 2*shadowSize, yp2: 2*shadowSize); |
64 | QRadialGradient shadowGradient(shadowRect.center().x(), |
65 | shadowRect.center().y(), shadowRect.width()/2.0, |
66 | shadowRect.center().x(), shadowRect.center().y()); |
67 | shadowGradient.setColorAt(pos: qreal(0.91), color: QColor(0, 0, 0, 40)); |
68 | shadowGradient.setColorAt(pos: qreal(1.0), color: Qt::transparent); |
69 | painter->setBrush(shadowGradient); |
70 | painter->setPen(Qt::NoPen); |
71 | painter->translate(dx: shadowSize, dy: shadowSize); |
72 | painter->drawEllipse(r: shadowRect); |
73 | painter->translate(dx: -shadowSize, dy: -shadowSize); |
74 | |
75 | // Main gradient |
76 | QRadialGradient gradient(br.center().x() - br.width()/3, dy, |
77 | br.width()*1.3, br.center().x(), |
78 | br.center().y() - br.height()/2); |
79 | gradient.setColorAt(pos: 0, color: buttonColor.lighter(f: 110)); |
80 | gradient.setColorAt(pos: qreal(0.5), color: buttonColor); |
81 | gradient.setColorAt(pos: qreal(0.501), color: buttonColor.darker(f: 102)); |
82 | gradient.setColorAt(pos: 1, color: buttonColor.darker(f: 115)); |
83 | painter->setBrush(gradient); |
84 | } else { |
85 | painter->setBrush(Qt::NoBrush); |
86 | } |
87 | |
88 | painter->setPen(QPen(buttonColor.darker(f: 280))); |
89 | painter->drawEllipse(r: br); |
90 | painter->setBrush(Qt::NoBrush); |
91 | painter->setPen(buttonColor.lighter(f: 110)); |
92 | painter->drawEllipse(r: br.adjusted(xp1: 1, yp1: 1, xp2: -1, yp2: -1)); |
93 | |
94 | if (m_highlight) { |
95 | QColor highlight = QQuickItemPrivate::get(item: this)->palette()->highlight().toHsv(); |
96 | highlight.setHsv(h: highlight.hue(), |
97 | s: qMin(a: 160, b: highlight.saturation()), |
98 | v: qMax(a: 230, b: highlight.value())); |
99 | highlight.setAlpha(127); |
100 | painter->setPen(QPen(highlight, 2.0)); |
101 | painter->setBrush(Qt::NoBrush); |
102 | painter->drawEllipse(r: br.adjusted(xp1: -1, yp1: -1, xp2: 1, yp2: 1)); |
103 | } |
104 | } |
105 | |
106 | QT_END_NAMESPACE |
107 | |
108 | #include "moc_qquickfusiondial_p.cpp" |
109 | |