aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/statemachine/signaltransition.cpp
blob: 52dffe90045fc7518de2f2c3d4d892b5d5543a2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/****************************************************************************
**
** Copyright (C) 2016 Ford Motor Company
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtQml module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "signaltransition.h"

#include <QStateMachine>
#include <QMetaProperty>
#include <QQmlInfo>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQmlExpression>

#include <private/qv4qobjectwrapper_p.h>
#include <private/qjsvalue_p.h>
#include <private/qv4scopedvalue_p.h>
#include <private/qqmlcontext_p.h>
#include <private/qqmlboundsignal_p.h>

SignalTransition::SignalTransition(QState *parent)
    : QSignalTransition(this, SIGNAL(invokeYourself()), parent), m_complete(false), m_signalExpression(nullptr)
{
    connect(this, SIGNAL(signalChanged()), SIGNAL(qmlSignalChanged()));
}

bool SignalTransition::eventTest(QEvent *event)
{
    Q_ASSERT(event);
    if (!QSignalTransition::eventTest(event))
        return false;

    if (m_guard.isEmpty())
        return true;

    QQmlContext *outerContext = QQmlEngine::contextForObject(this);
    QQmlContext context(outerContext);
    QQmlContextData::get(outerContext)->imports->addref();
    QQmlContextData::get(&context)->imports = QQmlContextData::get(outerContext)->imports;

    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());
    const auto parameterNames = metaMethod.parameterNames();
    for (int i = 0; i < count; i++)
        context.setContextProperty(parameterNames[i], QVariant::fromValue(e->arguments().at(i)));

    QQmlExpression expr(m_guard, &context, this);
    QVariant result = expr.evaluate();

    return result.toBool();
}

void SignalTransition::onTransition(QEvent *event)
{
    if (m_signalExpression) {
        QStateMachine::SignalEvent *e = static_cast<QStateMachine::SignalEvent*>(event);
        m_signalExpression->evaluate(e->arguments());
    }
    QSignalTransition::onTransition(event);
}

const QJSValue& SignalTransition::signal()
{
    return m_signal;
}

void SignalTransition::setSignal(const QJSValue &signal)
{
    if (m_signal.strictlyEquals(signal))
        return;

    m_signal = signal;

    QV4::ExecutionEngine *jsEngine = QQmlEngine::contextForObject(this)->engine()->handle();
    QV4::Scope scope(jsEngine);

    QObject *sender;
    QMetaMethod signalMethod;

    QV4::ScopedValue value(scope, QJSValuePrivate::convertedToValue(jsEngine, m_signal));

    // Did we get the "slot" that can be used to invoke the signal?
    if (QV4::QObjectMethod *signalSlot = value->as<QV4::QObjectMethod>()) {
        sender = signalSlot->object();
        Q_ASSERT(sender);
        signalMethod = sender->metaObject()->method(signalSlot->methodIndex());
    } else if (QV4::QmlSignalHandler *signalObject = value->as<QV4::QmlSignalHandler>()) { // or did we get the signal object (the one with the connect()/disconnect() functions) ?
        sender = signalObject->object();
        Q_ASSERT(sender);
        signalMethod = sender->metaObject()->method(signalObject->signalIndex());
    } else {
        qmlWarning(this) << tr("Specified signal does not exist.");
        return;
    }

    QSignalTransition::setSenderObject(sender);
    QSignalTransition::setSignal(signalMethod.methodSignature());

    connectTriggered();
}

QQmlScriptString SignalTransition::guard() const
{
    return m_guard;
}

void SignalTransition::setGuard(const QQmlScriptString &guard)
{
    if (m_guard == guard)
        return;

    m_guard = guard;
    emit guardChanged();
}

void SignalTransition::invoke()
{
    emit invokeYourself();
}

void SignalTransition::connectTriggered()
{
    if (!m_complete || !m_compilationUnit)
        return;

    QObject *target = senderObject();
    QQmlData *ddata = QQmlData::get(this);
    QQmlContextData *ctxtdata = ddata ? ddata->outerContext : nullptr;

    Q_ASSERT(m_bindings.count() == 1);
    const QV4::CompiledData::Binding *binding = m_bindings.at(0);
    Q_ASSERT(binding->type == QV4::CompiledData::Binding::Type_Script);

    QV4::ExecutionEngine *jsEngine = QQmlEngine::contextForObject(this)->engine()->handle();
    QV4::Scope scope(jsEngine);
    QV4::Scoped<QV4::QObjectMethod> qobjectSignal(scope, QJSValuePrivate::convertedToValue(jsEngine, m_signal));
    Q_ASSERT(qobjectSignal);
    QMetaMethod metaMethod = target->metaObject()->method(qobjectSignal->methodIndex());
    int signalIndex = QMetaObjectPrivate::signalIndex(metaMethod);

    auto f = m_compilationUnit->runtimeFunctions[binding->value.compiledScriptIndex];
    QQmlBoundSignalExpression *expression =
            ctxtdata ? new QQmlBoundSignalExpression(target, signalIndex, ctxtdata, this, f)
                     : nullptr;
    if (expression)
        expression->setNotifyOnValueChanged(false);
    m_signalExpression = expression;
}

void SignalTransitionParser::verifyBindings(const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit, const QList<const QV4::CompiledData::Binding *> &props)
{
    for (int ii = 0; ii < props.count(); ++ii) {
        const QV4::CompiledData::Binding *binding = props.at(ii);

        QString propName = compilationUnit->stringAt(binding->propertyNameIndex);

        if (propName != QLatin1String("onTriggered")) {
            error(props.at(ii), SignalTransition::tr("Cannot assign to non-existent property \"%1\"").arg(propName));
            return;
        }

        if (binding->type != QV4::CompiledData::Binding::Type_Script) {
            error(binding, SignalTransition::tr("SignalTransition: script expected"));
            return;
        }
    }
}

void SignalTransitionParser::applyBindings(QObject *object, const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit, const QList<const QV4::CompiledData::Binding *> &bindings)
{
    SignalTransition *st = qobject_cast<SignalTransition*>(object);
    st->m_compilationUnit = compilationUnit;
    st->m_bindings = bindings;
}

/*!
    \qmltype QAbstractTransition
    \inqmlmodule QtQml.StateMachine
    \omit
    \ingroup statemachine-qmltypes
    \endomit
    \since 5.4

    \brief The QAbstractTransition type is the base type of transitions between QAbstractState objects.

    The QAbstractTransition type is the abstract base type of transitions
    between states (QAbstractState objects) of a StateMachine.
    QAbstractTransition is part of \l{The Declarative State Machine Framework}.

    The sourceState() property has the source of the transition. The
    targetState and targetStates properties return the target(s) of the
    transition.

    The triggered() signal is emitted when the transition has been triggered.

    Do not use QAbstractTransition directly; use SignalTransition or
    TimeoutTransition instead.

    \sa SignalTransition, TimeoutTransition
*/

