diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-10-17 22:04:39 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-10-18 14:42:47 +0000 |
commit | 4077c605ab4c971ccabc37001875cfd19a744e98 (patch) | |
tree | e0800b6d1bd534582be89078c6f6d7a0c85d3d92 /src/imports/controls | |
parent | 65b690d82fb70e2960a5d2c40a0dac9707718b15 (diff) |
Universal: rewrite the busy indicator animation
Use a simple animated node instead of using the private animator API.
Task-number: QTBUG-56601
Change-Id: I0ff6c8b55bc4a0a991bcbef8fc18086226beb6b2
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/controls')
-rw-r--r-- | src/imports/controls/universal/BusyIndicator.qml | 10 | ||||
-rw-r--r-- | src/imports/controls/universal/qquickuniversalbusyindicator.cpp (renamed from src/imports/controls/universal/qquickuniversalprogressring.cpp) | 201 | ||||
-rw-r--r-- | src/imports/controls/universal/qquickuniversalbusyindicator_p.h (renamed from src/imports/controls/universal/qquickuniversalprogressring_p.h) | 35 | ||||
-rw-r--r-- | src/imports/controls/universal/qtquickcontrols2universalstyleplugin.cpp | 5 | ||||
-rw-r--r-- | src/imports/controls/universal/universal.pri | 4 |
5 files changed, 114 insertions, 141 deletions
diff --git a/src/imports/controls/universal/BusyIndicator.qml b/src/imports/controls/universal/BusyIndicator.qml index 514a233e..180396f2 100644 --- a/src/imports/controls/universal/BusyIndicator.qml +++ b/src/imports/controls/universal/BusyIndicator.qml @@ -45,9 +45,7 @@ T.BusyIndicator { implicitWidth: contentItem.implicitWidth + leftPadding + rightPadding implicitHeight: contentItem.implicitHeight + topPadding + bottomPadding - contentItem: ProgressRing { - id: ring - + contentItem: BusyIndicatorImpl { implicitWidth: 20 implicitHeight: 20 @@ -55,10 +53,6 @@ T.BusyIndicator { count: size < 60 ? 5 : 6 // "Small" vs. "Large" color: control.Universal.accent - - ProgressRingAnimator { - target: ring - running: control.visible && control.running - } + visible: control.running } } diff --git a/src/imports/controls/universal/qquickuniversalprogressring.cpp b/src/imports/controls/universal/qquickuniversalbusyindicator.cpp index 1d733fed..acefc5e3 100644 --- a/src/imports/controls/universal/qquickuniversalprogressring.cpp +++ b/src/imports/controls/universal/qquickuniversalbusyindicator.cpp @@ -34,12 +34,12 @@ ** ****************************************************************************/ -#include "qquickuniversalprogressring_p.h" +#include "qquickuniversalbusyindicator_p.h" #include <QtCore/qmath.h> #include <QtCore/qeasingcurve.h> +#include <QtCore/qelapsedtimer.h> #include <QtQuick/private/qquickitem_p.h> -#include <QtQuick/private/qquickanimatorjob_p.h> #include <QtQuick/private/qsgadaptationlayer_p.h> QT_BEGIN_NAMESPACE @@ -48,16 +48,15 @@ static const int PhaseCount = 6; static const int Interval = 167; static const int TotalDuration = 4052; -class QQuickUniversalProgressRingAnimatorJob : public QQuickAnimatorJob +class QQuickUniversalBusyIndicatorNode : public QObject, public QSGTransformNode { public: - QQuickUniversalProgressRingAnimatorJob(); + QQuickUniversalBusyIndicatorNode(QQuickUniversalBusyIndicator *item); - void initialize(QQuickAnimatorController *controller) override; - void updateCurrentTime(int time) override; - void writeBack() override; - void nodeWasDestroyed() override; - void afterNodeSync() override; + int elapsed() const; + + void animate(); + void sync(QQuickUniversalBusyIndicator *item); private: struct Phase { @@ -69,12 +68,18 @@ private: QEasingCurve curve; }; - QSGNode *m_node; + int m_offset; + QElapsedTimer m_timer; Phase m_phases[PhaseCount]; }; -QQuickUniversalProgressRingAnimatorJob::QQuickUniversalProgressRingAnimatorJob() : m_node(nullptr) +QQuickUniversalBusyIndicatorNode::QQuickUniversalBusyIndicatorNode(QQuickUniversalBusyIndicator *item) + : m_offset(item->elapsed()) { + QQuickWindow *window = item->window(); + connect(window, &QQuickWindow::frameSwapped, window, &QQuickWindow::update); + connect(window, &QQuickWindow::beforeRendering, this, &QQuickUniversalBusyIndicatorNode::animate); + m_phases[0] = Phase(433, -110, 10, QEasingCurve::BezierSpline); m_phases[1] = Phase(767, 10, 93, QEasingCurve::Linear ); m_phases[2] = Phase(417, 93, 205, QEasingCurve::BezierSpline); @@ -86,27 +91,26 @@ QQuickUniversalProgressRingAnimatorJob::QQuickUniversalProgressRingAnimatorJob() m_phases[2].curve.addCubicBezierSegment(QPointF(0.57, 0.17), QPointF(0.95, 0.75), QPointF(1.00, 1.00)); m_phases[3].curve.addCubicBezierSegment(QPointF(0.00, 0.19), QPointF(0.07, 0.72), QPointF(1.00, 1.00)); m_phases[5].curve.addCubicBezierSegment(QPointF(0.00, 0.00), QPointF(0.95, 0.37), QPointF(1.00, 1.00)); + + m_timer.restart(); } -void QQuickUniversalProgressRingAnimatorJob::initialize(QQuickAnimatorController *controller) +int QQuickUniversalBusyIndicatorNode::elapsed() const { - QQuickAnimatorJob::initialize(controller); - m_node = QQuickItemPrivate::get(m_target)->childContainerNode(); + return m_timer.elapsed() + m_offset; } -void QQuickUniversalProgressRingAnimatorJob::updateCurrentTime(int time) +void QQuickUniversalBusyIndicatorNode::animate() { - if (!m_node) - return; - - QSGNode *containerNode = m_node->firstChild(); - Q_ASSERT(!containerNode || containerNode->type() == QSGNode::TransformNodeType); - if (!containerNode) - return; + qint64 time = m_timer.elapsed() + m_offset; + if (time >= TotalDuration) { + m_timer.restart(); + m_offset = 0; + } int nodeIndex = 0; - int count = containerNode->childCount(); - QSGTransformNode *transformNode = static_cast<QSGTransformNode *>(containerNode->firstChild()); + int count = childCount(); + QSGTransformNode *transformNode = static_cast<QSGTransformNode *>(firstChild()); while (transformNode) { Q_ASSERT(transformNode->type() == QSGNode::TransformNodeType); @@ -147,126 +151,115 @@ void QQuickUniversalProgressRingAnimatorJob::updateCurrentTime(int time) } } -void QQuickUniversalProgressRingAnimatorJob::writeBack() +void QQuickUniversalBusyIndicatorNode::sync(QQuickUniversalBusyIndicator *item) { -} + QQuickItemPrivate *d = QQuickItemPrivate::get(item); -void QQuickUniversalProgressRingAnimatorJob::nodeWasDestroyed() -{ - m_node = nullptr; -} + QMatrix4x4 matrix; + matrix.translate(item->width() / 2, item->height() / 2); + setMatrix(matrix); -void QQuickUniversalProgressRingAnimatorJob::afterNodeSync() -{ - m_node = QQuickItemPrivate::get(m_target)->childContainerNode(); -} + qreal size = qMin(item->width(), item->height()); + qreal diameter = size / 10.0; + qreal radius = diameter / 2; + qreal offset = (size - diameter * 2) / M_PI; + const QRectF rect(offset, offset, diameter, diameter); -QQuickUniversalProgressRingAnimator::QQuickUniversalProgressRingAnimator(QObject *parent) - : QQuickAnimator(parent) -{ - setDuration(TotalDuration); - setLoops(QQuickAnimator::Infinite); -} + int count = item->count(); + QSGNode *transformNode = firstChild(); + for (int i = 0; i < count; ++i) { + if (!transformNode) { + transformNode = new QSGTransformNode; + appendChildNode(transformNode); -QString QQuickUniversalProgressRingAnimator::propertyName() const -{ - return QString(); -} + QSGOpacityNode *opacityNode = new QSGOpacityNode; + transformNode->appendChildNode(opacityNode); -QQuickAnimatorJob *QQuickUniversalProgressRingAnimator::createJob() const -{ - return new QQuickUniversalProgressRingAnimatorJob; + QSGInternalRectangleNode *rectNode = d->sceneGraphContext()->createInternalRectangleNode(); + rectNode->setAntialiasing(true); + opacityNode->appendChildNode(rectNode); + } + + QSGNode *opacityNode = transformNode->firstChild(); + Q_ASSERT(opacityNode->type() == QSGNode::OpacityNodeType); + + QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(opacityNode->firstChild()); + Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType); + + rectNode->setRect(rect); + rectNode->setColor(item->color()); + rectNode->setRadius(radius); + rectNode->update(); + + transformNode = transformNode->nextSibling(); + } + + while (transformNode) { + QSGNode *nextSibling = transformNode->nextSibling(); + delete transformNode; + transformNode = nextSibling; + } } -QQuickUniversalProgressRing::QQuickUniversalProgressRing(QQuickItem *parent) - : QQuickItem(parent), m_count(5), m_color(Qt::black) +QQuickUniversalBusyIndicator::QQuickUniversalBusyIndicator(QQuickItem *parent) + : QQuickItem(parent), m_count(5), m_elapsed(0), m_color(Qt::black) { setFlag(ItemHasContents); } -int QQuickUniversalProgressRing::count() const +int QQuickUniversalBusyIndicator::count() const { return m_count; } -void QQuickUniversalProgressRing::setCount(int count) +void QQuickUniversalBusyIndicator::setCount(int count) { if (m_count == count) return; m_count = count; update(); - emit countChanged(); } -QColor QQuickUniversalProgressRing::color() const +QColor QQuickUniversalBusyIndicator::color() const { return m_color; } -void QQuickUniversalProgressRing::setColor(const QColor &color) +void QQuickUniversalBusyIndicator::setColor(const QColor &color) { if (m_color == color) return; m_color = color; update(); - emit colorChanged(); } -QSGNode *QQuickUniversalProgressRing::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) +int QQuickUniversalBusyIndicator::elapsed() const { - QQuickItemPrivate *d = QQuickItemPrivate::get(this); - - if (!oldNode) - oldNode = new QSGTransformNode; - Q_ASSERT(oldNode->type() == QSGNode::TransformNodeType); - - QMatrix4x4 matrix; - matrix.translate(width() / 2, height() / 2); - static_cast<QSGTransformNode *>(oldNode)->setMatrix(matrix); - - qreal size = qMin(width(), height()); - qreal diameter = size / 10.0; - qreal radius = diameter / 2; - qreal offset = (size - diameter * 2) / M_PI; - const QRectF rect(offset, offset, diameter, diameter); - - QSGNode *transformNode = oldNode->firstChild(); - for (int i = 0; i < m_count; ++i) { - if (!transformNode) { - transformNode = new QSGTransformNode; - oldNode->appendChildNode(transformNode); - - QSGOpacityNode *opacityNode = new QSGOpacityNode; - transformNode->appendChildNode(opacityNode); - - QSGInternalRectangleNode *rectNode = d->sceneGraphContext()->createInternalRectangleNode(); - rectNode->setAntialiasing(true); - opacityNode->appendChildNode(rectNode); - } - - QSGNode *opacityNode = transformNode->firstChild(); - Q_ASSERT(opacityNode->type() == QSGNode::OpacityNodeType); - - QSGInternalRectangleNode *rectNode = static_cast<QSGInternalRectangleNode *>(opacityNode->firstChild()); - Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType); - - rectNode->setRect(rect); - rectNode->setColor(m_color); - rectNode->setRadius(radius); - rectNode->update(); + return m_elapsed; +} - transformNode = transformNode->nextSibling(); - } +void QQuickUniversalBusyIndicator::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &data) +{ + QQuickItem::itemChange(change, data); + if (change == ItemVisibleHasChanged) + update(); +} - while (transformNode) { - QSGNode *nextSibling = transformNode->nextSibling(); - delete transformNode; - transformNode = nextSibling; +QSGNode *QQuickUniversalBusyIndicator::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) +{ + QQuickUniversalBusyIndicatorNode *node = static_cast<QQuickUniversalBusyIndicatorNode *>(oldNode); + if (isVisible() && width() > 0 && height() > 0) { + if (!node) + node = new QQuickUniversalBusyIndicatorNode(this); + node->sync(this); + } else { + m_elapsed = node ? node->elapsed() : 0; + delete node; + node = nullptr; } - - return oldNode; + return node; } QT_END_NAMESPACE diff --git a/src/imports/controls/universal/qquickuniversalprogressring_p.h b/src/imports/controls/universal/qquickuniversalbusyindicator_p.h index ef73ebed..7e3484d8 100644 --- a/src/imports/controls/universal/qquickuniversalprogressring_p.h +++ b/src/imports/controls/universal/qquickuniversalbusyindicator_p.h @@ -34,8 +34,8 @@ ** ****************************************************************************/ -#ifndef QQUICKUNIVERSALPROGRESSRING_P_H -#define QQUICKUNIVERSALPROGRESSRING_P_H +#ifndef QQUICKUNIVERSALBUSYINDICATOR_P_H +#define QQUICKUNIVERSALBUSYINDICATOR_P_H // // W A R N I N G @@ -49,18 +49,17 @@ // #include <QtQuick/qquickitem.h> -#include <QtQuick/private/qquickanimator_p.h> QT_BEGIN_NAMESPACE -class QQuickUniversalProgressRing : public QQuickItem +class QQuickUniversalBusyIndicator : public QQuickItem { Q_OBJECT - Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged FINAL) - Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged FINAL) + Q_PROPERTY(int count READ count WRITE setCount FINAL) + Q_PROPERTY(QColor color READ color WRITE setColor FINAL) public: - QQuickUniversalProgressRing(QQuickItem *parent = nullptr); + explicit QQuickUniversalBusyIndicator(QQuickItem *parent = nullptr); int count() const; void setCount(int count); @@ -68,32 +67,20 @@ public: QColor color() const; void setColor(const QColor &color); -Q_SIGNALS: - void countChanged(); - void colorChanged(); + int elapsed() const; protected: + void itemChange(ItemChange change, const ItemChangeData &data) override; QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) override; private: int m_count; + int m_elapsed; QColor m_color; }; -class QQuickUniversalProgressRingAnimator : public QQuickAnimator -{ - Q_OBJECT - -public: - QQuickUniversalProgressRingAnimator(QObject *parent = nullptr); - -protected: - QString propertyName() const override; - QQuickAnimatorJob *createJob() const override; -}; - QT_END_NAMESPACE -QML_DECLARE_TYPE(QQuickUniversalProgressRing) +QML_DECLARE_TYPE(QQuickUniversalBusyIndicator) -#endif // QQUICKUNIVERSALPROGRESSRING_P_H +#endif // QQUICKUNIVERSALBUSYINDICATOR_P_H diff --git a/src/imports/controls/universal/qtquickcontrols2universalstyleplugin.cpp b/src/imports/controls/universal/qtquickcontrols2universalstyleplugin.cpp index 394e9162..e6595c72 100644 --- a/src/imports/controls/universal/qtquickcontrols2universalstyleplugin.cpp +++ b/src/imports/controls/universal/qtquickcontrols2universalstyleplugin.cpp @@ -36,8 +36,8 @@ #include <QtQuickControls2/private/qquickstyleplugin_p.h> +#include "qquickuniversalbusyindicator_p.h" #include "qquickuniversalfocusrectangle_p.h" -#include "qquickuniversalprogressring_p.h" #include "qquickuniversalprogressstrip_p.h" #include "qquickuniversalstyle_p.h" #include "qquickuniversaltheme_p.h" @@ -88,9 +88,8 @@ void QtQuickControls2UniversalStylePlugin::initializeEngine(QQmlEngine *engine, QByteArray import = QByteArray(uri) + ".impl"; qmlRegisterType<QQuickUniversalFocusRectangle>(import, 2, 0, "FocusRectangle"); + qmlRegisterType<QQuickUniversalBusyIndicator>(import, 2, 0, "BusyIndicatorImpl"); qmlRegisterRevision<QQuickUniversalFocusRectangle, 1>(import, 2, 1); - qmlRegisterType<QQuickUniversalProgressRing>(import, 2, 0, "ProgressRing"); - qmlRegisterType<QQuickUniversalProgressRingAnimator>(import, 2, 0, "ProgressRingAnimator"); qmlRegisterType<QQuickUniversalProgressStrip>(import, 2, 0, "ProgressStrip"); qmlRegisterType<QQuickUniversalProgressStripAnimator>(import, 2, 0, "ProgressStripAnimator"); diff --git a/src/imports/controls/universal/universal.pri b/src/imports/controls/universal/universal.pri index 3697a2a6..8d8c2e87 100644 --- a/src/imports/controls/universal/universal.pri +++ b/src/imports/controls/universal/universal.pri @@ -47,15 +47,15 @@ QML_FILES += \ $$PWD/Tumbler.qml HEADERS += \ + $$PWD/qquickuniversalbusyindicator_p.h \ $$PWD/qquickuniversalfocusrectangle_p.h \ - $$PWD/qquickuniversalprogressring_p.h \ $$PWD/qquickuniversalprogressstrip_p.h \ $$PWD/qquickuniversalstyle_p.h \ $$PWD/qquickuniversaltheme_p.h SOURCES += \ + $$PWD/qquickuniversalbusyindicator.cpp \ $$PWD/qquickuniversalfocusrectangle.cpp \ - $$PWD/qquickuniversalprogressring.cpp \ $$PWD/qquickuniversalprogressstrip.cpp \ $$PWD/qquickuniversalstyle.cpp \ $$PWD/qquickuniversaltheme.cpp |