From 3386479da9526f0ae8978cd9e0afdd0559049ef9 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 23 Oct 2009 12:45:47 +1000 Subject: Update ScriptAction and StateChangeScript to use QmlScriptString. Now you can assign the script you want to run to the property directly, rather than assigning a string. Task-number: QT-734 --- demos/declarative/flickr/common/MediaLineEdit.qml | 4 ++-- doc/src/declarative/qmlstates.qdoc | 8 ++++---- src/declarative/QmlChanges.txt | 1 + src/declarative/util/qmlanimation.cpp | 22 ++++++++++------------ src/declarative/util/qmlanimation.h | 10 ++++------ src/declarative/util/qmlanimation_p.h | 8 +++++--- src/declarative/util/qmlstateoperations.cpp | 13 +++++++------ src/declarative/util/qmlstateoperations.h | 7 ++++--- 8 files changed, 37 insertions(+), 36 deletions(-) diff --git a/demos/declarative/flickr/common/MediaLineEdit.qml b/demos/declarative/flickr/common/MediaLineEdit.qml index f959bc5..b24b296 100644 --- a/demos/declarative/flickr/common/MediaLineEdit.qml +++ b/demos/declarative/flickr/common/MediaLineEdit.qml @@ -30,13 +30,13 @@ Item { focus: true } StateChangeScript { - script:"editor.selectAll()" + script:editor.selectAll() } }, State { // When returning to default state, typed text is propagated StateChangeScript { - script: "container.text = editor.text" + script: container.text = editor.text } } ] diff --git a/doc/src/declarative/qmlstates.qdoc b/doc/src/declarative/qmlstates.qdoc index 955f7de..078b718 100644 --- a/doc/src/declarative/qmlstates.qdoc +++ b/doc/src/declarative/qmlstates.qdoc @@ -51,9 +51,9 @@ To animate state changes, you can use \l{state-transitions}{transitions}. Other things you can do in a state change: \list -\o override signal handlers -\o change an item's parent -\o change an item's anchors -\o run some script +\o override signal handlers with PropertyChanges +\o change an item's parent with ParentChange +\o change an item's anchors with AnchorChanges +\o run some script with StateChangeScript */ diff --git a/src/declarative/QmlChanges.txt b/src/declarative/QmlChanges.txt index 6a6c394..465ee9e 100644 --- a/src/declarative/QmlChanges.txt +++ b/src/declarative/QmlChanges.txt @@ -105,3 +105,4 @@ Script: now an intrinsic type in the language - bad: Item { resources: Script { ... } } Script: delay-loaded of the QML file until their source has been loaded (this only effects QML files loaded across the network.) Scope: declared properties shadow a property of the same name (was previously the reverse) +ScriptAction and StateChangeScript: the script property now takes script rather than a string containing script (script: doSomething() rather than script: "doSomething()") diff --git a/src/declarative/util/qmlanimation.cpp b/src/declarative/util/qmlanimation.cpp index c5a7f38..98453ed 100644 --- a/src/declarative/util/qmlanimation.cpp +++ b/src/declarative/util/qmlanimation.cpp @@ -790,22 +790,19 @@ void QmlScriptActionPrivate::init() } /*! - \qmlproperty QString ScriptAction::script + \qmlproperty script ScriptAction::script This property holds the script to run. */ -QString QmlScriptAction::script() const +QmlScriptString QmlScriptAction::script() const { Q_D(const QmlScriptAction); return d->script; } -void QmlScriptAction::setScript(const QString &script) +void QmlScriptAction::setScript(const QmlScriptString &script) { Q_D(QmlScriptAction); - if (script == d->script) - return; d->script = script; - emit scriptChanged(script); } /*! @@ -818,7 +815,7 @@ void QmlScriptAction::setScript(const QString &script) QString QmlScriptAction::stateChangeScriptName() const { Q_D(const QmlScriptAction); - return d->script; + return d->name; } void QmlScriptAction::setStateChangeScriptName(const QString &name) @@ -829,11 +826,11 @@ void QmlScriptAction::setStateChangeScriptName(const QString &name) void QmlScriptActionPrivate::execute() { - Q_Q(QmlScriptAction); - QString scriptStr = runScriptScript.isEmpty() ? script : runScriptScript; + QmlScriptString scriptStr = hasRunScriptScript ? script : runScriptScript; - if (!scriptStr.isEmpty()) { - QmlExpression expr(qmlContext(q), scriptStr, q); + const QString &str = scriptStr.script(); + if (!str.isEmpty()) { + QmlExpression expr(scriptStr.context(), str, scriptStr.scopeObject()); expr.setTrackChange(false); expr.value(); } @@ -847,7 +844,7 @@ void QmlScriptAction::transition(QmlStateActions &actions, Q_UNUSED(modified); Q_UNUSED(direction); - d->runScriptScript.clear(); + d->hasRunScriptScript = false; for (int ii = 0; ii < actions.count(); ++ii) { Action &action = actions[ii]; @@ -855,6 +852,7 @@ void QmlScriptAction::transition(QmlStateActions &actions, && static_cast(action.event)->name() == d->name) { //### how should we handle reverse direction? d->runScriptScript = static_cast(action.event)->script(); + d->hasRunScriptScript = true; action.actionDone = true; break; //assumes names are unique } diff --git a/src/declarative/util/qmlanimation.h b/src/declarative/util/qmlanimation.h index 508b2b5..7898980 100644 --- a/src/declarative/util/qmlanimation.h +++ b/src/declarative/util/qmlanimation.h @@ -49,6 +49,7 @@ #include #include #include +#include QT_BEGIN_HEADER @@ -161,22 +162,19 @@ class QmlScriptAction : public QmlAbstractAnimation Q_OBJECT Q_DECLARE_PRIVATE(QmlScriptAction) - Q_PROPERTY(QString script READ script WRITE setScript NOTIFY scriptChanged) + Q_PROPERTY(QmlScriptString script READ script WRITE setScript) Q_PROPERTY(QString stateChangeScriptName READ stateChangeScriptName WRITE setStateChangeScriptName) public: QmlScriptAction(QObject *parent=0); virtual ~QmlScriptAction(); - QString script() const; - void setScript(const QString &); + QmlScriptString script() const; + void setScript(const QmlScriptString &); QString stateChangeScriptName() const; void setStateChangeScriptName(const QString &); -Q_SIGNALS: - void scriptChanged(const QString &); - protected: virtual void transition(QmlStateActions &actions, QmlMetaProperties &modified, diff --git a/src/declarative/util/qmlanimation_p.h b/src/declarative/util/qmlanimation_p.h index 22c4e2d..1f52fcd 100644 --- a/src/declarative/util/qmlanimation_p.h +++ b/src/declarative/util/qmlanimation_p.h @@ -170,6 +170,7 @@ private: template class QTickAnimationProxy : public QAbstractAnimation { + //Q_OBJECT //doesn't work with templating public: QTickAnimationProxy(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {} virtual int duration() const { return -1; } @@ -227,13 +228,14 @@ class QmlScriptActionPrivate : public QmlAbstractAnimationPrivate Q_DECLARE_PUBLIC(QmlScriptAction) public: QmlScriptActionPrivate() - : QmlAbstractAnimationPrivate(), proxy(this), rsa(0) {} + : QmlAbstractAnimationPrivate(), hasRunScriptScript(false), proxy(this), rsa(0) {} void init(); - QString script; + QmlScriptString script; QString name; - QString runScriptScript; + QmlScriptString runScriptScript; + bool hasRunScriptScript; void execute(); diff --git a/src/declarative/util/qmlstateoperations.cpp b/src/declarative/util/qmlstateoperations.cpp index 053cdc3..4d469f9 100644 --- a/src/declarative/util/qmlstateoperations.cpp +++ b/src/declarative/util/qmlstateoperations.cpp @@ -285,7 +285,7 @@ class QmlStateChangeScriptPrivate : public QObjectPrivate public: QmlStateChangeScriptPrivate() {} - QString script; + QmlScriptString script; QString name; }; @@ -304,16 +304,16 @@ QmlStateChangeScript::~QmlStateChangeScript() } /*! - \qmlproperty string StateChangeScript::script + \qmlproperty script StateChangeScript::script This property holds the script to run when the state is current. */ -QString QmlStateChangeScript::script() const +QmlScriptString QmlStateChangeScript::script() const { Q_D(const QmlStateChangeScript); return d->script; } -void QmlStateChangeScript::setScript(const QString &s) +void QmlStateChangeScript::setScript(const QmlScriptString &s) { Q_D(QmlStateChangeScript); d->script = s; @@ -334,8 +334,9 @@ void QmlStateChangeScript::setName(const QString &n) void QmlStateChangeScript::execute() { Q_D(QmlStateChangeScript); - if (!d->script.isEmpty()) { - QmlExpression expr(qmlContext(this), d->script, this); + const QString &script = d->script.script(); + if (!script.isEmpty()) { + QmlExpression expr(d->script.context(), script, d->script.scopeObject()); expr.setTrackChange(false); expr.value(); } diff --git a/src/declarative/util/qmlstateoperations.h b/src/declarative/util/qmlstateoperations.h index d8132fd..237e3e8 100644 --- a/src/declarative/util/qmlstateoperations.h +++ b/src/declarative/util/qmlstateoperations.h @@ -45,6 +45,7 @@ #include #include #include +#include QT_BEGIN_HEADER @@ -86,7 +87,7 @@ class Q_DECLARATIVE_EXPORT QmlStateChangeScript : public QmlStateOperation, publ Q_OBJECT Q_DECLARE_PRIVATE(QmlStateChangeScript) - Q_PROPERTY(QString script READ script WRITE setScript) + Q_PROPERTY(QmlScriptString script READ script WRITE setScript) Q_PROPERTY(QString name READ name WRITE setName) public: @@ -95,8 +96,8 @@ public: virtual ActionList actions(); - QString script() const; - void setScript(const QString &); + QmlScriptString script() const; + void setScript(const QmlScriptString &); QString name() const; void setName(const QString &); -- cgit v0.12