/*!
    \qmlproperty bool QAbstractTransition::sourceState
    \readonly sourceState

    \brief The source state (parent) of this transition.
*/

/*!
    \qmlproperty QAbstractState QAbstractTransition::targetState

    \brief The target state of this transition.

    If a transition has no target state, the transition may still be
    triggered, but this will not cause the state machine's configuration to
    change (i.e. the current state will not be exited and re-entered).
*/

/*!
    \qmlproperty list<QAbstractState> QAbstractTransition::targetStates

    \brief The target states of this transition.

    If multiple states are specified, they all must be descendants of the
    same parallel group state.
*/

/*!
    \qmlsignal QAbstractTransition::triggered()

    This signal is emitted when the transition has been triggered.

    The corresponding handler is \c onTriggered.
*/

/*!
    \qmltype QSignalTransition
    \inqmlmodule QtQml.StateMachine
    \inherits QAbstractTransition
    \omit
    \ingroup statemachine-qmltypes
    \endomit
    \since 5.4

    \brief The QSignalTransition type provides a transition based on a Qt signal.

    Do not use QSignalTransition directly; use SignalTransition or
    TimeoutTransition instead.

    \sa SignalTransition, TimeoutTransition
*/

/*!
    \qmlproperty string QSignalTransition::signal

    \brief The signal which is associated with this signal transition.
*/

/*!
    \qmlproperty QObject QSignalTransition::senderObject

    \brief The sender object which is associated with this signal transition.
*/


/*!
    \qmltype SignalTransition
    \inqmlmodule QtQml.StateMachine
    \inherits QSignalTransition
    \ingroup statemachine-qmltypes
    \since 5.4

    \brief The SignalTransition type provides a transition based on a Qt signal.

    SignalTransition is part of \l{The Declarative State Machine Framework}.

    \section1 Example Usage

    \snippet qml/statemachine/signaltransition.qml document

    \clearfloat

    \sa StateMachine, FinalState, TimeoutTransition
*/

/*!
    \qmlproperty signal SignalTransition::signal

    \brief The signal which is associated with this signal transition.

    \snippet qml/statemachine/signaltransitionsignal.qml document
*/

/*!
    \qmlproperty bool SignalTransition::guard

    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
*/

#include "moc_signaltransition.cpp"