Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qquickmaterialprogressbar.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/qmath.h>
7#include <QtCore/qeasingcurve.h>
8#include <QtQuick/private/qquickitem_p.h>
9#include <QtQuick/private/qsgadaptationlayer_p.h>
10#include <QtQuick/qsgrectanglenode.h>
11#include <QtQuick/qsgimagenode.h>
12#include <QtQuick/qquickwindow.h>
13#include <QtQuickControls2Impl/private/qquickanimatednode_p.h>
14
16
17static const int PauseDuration = 520;
18static const int SlideDuration = 1240;
20
22{
23public:
25
26 void updateCurrentTime(int time) override;
27 void sync(QQuickItem *item) override;
28
29private:
30 void moveNode(QSGTransformNode *node, const QRectF &geometry, qreal progress);
31
32 bool m_indeterminate = false;
34};
35
42
44{
45 QSGRectangleNode *geometryNode = static_cast<QSGRectangleNode *>(firstChild());
46 Q_ASSERT(geometryNode->type() == QSGNode::GeometryNodeType);
47 const QRectF geometry = geometryNode->rect();
48
49 QSGTransformNode *firstNode = static_cast<QSGTransformNode *>(geometryNode->firstChild());
50 if (firstNode) {
52
53 const qreal progress = qMin<qreal>(1.0, static_cast<qreal>(time) / SlideDuration);
54 moveNode(static_cast<QSGTransformNode *>(firstNode), geometry, progress);
55 }
56
57 QSGTransformNode *secondNode = static_cast<QSGTransformNode *>(geometryNode->lastChild());
58 if (secondNode) {
59 Q_ASSERT(secondNode->type() == QSGNode::TransformNodeType);
60
61 const qreal progress = qMax<qreal>(0.0, static_cast<qreal>(time - PauseDuration) / SlideDuration);
62 moveNode(static_cast<QSGTransformNode *>(secondNode), geometry, progress);
63 }
64}
65
67{
69 if (m_indeterminate != bar->isIndeterminate()) {
70 m_indeterminate = bar->isIndeterminate();
71 if (m_indeterminate)
72 start();
73 else
74 stop();
75 }
76
78
79 QRectF bounds = item->boundingRect();
80 bounds.setHeight(item->implicitHeight());
81 bounds.moveTop((item->height() - bounds.height()) / 2.0);
82
83 QSGRectangleNode *geometryNode = static_cast<QSGRectangleNode *>(firstChild());
84 if (!geometryNode) {
85 geometryNode = item->window()->createRectangleNode();
86 geometryNode->setColor(Qt::transparent);
87 appendChildNode(geometryNode);
88 }
89 geometryNode->setRect(bounds);
90
91 const int count = m_indeterminate ? 2 : 1;
92 const qreal w = m_indeterminate ? 0 : bar->progress() * item->width();
93 const QRectF rect(0, bounds.y(), w, bounds.height());
94
95 QSGNode *transformNode = geometryNode->firstChild();
96 for (int i = 0; i < count; ++i) {
97 if (!transformNode) {
98 transformNode = new QSGTransformNode;
99 geometryNode->appendChildNode(transformNode);
100
101 QSGInternalRectangleNode *rectNode = d->sceneGraphContext()->createInternalRectangleNode();
102 rectNode->setAntialiasing(true);
103 transformNode->appendChildNode(rectNode);
104 }
105 Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType);
106 static_cast<QSGTransformNode *>(transformNode)->setMatrix(QMatrix4x4());
107
108 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(transformNode->firstChild());
109 Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType);
110
111 rectNode->setRect(rect);
112 rectNode->setColor(bar->color());
113 rectNode->update();
114
115 transformNode = transformNode->nextSibling();
116 }
117
118 while (transformNode) {
119 QSGNode *nextSibling = transformNode->nextSibling();
120 delete transformNode;
121 transformNode = nextSibling;
122 }
123}
124
125void QQuickMaterialProgressBarNode::moveNode(QSGTransformNode *transformNode, const QRectF &geometry, qreal progress)
126{
127 const qreal value = m_easing.valueForProgress(progress);
128 const qreal x = value * geometry.width();
129
131 matrix.translate(x, 0);
132 transformNode->setMatrix(matrix);
133
134 QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(transformNode->firstChild());
135 Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType);
136
137 QRectF r = geometry;
138 r.setWidth(value * (geometry.width() - x));
139 rectNode->setRect(r);
140 rectNode->update();
141}
142
148
150{
151 return m_color;
152}
153
155{
156 if (color == m_color)
157 return;
158
159 m_color = color;
160 update();
161}
162
164{
165 return m_progress;
166}
167
169{
170 if (progress == m_progress)
171 return;
172
173 m_progress = progress;
174 update();
175}
176
178{
179 return m_indeterminate;
180}
181
183{
184 if (indeterminate == m_indeterminate)
185 return;
186
187 m_indeterminate = indeterminate;
188 update();
189}
190
197
199{
200 QQuickMaterialProgressBarNode *node = static_cast<QQuickMaterialProgressBarNode *>(oldNode);
201 if (isVisible() && width() > 0 && height() > 0) {
202 if (!node)
203 node = new QQuickMaterialProgressBarNode(this);
204 node->sync(this);
205 } else {
206 delete node;
207 node = nullptr;
208 }
209 return node;
210}
211
213
214#include "moc_qquickmaterialprogressbar_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.
QGraphicsWidget * window() const
virtual QRectF boundingRect() const =0
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be ...
The QMatrix4x4 class represents a 4x4 transformation matrix in 3D space.
Definition qmatrix4x4.h:25
void translate(const QVector3D &vector)
Multiplies this matrix by another that translates coordinates by the components of vector.
void setLoopCount(int count)
void setDuration(int duration)
static QQuickItemPrivate * get(QQuickItem *item)
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:63
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:75
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:76
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
Definition qquickitem.h:144
@ ItemVisibleHasChanged
Definition qquickitem.h:148
void update()
Schedules a call to updatePaintNode() for this item.
QQuickMaterialProgressBarNode(QQuickMaterialProgressBar *item)
void sync(QQuickItem *item) override
void itemChange(ItemChange change, const ItemChangeData &data) override
Called when change occurs for this item.
void setIndeterminate(bool indeterminate)
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.
QQuickMaterialProgressBar(QQuickItem *parent=nullptr)
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr qreal y() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:672
constexpr qreal height() const noexcept
Returns the height of the rectangle.
Definition qrect.h:732
constexpr qreal width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:729
constexpr void setWidth(qreal w) noexcept
Sets the width of the rectangle to the given finite width.
Definition qrect.h:818
constexpr void setHeight(qreal h) noexcept
Sets the height of the rectangle to the given finite height.
Definition qrect.h:821
constexpr void moveTop(qreal pos) noexcept
Moves the rectangle vertically, leaving the rectangle's top line at the given finite y coordinate.
Definition qrect.h:705
virtual void setAntialiasing(bool antialiasing)
\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
void appendChildNode(QSGNode *node)
Appends node to this node's list of children.
Definition qsgnode.cpp:398
@ TransformNodeType
Definition qsgnode.h:42
@ GeometryNodeType
Definition qsgnode.h:41
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
The QSGRectangleNode class is a convenience class for drawing solid filled rectangles using scenegrap...
The QSGTransformNode class implements transformations in the scene graph.
Definition qsgnode.h:241
void setMatrix(const QMatrix4x4 &matrix)
Sets this transform node's matrix to matrix.
Definition qsgnode.cpp:1162
const QMatrix4x4 & matrix() const
Returns this transform node's matrix.
Definition qsgnode.h:247
QSGTransformNode()
Create a new QSGTransformNode with its matrix set to the identity matrix.
Definition qsgnode.cpp:1133
rect
[4]
Combined button and popup list for selecting options.
@ transparent
Definition qnamespace.h:47
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLint GLint GLint GLint GLint x
[0]
GLfloat GLfloat GLfloat w
[0]
GLboolean r
[2]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLuint color
[2]
GLuint start
GLuint GLenum matrix
static const int QmpbTotalDuration
static const int SlideDuration
static QT_BEGIN_NAMESPACE const int PauseDuration
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
double qreal
Definition qtypes.h:187
QGraphicsItem * item
\inmodule QtQuick
Definition qquickitem.h:159