From 318775ded2371b4033b9a06ffec23e75c6dbad96 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Wed, 11 Nov 2009 11:37:15 +1000 Subject: AnimatedImage cleanup and tests. --- src/declarative/extra/extra.pri | 3 - .../extra/qmlgraphicsanimatedimageitem.cpp | 305 --------------------- .../extra/qmlgraphicsanimatedimageitem_p.h | 107 -------- .../extra/qmlgraphicsanimatedimageitem_p_p.h | 82 ------ src/declarative/graphicsitems/graphicsitems.pri | 3 + .../graphicsitems/qmlgraphicsanimatedimage.cpp | 300 ++++++++++++++++++++ .../graphicsitems/qmlgraphicsanimatedimage_p.h | 106 +++++++ .../graphicsitems/qmlgraphicsanimatedimage_p_p.h | 82 ++++++ .../auto/declarative/animatedimage/data/colors.gif | Bin 0 -> 505 bytes .../auto/declarative/animatedimage/data/colors.qml | 5 + .../animatedimage/data/stickmanstopped.qml | 6 + .../animatedimage/tst_animatedimage.cpp | 38 ++- 12 files changed, 535 insertions(+), 502 deletions(-) delete mode 100644 src/declarative/extra/qmlgraphicsanimatedimageitem.cpp delete mode 100644 src/declarative/extra/qmlgraphicsanimatedimageitem_p.h delete mode 100644 src/declarative/extra/qmlgraphicsanimatedimageitem_p_p.h create mode 100644 src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp create mode 100644 src/declarative/graphicsitems/qmlgraphicsanimatedimage_p.h create mode 100644 src/declarative/graphicsitems/qmlgraphicsanimatedimage_p_p.h create mode 100644 tests/auto/declarative/animatedimage/data/colors.gif create mode 100644 tests/auto/declarative/animatedimage/data/colors.qml create mode 100644 tests/auto/declarative/animatedimage/data/stickmanstopped.qml diff --git a/src/declarative/extra/extra.pri b/src/declarative/extra/extra.pri index 78272a9..85ff6ea 100644 --- a/src/declarative/extra/extra.pri +++ b/src/declarative/extra/extra.pri @@ -2,7 +2,6 @@ SOURCES += \ extra/qnumberformat.cpp \ extra/qmlnumberformatter.cpp \ extra/qmldatetimeformatter.cpp \ - extra/qmlgraphicsanimatedimageitem.cpp \ extra/qmlbehavior.cpp \ extra/qmlfontloader.cpp @@ -10,8 +9,6 @@ HEADERS += \ extra/qnumberformat_p.h \ extra/qmlnumberformatter_p.h \ extra/qmldatetimeformatter_p.h \ - extra/qmlgraphicsanimatedimageitem_p.h \ - extra/qmlgraphicsanimatedimageitem_p_p.h \ extra/qmlbehavior_p.h \ extra/qmlfontloader_p.h diff --git a/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp b/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp deleted file mode 100644 index 2405bde..0000000 --- a/src/declarative/extra/qmlgraphicsanimatedimageitem.cpp +++ /dev/null @@ -1,305 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, 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. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include -#include -#include "qmlgraphicsanimatedimageitem_p.h" -#include "qmlgraphicsanimatedimageitem_p_p.h" -#include -#include - -QT_BEGIN_NAMESPACE - -/*! - \class QmlGraphicsAnimatedImageItem - \internal -*/ - -/*! - \qmlclass AnimatedImage QFxAnimatedImageItem - \inherits Image - - This item provides for playing animations stored as images containing a series of frames, - such as GIF files. The full list of supported formats can be determined with - QMovie::supportedFormats(). - - \table - \row - \o \image animatedimageitem.gif - \o - \qml -Item { - width: anim.width; height: anim.height+8 - AnimatedImage { id: anim; source: "pics/games-anim.gif" } - Rectangle { color: "red"; width: 4; height: 8; y: anim.height - x: (anim.width-width)*anim.currentFrame/(anim.frameCount-1) - } -} - \endqml - \endtable -*/ -QML_DEFINE_TYPE(Qt,4,6,AnimatedImage,QmlGraphicsAnimatedImageItem) - -QmlGraphicsAnimatedImageItem::QmlGraphicsAnimatedImageItem(QmlGraphicsItem *parent) - : QmlGraphicsImage(*(new QmlGraphicsAnimatedImageItemPrivate), parent) -{ -} - -QmlGraphicsAnimatedImageItem::QmlGraphicsAnimatedImageItem(QmlGraphicsAnimatedImageItemPrivate &dd, QmlGraphicsItem *parent) - : QmlGraphicsImage(dd, parent) -{ -} - -QmlGraphicsAnimatedImageItem::~QmlGraphicsAnimatedImageItem() -{ - Q_D(QmlGraphicsAnimatedImageItem); - delete d->_movie; -} - -/*! - \qmlproperty bool AnimatedImage::paused - This property holds whether the animated image is paused or not - - Defaults to false, and can be set to true when you want to pause. -*/ -bool QmlGraphicsAnimatedImageItem::isPaused() const -{ - Q_D(const QmlGraphicsAnimatedImageItem); - if(!d->_movie) - return false; - return d->_movie->state()==QMovie::Paused; -} - -void QmlGraphicsAnimatedImageItem::setPaused(bool pause) -{ - Q_D(QmlGraphicsAnimatedImageItem); - if(pause == d->paused) - return; - d->paused = pause; - if(!d->_movie) - return; - d->_movie->setPaused(pause); -} -/*! - \qmlproperty bool AnimatedImage::playing - This property holds whether the animated image is playing or not - - Defaults to true, so as to start playing immediately. -*/ -bool QmlGraphicsAnimatedImageItem::isPlaying() const -{ - Q_D(const QmlGraphicsAnimatedImageItem); - if (!d->_movie) - return false; - return d->_movie->state()!=QMovie::NotRunning; -} - -void QmlGraphicsAnimatedImageItem::setPlaying(bool play) -{ - Q_D(QmlGraphicsAnimatedImageItem); - if(play == d->playing) - return; - d->playing = play; - if (!d->_movie) - return; - if (play) - d->_movie->start(); - else - d->_movie->stop(); -} - -/*! - \qmlproperty int AnimatedImage::currentFrame - \qmlproperty int AnimatedImage::frameCount - - currentFrame is the frame that is currently visible. Watching when this changes can - allow other things to animate at the same time as the image. frameCount is the number - of frames in the animation. For some animation formats, frameCount is unknown and set to zero. -*/ -int QmlGraphicsAnimatedImageItem::currentFrame() const -{ - Q_D(const QmlGraphicsAnimatedImageItem); - if (!d->_movie) - return d->preset_currentframe; - return d->_movie->currentFrameNumber(); -} - -void QmlGraphicsAnimatedImageItem::setCurrentFrame(int frame) -{ - Q_D(QmlGraphicsAnimatedImageItem); - if (!d->_movie) { - d->preset_currentframe = frame; - return; - } - d->_movie->jumpToFrame(frame); -} - -int QmlGraphicsAnimatedImageItem::frameCount() const -{ - Q_D(const QmlGraphicsAnimatedImageItem); - if (!d->_movie) - return 0; - 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); - if (url == d->url) - return; - - delete d->_movie; - d->_movie = 0; - - if (d->reply) { - d->reply->deleteLater(); - d->reply = 0; - } - - 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 = qmlEngine(this)->networkAccessManager()->get(req); - QObject::connect(d->reply, SIGNAL(finished()), - this, SLOT(movieRequestFinished())); - } - - emit statusChanged(d->status); -} - -void QmlGraphicsAnimatedImageItem::movieRequestFinished() -{ - Q_D(QmlGraphicsAnimatedImageItem); - d->_movie = new QMovie(d->reply); - 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(d->preset_currentframe); - d->preset_currentframe = 0; - } - if(d->paused) - d->_movie->setPaused(true); - d->setPixmap(d->_movie->currentPixmap()); -} - -void QmlGraphicsAnimatedImageItem::movieUpdate() -{ - Q_D(QmlGraphicsAnimatedImageItem); - d->setPixmap(d->_movie->currentPixmap()); - emit frameChanged(); -} - -void QmlGraphicsAnimatedImageItem::playingStatusChanged() -{ - Q_D(QmlGraphicsAnimatedImageItem); - if((d->_movie->state() != QMovie::NotRunning) != d->playing){ - d->playing = (d->_movie->state() != QMovie::NotRunning); - emit playingChanged(); - } - if((d->_movie->state() == QMovie::Paused) != d->paused){ - d->playing = (d->_movie->state() == QMovie::Paused); - emit pausedChanged(); - } -} - -void QmlGraphicsAnimatedImageItem::componentComplete() -{ - Q_D(QmlGraphicsAnimatedImageItem); - setCurrentFrame(d->preset_currentframe); - d->preset_currentframe = 0; -} - -QT_END_NAMESPACE diff --git a/src/declarative/extra/qmlgraphicsanimatedimageitem_p.h b/src/declarative/extra/qmlgraphicsanimatedimageitem_p.h deleted file mode 100644 index b581ea3..0000000 --- a/src/declarative/extra/qmlgraphicsanimatedimageitem_p.h +++ /dev/null @@ -1,107 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, 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. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QMLGRAPHICSANIMATEDIMAGEITEM_H -#define QMLGRAPHICSANIMATEDIMAGEITEM_H - -#include - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE - -QT_MODULE(Declarative) - -class QMovie; -class QmlGraphicsAnimatedImageItemPrivate; - -class Q_DECLARATIVE_EXPORT QmlGraphicsAnimatedImageItem : public QmlGraphicsImage -{ - Q_OBJECT - - Q_PROPERTY(bool playing READ isPlaying WRITE setPlaying NOTIFY playingChanged) - Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged) - Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame NOTIFY frameChanged) - Q_PROPERTY(int frameCount READ frameCount) -public: - QmlGraphicsAnimatedImageItem(QmlGraphicsItem *parent=0); - ~QmlGraphicsAnimatedImageItem(); - - bool isPlaying() const; - void setPlaying(bool play); - - bool isPaused() const; - void setPaused(bool pause); - - int currentFrame() const; - void setCurrentFrame(int frame); - - int frameCount() const; - - // Extends QmlGraphicsImage's src property*/ - virtual void setSource(const QUrl&); - -Q_SIGNALS: - void playingChanged(); - void pausedChanged(); - void frameChanged(); - -private Q_SLOTS: - void movieUpdate(); - void movieRequestFinished(); - void playingStatusChanged(); - -protected: - QmlGraphicsAnimatedImageItem(QmlGraphicsAnimatedImageItemPrivate &dd, QmlGraphicsItem *parent); - void componentComplete(); - -private: - Q_DISABLE_COPY(QmlGraphicsAnimatedImageItem) - Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QmlGraphicsAnimatedImageItem) -}; - -QT_END_NAMESPACE - -QML_DECLARE_TYPE(QmlGraphicsAnimatedImageItem) - -QT_END_HEADER - -#endif diff --git a/src/declarative/extra/qmlgraphicsanimatedimageitem_p_p.h b/src/declarative/extra/qmlgraphicsanimatedimageitem_p_p.h deleted file mode 100644 index 2256b9b..0000000 --- a/src/declarative/extra/qmlgraphicsanimatedimageitem_p_p.h +++ /dev/null @@ -1,82 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). -** All rights reserved. -** Contact: Nokia Corporation (qt-info@nokia.com) -** -** This file is part of the QtDeclarative module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** No Commercial Usage -** This file contains pre-release code and may not be distributed. -** You may use this file in accordance with the terms and conditions -** contained in the Technology Preview License Agreement accompanying -** this package. -** -** GNU Lesser General Public License Usage -** Alternatively, 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. -** -** If you have questions regarding the use of this file, please contact -** Nokia at qt-info@nokia.com. -** -** -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QMLGRAPHICSANIMATEDIMAGE_P_H -#define QMLGRAPHICSANIMATEDIMAGE_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include - -QT_BEGIN_NAMESPACE - -class QMovie; -class QNetworkReply; - -class QmlGraphicsAnimatedImageItemPrivate : public QmlGraphicsImagePrivate -{ - Q_DECLARE_PUBLIC(QmlGraphicsAnimatedImageItem) - -public: - QmlGraphicsAnimatedImageItemPrivate() - : playing(true), paused(false), preset_currentframe(0), _movie(0), reply(0) - { - } - - bool playing; - bool paused; - int preset_currentframe; - QMovie *_movie; - QNetworkReply *reply; -}; - -QT_END_NAMESPACE - -#endif // QMLGRAPHICSANIMATEDIMAGE_P_H diff --git a/src/declarative/graphicsitems/graphicsitems.pri b/src/declarative/graphicsitems/graphicsitems.pri index 3c4e39a..ef10e51 100644 --- a/src/declarative/graphicsitems/graphicsitems.pri +++ b/src/declarative/graphicsitems/graphicsitems.pri @@ -14,6 +14,8 @@ HEADERS += \ graphicsitems/qmlgraphicsimage_p_p.h \ graphicsitems/qmlgraphicsborderimage_p_p.h \ graphicsitems/qmlgraphicsimagebase_p_p.h \ + graphicsitems/qmlgraphicsanimatedimage_p.h \ + graphicsitems/qmlgraphicsanimatedimage_p_p.h \ graphicsitems/qmlgraphicsitem.h \ graphicsitems/qmlgraphicsitem_p.h \ graphicsitems/qmlgraphicsfocuspanel_p.h \ @@ -55,6 +57,7 @@ SOURCES += \ graphicsitems/qmlgraphicsimage.cpp \ graphicsitems/qmlgraphicsborderimage.cpp \ graphicsitems/qmlgraphicsimagebase.cpp \ + graphicsitems/qmlgraphicsanimatedimage.cpp \ graphicsitems/qmlgraphicspainteditem.cpp \ graphicsitems/qmlgraphicsitem.cpp \ graphicsitems/qmlgraphicsfocuspanel.cpp \ diff --git a/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp b/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp new file mode 100644 index 0000000..db28134 --- /dev/null +++ b/src/declarative/graphicsitems/qmlgraphicsanimatedimage.cpp @@ -0,0 +1,300 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include "qmlgraphicsanimatedimage_p.h" +#include "qmlgraphicsanimatedimage_p_p.h" +#include +#include + +QT_BEGIN_NAMESPACE + +/*! + \class QmlGraphicsAnimatedImage + \internal +*/ + +/*! + \qmlclass AnimatedImage QmlGraphicsAnimatedImage + \inherits Image + + This item provides for playing animations stored as images containing a series of frames, + such as GIF files. The full list of supported formats can be determined with + QMovie::supportedFormats(). + + \table + \row + \o \image animatedimageitem.gif + \o + \qml +Item { + width: anim.width; height: anim.height+8 + AnimatedImage { id: anim; source: "pics/games-anim.gif" } + Rectangle { color: "red"; width: 4; height: 8; y: anim.height + x: (anim.width-width)*anim.currentFrame/(anim.frameCount-1) + } +} + \endqml + \endtable +*/ +QML_DEFINE_TYPE(Qt,4,6,AnimatedImage,QmlGraphicsAnimatedImage) + +QmlGraphicsAnimatedImage::QmlGraphicsAnimatedImage(QmlGraphicsItem *parent) + : QmlGraphicsImage(*(new QmlGraphicsAnimatedImagePrivate), parent) +{ +} + +QmlGraphicsAnimatedImage::~QmlGraphicsAnimatedImage() +{ + Q_D(QmlGraphicsAnimatedImage); + delete d->_movie; +} + +/*! + \qmlproperty bool AnimatedImage::paused + This property holds whether the animated image is paused or not + + Defaults to false, and can be set to true when you want to pause. +*/ +bool QmlGraphicsAnimatedImage::isPaused() const +{ + Q_D(const QmlGraphicsAnimatedImage); + if(!d->_movie) + return false; + return d->_movie->state()==QMovie::Paused; +} + +void QmlGraphicsAnimatedImage::setPaused(bool pause) +{ + Q_D(QmlGraphicsAnimatedImage); + if(pause == d->paused) + return; + d->paused = pause; + if(!d->_movie) + return; + d->_movie->setPaused(pause); +} +/*! + \qmlproperty bool AnimatedImage::playing + This property holds whether the animated image is playing or not + + Defaults to true, so as to start playing immediately. +*/ +bool QmlGraphicsAnimatedImage::isPlaying() const +{ + Q_D(const QmlGraphicsAnimatedImage); + if (!d->_movie) + return false; + return d->_movie->state()!=QMovie::NotRunning; +} + +void QmlGraphicsAnimatedImage::setPlaying(bool play) +{ + Q_D(QmlGraphicsAnimatedImage); + if(play == d->playing) + return; + d->playing = play; + if (!d->_movie) + return; + if (play) + d->_movie->start(); + else + d->_movie->stop(); +} + +/*! + \qmlproperty int AnimatedImage::currentFrame + \qmlproperty int AnimatedImage::frameCount + + currentFrame is the frame that is currently visible. Watching when this changes can + allow other things to animate at the same time as the image. frameCount is the number + of frames in the animation. For some animation formats, frameCount is unknown and set to zero. +*/ +int QmlGraphicsAnimatedImage::currentFrame() const +{ + Q_D(const QmlGraphicsAnimatedImage); + if (!d->_movie) + return d->preset_currentframe; + return d->_movie->currentFrameNumber(); +} + +void QmlGraphicsAnimatedImage::setCurrentFrame(int frame) +{ + Q_D(QmlGraphicsAnimatedImage); + if (!d->_movie) { + d->preset_currentframe = frame; + return; + } + d->_movie->jumpToFrame(frame); +} + +int QmlGraphicsAnimatedImage::frameCount() const +{ + Q_D(const QmlGraphicsAnimatedImage); + if (!d->_movie) + return 0; + 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 QmlGraphicsAnimatedImage::setSource(const QUrl &url) +{ + Q_D(QmlGraphicsAnimatedImage); + if (url == d->url) + return; + + delete d->_movie; + d->_movie = 0; + + if (d->reply) { + d->reply->deleteLater(); + d->reply = 0; + } + + 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 = qmlEngine(this)->networkAccessManager()->get(req); + QObject::connect(d->reply, SIGNAL(finished()), + this, SLOT(movieRequestFinished())); + } + + emit statusChanged(d->status); +} + +void QmlGraphicsAnimatedImage::movieRequestFinished() +{ + Q_D(QmlGraphicsAnimatedImage); + d->_movie = new QMovie(d->reply); + 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(d->preset_currentframe); + d->preset_currentframe = 0; + } + if(d->paused) + d->_movie->setPaused(true); + d->setPixmap(d->_movie->currentPixmap()); +} + +void QmlGraphicsAnimatedImage::movieUpdate() +{ + Q_D(QmlGraphicsAnimatedImage); + d->setPixmap(d->_movie->currentPixmap()); + emit frameChanged(); +} + +void QmlGraphicsAnimatedImage::playingStatusChanged() +{ + Q_D(QmlGraphicsAnimatedImage); + if((d->_movie->state() != QMovie::NotRunning) != d->playing){ + d->playing = (d->_movie->state() != QMovie::NotRunning); + emit playingChanged(); + } + if((d->_movie->state() == QMovie::Paused) != d->paused){ + d->playing = (d->_movie->state() == QMovie::Paused); + emit pausedChanged(); + } +} + +void QmlGraphicsAnimatedImage::componentComplete() +{ + Q_D(QmlGraphicsAnimatedImage); + setCurrentFrame(d->preset_currentframe); + d->preset_currentframe = 0; +} + +QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qmlgraphicsanimatedimage_p.h b/src/declarative/graphicsitems/qmlgraphicsanimatedimage_p.h new file mode 100644 index 0000000..79daa0f --- /dev/null +++ b/src/declarative/graphicsitems/qmlgraphicsanimatedimage_p.h @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLGRAPHICSANIMATEDIMAGE_H +#define QMLGRAPHICSANIMATEDIMAGE_H + +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QMovie; +class QmlGraphicsAnimatedImagePrivate; + +class Q_DECLARATIVE_EXPORT QmlGraphicsAnimatedImage : public QmlGraphicsImage +{ + Q_OBJECT + + Q_PROPERTY(bool playing READ isPlaying WRITE setPlaying NOTIFY playingChanged) + Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged) + Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame NOTIFY frameChanged) + Q_PROPERTY(int frameCount READ frameCount) +public: + QmlGraphicsAnimatedImage(QmlGraphicsItem *parent=0); + ~QmlGraphicsAnimatedImage(); + + bool isPlaying() const; + void setPlaying(bool play); + + bool isPaused() const; + void setPaused(bool pause); + + int currentFrame() const; + void setCurrentFrame(int frame); + + int frameCount() const; + + // Extends QmlGraphicsImage's src property*/ + virtual void setSource(const QUrl&); + +Q_SIGNALS: + void playingChanged(); + void pausedChanged(); + void frameChanged(); + +private Q_SLOTS: + void movieUpdate(); + void movieRequestFinished(); + void playingStatusChanged(); + +protected: + void componentComplete(); + +private: + Q_DISABLE_COPY(QmlGraphicsAnimatedImage) + Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QmlGraphicsAnimatedImage) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlGraphicsAnimatedImage) + +QT_END_HEADER + +#endif diff --git a/src/declarative/graphicsitems/qmlgraphicsanimatedimage_p_p.h b/src/declarative/graphicsitems/qmlgraphicsanimatedimage_p_p.h new file mode 100644 index 0000000..c24afa0 --- /dev/null +++ b/src/declarative/graphicsitems/qmlgraphicsanimatedimage_p_p.h @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, 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. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLGRAPHICSANIMATEDIMAGE_P_H +#define QMLGRAPHICSANIMATEDIMAGE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include + +QT_BEGIN_NAMESPACE + +class QMovie; +class QNetworkReply; + +class QmlGraphicsAnimatedImagePrivate : public QmlGraphicsImagePrivate +{ + Q_DECLARE_PUBLIC(QmlGraphicsAnimatedImage) + +public: + QmlGraphicsAnimatedImagePrivate() + : playing(true), paused(false), preset_currentframe(0), _movie(0), reply(0) + { + } + + bool playing; + bool paused; + int preset_currentframe; + QMovie *_movie; + QNetworkReply *reply; +}; + +QT_END_NAMESPACE + +#endif // QMLGRAPHICSANIMATEDIMAGE_P_H diff --git a/tests/auto/declarative/animatedimage/data/colors.gif b/tests/auto/declarative/animatedimage/data/colors.gif new file mode 100644 index 0000000..1270bfa Binary files /dev/null and b/tests/auto/declarative/animatedimage/data/colors.gif differ diff --git a/tests/auto/declarative/animatedimage/data/colors.qml b/tests/auto/declarative/animatedimage/data/colors.qml new file mode 100644 index 0000000..5bada34 --- /dev/null +++ b/tests/auto/declarative/animatedimage/data/colors.qml @@ -0,0 +1,5 @@ +import Qt 4.6 + +AnimatedImage { + source: "colors.gif" +} diff --git a/tests/auto/declarative/animatedimage/data/stickmanstopped.qml b/tests/auto/declarative/animatedimage/data/stickmanstopped.qml new file mode 100644 index 0000000..53b0c3a --- /dev/null +++ b/tests/auto/declarative/animatedimage/data/stickmanstopped.qml @@ -0,0 +1,6 @@ +import Qt 4.6 + +AnimatedImage { + source: "stickman.gif" + playing: false +} diff --git a/tests/auto/declarative/animatedimage/tst_animatedimage.cpp b/tests/auto/declarative/animatedimage/tst_animatedimage.cpp index 262ddda..484fd1a 100644 --- a/tests/auto/declarative/animatedimage/tst_animatedimage.cpp +++ b/tests/auto/declarative/animatedimage/tst_animatedimage.cpp @@ -44,7 +44,7 @@ #include #include #include -#include +#include class tst_animatedimage : public QObject { @@ -55,6 +55,7 @@ public: private slots: void play(); void pause(); + void stopped(); void setFrame(); void frameCount(); }; @@ -63,34 +64,61 @@ void tst_animatedimage::play() { QmlEngine engine; QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickman.qml")); - QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QmlGraphicsAnimatedImage *anim = qobject_cast(component.create()); QVERIFY(anim); QVERIFY(anim->isPlaying()); + + delete anim; } void tst_animatedimage::pause() { QmlEngine engine; QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickmanpause.qml")); - QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QmlGraphicsAnimatedImage *anim = qobject_cast(component.create()); QVERIFY(anim); QVERIFY(anim->isPlaying()); QVERIFY(anim->isPaused()); + + delete anim; +} + +void tst_animatedimage::stopped() +{ + QmlEngine engine; + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickmanstopped.qml")); + QmlGraphicsAnimatedImage *anim = qobject_cast(component.create()); + QVERIFY(anim); + QVERIFY(!anim->isPlaying()); + QCOMPARE(anim->currentFrame(), 0); + + delete anim; } void tst_animatedimage::setFrame() { QmlEngine engine; QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/stickmanpause.qml")); - QmlGraphicsAnimatedImageItem *anim = qobject_cast(component.create()); + QmlGraphicsAnimatedImage *anim = qobject_cast(component.create()); QVERIFY(anim); QVERIFY(anim->isPlaying()); QCOMPARE(anim->currentFrame(), 2); + + delete anim; } void tst_animatedimage::frameCount() { - // GIF doesn't support frameCount until first pass through + QmlEngine engine; + QmlComponent component(&engine, QUrl("file://" SRCDIR "/data/colors.qml")); + QmlGraphicsAnimatedImage *anim = qobject_cast(component.create()); + QVERIFY(anim); + QVERIFY(anim->isPlaying()); + QCOMPARE(anim->frameCount(), 0); // GIF doesn't support frameCount until first pass through + QTest::qWait(600 + 100); + QCOMPARE(anim->frameCount(), 3); + + delete anim; } QTEST_MAIN(tst_animatedimage) -- cgit v0.12