From cf851eced1d03c9d839626f86d00fd0734949c9d Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Tue, 21 Jul 2009 09:06:24 +1000 Subject: Update AnimatedImage item to be more similar to Animations Now has playing and paused properties that should behave the same. --- demos/declarative/samegame/content/SpinBlock.qml | 2 +- demos/declarative/samegame/content/samegame.js | 2 +- src/declarative/extra/qfxanimatedimageitem.cpp | 45 ++++++++++++++++++++++-- src/declarative/extra/qfxanimatedimageitem.h | 6 ++++ src/declarative/extra/qfxanimatedimageitem_p.h | 3 +- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/demos/declarative/samegame/content/SpinBlock.qml b/demos/declarative/samegame/content/SpinBlock.qml index 42276d0..2597bfb 100644 --- a/demos/declarative/samegame/content/SpinBlock.qml +++ b/demos/declarative/samegame/content/SpinBlock.qml @@ -15,7 +15,7 @@ Item { id:block } else { "pics/gnome/greenStone.gif"; } - playing: selected + paused: !selected } opacity: 0 y: targetY diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js index 1814031..3592edc 100644 --- a/demos/declarative/samegame/content/samegame.js +++ b/demos/declarative/samegame/content/samegame.js @@ -49,7 +49,7 @@ function initBoard() var fillFound; var floodBoard; -var lastHoveredIdx = -1 +var lastHoveredIdx = -2 function handleHover(x,y, btn) { xIdx = Math.floor(x/tileSize); diff --git a/src/declarative/extra/qfxanimatedimageitem.cpp b/src/declarative/extra/qfxanimatedimageitem.cpp index 5516a00..4c74f6f 100644 --- a/src/declarative/extra/qfxanimatedimageitem.cpp +++ b/src/declarative/extra/qfxanimatedimageitem.cpp @@ -96,6 +96,30 @@ QFxAnimatedImageItem::~QFxAnimatedImageItem() } /*! + \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 QFxAnimatedImageItem::isPaused() const +{ + Q_D(const QFxAnimatedImageItem); + if(!d->_movie) + return false; + return d->_movie->state()==QMovie::Paused; +} + +void QFxAnimatedImageItem::setPaused(bool pause) +{ + Q_D(QFxAnimatedImageItem); + 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 @@ -106,7 +130,7 @@ bool QFxAnimatedImageItem::isPlaying() const Q_D(const QFxAnimatedImageItem); if (!d->_movie) return false; - return d->_movie->state()==QMovie::Running; + return d->_movie->state()!=QMovie::NotRunning; } void QFxAnimatedImageItem::setPlaying(bool play) @@ -120,7 +144,7 @@ void QFxAnimatedImageItem::setPlaying(bool play) if (play) d->_movie->start(); else - d->_movie->setPaused(true); + d->_movie->stop(); } /*! @@ -197,7 +221,7 @@ void QFxAnimatedImageItem::movieRequestFinished() return; } connect(d->_movie, SIGNAL(stateChanged(QMovie::MovieState)), - this, SIGNAL(playingChanged())); + this, SLOT(playingStatusChanged())); connect(d->_movie, SIGNAL(frameChanged(int)), this, SLOT(movieUpdate())); d->_movie->setCacheMode(QMovie::CacheAll); @@ -205,6 +229,8 @@ void QFxAnimatedImageItem::movieRequestFinished() d->_movie->start(); else d->_movie->jumpToFrame(0); + if(d->paused) + d->_movie->setPaused(true); setPixmap(d->_movie->currentPixmap()); } @@ -215,4 +241,17 @@ void QFxAnimatedImageItem::movieUpdate() emit frameChanged(); } +void QFxAnimatedImageItem::playingStatusChanged() +{ + Q_D(QFxAnimatedImageItem); + 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(); + } +} + QT_END_NAMESPACE diff --git a/src/declarative/extra/qfxanimatedimageitem.h b/src/declarative/extra/qfxanimatedimageitem.h index 720d187..2d531ee 100644 --- a/src/declarative/extra/qfxanimatedimageitem.h +++ b/src/declarative/extra/qfxanimatedimageitem.h @@ -58,6 +58,7 @@ class Q_DECLARATIVE_EXPORT QFxAnimatedImageItem : public QFxImage 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: @@ -67,6 +68,9 @@ public: bool isPlaying() const; void setPlaying(bool play); + bool isPaused() const; + void setPaused(bool pause); + int currentFrame() const; void setCurrentFrame(int frame); @@ -77,11 +81,13 @@ public: Q_SIGNALS: void playingChanged(); + void pausedChanged(); void frameChanged(); private Q_SLOTS: void movieUpdate(); void movieRequestFinished(); + void playingStatusChanged(); protected: QFxAnimatedImageItem(QFxAnimatedImageItemPrivate &dd, QFxItem *parent); diff --git a/src/declarative/extra/qfxanimatedimageitem_p.h b/src/declarative/extra/qfxanimatedimageitem_p.h index d743ba4..859f869 100644 --- a/src/declarative/extra/qfxanimatedimageitem_p.h +++ b/src/declarative/extra/qfxanimatedimageitem_p.h @@ -65,11 +65,12 @@ class QFxAnimatedImageItemPrivate : public QFxImagePrivate public: QFxAnimatedImageItemPrivate() - : playing(true), _movie(0) + : playing(true), paused(false), _movie(0) { } bool playing; + bool paused; QMovie *_movie; }; -- cgit v0.12 From d2ad09f42db2b6b80427918261f96081eaabfe6c Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Tue, 21 Jul 2009 16:38:15 +1000 Subject: Move the Script interface of QmlComponent to QmlBindableComponent --- demos/declarative/samegame/content/samegame.js | 4 +- examples/declarative/dynamic/dynamic.js | 4 +- src/declarative/qml/qml.pri | 3 + src/declarative/qml/qmlbindablecomponent.cpp | 104 +++++++++++++++++++++++++ src/declarative/qml/qmlbindablecomponent.h | 91 ++++++++++++++++++++++ src/declarative/qml/qmlbindablecomponent_p.h | 77 ++++++++++++++++++ src/declarative/qml/qmlcomponent.cpp | 17 ---- src/declarative/qml/qmlcomponent.h | 9 +-- src/declarative/qml/qmlengine.cpp | 12 +-- 9 files changed, 290 insertions(+), 31 deletions(-) create mode 100644 src/declarative/qml/qmlbindablecomponent.cpp create mode 100644 src/declarative/qml/qmlbindablecomponent.h create mode 100644 src/declarative/qml/qmlbindablecomponent_p.h diff --git a/demos/declarative/samegame/content/samegame.js b/demos/declarative/samegame/content/samegame.js index 3592edc..b1b2c50 100644 --- a/demos/declarative/samegame/content/samegame.js +++ b/demos/declarative/samegame/content/samegame.js @@ -201,7 +201,7 @@ var waitTop = -1; function finishCreatingBlock(xIdx,yIdx){ //TODO: Doc that the 'x', 'y' that were here are hidden properties from the calling QFxItem - if(component.isReady()){ + if(component.isReady){ if(xIdx == undefined){ //Called without arguments, create a previously stored (xIdx,yIdx) if(waitTop == -1) @@ -226,7 +226,7 @@ function finishCreatingBlock(xIdx,yIdx){ dynamicObject.spawned = true; board[index(xIdx,yIdx)] = dynamicObject; return true; - }else if(component.isError()){ + }else if(component.isError){ print("error creating block"); print(component.errorsString()); }else{ diff --git a/examples/declarative/dynamic/dynamic.js b/examples/declarative/dynamic/dynamic.js index 13317df..8f1e138 100644 --- a/examples/declarative/dynamic/dynamic.js +++ b/examples/declarative/dynamic/dynamic.js @@ -28,10 +28,10 @@ function instantCreateWithComponent() {//Like create, but assumes instant readyn } function finishCreation(){ - if(component.isReady() && dynamicObject == null){ + if(component.isReady && dynamicObject == null){ dynamicObject = component.createObject(); dynamicObject.parent = targetItem; - }else if(component.isError()){ + }else if(component.isError){ dynamicObject = null; print("error creating component"); print(component.errorsString()); diff --git a/src/declarative/qml/qml.pri b/src/declarative/qml/qml.pri index b3fa063..b71ca1f 100644 --- a/src/declarative/qml/qml.pri +++ b/src/declarative/qml/qml.pri @@ -5,6 +5,7 @@ SOURCES += qml/qmlparser.cpp \ qml/qmlexpression.cpp \ qml/qmlbinding.cpp \ qml/qmlmetaproperty.cpp \ + qml/qmlbindablecomponent.cpp \ qml/qmlcomponent.cpp \ qml/qmlcontext.cpp \ qml/qmlcustomparser.cpp \ @@ -36,6 +37,8 @@ HEADERS += qml/qmlparser_p.h \ qml/qmlbinding.h \ qml/qmlbinding_p.h \ qml/qmlmetaproperty.h \ + qml/qmlbindablecomponent.h \ + qml/qmlbindablecomponent_p.h \ qml/qmlcomponent.h \ qml/qmlcomponent_p.h \ qml/qmlcustomparser_p.h \ diff --git a/src/declarative/qml/qmlbindablecomponent.cpp b/src/declarative/qml/qmlbindablecomponent.cpp new file mode 100644 index 0000000..ed9924a --- /dev/null +++ b/src/declarative/qml/qmlbindablecomponent.cpp @@ -0,0 +1,104 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qmlbindablecomponent.h" +#include "qmlbindablecomponent_p.h" +#include "qmlcomponent.h" + +QT_BEGIN_NAMESPACE +QmlBindableComponent::QmlBindableComponent(QmlEngine *engine, QObject *parent) + : QmlComponent(*(new QmlBindableComponentPrivate), parent) +{ + Q_D(QmlBindableComponent); + d->engine = engine; + connect(this, SIGNAL(statusChanged(QmlComponent::Status)), + this, SLOT(statusChange(QmlComponent::Status))); +} + +QmlBindableComponent::QmlBindableComponent(QmlEngine *engine, const QUrl &url, QObject *parent) + : QmlComponent(*(new QmlBindableComponentPrivate), parent) +{ + Q_D(QmlBindableComponent); + d->engine = engine; + loadUrl(url); + connect(this, SIGNAL(statusChanged(QmlComponent::Status)), + this, SLOT(statusChange(QmlComponent::Status))); +} + +void QmlBindableComponent::setContext(QmlContext* c) +{ + Q_D(QmlBindableComponent); + d->ctxt =c; +} +/*! + Create a script object instance from this component. Returns a null + script object if creation failed. It will create the instance in the + same context that it was created it. QmlBindableComponent is only + meant to be created from with script - C++ developers should just use + QmlComponent directly. + + Similar to QmlComponent::create(), but creates an object suitable + for usage within scripts. +*/ +QScriptValue QmlBindableComponent::createObject() +{ + Q_D(QmlBindableComponent); + QObject* ret = create(d->ctxt); + return QmlEngine::qmlScriptObject(ret, d->engine); +} + +void QmlBindableComponent::statusChange(QmlComponent::Status newStatus) +{ + Q_D(QmlBindableComponent); + if(newStatus == d->prevStatus) + return; + if(newStatus == QmlComponent::Null || d->prevStatus == QmlComponent::Null) + emit isNullChanged(); + if(newStatus == QmlComponent::Ready || d->prevStatus == QmlComponent::Ready) + emit isReadyChanged(); + if(newStatus == QmlComponent::Loading || d->prevStatus == QmlComponent::Loading) + emit isLoadingChanged(); + if(newStatus == QmlComponent::Error || d->prevStatus == QmlComponent::Error) + emit isErrorChanged(); + d->prevStatus = newStatus; +} + +QT_END_NAMESPACE diff --git a/src/declarative/qml/qmlbindablecomponent.h b/src/declarative/qml/qmlbindablecomponent.h new file mode 100644 index 0000000..2b3e0ce --- /dev/null +++ b/src/declarative/qml/qmlbindablecomponent.h @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLBINDABLECOMPONENT_H +#define QMLBINDABLECOMPONENT_H + +#include +#include +#include +#include +#include +#include + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class QmlBindableComponentPrivate; +class QmlEngine; +class QmlContext; +class Q_DECLARATIVE_EXPORT QmlBindableComponent : public QmlComponent +{ + Q_OBJECT + Q_DECLARE_PRIVATE(QmlBindableComponent) + friend class QmlEngine; +public: + QmlBindableComponent(QmlEngine *, const QUrl &url, QObject *parent = 0); + QmlBindableComponent(QmlEngine *, QObject *parent=0); + Q_PROPERTY(bool isNull READ isNull NOTIFY isNullChanged); + Q_PROPERTY(bool isReady READ isReady NOTIFY isReadyChanged); + Q_PROPERTY(bool isError READ isError NOTIFY isErrorChanged); + Q_PROPERTY(bool isLoading READ isLoading NOTIFY isLoadingChanged); + + Q_INVOKABLE QScriptValue createObject(); + + void setContext(QmlContext* c); +Q_SIGNALS: + void isNullChanged(); + void isErrorChanged(); + void isReadyChanged(); + void isLoadingChanged(); +private slots: + void statusChange(QmlComponent::Status newStatus); +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QmlBindableComponent) + +QT_END_HEADER +#endif diff --git a/src/declarative/qml/qmlbindablecomponent_p.h b/src/declarative/qml/qmlbindablecomponent_p.h new file mode 100644 index 0000000..79335df --- /dev/null +++ b/src/declarative/qml/qmlbindablecomponent_p.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (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 either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** 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.0, 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QMLBINDABLECOMPONENT_P_H +#define QMLBINDABLECOMPONENT_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 "qmlcomponent.h" +#include "qmlbindablecomponent.h" +#include "qmlcomponent_p.h" + +QT_BEGIN_NAMESPACE + +class QmlContext; +class QmlBindableComponentPrivate : public QmlComponentPrivate +{ + Q_DECLARE_PUBLIC(QmlBindableComponent) +public: + QmlBindableComponentPrivate() : QmlComponentPrivate(), + prevStatus(QmlBindableComponent::Null), ctxt(0) + { } + + QmlComponent::Status prevStatus; + QmlContext* ctxt; +}; + +QT_END_NAMESPACE + +#endif diff --git a/src/declarative/qml/qmlcomponent.cpp b/src/declarative/qml/qmlcomponent.cpp index 4e39d7f..0fdba64 100644 --- a/src/declarative/qml/qmlcomponent.cpp +++ b/src/declarative/qml/qmlcomponent.cpp @@ -431,23 +431,6 @@ QmlComponent::QmlComponent(QmlComponentPrivate &dd, QObject *parent) { } -/*! - Create a script object instance from this component. Returns a null - script object if creation failed. It will create the instance in the - engine's \l {QmlEngine::rootContext()}{root context}. - - Similar to QmlComponent::create(), but creates an object suitable - for usage within scripts. -*/ -QScriptValue QmlComponent::createObject() -{ - Q_D(QmlComponent); - QObject* ret = create(); - if(ret) - return QmlEngine::qmlScriptObject(ret, d->engine); - else - return d->engine->scriptEngine()->nullValue(); -} /*! Create an object instance from this component. Returns 0 if creation diff --git a/src/declarative/qml/qmlcomponent.h b/src/declarative/qml/qmlcomponent.h index 9c712df..45b26eb 100644 --- a/src/declarative/qml/qmlcomponent.h +++ b/src/declarative/qml/qmlcomponent.h @@ -77,17 +77,16 @@ public: enum Status { Null, Ready, Loading, Error }; Status status() const; - Q_INVOKABLE bool isNull() const; - Q_INVOKABLE bool isReady() const; - Q_INVOKABLE bool isError() const; - Q_INVOKABLE bool isLoading() const; + bool isNull() const; + bool isReady() const; + bool isError() const; + bool isLoading() const; QList errors() const; Q_INVOKABLE QString errorsString() const; QUrl url() const; - Q_INVOKABLE QScriptValue createObject(); virtual QObject *create(QmlContext *context = 0); virtual QObject *beginCreate(QmlContext *); virtual void completeCreate(); diff --git a/src/declarative/qml/qmlengine.cpp b/src/declarative/qml/qmlengine.cpp index 8b21290..c4b3c0d 100644 --- a/src/declarative/qml/qmlengine.cpp +++ b/src/declarative/qml/qmlengine.cpp @@ -71,6 +71,7 @@ #include #include #include +#include #include "private/qmlmetaproperty_p.h" #include #include @@ -583,19 +584,20 @@ QScriptValue QmlEngine::qmlScriptObject(QObject* object, QmlEngine* engine) */ QScriptValue QmlEngine::createComponent(QScriptContext *ctxt, QScriptEngine *engine) { - QmlComponent* c; + QmlBindableComponent* c; QmlEngine* activeEngine = qobject_cast( engine->globalObject().property(QLatin1String("qmlEngine")).toQObject()); + QmlContext* context =activeEngine->d_func()->currentExpression->context(); if(ctxt->argumentCount() != 1 || !activeEngine){ - c = new QmlComponent(activeEngine); + c = new QmlBindableComponent(activeEngine); }else{ - QUrl url = QUrl(activeEngine->d_func()->currentExpression->context() - ->resolvedUrl(ctxt->argument(0).toString())); + QUrl url = QUrl(context->resolvedUrl(ctxt->argument(0).toString())); if(!url.isValid()){ url = QUrl(ctxt->argument(0).toString()); } - c = new QmlComponent(activeEngine, url, activeEngine); + c = new QmlBindableComponent(activeEngine, url, activeEngine); } + c->setContext(context); return engine->newQObject(c); } -- cgit v0.12