diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2018-02-26 11:49:47 +0100 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2018-02-26 22:59:42 +0000 |
commit | e41d067227eb6225b05df88ab724708588fa5304 (patch) | |
tree | 83c59a2348b46bdff62d55a9b0661cc3cce49e9c | |
parent | 18615b6216f0f6b1d5e8eef3ad2013fd87a3bc40 (diff) |
AnimatedSprite: set implicit size based on implicit frame size
By doing so, users no longer need to set an implicit or explicit size
for AnimatedSprite.
[ChangeLog][QtQuick][AnimatedSprite] AnimatedSprite's implicitWidth and
implicitHeight are now based on frameWidth and frameHeight,
respectively. This means it is no longer necessary to explicitly size
AnimatedSprite.
Task-number: QTBUG-36341
Change-Id: I3eb87e9b1c6bb93b3c667123c345341f34e2eee8
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
-rw-r--r-- | examples/quick/imageelements/animatedsprite.qml | 2 | ||||
-rw-r--r-- | src/quick/items/qquickanimatedsprite.cpp | 14 | ||||
-rw-r--r-- | tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp | 39 |
3 files changed, 52 insertions, 3 deletions
diff --git a/examples/quick/imageelements/animatedsprite.qml b/examples/quick/imageelements/animatedsprite.qml index ba3d8ffdbc..0c6bf5e28d 100644 --- a/examples/quick/imageelements/animatedsprite.qml +++ b/examples/quick/imageelements/animatedsprite.qml @@ -60,8 +60,6 @@ Item { //! [sprite] AnimatedSprite { id: sprite - width: 170 - height: 170 anchors.centerIn: parent source: "content/speaker.png" frameCount: 60 diff --git a/src/quick/items/qquickanimatedsprite.cpp b/src/quick/items/qquickanimatedsprite.cpp index c644433115..8e486add44 100644 --- a/src/quick/items/qquickanimatedsprite.cpp +++ b/src/quick/items/qquickanimatedsprite.cpp @@ -210,7 +210,6 @@ QT_BEGIN_NAMESPACE Stops, then starts the sprite animation. */ -//TODO: Implicitly size element to size of sprite QQuickAnimatedSprite::QQuickAnimatedSprite(QQuickItem *parent) : QQuickItem(*(new QQuickAnimatedSpritePrivate), parent) { @@ -516,6 +515,7 @@ void QQuickAnimatedSprite::setFrameHeight(int arg) if (d->m_sprite->m_frameHeight != arg) { d->m_sprite->setFrameHeight(arg); Q_EMIT frameHeightChanged(arg); + setImplicitHeight(frameHeight()); reloadImage(); } } @@ -527,6 +527,7 @@ void QQuickAnimatedSprite::setFrameWidth(int arg) if (d->m_sprite->m_frameWidth != arg) { d->m_sprite->setFrameWidth(arg); Q_EMIT frameWidthChanged(arg); + setImplicitWidth(frameWidth()); reloadImage(); } } @@ -641,6 +642,17 @@ QSGSpriteNode* QQuickAnimatedSprite::initNode() if (image.isNull()) return nullptr; + // If frameWidth or frameHeight are not explicitly set, frameWidth + // will be set to the width of the image divided by the number of frames, + // and frameHeight will be set to the height of the image. + // In this case, QQuickAnimatedSprite currently won't emit frameWidth/HeightChanged + // at all, so we have to do this here, as it's the only place where assembledImage() + // is called (which calculates the "implicit" frameWidth/Height. + // In addition, currently the "implicit" frameWidth/Height are only calculated once, + // even after changing to a different source. + setImplicitWidth(frameWidth()); + setImplicitHeight(frameHeight()); + QSGSpriteNode *node = d->sceneGraphContext()->createSpriteNode(); d->m_sheetSize = QSize(image.size()); diff --git a/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp b/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp index 368779f52f..d24ebd9878 100644 --- a/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp +++ b/tests/auto/quick/qquickanimatedsprite/tst_qquickanimatedsprite.cpp @@ -53,6 +53,7 @@ private slots: void test_largeAnimation(); void test_reparenting(); void test_changeSourceToSmallerImgKeepingBigFrameSize(); + void test_implicitSize(); }; void tst_qquickanimatedsprite::initTestCase() @@ -318,6 +319,44 @@ void tst_qquickanimatedsprite::test_changeSourceToSmallerImgKeepingBigFrameSize( killer->wait(); } +void tst_qquickanimatedsprite::test_implicitSize() +{ + QQuickView window; + window.setSource(testFileUrl("basic.qml")); + window.show(); + QVERIFY(QTest::qWaitForWindowExposed(&window)); + QVERIFY(window.rootObject()); + + QQuickAnimatedSprite* sprite = window.rootObject()->findChild<QQuickAnimatedSprite*>("sprite"); + QVERIFY(sprite); + QCOMPARE(sprite->frameWidth(), 31); + QCOMPARE(sprite->frameHeight(), 30); + QCOMPARE(sprite->implicitWidth(), 31); + QCOMPARE(sprite->implicitHeight(), 30); + + // Ensure that implicitWidth matches frameWidth. + QSignalSpy frameWidthChangedSpy(sprite, SIGNAL(frameWidthChanged(int))); + QVERIFY(frameWidthChangedSpy.isValid()); + + QSignalSpy frameImplicitWidthChangedSpy(sprite, SIGNAL(implicitWidthChanged())); + QVERIFY(frameImplicitWidthChangedSpy.isValid()); + + sprite->setFrameWidth(20); + QCOMPARE(frameWidthChangedSpy.count(), 1); + QCOMPARE(frameImplicitWidthChangedSpy.count(), 1); + + // Ensure that implicitHeight matches frameHeight. + QSignalSpy frameHeightChangedSpy(sprite, SIGNAL(frameHeightChanged(int))); + QVERIFY(frameHeightChangedSpy.isValid()); + + QSignalSpy frameImplicitHeightChangedSpy(sprite, SIGNAL(implicitHeightChanged())); + QVERIFY(frameImplicitHeightChangedSpy.isValid()); + + sprite->setFrameHeight(20); + QCOMPARE(frameHeightChangedSpy.count(), 1); + QCOMPARE(frameImplicitHeightChangedSpy.count(), 1); +} + QTEST_MAIN(tst_qquickanimatedsprite) #include "tst_qquickanimatedsprite.moc" |