diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2016-10-20 00:31:41 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2016-10-20 09:12:30 +0000 |
commit | 2466612283d06f2fc8365e82f492ca1da55f68d1 (patch) | |
tree | 8493676110c5059090bc80ec7d5fcd21bf77f04c /src/imports/controls/material | |
parent | a451c1a4ad20320a096e997c7173a3c61e8051d4 (diff) |
Add internal QQuickAnimatedNode helper
This handles the timers, signals, updates etc. so that the animated
node implementations for busy indicators and indeterminate progress
bars become a bit simpler. The implementation is based on
qtdeclarative/examples/quick/scenegraph/threadedanimation.
Task-number: QTBUG-56601
Change-Id: Ibd82060aa103e6447ee16814c3e0d6ff2c14d608
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/imports/controls/material')
-rw-r--r-- | src/imports/controls/material/qquickmaterialbusyindicator.cpp | 73 | ||||
-rw-r--r-- | src/imports/controls/material/qquickmaterialprogressbar.cpp | 48 |
2 files changed, 51 insertions, 70 deletions
diff --git a/src/imports/controls/material/qquickmaterialbusyindicator.cpp b/src/imports/controls/material/qquickmaterialbusyindicator.cpp index d55b131b..2c02e2ba 100644 --- a/src/imports/controls/material/qquickmaterialbusyindicator.cpp +++ b/src/imports/controls/material/qquickmaterialbusyindicator.cpp @@ -37,11 +37,10 @@ #include "qquickmaterialbusyindicator_p.h" #include <QtCore/qeasingcurve.h> -#include <QtCore/qelapsedtimer.h> #include <QtGui/qpainter.h> -#include <QtQuick/private/qquickitem_p.h> #include <QtQuick/qsgimagenode.h> #include <QtQuick/qquickwindow.h> +#include <QtQuickControls2/private/qquickanimatednode_p.h> QT_BEGIN_NAMESPACE @@ -66,42 +65,38 @@ static const int OneDegree = 16; static const qreal MinSweepSpan = 10 * OneDegree; static const qreal MaxSweepSpan = 300 * OneDegree; -class QQuickMaterialBusyIndicatorNode : public QObject, public QSGNode +class QQuickMaterialBusyIndicatorNode : public QQuickAnimatedNode { public: QQuickMaterialBusyIndicatorNode(QQuickMaterialBusyIndicator *item); - int elapsed() const; + void sync(QQuickItem *item) override; - void animate(); - void sync(QQuickMaterialBusyIndicator *item); +protected: + void updateCurrentTime(int time) override; private: - int m_offset; int m_lastStartAngle; int m_lastEndAngle; qreal m_width; qreal m_height; qreal m_devicePixelRatio; - QSGNode *m_containerNode; - QQuickWindow *m_window; - QElapsedTimer m_timer; QColor m_color; }; -QQuickMaterialBusyIndicatorNode::QQuickMaterialBusyIndicatorNode(QQuickMaterialBusyIndicator *item) : - m_offset(item->elapsed()), - m_lastStartAngle(0), - m_lastEndAngle(0), - m_width(0), - m_height(0), - m_devicePixelRatio(1), - m_window(item->window()) +QQuickMaterialBusyIndicatorNode::QQuickMaterialBusyIndicatorNode(QQuickMaterialBusyIndicator *item) + : QQuickAnimatedNode(item), + m_lastStartAngle(0), + m_lastEndAngle(0), + m_width(0), + m_height(0), + m_devicePixelRatio(1) { - connect(m_window, &QQuickWindow::frameSwapped, m_window, &QQuickWindow::update); - connect(m_window, &QQuickWindow::beforeRendering, this, &QQuickMaterialBusyIndicatorNode::animate); + setLoopCount(Infinite); + setCurrentTime(item->elapsed()); + setDuration(RotationAnimationDuration); - QSGImageNode *textureNode = m_window->createImageNode(); + QSGImageNode *textureNode = item->window()->createImageNode(); textureNode->setOwnsTexture(true); appendChildNode(textureNode); @@ -109,24 +104,11 @@ QQuickMaterialBusyIndicatorNode::QQuickMaterialBusyIndicatorNode(QQuickMaterialB // so just use a blank image. QImage blankImage(item->width(), item->height(), QImage::Format_ARGB32_Premultiplied); blankImage.fill(Qt::transparent); - textureNode->setTexture(m_window->createTextureFromImage(blankImage)); - - m_timer.restart(); -} - -int QQuickMaterialBusyIndicatorNode::elapsed() const -{ - return m_timer.elapsed() + m_offset; + textureNode->setTexture(item->window()->createTextureFromImage(blankImage)); } -void QQuickMaterialBusyIndicatorNode::animate() +void QQuickMaterialBusyIndicatorNode::updateCurrentTime(int time) { - qint64 time = m_timer.elapsed() + m_offset; - if (time >= RotationAnimationDuration) { - m_timer.restart(); - m_offset = 0; - } - const qreal w = m_width; const qreal h = m_height; const qreal size = qMin(w, h); @@ -183,15 +165,16 @@ void QQuickMaterialBusyIndicatorNode::animate() painter.end(); textureNode->setRect(QRectF(dx, dy, size, size)); - textureNode->setTexture(m_window->createTextureFromImage(image)); + textureNode->setTexture(window()->createTextureFromImage(image)); } -void QQuickMaterialBusyIndicatorNode::sync(QQuickMaterialBusyIndicator *item) +void QQuickMaterialBusyIndicatorNode::sync(QQuickItem *item) { - m_color = item->color(); - m_width = item->width(); - m_height = item->height(); - m_devicePixelRatio = m_window->effectiveDevicePixelRatio(); + QQuickMaterialBusyIndicator *indicator = static_cast<QQuickMaterialBusyIndicator *>(item); + m_color = indicator->color(); + m_width = indicator->width(); + m_height = indicator->height(); + m_devicePixelRatio = indicator->window()->effectiveDevicePixelRatio(); } QQuickMaterialBusyIndicator::QQuickMaterialBusyIndicator(QQuickItem *parent) : @@ -230,11 +213,13 @@ QSGNode *QQuickMaterialBusyIndicator::updatePaintNode(QSGNode *oldNode, UpdatePa { QQuickMaterialBusyIndicatorNode *node = static_cast<QQuickMaterialBusyIndicatorNode *>(oldNode); if (isVisible() && width() > 0 && height() > 0) { - if (!node) + if (!node) { node = new QQuickMaterialBusyIndicatorNode(this); + node->start(); + } node->sync(this); } else { - m_elapsed = node ? node->elapsed() : 0; + m_elapsed = node ? node->currentTime() : 0; delete node; node = nullptr; } diff --git a/src/imports/controls/material/qquickmaterialprogressbar.cpp b/src/imports/controls/material/qquickmaterialprogressbar.cpp index 36b18781..bc909748 100644 --- a/src/imports/controls/material/qquickmaterialprogressbar.cpp +++ b/src/imports/controls/material/qquickmaterialprogressbar.cpp @@ -38,11 +38,12 @@ #include <QtCore/qmath.h> #include <QtCore/qeasingcurve.h> -#include <QtCore/qelapsedtimer.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuick/private/qsgadaptationlayer_p.h> #include <QtQuick/qsgrectanglenode.h> #include <QtQuick/qsgimagenode.h> +#include <QtQuick/qquickwindow.h> +#include <QtQuickControls2/private/qquickanimatednode_p.h> QT_BEGIN_NAMESPACE @@ -50,34 +51,32 @@ static const int PauseDuration = 520; static const int SlideDuration = 1240; static const int TotalDuration = SlideDuration + PauseDuration; -class QQuickMaterialProgressBarNode : public QObject, public QSGNode +class QQuickMaterialProgressBarNode : public QQuickAnimatedNode { public: QQuickMaterialProgressBarNode(QQuickMaterialProgressBar *item); - void animate(); - void sync(QQuickMaterialProgressBar *item); + void updateCurrentTime(int time) override; + void sync(QQuickItem *item) override; private: void moveNode(QSGTransformNode *node, const QRectF &geometry, qreal progress); bool m_indeterminate; - QElapsedTimer m_timer; QEasingCurve m_easing; }; -QQuickMaterialProgressBarNode::QQuickMaterialProgressBarNode(QQuickMaterialProgressBar *) - : m_indeterminate(false), m_easing(QEasingCurve::OutCubic) +QQuickMaterialProgressBarNode::QQuickMaterialProgressBarNode(QQuickMaterialProgressBar *item) + : QQuickAnimatedNode(item), + m_indeterminate(false), + m_easing(QEasingCurve::OutCubic) { - m_timer.start(); + setLoopCount(Infinite); + setDuration(TotalDuration); } -void QQuickMaterialProgressBarNode::animate() +void QQuickMaterialProgressBarNode::updateCurrentTime(int time) { - qint64 time = m_timer.elapsed(); - if (time >= TotalDuration) - m_timer.restart(); - QSGRectangleNode *geometryNode = static_cast<QSGRectangleNode *>(firstChild()); Q_ASSERT(geometryNode->type() == QSGNode::GeometryNodeType); const QRectF geometry = geometryNode->rect(); @@ -99,18 +98,15 @@ void QQuickMaterialProgressBarNode::animate() } } -void QQuickMaterialProgressBarNode::sync(QQuickMaterialProgressBar *item) +void QQuickMaterialProgressBarNode::sync(QQuickItem *item) { - if (m_indeterminate != item->isIndeterminate()) { - m_indeterminate = item->isIndeterminate(); - QQuickWindow *window = item->window(); - if (m_indeterminate) { - connect(window, &QQuickWindow::frameSwapped, window, &QQuickWindow::update); - connect(window, &QQuickWindow::beforeRendering, this, &QQuickMaterialProgressBarNode::animate); - } else { - disconnect(window, &QQuickWindow::frameSwapped, window, &QQuickWindow::update); - disconnect(window, &QQuickWindow::beforeRendering, this, &QQuickMaterialProgressBarNode::animate); - } + QQuickMaterialProgressBar *bar = static_cast<QQuickMaterialProgressBar *>(item); + if (m_indeterminate != bar->isIndeterminate()) { + m_indeterminate = bar->isIndeterminate(); + if (m_indeterminate) + start(); + else + stop(); } QQuickItemPrivate *d = QQuickItemPrivate::get(item); @@ -128,7 +124,7 @@ void QQuickMaterialProgressBarNode::sync(QQuickMaterialProgressBar *item) geometryNode->setRect(bounds); const int count = m_indeterminate ? 2 : 1; - const qreal w = m_indeterminate ? 0 : item->progress() * item->width(); + const qreal w = m_indeterminate ? 0 : bar->progress() * item->width(); const QRectF rect(0, bounds.y(), w, bounds.height()); QSGNode *transformNode = geometryNode->firstChild(); @@ -148,7 +144,7 @@ void QQuickMaterialProgressBarNode::sync(QQuickMaterialProgressBar *item) Q_ASSERT(rectNode->type() == QSGNode::GeometryNodeType); rectNode->setRect(rect); - rectNode->setColor(item->color()); + rectNode->setColor(bar->color()); rectNode->update(); transformNode = transformNode->nextSibling(); |