From 754905afb1d16f9cec274b3ce3ff2b937bc13e84 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Thu, 10 May 2012 20:26:13 +1000 Subject: Emit currentFrameChanged signal when needed Task-number: QTBUG-25039 Change-Id: I8bd5fd40a5fee1314f0401ed4708d73ed1cfad94 Reviewed-by: Alan Alpert --- src/quick/items/qquickanimatedsprite.cpp | 9 ++++ src/quick/items/qquickanimatedsprite_p.h | 1 + .../qquickanimatedsprite/data/frameChange.qml | 59 ++++++++++++++++++++++ .../tst_qquickanimatedsprite.cpp | 34 +++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 tests/auto/quick/qquickanimatedsprite/data/frameChange.qml diff --git a/src/quick/items/qquickanimatedsprite.cpp b/src/quick/items/qquickanimatedsprite.cpp index 24f3de2491..acb38863be 100644 --- a/src/quick/items/qquickanimatedsprite.cpp +++ b/src/quick/items/qquickanimatedsprite.cpp @@ -44,6 +44,7 @@ #include "qquickspriteengine_p.h" #include #include +#include #include #include #include @@ -377,6 +378,11 @@ QQuickAnimatedSprite::QQuickAnimatedSprite(QQuickItem *parent) : this, SLOT(sizeVertices())); } +bool QQuickAnimatedSprite::isCurrentFrameChangedConnected() +{ + IS_SIGNAL_CONNECTED(this, QQuickAnimatedSprite, currentFrameChanged, (int)); +} + void QQuickAnimatedSprite::reloadImage() { if (!isComponentComplete()) @@ -597,6 +603,7 @@ void QQuickAnimatedSprite::prepareNextFrame() double frameAt; //double just for modf qreal progress = 0.0; + int lastFrame = m_curFrame; if (!m_paused) { //Advance State (keeps time for psuedostates) m_spriteEngine->updateSprites(timeInt); @@ -629,6 +636,8 @@ void QQuickAnimatedSprite::prepareNextFrame() } else { frameAt = m_curFrame; } + if (m_curFrame != lastFrame && isCurrentFrameChangedConnected()) + emit currentFrameChanged(m_curFrame); if (m_spriteEngine->sprite()->reverse()) frameAt = (m_spriteEngine->spriteFrames() - 1) - frameAt; qreal y = m_spriteEngine->spriteY() / m_sheetSize.height(); diff --git a/src/quick/items/qquickanimatedsprite_p.h b/src/quick/items/qquickanimatedsprite_p.h index c9a74b2231..4d9c008c36 100644 --- a/src/quick/items/qquickanimatedsprite_p.h +++ b/src/quick/items/qquickanimatedsprite_p.h @@ -353,6 +353,7 @@ protected: void componentComplete(); QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *); private: + bool isCurrentFrameChangedConnected(); void prepareNextFrame(); void reloadImage(); QSGGeometryNode* buildNode(); diff --git a/tests/auto/quick/qquickanimatedsprite/data/frameChange.qml b/tests/auto/quick/qquickanimatedsprite/data/frameChange.qml new file mode 100644 index 0000000000..74ff09533b --- /dev/null +++ b/tests/auto/quick/qquickanimatedsprite/data/frameChange.qml @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Rectangle { + color: "black" + width: 320 + height: 320 + + AnimatedSprite { + objectName: "sprite" + source: "squarefacesprite.png" + frameCount: 6 + loops: 3 + frameSync: true + running: false + width: 160 + height: 160 + } +} diff --git a/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp b/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp index 37625cfed6..b45b5d53be 100644 --- a/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp +++ b/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp @@ -41,6 +41,7 @@ #include #include "../../shared/util.h" #include +#include #include class tst_qquickanimatedsprite : public QQmlDataTest @@ -50,9 +51,17 @@ public: tst_qquickanimatedsprite(){} private slots: + void initTestCase(); void test_properties(); + void test_frameChangedSignal(); }; +void tst_qquickanimatedsprite::initTestCase() +{ + QQmlDataTest::initTestCase(); + QUnifiedTimer::instance()->setConsistentTiming(true); +} + void tst_qquickanimatedsprite::test_properties() { QQuickView *canvas = new QQuickView(0); @@ -78,6 +87,31 @@ void tst_qquickanimatedsprite::test_properties() delete canvas; } +void tst_qquickanimatedsprite::test_frameChangedSignal() +{ + QQuickView *canvas = new QQuickView(0); + + canvas->setSource(testFileUrl("frameChange.qml")); + canvas->show(); + QTest::qWaitForWindowShown(canvas); + + QVERIFY(canvas->rootObject()); + QQuickAnimatedSprite* sprite = canvas->rootObject()->findChild("sprite"); + QVERIFY(sprite); + + QVERIFY(!sprite->running()); + QVERIFY(!sprite->paused()); + QCOMPARE(sprite->loops(), 3); + QCOMPARE(sprite->frameCount(), 6); + + QSignalSpy frameChangedSpy(sprite, SIGNAL(currentFrameChanged(int))); + sprite->setRunning(true); + QTRY_COMPARE(frameChangedSpy.count(), 3*6); + QTRY_VERIFY(!sprite->running()); + + delete canvas; +} + QTEST_MAIN(tst_qquickanimatedsprite) #include "tst_qquickanimatedsprite.moc" -- cgit v1.2.3