Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickbasicbusyindicator.cpp
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
6
7#include <QtQuick/private/qquickitem_p.h>
8#include <QtQuick/private/qsgadaptationlayer_p.h>
9#include <QtQuickControls2Impl/private/qquickanimatednode_p.h>
10
12
13static const int CircleCount = 10;
14static const int QbbiTotalDuration = 100 * CircleCount * 2;
15static const QRgb TransparentColor = 0x00000000;
16
17static QPointF moveCircle(const QPointF &pos, qreal rotation, qreal distance)
18{
19 return pos - QTransform().rotate(rotation).map(QPointF(0, distance));
20}
21
23{
24public:
25 QQuickBasicBusyIndicatorNode(QQuickBasicBusyIndicator *item);
26
28 void sync(QQuickItem *item) override;
29
30private:
31 QColor m_pen;
32 QColor m_fill;
33};
34
37{
38 setLoopCount(Infinite);
39 setDuration(QbbiTotalDuration);
40 setCurrentTime(item->elapsed());
41
42 for (int i = 0; i < CircleCount; ++i) {
43 QSGTransformNode *transformNode = new QSGTransformNode;
44 appendChildNode(transformNode);
45
46 QQuickItemPrivate *d = QQuickItemPrivate::get(item);
47 QSGInternalRectangleNode *rectNode = d->sceneGraphContext()->createInternalRectangleNode();
48 rectNode->setAntialiasing(true);
49 transformNode->appendChildNode(rectNode);
50 }
51}
52
54{
55 const qreal percentageComplete = time / qreal(QbbiTotalDuration);
56 const qreal firstPhaseProgress = percentageComplete <= 0.5 ? percentageComplete * 2 : 0;
57 const qreal secondPhaseProgress = percentageComplete > 0.5 ? (percentageComplete - 0.5) * 2 : 0;
58
59 QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild());
60 Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType);
61 for (int i = 0; i < CircleCount; ++i) {
62 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode*>(transformNode->firstChild());
63 Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType);
64
65 const bool fill = (firstPhaseProgress > qreal(i) / CircleCount) || (secondPhaseProgress > 0 && secondPhaseProgress < qreal(i) / CircleCount);
66 rectNode->setColor(fill ? m_fill : QColor::fromRgba(TransparentColor));
67 rectNode->setPenColor(m_pen);
68 rectNode->setPenWidth(1);
69 rectNode->update();
70
71 transformNode = static_cast<QSGTransformNode*>(transformNode->nextSibling());
72 }
73}
74
75void QQuickBasicBusyIndicatorNode::sync(QQuickItem *item)
76{
77 const qreal w = item->width();
78 const qreal h = item->height();
79 const qreal sz = qMin(w, h);
80 const qreal dx = (w - sz) / 2;
81 const qreal dy = (h - sz) / 2;
82 const int circleRadius = sz / 12;
83
84 m_pen = static_cast<QQuickBasicBusyIndicator *>(item)->pen();
85 m_fill = static_cast<QQuickBasicBusyIndicator *>(item)->fill();
86
87 QSGTransformNode *transformNode = static_cast<QSGTransformNode *>(firstChild());
88 for (int i = 0; i < CircleCount; ++i) {
89 Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType);
90
91 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(transformNode->firstChild());
92 Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType);
93
94 QPointF pos = QPointF(sz / 2 - circleRadius, sz / 2 - circleRadius);
95 pos = moveCircle(pos, 360.0 / CircleCount * i, sz / 2 - circleRadius);
96
97 QMatrix4x4 m;
98 m.translate(dx + pos.x(), dy + pos.y());
99 transformNode->setMatrix(m);
100
101 rectNode->setRect(QRectF(QPointF(), QSizeF(circleRadius * 2, circleRadius * 2)));
102 rectNode->setRadius(circleRadius);
103
104 transformNode = static_cast<QSGTransformNode *>(transformNode->nextSibling());
105 }
106}
107
108QQuickBasicBusyIndicator::QQuickBasicBusyIndicator(QQuickItem *parent) :
109 QQuickItem(parent)
110{
111 setFlag(ItemHasContents);
112}
113
114QColor QQuickBasicBusyIndicator::pen() const
115{
116 return m_pen;
117}
118
119void QQuickBasicBusyIndicator::setPen(const QColor &pen)
120{
121 if (pen == m_pen)
122 return;
123
124 m_pen = pen;
125 update();
126}
127
128QColor QQuickBasicBusyIndicator::fill() const
129{
130 return m_fill;
131}
132
133void QQuickBasicBusyIndicator::setFill(const QColor &fill)
134{
135 if (fill == m_fill)
136 return;
137
138 m_fill = fill;
139 update();
140}
141
142bool QQuickBasicBusyIndicator::isRunning() const
143{
144 return isVisible();
145}
146
147void QQuickBasicBusyIndicator::setRunning(bool running)
148{
149 m_running = running;
150
151 if (m_running)
152 setVisible(true);
153 // Don't set visible to false if not running, because we use an opacity animation (in QML) to hide ourselves.
154}
155
156int QQuickBasicBusyIndicator::elapsed() const
157{
158 return m_elapsed;
159}
160
161void QQuickBasicBusyIndicator::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
162{
163 QQuickItem::itemChange(change, data);
164 switch (change) {
165 case ItemOpacityHasChanged:
166 // If running is set to false and then true within a short period (QTBUG-85860), our
167 // OpacityAnimator cancels the 1 => 0 animation (which was for running being set to false),
168 // setting opacity to 0 and hence visible to false. This happens _after_ setRunning(true)
169 // was called, because the properties were set synchronously but the animation is
170 // asynchronous. To account for this situation, we only hide ourselves if we're not running.
171 if (qFuzzyIsNull(data.realValue) && !m_running)
172 setVisible(false);
173 break;
174 case ItemVisibleHasChanged:
175 update();
176 break;
177 default:
178 break;
179 }
180}
181
182QSGNode *QQuickBasicBusyIndicator::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
183{
184 QQuickBasicBusyIndicatorNode *node = static_cast<QQuickBasicBusyIndicatorNode *>(oldNode);
185 if (isRunning() && width() > 0 && height() > 0) {
186 if (!node) {
187 node = new QQuickBasicBusyIndicatorNode(this);
188 node->start();
189 }
190 node->sync(this);
191 } else {
192 m_elapsed = node ? node->currentTime() : 0;
193 delete node;
194 node = nullptr;
195 }
196 return node;
197}
198
199QT_END_NAMESPACE
200
201#include "moc_qquickbasicbusyindicator_p.cpp"
void sync(QQuickItem *item) override
void updateCurrentTime(int time) override
QQuickBasicBusyIndicatorNode(QQuickBasicBusyIndicator *item)
static const int QbbiTotalDuration
static QT_BEGIN_NAMESPACE const int CircleCount
static QPointF moveCircle(const QPointF &pos, qreal rotation, qreal distance)
static const QRgb TransparentColor