1 | /**************************************************************************** |
---|---|
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qquickstatechangescript_p.h" |
41 | |
42 | #include <qqml.h> |
43 | #include <qqmlcontext.h> |
44 | #include <qqmlexpression.h> |
45 | #include <qqmlinfo.h> |
46 | #include <private/qqmlcontext_p.h> |
47 | #include <private/qqmlproperty_p.h> |
48 | #include <private/qqmlbinding_p.h> |
49 | #include "qquickstate_p_p.h" |
50 | |
51 | #include <QtCore/qdebug.h> |
52 | #include <QtCore/qmath.h> |
53 | |
54 | #include <private/qobject_p.h> |
55 | |
56 | QT_BEGIN_NAMESPACE |
57 | |
58 | class QQuickStateChangeScriptPrivate : public QQuickStateOperationPrivate |
59 | { |
60 | public: |
61 | QQuickStateChangeScriptPrivate() {} |
62 | |
63 | QQmlScriptString script; |
64 | QString name; |
65 | }; |
66 | |
67 | /*! |
68 | \qmltype StateChangeScript |
69 | \instantiates QQuickStateChangeScript |
70 | \inqmlmodule QtQuick |
71 | \ingroup qtquick-states |
72 | \brief Specifies how to run a script in a state. |
73 | |
74 | A StateChangeScript is run upon entering a state. You can optionally use |
75 | ScriptAction to specify the point in the transition at which |
76 | the StateChangeScript should be run. |
77 | |
78 | \snippet qml/states/statechangescript.qml state and transition |
79 | |
80 | \sa ScriptAction |
81 | */ |
82 | |
83 | QQuickStateChangeScript::QQuickStateChangeScript(QObject *parent) |
84 | : QQuickStateOperation(*(new QQuickStateChangeScriptPrivate), parent) |
85 | { |
86 | } |
87 | |
88 | QQuickStateChangeScript::~QQuickStateChangeScript() |
89 | { |
90 | } |
91 | |
92 | /*! |
93 | \qmlproperty script QtQuick::StateChangeScript::script |
94 | This property holds the script to run when the state is current. |
95 | */ |
96 | QQmlScriptString QQuickStateChangeScript::script() const |
97 | { |
98 | Q_D(const QQuickStateChangeScript); |
99 | return d->script; |
100 | } |
101 | |
102 | void QQuickStateChangeScript::setScript(const QQmlScriptString &s) |
103 | { |
104 | Q_D(QQuickStateChangeScript); |
105 | d->script = s; |
106 | } |
107 | |
108 | /*! |
109 | \qmlproperty string QtQuick::StateChangeScript::name |
110 | This property holds the name of the script. This name can be used by a |
111 | ScriptAction to target a specific script. |
112 | |
113 | \sa ScriptAction::scriptName |
114 | */ |
115 | QString QQuickStateChangeScript::name() const |
116 | { |
117 | Q_D(const QQuickStateChangeScript); |
118 | return d->name; |
119 | } |
120 | |
121 | void QQuickStateChangeScript::setName(const QString &n) |
122 | { |
123 | Q_D(QQuickStateChangeScript); |
124 | d->name = n; |
125 | } |
126 | |
127 | void QQuickStateChangeScript::execute() |
128 | { |
129 | Q_D(QQuickStateChangeScript); |
130 | if (!d->script.isEmpty()) { |
131 | QQmlExpression expr(d->script); |
132 | expr.evaluate(); |
133 | if (expr.hasError()) |
134 | qmlWarning(me: this, error: expr.error()); |
135 | } |
136 | } |
137 | |
138 | QQuickStateChangeScript::ActionList QQuickStateChangeScript::actions() |
139 | { |
140 | ActionList rv; |
141 | QQuickStateAction a; |
142 | a.event = this; |
143 | rv << a; |
144 | return rv; |
145 | } |
146 | |
147 | QQuickStateActionEvent::EventType QQuickStateChangeScript::type() const |
148 | { |
149 | return Script; |
150 | } |
151 | |
152 | |
153 | #include <moc_qquickstatechangescript_p.cpp> |
154 | |
155 | QT_END_NAMESPACE |
156 | |
157 |