1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickstatechangescript_p.h"
5
6#include <qqml.h>
7#include <qqmlcontext.h>
8#include <qqmlexpression.h>
9#include <qqmlinfo.h>
10#include <private/qqmlcontext_p.h>
11#include <private/qqmlproperty_p.h>
12#include <private/qqmlbinding_p.h>
13#include "qquickstate_p_p.h"
14
15#include <QtCore/qdebug.h>
16#include <QtCore/qmath.h>
17
18#include <private/qobject_p.h>
19
20QT_BEGIN_NAMESPACE
21
22class QQuickStateChangeScriptPrivate : public QQuickStateOperationPrivate
23{
24public:
25 QQuickStateChangeScriptPrivate() {}
26
27 QQmlScriptString script;
28 QString name;
29};
30
31/*!
32 \qmltype StateChangeScript
33 \instantiates QQuickStateChangeScript
34 \inqmlmodule QtQuick
35 \ingroup qtquick-states
36 \brief Specifies how to run a script in a state.
37
38 A StateChangeScript is run upon entering a state. You can optionally use
39 ScriptAction to specify the point in the transition at which
40 the StateChangeScript should be run.
41
42 \snippet qml/states/statechangescript.qml state and transition
43
44 \sa ScriptAction
45*/
46
47QQuickStateChangeScript::QQuickStateChangeScript(QObject *parent)
48: QQuickStateOperation(*(new QQuickStateChangeScriptPrivate), parent)
49{
50}
51
52/*!
53 \qmlproperty script QtQuick::StateChangeScript::script
54 This property holds the script to run when the state is current.
55*/
56QQmlScriptString QQuickStateChangeScript::script() const
57{
58 Q_D(const QQuickStateChangeScript);
59 return d->script;
60}
61
62void QQuickStateChangeScript::setScript(const QQmlScriptString &s)
63{
64 Q_D(QQuickStateChangeScript);
65 d->script = s;
66}
67
68/*!
69 \qmlproperty string QtQuick::StateChangeScript::name
70 This property holds the name of the script. This name can be used by a
71 ScriptAction to target a specific script.
72
73 \sa ScriptAction::scriptName
74*/
75QString QQuickStateChangeScript::name() const
76{
77 Q_D(const QQuickStateChangeScript);
78 return d->name;
79}
80
81void QQuickStateChangeScript::setName(const QString &n)
82{
83 Q_D(QQuickStateChangeScript);
84 d->name = n;
85}
86
87void QQuickStateChangeScript::execute()
88{
89 Q_D(QQuickStateChangeScript);
90 if (!d->script.isEmpty()) {
91 QQmlExpression expr(d->script);
92 expr.evaluate();
93 if (expr.hasError())
94 qmlWarning(me: this, error: expr.error());
95 }
96}
97
98QQuickStateChangeScript::ActionList QQuickStateChangeScript::actions()
99{
100 ActionList rv;
101 QQuickStateAction a;
102 a.event = this;
103 rv << a;
104 return rv;
105}
106
107QQuickStateActionEvent::EventType QQuickStateChangeScript::type() const
108{
109 return Script;
110}
111
112QT_END_NAMESPACE
113
114#include <moc_qquickstatechangescript_p.cpp>
115

source code of qtdeclarative/src/quick/util/qquickstatechangescript.cpp