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 "qquickfusionbusyindicator_p.h" |
38 | |
39 | #include <QtGui/qpainter.h> |
40 | |
41 | QT_BEGIN_NAMESPACE |
42 | |
43 | QQuickFusionBusyIndicator::QQuickFusionBusyIndicator(QQuickItem *parent) |
44 | : QQuickPaintedItem(parent) |
45 | { |
46 | } |
47 | |
48 | QColor QQuickFusionBusyIndicator::color() const |
49 | { |
50 | return m_color; |
51 | } |
52 | |
53 | void QQuickFusionBusyIndicator::setColor(const QColor &color) |
54 | { |
55 | if (color == m_color) |
56 | return; |
57 | |
58 | m_color = color; |
59 | update(); |
60 | } |
61 | |
62 | bool QQuickFusionBusyIndicator::isRunning() const |
63 | { |
64 | return isVisible(); |
65 | } |
66 | |
67 | void QQuickFusionBusyIndicator::setRunning(bool running) |
68 | { |
69 | if (running) { |
70 | setVisible(true); |
71 | update(); |
72 | } |
73 | } |
74 | |
75 | void QQuickFusionBusyIndicator::paint(QPainter *painter) |
76 | { |
77 | const qreal w = width(); |
78 | const qreal h = height(); |
79 | if (w <= 0 || h <= 0 || !isRunning()) |
80 | return; |
81 | |
82 | const qreal sz = qMin(a: w, b: h); |
83 | const qreal dx = (w - sz) / 2; |
84 | const qreal dy = (h - sz) / 2; |
85 | const int hpw = qRound(d: qMax(a: qreal(1), b: sz / 14)) & -1; |
86 | const int pw = 2 * hpw; |
87 | const QRectF bounds(dx + hpw, dy + hpw, sz - pw - 1, sz - pw - 1); |
88 | |
89 | QConicalGradient gradient; |
90 | gradient.setCenter(QPointF(dx + sz / 2, dy + sz / 2)); |
91 | gradient.setColorAt(pos: 0, color: m_color); |
92 | gradient.setColorAt(pos: 0.1, color: m_color); |
93 | gradient.setColorAt(pos: 1, color: Qt::transparent); |
94 | |
95 | painter->translate(dx: 0.5, dy: 0.5); |
96 | painter->setRenderHint(hint: QPainter::Antialiasing, on: true); |
97 | painter->setPen(QPen(gradient, pw, Qt::SolidLine)); |
98 | painter->drawArc(rect: bounds, a: 0, alen: 360 * 16); |
99 | painter->setPen(QPen(m_color, pw, Qt::SolidLine, Qt::RoundCap)); |
100 | painter->drawArc(rect: bounds, a: 0, alen: 20 * 16); |
101 | } |
102 | |
103 | void QQuickFusionBusyIndicator::itemChange(ItemChange change, const ItemChangeData &data) |
104 | { |
105 | QQuickPaintedItem::itemChange(change, data); |
106 | |
107 | if (change == ItemOpacityHasChanged && qFuzzyIsNull(d: data.realValue)) |
108 | setVisible(false); |
109 | } |
110 | |
111 | QT_END_NAMESPACE |
112 | |