KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
state.cpp
Go to the documentation of this file.
1/*
2 This file is part of the KDAB State Machine Editor Library.
3
4 SPDX-FileCopyrightText: 2014 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 Author: Kevin Funk <kevin.funk@kdab.com>
6
7 SPDX-License-Identifier: LGPL-2.1-only OR LicenseRef-KDAB-KDStateMachineEditor
8
9 Licensees holding valid commercial KDAB State Machine Editor Library
10 licenses may use this file in accordance with the KDAB State Machine Editor
11 Library License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14*/
15
16#include "state.h"
17
18#include "elementutil.h"
19#include "objecthelper.h"
20#include "runtimecontroller.h"
21#include "transition.h"
22
23#include <QDebug>
24#include <QEvent>
25#include <QQmlEngine>
26
27using namespace KDSME;
28
29namespace {
30
31struct StandardRuntimeController : public RuntimeController // clazy:exclude=ctor-missing-parent-argument
32{
33 Q_OBJECT
34};
35
36QString kindToString(PseudoState::Kind kind)
37{
38 switch (kind) {
40 return QObject::tr("Initial");
41 }
42 return QString();
43}
44
45}
46
47struct State::Private
48{
49 Private()
50 : m_childMode(ExclusiveStates)
51 , m_isComposite(false)
52 , m_isExpanded(true)
53 {
54 }
55
56 QString m_onEntry;
57 QString m_onExit;
58 ChildMode m_childMode;
59 bool m_isComposite;
60 bool m_isExpanded;
61};
62
64 : Element(parent)
65 , d(new Private)
66{
67}
68
70{
71}
72
74{
75 return StateType;
76}
77
79{
80 return qobject_cast<State *>(parent());
81}
82
87
92
93QList<State *> State::childStates() const
94{
95 return ObjectHelper::copy_if_type<State *>(children());
96}
97
98QList<Transition *> State::transitions() const
99{
101}
102
104{
105 transition->setParent(this);
106}
107
108SignalTransition *State::addSignalTransition(State *target, const QString &silgnal)
109{
110 if (!target) {
111 return nullptr;
112 }
113
114 auto *transition = new SignalTransition(this);
115 transition->setTargetState(target);
116 transition->setSignal(silgnal);
117 addTransition(transition);
118 return transition;
119}
120
122{
123 if (!target) {
124 return nullptr;
125 }
126
127 auto *transition = new TimeoutTransition(this);
128 transition->setTargetState(target);
129 transition->setTimeout(timeout);
130 addTransition(transition);
131 return transition;
132}
133
134QString State::onEntry() const
135{
136 return d->m_onEntry;
137}
138
139QString State::onExit() const
140{
141 return d->m_onExit;
142}
143
144void State::setOnEntry(const QString &onEntry)
145{
146 if (d->m_onEntry == onEntry)
147 return;
148
149 d->m_onEntry = onEntry;
150 Q_EMIT onEntryChanged(d->m_onEntry);
151}
152
153void State::setOnExit(const QString &onExit)
154{
155 if (d->m_onExit == onExit)
156 return;
157
158 d->m_onExit = onExit;
159 Q_EMIT onExitChanged(d->m_onExit);
160}
161
163{
164 return d->m_childMode;
165}
166
168{
169 if (d->m_childMode == childMode)
170 return;
171
172 d->m_childMode = childMode;
173 Q_EMIT childModeChanged(d->m_childMode);
174}
175
177{
178 return d->m_isComposite;
179}
180
182{
183 return d->m_isExpanded;
184}
185
186void State::setExpanded(bool expanded)
187{
188 if (d->m_isExpanded == expanded)
189 return;
190
191 d->m_isExpanded = expanded;
192 Q_EMIT expandedChanged(d->m_isExpanded);
193}
194
196{
198 QQmlEngine::setObjectOwnership(m, QQmlEngine::CppOwnership);
199 return m;
200}
201
202bool State::event(QEvent *event)
203{
204 if (event->type() == QEvent::ChildAdded || event->type() == QEvent::ChildRemoved) {
205 const bool newIsComposite = !childStates().empty();
206 if (d->m_isComposite != newIsComposite) {
207 d->m_isComposite = newIsComposite;
208 Q_EMIT isCompositeChanged(d->m_isComposite);
209 }
210 }
211
212 return QObject::event(event);
213}
214
215struct StateMachine::Private
216{
217 Private(StateMachine *q)
218 : q(q)
219 , m_runtimeController(new StandardRuntimeController)
220 {
221 }
222
223 ~Private()
224 {
225 if (qobject_cast<StandardRuntimeController *>(m_runtimeController)) {
226 delete m_runtimeController;
227 }
228 }
229
230 StateMachine *q;
231
232 RuntimeController *m_runtimeController;
233};
234
236 : State(nullptr)
237 , d(new Private(this))
238{
239 // Can't pass the parent to the State constructor, as it expects a State
240 // But this works as expected regardless of whether parent is a State or not
242
243 setWidth(128);
244 setHeight(128);
245}
246
248{
249 return d->m_runtimeController;
250}
251
253{
254 if (d->m_runtimeController == runtimeController)
255 return;
256
257 if (qobject_cast<StandardRuntimeController *>(d->m_runtimeController)) {
258 d->m_runtimeController->deleteLater();
259 }
260 d->m_runtimeController = runtimeController;
261 if (!d->m_runtimeController) {
262 d->m_runtimeController = new StandardRuntimeController;
263 }
264
265 Q_EMIT runtimeControllerChanged(d->m_runtimeController);
266}
267
268struct HistoryState::Private
269{
270 State *m_defaultState = nullptr;
271 HistoryType m_historyType = ShallowHistory;
272};
273
275 : State(parent)
276 , d(new Private)
277{
278}
279
281 : State(parent)
282 , d(new Private)
283{
284 d->m_historyType = type;
285}
286
290
295
297{
298 const QString thisClassName = ObjectHelper::className(this, ObjectHelper::StripNameSpace);
299 const QString defaultClassName = d->m_defaultState ? ObjectHelper::className(d->m_defaultState, ObjectHelper::StripNameSpace) : QStringLiteral("None");
300 return QStringLiteral("%1 [Default: %2]").arg(thisClassName).arg(defaultClassName); // clazy:exclude=qstring-arg
301}
302
304{
305 return d->m_defaultState;
306}
307
309{
310 if (d->m_defaultState == state)
311 return;
312 d->m_defaultState = state;
313 Q_EMIT defaultStateChanged(d->m_defaultState);
314}
315
317{
318 return d->m_historyType;
319}
320
322{
323 if (d->m_historyType == historyType)
324 return;
325 d->m_historyType = historyType;
326 Q_EMIT historyTypeChanged();
327}
328
329struct FinalState::Private
330{
331};
332
334 : State(parent)
335 , d(new Private)
336{
337}
338
342
344{
345 return FinalStateType;
346}
347
351
356
357struct PseudoState::Private
358{
359 Private()
360 : m_kind(PseudoState::InitialState)
361 {
362 }
363
364 Kind m_kind;
365};
366
368 : State(parent)
369 , d(new Private)
370{
371 d->m_kind = kind;
372}
373
377
379{
380 return PseudoStateType;
381}
382
384{
385 return d->m_kind;
386}
387
389{
390 if (d->m_kind == kind)
391 return;
392
393 d->m_kind = kind;
394 Q_EMIT kindChanged(d->m_kind);
395}
396
398{
399 return kindToString(d->m_kind);
400}
401
403{
404 const QString str = ObjectHelper::className(this, ObjectHelper::StripNameSpace);
405 return QStringLiteral("%1 [Kind: %2]").arg(str).arg(kindString()); // clazy:exclude=qstring-arg
406}
407
408QDebug KDSME::operator<<(QDebug dbg, const State *state)
409{
410 if (!state) {
411 return operator<<(dbg, static_cast<QObject *>(nullptr));
412 }
413 dbg.nospace() << "State["
414 << "this=" << ( const void * )state
415 << ", label=" << state->label()
416 << "]";
417 return dbg.space();
418}
419
420QDebug KDSME::operator<<(QDebug dbg, const PseudoState *state)
421{
422 if (!state) {
423 return operator<<(dbg, static_cast<QObject *>(nullptr));
424 }
425 dbg.nospace() << "PseudoState["
426 << "this=" << ( const void * )state
427 << ", kind=" << state->kindString()
428 << "]";
429 return dbg.space();
430}
431
432#include "state.moc"
void setWidth(qreal width)
Definition element.cpp:111
void setParent(QObject *object)
Definition element.cpp:240
QString label
Definition element.h:40
KDSME::Element * parent
Definition element.h:37
void setHeight(qreal height)
Definition element.cpp:125
FinalState(State *parent=nullptr)
Definition state.cpp:333
Type type() const override
Definition state.cpp:343
HistoryState(State *parent=nullptr)
Definition state.cpp:274
Type type() const override
Definition state.cpp:291
KDSME::State * defaultState
Definition state.h:105
QString toDisplayString() const override
Definition state.cpp:296
void defaultStateChanged(KDSME::State *state)
void setDefaultState(State *state)
Definition state.cpp:308
HistoryType historyType
Definition state.h:106
void setHistoryType(HistoryType historyType)
Definition state.cpp:321
PseudoState(Kind kind=InitialState, State *parent=nullptr)
Definition state.cpp:367
QString kindString() const
Definition state.cpp:397
void kindChanged(KDSME::PseudoState::Kind kind)
void setKind(Kind kind)
Definition state.cpp:388
Type type() const override
Definition state.cpp:378
QString toDisplayString() const override
Definition state.cpp:402
void setRuntimeController(RuntimeController *runtimeController)
Definition state.cpp:252
Type type() const override
Definition state.cpp:352
KDSME::RuntimeController * runtimeController
Definition state.h:158
void runtimeControllerChanged(KDSME::RuntimeController *runtimeController)
StateMachine(QObject *parent=nullptr)
Definition state.cpp:235
void setInitialState(State *initialState)
Definition state.cpp:88
bool expanded
Definition state.h:35
void setOnExit(const QString &onExit)
Definition state.cpp:153
void setExpanded(bool expanded)
Definition state.cpp:186
void addTransition(Transition *transition)
Definition state.cpp:103
void setOnEntry(const QString &onEntry)
Definition state.cpp:144
void onExitChanged(const QString &onExit)
ChildMode childMode
Definition state.h:33
TimeoutTransition * addTimeoutTransition(State *target, int timeout)
Definition state.cpp:121
QList< Transition * > transitions() const
Definition state.cpp:98
bool isExpanded() const
Definition state.cpp:181
QList< State * > childStates() const
Definition state.cpp:93
void childModeChanged(KDSME::State::ChildMode childMode)
void onEntryChanged(const QString &onEntry)
State(State *parent=nullptr)
Definition state.cpp:63
SignalTransition * addSignalTransition(State *target, const QString &silgnal=QString())
Definition state.cpp:108
bool event(QEvent *event) override
Definition state.cpp:202
@ ExclusiveStates
Definition state.h:40
bool isComposite
Definition state.h:34
State * initialState() const
Definition state.cpp:83
QString onExit
Definition state.h:32
void isCompositeChanged(bool isComposite)
void setChildMode(ChildMode childMode)
Definition state.cpp:167
Q_INVOKABLE KDSME::StateMachine * machine() const
Definition state.cpp:195
Q_INVOKABLE KDSME::State * parentState() const
Definition state.cpp:78
void expandedChanged(bool expanded)
QString onEntry
Definition state.h:31
Type type() const override
Definition state.cpp:73
KDSME_CORE_EXPORT void setInitialState(State *state, State *initialState)
KDSME_CORE_EXPORT State * findInitialState(const State *state)
KDSME_CORE_EXPORT StateMachine * findStateMachine(const Element *element)
KDSME_CORE_EXPORT QString className(const QObject *object, DisplayOption option=NoStrip)
QList< FilterType > copy_if_type(const QList< ItemType > &list)
KDSME_CORE_EXPORT QDebug operator<<(QDebug dbg, const State *state)
Definition state.cpp:408

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8