Qt 6.x
The Qt SDK
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
5
6#include <QtCore/qeasingcurve.h>
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 Blocks = 4;
14static const int BlockWidth = 16;
15static const int BlockRestingSpacing = 4;
16static const int BlockMovingSpacing = 48;
18static const int QbpbTotalDuration = 4000;
19static const int SecondPhaseStart = QbpbTotalDuration * 0.4;
20static const int ThirdPhaseStart = QbpbTotalDuration * 0.6;
21
22static inline qreal blockStartX(int blockIndex)
23{
24 return ((blockIndex + 1) * -BlockWidth) - (blockIndex * BlockMovingSpacing);
25}
26
27static inline qreal blockRestX(int blockIndex, qreal availableWidth)
28{
29 const qreal spanRightEdgePos = availableWidth / 2 + BlockSpan / 2.0;
30 return spanRightEdgePos - (blockIndex + 1) * BlockWidth - (blockIndex * BlockRestingSpacing);
31}
32
33static inline qreal blockEndX(int blockIndex, qreal availableWidth)
34{
35 return availableWidth - blockStartX(Blocks - 1 - blockIndex) - BlockWidth;
36}
37
39{
40public:
42
43 void updateCurrentTime(int time) override;
44 void sync(QQuickItem *item) override;
45
46private:
47 bool m_indeterminate = false;
48 qreal m_pixelsPerSecond = 0;
49};
50
53 m_pixelsPerSecond(item->width())
54{
57}
58
60{
61 QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild());
62 for (int i = 0; i < Blocks; ++i) {
63 Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType);
64
66 const qreal restX = blockRestX(i, m_pixelsPerSecond);
67 const qreal timeInSeconds = time / 1000.0;
68
69 if (time < SecondPhaseStart) {
70 // Move into the resting position for the first phase.
72 const qreal easedCompletion = easingCurve.valueForProgress(time / qreal(SecondPhaseStart));
73 const qreal distance = m_pixelsPerSecond * (easedCompletion * (SecondPhaseStart / 1000.0));
75 const qreal destination = restX;
76 m.translate(qMin(position, destination), 0);
77 } else if (time < ThirdPhaseStart) {
78 // Stay in the same position for the second phase.
79 m.translate(restX, 0);
80 } else {
81 // Move out of view for the third phase.
82 const int thirdPhaseSubKickoff = (BlockMovingSpacing / m_pixelsPerSecond) * 1000;
83 const int subphase = (time - ThirdPhaseStart) / thirdPhaseSubKickoff;
84 // If we're not at this subphase yet, don't try to animate movement,
85 // because it will be incorrect.
86 if (subphase < i)
87 return;
88
89 const qreal timeSinceSecondPhase = timeInSeconds - (ThirdPhaseStart / 1000.0);
90 // We only want to start keeping track of time once our subphase has started,
91 // otherwise we move too much because we account for time that has already elapsed.
92 // For example, if we were 60 milliseconds into the third subphase:
93 //
94 // 0 ..... 1 ..... 2 ...
95 // 100 100 60
96 //
97 // i == 0, timeSinceOurKickoff == 260
98 // i == 1, timeSinceOurKickoff == 160
99 // i == 2, timeSinceOurKickoff == 60
100 const qreal timeSinceOurKickoff = timeSinceSecondPhase - (thirdPhaseSubKickoff / 1000.0 * i);
101 const qreal position = restX + (m_pixelsPerSecond * (timeSinceOurKickoff));
102 const qreal destination = blockEndX(i, m_pixelsPerSecond);
103 m.translate(qMin(position, destination), 0);
104 }
105
106 transformNode->setMatrix(m);
107
108 transformNode = static_cast<QSGTransformNode*>(transformNode->nextSibling());
109 }
110}
111
113{
115 if (m_indeterminate != bar->isIndeterminate()) {
116 m_indeterminate = bar->isIndeterminate();
117 if (m_indeterminate)
118 start();
119 else
120 stop();
121 }
122 m_pixelsPerSecond = item->width();
123
125
127 m.translate(0, (item->height() - item->implicitHeight()) / 2);
128 setMatrix(m);
129
130 if (m_indeterminate) {
131 if (childCount() != Blocks) {
132 // This was previously a regular progress bar; remove the old nodes.
134 }
135
136 QSGTransformNode *transformNode = static_cast<QSGTransformNode*>(firstChild());
137 for (int i = 0; i < Blocks; ++i) {
138 if (!transformNode) {
139 transformNode = new QSGTransformNode;
140 appendChildNode(transformNode);
141 }
142
143 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode*>(transformNode->firstChild());
144 if (!rectNode) {
145 rectNode = d->sceneGraphContext()->createInternalRectangleNode();
146 rectNode->setColor(bar->color());
147 transformNode->appendChildNode(rectNode);
148 }
149
151 m.translate(blockStartX(i), 0);
152 transformNode->setMatrix(m);
153
154 rectNode->setRect(QRectF(QPointF(0, 0), QSizeF(BlockWidth, item->implicitHeight())));
155 rectNode->update();
156
157 transformNode = static_cast<QSGTransformNode *>(transformNode->nextSibling());
158 }
159 } else {
160 if (childCount() > 1) {
161 // This was previously an indeterminate progress bar; remove the old nodes.
163 }
164
166 if (!rectNode) {
167 rectNode = d->sceneGraphContext()->createInternalRectangleNode();
168 rectNode->setColor(bar->color());
169 appendChildNode(rectNode);
170 }
171
172 rectNode->setRect(QRectF(QPointF(0, 0), QSizeF(bar->progress() * item->width(), item->implicitHeight())));
173 rectNode->update();
174 }
175}
176
179{
181}
182
184{
185 return m_progress;
186}
187
189{
190 if (progress == m_progress)
191 return;
192
193 m_progress = progress;
194 update();
195}
196
198{
199 return m_indeterminate;
200}
201
203{
204 if (indeterminate == m_indeterminate)
205 return;
206
207 m_indeterminate = indeterminate;
208 setClip(m_indeterminate);
209 update();
210}
211
213{
214 return m_color;
215}
216
218{
219 if (color == m_color)
220 return;
221
222 m_color = color;
223 update();
224}
225
227{
229 if (change == ItemVisibleHasChanged)
230 update();
231}
232
234{
235 QQuickBasicProgressBarNode *node = static_cast<QQuickBasicProgressBarNode *>(oldNode);
236 if (isVisible() && width() > 0 && height() > 0) {
237 if (!node)
238 node = new QQuickBasicProgressBarNode(this);
239 node->sync(this);
240 } else {
241 delete node;
242 node = nullptr;
243 }
244 return node;
245}
246
248
249#include "moc_qquickbasicprogressbar_p.cpp"
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtCore
qreal valueForProgress(qreal progress) const
Return the effective progress for the easing curve at progress.
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
\inmodule QtCore\reentrant
Definition qpoint.h:214
void setLoopCount(int count)
void setDuration(int duration)
QQuickBasicProgressBarNode(QQuickBasicProgressBar *item)
void updateCurrentTime(int time) override
void sync(QQuickItem *item) override
void setIndeterminate(bool indeterminate)
void setColor(const QColor &color)
QQuickBasicProgressBar(QQuickItem *parent=nullptr)
void itemChange(ItemChange change, const ItemChangeData &data) override
Called when change occurs for this item.
QSGNode * updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) override
Called on the render thread when it is time to sync the state of the item with the scene graph.
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
void setFlag(Flag flag, bool enabled=true)
Enables the specified flag for this item if enabled is true; if enabled is false, the flag is disable...
bool isVisible() const
qreal width
This property holds the width of this item.
Definition qquickitem.h:76
virtual void itemChange(ItemChange, const ItemChangeData &)
Called when change occurs for this item.
qreal height
This property holds the height of this item.
Definition qquickitem.h:77
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
Definition qquickitem.h:143
@ ItemVisibleHasChanged
Definition qquickitem.h:147
void setClip(bool)
void update()
Schedules a call to updatePaintNode() for this item.
\inmodule QtCore\reentrant
Definition qrect.h:483
virtual void setRect(const QRectF &rect)=0
virtual void update()=0
virtual void setColor(const QColor &color)=0
\group qtquick-scenegraph-nodes \title Qt Quick Scene Graph Node classes
Definition qsgnode.h:37
QSGNode * nextSibling() const
Returns the node after this in the parent's list of children.
Definition qsgnode.h:107
int childCount() const
Returns the number of child nodes.
Definition qsgnode.cpp:554
void appendChildNode(QSGNode *node)
Appends node to this node's list of children.
Definition qsgnode.cpp:396
@ TransformNodeType
Definition qsgnode.h:42
QSGNode * firstChild() const
Returns the first child of this node.
Definition qsgnode.h:105
NodeType type() const
Returns the type of this node.
Definition qsgnode.h:110
void removeAllChildNodes()
Removes all child nodes from this node's list of children.
Definition qsgnode.cpp:525
The QSGTransformNode class implements transformations in the scene graph.
Definition qsgnode.h:244
void setMatrix(const QMatrix4x4 &matrix)
Sets this transform node's matrix to matrix.
Definition qsgnode.cpp:1160
QSGTransformNode()
Create a new QSGTransformNode with its matrix set to the identity matrix.
Definition qsgnode.cpp:1131
\inmodule QtCore
Definition qsize.h:207
Combined button and popup list for selecting options.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char * destination
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
const GLfloat * m
GLsizei GLsizei GLfloat distance
GLint GLsizei width
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint start
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
static const int SecondPhaseStart
static const int BlockWidth
static const int ThirdPhaseStart
static const int BlockSpan
static const int QbpbTotalDuration
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 BlockRestingSpacing
static const int BlockMovingSpacing
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
double qreal
Definition qtypes.h:92
QGraphicsItem * item
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent
\inmodule QtQuick
Definition qquickitem.h:158