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
qquickbasicprogressbar.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 <QtCore/qeasingcurve.h>
8#include <QtQuick/private/qquickitem_p.h>
9#include <QtQuick/private/qsgadaptationlayer_p.h>
10#include <QtQuickControls2Impl/private/qquickanimatednode_p.h>
11
13
14static const int Blocks = 4;
15static const int BlockWidth = 16;
16static const int BlockRestingSpacing = 4;
17static const int BlockMovingSpacing = 48;
19static const int QbpbTotalDuration = 4000;
20static const int SecondPhaseStart = QbpbTotalDuration * 0.4;
21static const int ThirdPhaseStart = QbpbTotalDuration * 0.6;
22
23static inline qreal blockStartX(int blockIndex)
24{
25 return ((blockIndex + 1) * -BlockWidth) - (blockIndex * BlockMovingSpacing);
26}
27
28static inline qreal blockRestX(int blockIndex, qreal availableWidth)
29{
30 const qreal spanRightEdgePos = availableWidth / 2 + BlockSpan / 2.0;
31 return spanRightEdgePos - (blockIndex + 1) * BlockWidth - (blockIndex * BlockRestingSpacing);
32}
33
34static inline qreal blockEndX(int blockIndex, qreal availableWidth)
35{
36 return availableWidth - blockStartX(Blocks - 1 - blockIndex) - BlockWidth;
37}
38
40{
41public:
42 QQuickBasicProgressBarNode(QQuickBasicProgressBar *item);
43
45 void sync(QQuickItem *item) override;
46
47private:
48 bool m_indeterminate = false;
49 qreal m_pixelsPerSecond = 0;
50};
51
55{
56 setLoopCount(Infinite);
57 setDuration(QbpbTotalDuration);
58}
59
61{
62 QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild());
63 for (int i = 0; i < Blocks; ++i) {
64 Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType);
65
66 QMatrix4x4 m;
67 const qreal restX = blockRestX(i, m_pixelsPerSecond);
68 const qreal timeInSeconds = time / 1000.0;
69
70 if (time < SecondPhaseStart) {
71 // Move into the resting position for the first phase.
72 QEasingCurve easingCurve(QEasingCurve::InQuad);
73 const qreal easedCompletion = easingCurve.valueForProgress(time / qreal(SecondPhaseStart));
74 const qreal distance = m_pixelsPerSecond * (easedCompletion * (SecondPhaseStart / 1000.0));
75 const qreal position = blockStartX(i) + distance;
76 const qreal destination = restX;
77 m.translate(qMin(position, destination), 0);
78 } else if (time < ThirdPhaseStart) {
79 // Stay in the same position for the second phase.
80 m.translate(restX, 0);
81 } else {
82 // Move out of view for the third phase.
83 const int thirdPhaseSubKickoff = (BlockMovingSpacing / m_pixelsPerSecond) * 1000;
84 const int subphase = (time - ThirdPhaseStart) / thirdPhaseSubKickoff;
85 // If we're not at this subphase yet, don't try to animate movement,
86 // because it will be incorrect.
87 if (subphase < i)
88 return;
89
90 const qreal timeSinceSecondPhase = timeInSeconds - (ThirdPhaseStart / 1000.0);
91 // We only want to start keeping track of time once our subphase has started,
92 // otherwise we move too much because we account for time that has already elapsed.
93 // For example, if we were 60 milliseconds into the third subphase:
94 //
95 // 0 ..... 1 ..... 2 ...
96 // 100 100 60
97 //
98 // i == 0, timeSinceOurKickoff == 260
99 // i == 1, timeSinceOurKickoff == 160
100 // i == 2, timeSinceOurKickoff == 60
101 const qreal timeSinceOurKickoff = timeSinceSecondPhase - (thirdPhaseSubKickoff / 1000.0 * i);
102 const qreal position = restX + (m_pixelsPerSecond * (timeSinceOurKickoff));
103 const qreal destination = blockEndX(i, m_pixelsPerSecond);
104 m.translate(qMin(position, destination), 0);
105 }
106
107 transformNode->setMatrix(m);
108
109 transformNode = static_cast<QSGTransformNode*>(transformNode->nextSibling());
110 }
111}
112
113void QQuickBasicProgressBarNode::sync(QQuickItem *item)
114{
115 QQuickBasicProgressBar *bar = static_cast<QQuickBasicProgressBar *>(item);
116 if (m_indeterminate != bar->isIndeterminate()) {
117 m_indeterminate = bar->isIndeterminate();
118 if (m_indeterminate)
119 start();
120 else
121 stop();
122 }
123 m_pixelsPerSecond = item->width();
124
125 QQuickItemPrivate *d = QQuickItemPrivate::get(item);
126
127 QMatrix4x4 m;
128 m.translate(0, (item->height() - item->implicitHeight()) / 2);
129 setMatrix(m);
130
131 if (m_indeterminate) {
132 if (childCount() != Blocks) {
133 // This was previously a regular progress bar; remove the old nodes.
134 removeAllChildNodes();
135 }
136
137 QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild());
138 for (int i = 0; i < Blocks; ++i) {
139 if (!transformNode) {
140 transformNode = new QSGTransformNode;
141 appendChildNode(transformNode);
142 }
143
144 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode*>(transformNode->firstChild());
145 if (!rectNode) {
146 rectNode = d->sceneGraphContext()->createInternalRectangleNode();
147 transformNode->appendChildNode(rectNode);
148 }
149
150 QMatrix4x4 m;
151 m.translate(blockStartX(i), 0);
152 transformNode->setMatrix(m);
153
154 // Set the color here too in case it was set after component completion,
155 // as updateCurrentTime doesn't sync it.
156 rectNode->setColor(bar->color());
157 rectNode->setRect(QRectF(QPointF(0, 0), QSizeF(BlockWidth, item->implicitHeight())));
158 rectNode->update();
159
160 transformNode = static_cast<QSGTransformNode *>(transformNode->nextSibling());
161 }
162 } else {
163 if (childCount() > 1) {
164 // This was previously an indeterminate progress bar; remove the old nodes.
165 removeAllChildNodes();
166 }
167
168 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(firstChild());
169 if (!rectNode) {
170 rectNode = d->sceneGraphContext()->createInternalRectangleNode();
171 appendChildNode(rectNode);
172 }
173
174 // Always set the color, not just when creating the rectangle node, so that we respect
175 // changes that are made after component completion.
176 rectNode->setColor(bar->color());
177 rectNode->setRect(QRectF(QPointF(0, 0), QSizeF(bar->progress() * item->width(), item->implicitHeight())));
178 rectNode->update();
179 }
180}
181
182QQuickBasicProgressBar::QQuickBasicProgressBar(QQuickItem *parent) :
183 QQuickItem(parent)
184{
185 setFlag(ItemHasContents);
186}
187
188qreal QQuickBasicProgressBar::progress() const
189{
190 return m_progress;
191}
192
193void QQuickBasicProgressBar::setProgress(qreal progress)
194{
195 if (progress == m_progress)
196 return;
197
198 m_progress = progress;
199 update();
200}
201
202bool QQuickBasicProgressBar::isIndeterminate() const
203{
204 return m_indeterminate;
205}
206
207void QQuickBasicProgressBar::setIndeterminate(bool indeterminate)
208{
209 if (indeterminate == m_indeterminate)
210 return;
211
212 m_indeterminate = indeterminate;
213 setClip(m_indeterminate);
214 update();
215}
216
217QColor QQuickBasicProgressBar::color() const
218{
219 return m_color;
220}
221
222void QQuickBasicProgressBar::setColor(const QColor &color)
223{
224 if (color == m_color)
225 return;
226
227 m_color = color;
228 update();
229}
230
231void QQuickBasicProgressBar::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data)
232{
233 QQuickItem::itemChange(change, data);
234 if (change == ItemVisibleHasChanged)
235 update();
236}
237
238QSGNode *QQuickBasicProgressBar::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *)
239{
240 QQuickBasicProgressBarNode *node = static_cast<QQuickBasicProgressBarNode *>(oldNode);
241 if (isVisible() && width() > 0 && height() > 0) {
242 if (!node)
243 node = new QQuickBasicProgressBarNode(this);
244 node->sync(this);
245 } else {
246 delete node;
247 node = nullptr;
248 }
249 return node;
250}
251
252QT_END_NAMESPACE
253
254#include "moc_qquickbasicprogressbar_p.cpp"
QQuickBasicProgressBarNode(QQuickBasicProgressBar *item)
void updateCurrentTime(int time) override
void sync(QQuickItem *item) override
static const int BlockMovingSpacing
static const int BlockRestingSpacing
static const int SecondPhaseStart
static const int BlockWidth
static qreal blockEndX(int blockIndex, qreal availableWidth)
static qreal blockRestX(int blockIndex, qreal availableWidth)
static QT_BEGIN_NAMESPACE const int Blocks
static qreal blockStartX(int blockIndex)
static const int QbpbTotalDuration
static const int BlockSpan
static const int ThirdPhaseStart