From c8be1c487994fda855b9f2dc7c005db4686981c0 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Tue, 3 Nov 2009 15:31:07 +1000 Subject: Get AnimatedImage + autotests working again. There are still failures, but at least the tests no longer crash. --- .../extra/qmlgraphicsanimatedimageitem.cpp | 48 +++++++++++++++++++--- src/declarative/graphicsitems/qmlgraphicsimage.cpp | 14 +++++-- .../graphicsitems/qmlgraphicsimage_p_p.h | 2 +- .../animatedimage/tst_animatedimage.cpp | 38 +++++++++-------- 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp b/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp index 5dbffc0..f3c2058 100644 --- a/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp +++ b/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp @@ -40,7 +40,6 @@ ****************************************************************************/ #include -#include #include #include "qmlgraphicsanimatedimageitem_p.h" #include "qmlgraphicsanimatedimageitem_p_p.h" @@ -179,6 +178,14 @@ int QmlGraphicsAnimatedImageItem::frameCount() const return d->_movie->frameCount(); } +static QString toLocalFileOrQrc(const QUrl& url) +{ + QString r = url.toLocalFile(); + if (r.isEmpty() && url.scheme() == QLatin1String("qrc")) + r = QLatin1Char(':') + url.path(); + return r; +} + void QmlGraphicsAnimatedImageItem::setSource(const QUrl &url) { Q_D(QmlGraphicsAnimatedImageItem); @@ -193,15 +200,46 @@ void QmlGraphicsAnimatedImageItem::setSource(const QUrl &url) d->reply = 0; } - d->url = qmlContext(this)->resolvedUrl(url); + d->url = url; if (url.isEmpty()) { delete d->_movie; d->status = Null; } else { +#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML + QString lf = toLocalFileOrQrc(url); + if (!lf.isEmpty()) { + //### should be unified with movieRequestFinished + d->_movie = new QMovie(lf); + if (!d->_movie->isValid()){ + qWarning() << "Error Reading Animated Image File " << d->url; + delete d->_movie; + d->_movie = 0; + return; + } + connect(d->_movie, SIGNAL(stateChanged(QMovie::MovieState)), + this, SLOT(playingStatusChanged())); + connect(d->_movie, SIGNAL(frameChanged(int)), + this, SLOT(movieUpdate())); + d->_movie->setCacheMode(QMovie::CacheAll); + if(d->playing) + d->_movie->start(); + else + d->_movie->jumpToFrame(0); + if(d->paused) + d->_movie->setPaused(true); + d->setPixmap(d->_movie->currentPixmap()); + d->status = Ready; + d->progress = 1.0; + emit statusChanged(d->status); + emit sourceChanged(d->url); + emit progressChanged(d->progress); + return; + } +#endif d->status = Loading; QNetworkRequest req(d->url); - d->reply = qmlContext(this)->engine()->networkAccessManager()->get(req); + d->reply = qmlEngine(this)->networkAccessManager()->get(req); QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(movieRequestFinished())); } @@ -230,13 +268,13 @@ void QmlGraphicsAnimatedImageItem::movieRequestFinished() d->_movie->jumpToFrame(0); if(d->paused) d->_movie->setPaused(true); - setPixmap(d->_movie->currentPixmap()); + d->setPixmap(d->_movie->currentPixmap()); } void QmlGraphicsAnimatedImageItem::movieUpdate() { Q_D(QmlGraphicsAnimatedImageItem); - setPixmap(d->_movie->currentPixmap()); + d->setPixmap(d->_movie->currentPixmap()); emit frameChanged(); } diff --git a/src/declarative/graphicsitems/qmlgraphicsimage.cpp b/src/declarative/graphicsitems/qmlgraphicsimage.cpp index fd220a3..938fe2a 100644 --- a/src/declarative/graphicsitems/qmlgraphicsimage.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsimage.cpp @@ -152,12 +152,18 @@ void QmlGraphicsImage::setPixmap(const QPixmap &pix) Q_D(QmlGraphicsImage); if (!d->url.isEmpty()) return; - d->pix = pix; + d->setPixmap(pix); +} + +void QmlGraphicsImagePrivate::setPixmap(const QPixmap &pixmap) +{ + Q_Q(QmlGraphicsImage); + pix = pixmap; - setImplicitWidth(d->pix.width()); - setImplicitHeight(d->pix.height()); + q->setImplicitWidth(pix.width()); + q->setImplicitHeight(pix.height()); - update(); + q->update(); } /*! diff --git a/src/declarative/graphicsitems/qmlgraphicsimage_p_p.h b/src/declarative/graphicsitems/qmlgraphicsimage_p_p.h index 62a4d1e..f6b4e51 100644 --- a/src/declarative/graphicsitems/qmlgraphicsimage_p_p.h +++ b/src/declarative/graphicsitems/qmlgraphicsimage_p_p.h @@ -69,7 +69,7 @@ public: } QmlGraphicsImage::FillMode fillMode; - + void setPixmap(const QPixmap &pix); }; QT_END_NAMESPACE diff --git a/tests/auto/declarative/animatedimage/tst_animatedimage.cpp b/tests/auto/declarative/animatedimage/tst_animatedimage.cpp index f6141cb..6ae2112 100644 --- a/tests/auto/declarative/animatedimage/tst_animatedimage.cpp +++ b/tests/auto/declarative/animatedimage/tst_animatedimage.cpp @@ -61,34 +61,40 @@ private slots: void tst_animatedimage::play() { - QmlGraphicsAnimatedImageItem anim; - anim.setSource(QUrl("file://" SRCDIR "/data/stickman.gif")); - QVERIFY(anim.isPlaying()); + QmlEngine engine; + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickman.qml")); + QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QVERIFY(anim); + QVERIFY(anim->isPlaying()); } void tst_animatedimage::pause() { - QmlGraphicsAnimatedImageItem anim; - anim.setSource(QUrl("file://" SRCDIR "/data/stickman.gif")); - anim.setPaused(true); - QVERIFY(!anim.isPlaying()); + QmlEngine engine; + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickmanpause.qml")); + QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QVERIFY(anim); + QVERIFY(anim->isPlaying()); + QVERIFY(anim->isPaused()); } void tst_animatedimage::setFrame() { - QmlGraphicsAnimatedImageItem anim; - anim.setSource(QUrl("file://" SRCDIR "/data/stickman.gif")); - anim.setPaused(true); - QVERIFY(!anim.isPlaying()); - anim.setCurrentFrame(2); - QCOMPARE(anim.currentFrame(), 2); + QmlEngine engine; + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickmanpause.qml")); + QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QVERIFY(anim); + QVERIFY(anim->isPlaying()); + QCOMPARE(anim->currentFrame(), 2); } void tst_animatedimage::frameCount() { - QmlGraphicsAnimatedImageItem anim; - anim.setSource(QUrl("file://" SRCDIR "/data/stickman.gif")); - QCOMPARE(anim.frameCount(), 299); + QmlEngine engine; + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickman.qml")); + QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QVERIFY(anim); + QCOMPARE(anim->frameCount(), 299); } QTEST_MAIN(tst_animatedimage) -- cgit v0.12