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 "qquickfusionbusyindicator_p.h" |
5 | |
6 | #include <QtGui/qpainter.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | QQuickFusionBusyIndicator::QQuickFusionBusyIndicator(QQuickItem *parent) |
11 | : QQuickPaintedItem(parent) |
12 | { |
13 | } |
14 | |
15 | QColor QQuickFusionBusyIndicator::color() const |
16 | { |
17 | return m_color; |
18 | } |
19 | |
20 | void QQuickFusionBusyIndicator::setColor(const QColor &color) |
21 | { |
22 | if (color == m_color) |
23 | return; |
24 | |
25 | m_color = color; |
26 | update(); |
27 | } |
28 | |
29 | bool QQuickFusionBusyIndicator::isRunning() const |
30 | { |
31 | return isVisible(); |
32 | } |
33 | |
34 | void QQuickFusionBusyIndicator::setRunning(bool running) |
35 | { |
36 | if (running) { |
37 | setVisible(true); |
38 | update(); |
39 | } |
40 | } |
41 | |
42 | void QQuickFusionBusyIndicator::paint(QPainter *painter) |
43 | { |
44 | const qreal w = width(); |
45 | const qreal h = height(); |
46 | if (w <= 0 || h <= 0 || !isRunning()) |
47 | return; |
48 | |
49 | const qreal sz = qMin(a: w, b: h); |
50 | const qreal dx = (w - sz) / 2; |
51 | const qreal dy = (h - sz) / 2; |
52 | const int hpw = qRound(d: qMax(a: qreal(1), b: sz / 14)) & -1; |
53 | const int pw = 2 * hpw; |
54 | const QRectF bounds(dx + hpw, dy + hpw, sz - pw - 1, sz - pw - 1); |
55 | |
56 | QConicalGradient gradient; |
57 | gradient.setCenter(QPointF(dx + sz / 2, dy + sz / 2)); |
58 | gradient.setColorAt(pos: 0, color: m_color); |
59 | gradient.setColorAt(pos: 0.1, color: m_color); |
60 | gradient.setColorAt(pos: 1, color: Qt::transparent); |
61 | |
62 | painter->translate(dx: 0.5, dy: 0.5); |
63 | painter->setRenderHint(hint: QPainter::Antialiasing, on: true); |
64 | painter->setPen(QPen(gradient, pw, Qt::SolidLine)); |
65 | painter->drawArc(rect: bounds, a: 0, alen: 360 * 16); |
66 | painter->setPen(QPen(m_color, pw, Qt::SolidLine, Qt::RoundCap)); |
67 | painter->drawArc(rect: bounds, a: 0, alen: 20 * 16); |
68 | } |
69 | |
70 | void QQuickFusionBusyIndicator::itemChange(ItemChange change, const ItemChangeData &data) |
71 | { |
72 | QQuickPaintedItem::itemChange(change, data); |
73 | |
74 | if (change == ItemOpacityHasChanged && qFuzzyIsNull(d: data.realValue)) |
75 | setVisible(false); |
76 | } |
77 | |
78 | QT_END_NAMESPACE |
79 | |
80 | #include "moc_qquickfusionbusyindicator_p.cpp" |
81 | |