aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Sauer <sebastian.sauer.ford@kdab.com>2014-08-12 18:53:00 +0700
committerSebastian Sauer <sebastian.sauer@kdab.com>2014-08-25 17:29:21 +0200
commitcae7176827eb3c23428cfcd8f6dabb00f8dcaef3 (patch)
tree948a32f66791eff082363c6aca5a1436cd4e9353 /src
parent99fd3a6b22a7eabf2aff656a942b0b7e32093254 (diff)
QSM: Reintroduce guard argument evaluation
Implements the suggestion from Simon Hausmann (codereview 89716 from 08-05 14:46) to use QQmlScriptString rather then the previous used MetaObject-manipulation. This also introduces comparison operators for QQmlScriptString to be able to determinate if a QQmlScriptString changed what is needed cause there is otherwise no way to access (all) the needed details within QQmlScriptStringPrivate. Change-Id: I198479eac8fd37cbdd98a99aacdd8eebf7b75d21 Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src')
-rw-r--r--src/imports/statemachine/signaltransition.cpp41
-rw-r--r--src/imports/statemachine/signaltransition.h10
-rw-r--r--src/qml/doc/snippets/qml/statemachine/guardcondition.qml69
-rw-r--r--src/qml/qml/qqmlscriptstring.cpp38
-rw-r--r--src/qml/qml/qqmlscriptstring.h3
5 files changed, 149 insertions, 12 deletions
diff --git a/src/imports/statemachine/signaltransition.cpp b/src/imports/statemachine/signaltransition.cpp
index 9b64550678..634ec5a43b 100644
--- a/src/imports/statemachine/signaltransition.cpp
+++ b/src/imports/statemachine/signaltransition.cpp
@@ -38,6 +38,7 @@
#include <QQmlInfo>
#include <QQmlEngine>
#include <QQmlContext>
+#include <QQmlExpression>
#include <private/qv4qobjectwrapper_p.h>
#include <private/qv8engine_p.h>
@@ -46,7 +47,6 @@
SignalTransition::SignalTransition(QState *parent)
: QSignalTransition(this, SIGNAL(invokeYourself()), parent)
- , m_guard(true)
{
connect(this, SIGNAL(signalChanged()), SIGNAL(qmlSignalChanged()));
}
@@ -57,7 +57,23 @@ bool SignalTransition::eventTest(QEvent *event)
if (!QSignalTransition::eventTest(event))
return false;
- return m_guard;
+ if (m_guard.isEmpty())
+ return true;
+
+ QQmlContext context(QQmlEngine::contextForObject(this));
+
+ QStateMachine::SignalEvent *e = static_cast<QStateMachine::SignalEvent*>(event);
+
+ // Set arguments as context properties
+ int count = e->arguments().count();
+ QMetaMethod metaMethod = e->sender()->metaObject()->method(e->signalIndex());
+ for (int i = 0; i < count; i++)
+ context.setContextProperty(metaMethod.parameterNames()[i], QVariant::fromValue(e->arguments().at(i)));
+
+ QQmlExpression expr(m_guard, &context, this);
+ QVariant result = expr.evaluate();
+
+ return result.toBool();
}
const QJSValue& SignalTransition::signal()
@@ -89,17 +105,18 @@ void SignalTransition::setSignal(const QJSValue &signal)
QSignalTransition::setSignal(metaMethod.methodSignature());
}
-bool SignalTransition::guard() const
+QQmlScriptString SignalTransition::guard() const
{
return m_guard;
}
-void SignalTransition::setGuard(bool guard)
+void SignalTransition::setGuard(const QQmlScriptString &guard)
{
- if (guard != m_guard) {
- m_guard = guard;
- emit guardChanged();
- }
+ if (m_guard == guard)
+ return;
+
+ m_guard = guard;
+ emit guardChanged();
}
void SignalTransition::invoke()
@@ -227,4 +244,12 @@ void SignalTransition::invoke()
Guard conditions affect the behavior of a state machine by enabling
transitions only when they evaluate to true and disabling them when
they evaluate to false.
+
+ When the signal associated with this signal transition is emitted the
+ guard condition is evaluated. In the guard condition the arguments
+ of the signal can be used as demonstrated in the example below.
+
+ \snippet qml/statemachine/guardcondition.qml document
+
+ \sa signal
*/
diff --git a/src/imports/statemachine/signaltransition.h b/src/imports/statemachine/signaltransition.h
index 311195e8e9..3ac563b15e 100644
--- a/src/imports/statemachine/signaltransition.h
+++ b/src/imports/statemachine/signaltransition.h
@@ -38,19 +38,21 @@
#include <QtCore/QVariant>
#include <QtQml/QJSValue>
+#include <QtQml/qqmlscriptstring.h>
+
QT_BEGIN_NAMESPACE
class SignalTransition : public QSignalTransition
{
Q_OBJECT
Q_PROPERTY(QJSValue signal READ signal WRITE setSignal NOTIFY qmlSignalChanged)
- Q_PROPERTY(bool guard READ guard WRITE setGuard NOTIFY guardChanged)
+ Q_PROPERTY(QQmlScriptString guard READ guard WRITE setGuard NOTIFY guardChanged)
public:
explicit SignalTransition(QState *parent = Q_NULLPTR);
- bool guard() const;
- void setGuard(bool guard);
+ QQmlScriptString guard() const;
+ void setGuard(const QQmlScriptString &guard);
bool eventTest(QEvent *event);
@@ -70,7 +72,7 @@ Q_SIGNALS:
private:
QByteArray m_data;
QJSValue m_signal;
- bool m_guard;
+ QQmlScriptString m_guard;
};
QT_END_NAMESPACE
diff --git a/src/qml/doc/snippets/qml/statemachine/guardcondition.qml b/src/qml/doc/snippets/qml/statemachine/guardcondition.qml
new file mode 100644
index 0000000000..f9ecf450bd
--- /dev/null
+++ b/src/qml/doc/snippets/qml/statemachine/guardcondition.qml
@@ -0,0 +1,69 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Ford Motor Company
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** You may use this file under the terms of the BSD license as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+** of its contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+//! [document]
+import QtQml.StateMachine 1.0
+import QtQuick 2.0
+
+Rectangle {
+ Button {
+ anchors.fill: parent
+ id: button
+ StateMachine {
+ StateBase {
+ SignalTransition {
+ targetState: finalState
+ signal: button.mysignal
+ // the guard condition uses the mystr string argument from mysignal
+ guard: mystr == "test"
+ }
+ }
+ FinalState {
+ id: finalState
+ }
+ }
+ // define the signal the SignalTransition is connected with
+ signal mysignal(string mystr)
+ // on clicking the button emit the signal with a single string argument
+ onClicked: button.mysignal("test")
+ }
+}
+//! [document]
+
diff --git a/src/qml/qml/qqmlscriptstring.cpp b/src/qml/qml/qqmlscriptstring.cpp
index b18bd4d420..c8227070c5 100644
--- a/src/qml/qml/qqmlscriptstring.cpp
+++ b/src/qml/qml/qqmlscriptstring.cpp
@@ -114,6 +114,44 @@ QQmlScriptString &QQmlScriptString::operator=(const QQmlScriptString &other)
}
/*!
+Returns \c true if this and the \a other QQmlScriptString objects are equal.
+
+\sa operator!=()
+*/
+bool QQmlScriptString::operator==(const QQmlScriptString &other) const
+{
+ if (d == other.d)
+ return true;
+
+ if (d->isNumberLiteral || other.d->isNumberLiteral)
+ return d->isNumberLiteral && other.d->isNumberLiteral && d->numberValue == other.d->numberValue;
+
+ if (d->isStringLiteral || other.d->isStringLiteral)
+ return d->isStringLiteral && other.d->isStringLiteral && d->script == other.d->script;
+
+ if (d->script == QStringLiteral("true") ||
+ d->script == QStringLiteral("false") ||
+ d->script == QStringLiteral("undefined") ||
+ d->script == QStringLiteral("null"))
+ return d->script == other.d->script;
+
+ return d->context == other.d->context &&
+ d->scope == other.d->scope &&
+ d->script == other.d->script &&
+ d->bindingId == other.d->bindingId;
+}
+
+/*!
+Returns \c true if this and the \a other QQmlScriptString objects are different.
+
+\sa operator==()
+*/
+bool QQmlScriptString::operator!=(const QQmlScriptString &other) const
+{
+ return !operator==(other);
+}
+
+/*!
Returns whether the QQmlScriptString is empty.
*/
bool QQmlScriptString::isEmpty() const
diff --git a/src/qml/qml/qqmlscriptstring.h b/src/qml/qml/qqmlscriptstring.h
index 1ff8903993..ccbe90535b 100644
--- a/src/qml/qml/qqmlscriptstring.h
+++ b/src/qml/qml/qqmlscriptstring.h
@@ -58,6 +58,9 @@ public:
QQmlScriptString &operator=(const QQmlScriptString &);
+ bool operator==(const QQmlScriptString &) const;
+ bool operator!=(const QQmlScriptString &) const;
+
bool isEmpty() const;
bool isUndefinedLiteral() const;