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 QtScxml 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 "qscxmldatamodel_p.h"
41#include "qscxmlnulldatamodel.h"
42#if QT_CONFIG(scxml_ecmascriptdatamodel)
43#include "qscxmlecmascriptdatamodel.h"
44#endif
45#include "qscxmlstatemachine_p.h"
46
47QT_BEGIN_NAMESPACE
48
49/*!
50 \class QScxmlDataModel::ForeachLoopBody
51 \brief The ForeachLoopBody class represents a function to be executed on
52 each iteration of an SCXML foreach loop.
53 \since 5.8
54 \inmodule QtScxml
55 */
56
57/*!
58 Creates a new foreach loop body.
59 */
60QScxmlDataModel::ForeachLoopBody::ForeachLoopBody()
61{}
62/*!
63 Destroys a foreach loop body.
64 */
65QScxmlDataModel::ForeachLoopBody::~ForeachLoopBody()
66{}
67
68/*!
69 \fn QScxmlDataModel::ForeachLoopBody::run(bool *ok)
70
71 This function is executed on each iteration. If the execution fails, \a ok is
72 set to \c false, otherwise it is set to \c true.
73 */
74
75/*!
76 * \class QScxmlDataModel
77 * \brief The QScxmlDataModel class is the data model base class for a Qt SCXML
78 * state machine.
79 * \since 5.7
80 * \inmodule QtScxml
81 *
82 * SCXML data models are described in
83 * \l {SCXML Specification - 5 Data Model and Data Manipulation}. For more
84 * information about supported data models, see \l {SCXML Compliance}.
85 *
86 * One data model can only belong to one state machine.
87 *
88 * \sa QScxmlStateMachine QScxmlCppDataModel QScxmlEcmaScriptDataModel QScxmlNullDataModel
89 */
90
91/*!
92 \property QScxmlDataModel::stateMachine
93
94 \brief The state machine this data model belongs to.
95
96 A data model can only belong to a single state machine and a state machine
97 can only have one data model. This relation needs to be set up before the
98 state machine is started. Setting this property on a data model will
99 automatically set the corresponding \c dataModel property on the
100 \a stateMachine.
101*/
102
103/*!
104 * Creates a new data model, with the parent object \a parent.
105 */
106QScxmlDataModel::QScxmlDataModel(QObject *parent)
107 : QObject(*(new QScxmlDataModelPrivate), parent)
108{
109}
110
111/*!
112 Creates a new data model from the private object \a dd, with the parent
113 object \a parent.
114 */
115QScxmlDataModel::QScxmlDataModel(QScxmlDataModelPrivate &dd, QObject *parent) :
116 QObject(dd, parent)
117{
118}
119
120/*!
121 * Sets the state machine this model belongs to to \a stateMachine. There is a
122 * 1:1 relation between state machines and models. After setting the state
123 * machine once you cannot change it anymore. Any further attempts to set the
124 * state machine using this method will be ignored.
125 */
126void QScxmlDataModel::setStateMachine(QScxmlStateMachine *stateMachine)
127{
128 Q_D(QScxmlDataModel);
129
130 if (d->m_stateMachine == nullptr && stateMachine != nullptr) {
131 d->m_stateMachine = stateMachine;
132 if (stateMachine)
133 stateMachine->setDataModel(this);
134 emit stateMachineChanged(stateMachine);
135 }
136}
137
138/*!
139 * Returns the state machine associated with the data model.
140 */
141QScxmlStateMachine *QScxmlDataModel::stateMachine() const
142{
143 Q_D(const QScxmlDataModel);
144 return d->m_stateMachine;
145}
146
147QScxmlDataModel *QScxmlDataModelPrivate::instantiateDataModel(DocumentModel::Scxml::DataModelType type)
148{
149 QScxmlDataModel *dataModel = nullptr;
150 switch (type) {
151 case DocumentModel::Scxml::NullDataModel:
152 dataModel = new QScxmlNullDataModel;
153 break;
154 case DocumentModel::Scxml::JSDataModel:
155#if QT_CONFIG(scxml_ecmascriptdatamodel)
156 dataModel = new QScxmlEcmaScriptDataModel;
157#endif
158 break;
159 case DocumentModel::Scxml::CppDataModel:
160 break;
161 default:
162 Q_UNREACHABLE();
163 }
164
165 return dataModel;
166}
167
168/*!
169 * \fn QScxmlDataModel::setup(const QVariantMap &initialDataValues)
170 *
171 * Initializes the data model with the initial values specified by
172 * \a initialDataValues.
173 *
174 * Returns \c false if parse errors occur or if any of the initialization steps
175 * fail. Returns \c true otherwise.
176 */
177
178/*!
179 * \fn QScxmlDataModel::setScxmlEvent(const QScxmlEvent &event)
180 *
181 * Sets the \a event to use in the subsequent executable content execution.
182 */
183
184/*!
185 * \fn QScxmlDataModel::scxmlProperty(const QString &name) const
186 *
187 * Returns the value of the property \a name.
188 */
189
190/*!
191 * \fn QScxmlDataModel::hasScxmlProperty(const QString &name) const
192 *
193 * Returns \c true if a property with the given \a name exists, \c false
194 * otherwise.
195 */
196
197/*!
198 * \fn QScxmlDataModel::setScxmlProperty(const QString &name,
199 * const QVariant &value,
200 * const QString &context)
201 *
202 * Sets a the value \a value for the property \a name.
203 *
204 * The \a context is a string that is used in error messages to indicate the
205 * location in the SCXML file where the error occurred.
206 *
207 * Returns \c true if successful or \c false if an error occurred.
208 */
209
210/*!
211 * \fn QScxmlDataModel::evaluateToString(
212 * QScxmlExecutableContent::EvaluatorId id, bool *ok)
213 * Evaluates the executable content pointed to by \a id and sets \a ok to
214 * \c false if there was an error or to \c true if there was not.
215 * Returns the result of the evaluation as a QString.
216 */
217
218/*!
219 * \fn QScxmlDataModel::evaluateToBool(QScxmlExecutableContent::EvaluatorId id,
220 * bool *ok)
221 * Evaluates the executable content pointed to by \a id and sets \a ok to
222 * \c false if there was an error or to \c true if there was not.
223 * Returns the result of the evaluation as a boolean value.
224 */
225
226/*!
227 * \fn QScxmlDataModel::evaluateToVariant(
228 * QScxmlExecutableContent::EvaluatorId id, bool *ok)
229 * Evaluates the executable content pointed to by \a id and sets \a ok to
230 * \c false if there was an error or to \c true if there was not.
231 * Returns the result of the evaluation as a QVariant.
232 */
233
234/*!
235 * \fn QScxmlDataModel::evaluateToVoid(QScxmlExecutableContent::EvaluatorId id,
236 * bool *ok)
237 * Evaluates the executable content pointed to by \a id and sets \a ok to
238 * \c false if there was an error or to \c true if there was not.
239 * The execution is expected to return no result.
240 */
241
242/*!
243 * \fn QScxmlDataModel::evaluateAssignment(
244 * QScxmlExecutableContent::EvaluatorId id, bool *ok)
245 * Evaluates the assignment pointed to by \a id and sets \a ok to
246 * \c false if there was an error or to \c true if there was not.
247 */
248
249/*!
250 * \fn QScxmlDataModel::evaluateInitialization(
251 * QScxmlExecutableContent::EvaluatorId id, bool *ok)
252 * Evaluates the initialization pointed to by \a id and sets \a ok to
253 * \c false if there was an error or to \c true if there was not.
254 */
255
256/*!
257 * \fn QScxmlDataModel::evaluateForeach(
258 * QScxmlExecutableContent::EvaluatorId id, bool *ok,
259 * ForeachLoopBody *body)
260 * Evaluates the foreach loop pointed to by \a id and sets \a ok to
261 * \c false if there was an error or to \c true if there was not. The
262 * \a body is executed on each iteration.
263 */
264
265QT_END_NAMESPACE
266

source code of qtscxml/src/scxml/qscxmldatamodel.cpp