| 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 | m_running = running; |
| 37 | |
| 38 | if (m_running) { |
| 39 | setVisible(true); |
| 40 | update(); |
| 41 | } |
| 42 | // Don't set visible to false if not running, because we use an opacity animation (in QML) to hide ourselves. |
| 43 | } |
| 44 | |
| 45 | void QQuickFusionBusyIndicator::paint(QPainter *painter) |
| 46 | { |
| 47 | const qreal w = width(); |
| 48 | const qreal h = height(); |
| 49 | if (w <= 0 || h <= 0 || !isRunning()) |
| 50 | return; |
| 51 | |
| 52 | const qreal sz = qMin(a: w, b: h); |
| 53 | const qreal dx = (w - sz) / 2; |
| 54 | const qreal dy = (h - sz) / 2; |
| 55 | const int hpw = qRound(d: qMax(a: qreal(1), b: sz / 14)) & -1; |
| 56 | const int pw = 2 * hpw; |
| 57 | const QRectF bounds(dx + hpw, dy + hpw, sz - pw - 1, sz - pw - 1); |
| 58 | |
| 59 | QConicalGradient gradient; |
| 60 | gradient.setCenter(QPointF(dx + sz / 2, dy + sz / 2)); |
| 61 | gradient.setColorAt(pos: 0, color: m_color); |
| 62 | gradient.setColorAt(pos: 0.1, color: m_color); |
| 63 | gradient.setColorAt(pos: 1, color: Qt::transparent); |
| 64 | |
| 65 | painter->translate(dx: 0.5, dy: 0.5); |
| 66 | painter->setRenderHint(hint: QPainter::Antialiasing, on: true); |
| 67 | painter->setPen(QPen(gradient, pw, Qt::SolidLine)); |
| 68 | painter->drawArc(rect: bounds, a: 0, alen: 360 * 16); |
| 69 | painter->setPen(QPen(m_color, pw, Qt::SolidLine, Qt::RoundCap)); |
| 70 | painter->drawArc(rect: bounds, a: 0, alen: 20 * 16); |
| 71 | } |
| 72 | |
| 73 | void QQuickFusionBusyIndicator::itemChange(ItemChange change, const ItemChangeData &data) |
| 74 | { |
| 75 | QQuickPaintedItem::itemChange(change, data); |
| 76 | |
| 77 | switch (change) { |
| 78 | case ItemOpacityHasChanged: |
| 79 | // If running is set to false and then true within a short period (QTBUG-85860), our |
| 80 | // OpacityAnimator cancels the 1 => 0 animation (which was for running being set to false), |
| 81 | // setting opacity to 0 and hence visible to false. This happens _after_ setRunning(true) |
| 82 | // was called, because the properties were set synchronously but the animation is |
| 83 | // asynchronous. To account for this situation, we only hide ourselves if we're not running. |
| 84 | if (qFuzzyIsNull(d: data.realValue) && !m_running) |
| 85 | setVisible(false); |
| 86 | break; |
| 87 | case ItemVisibleHasChanged: |
| 88 | update(); |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | QT_END_NAMESPACE |
| 96 | |
| 97 | #include "moc_qquickfusionbusyindicator_p.cpp" |
| 98 | |