KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
elementutil.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 "elementutil.h"
17
18#include "element.h"
19#include "state.h"
20#include "transition.h"
21
22using namespace KDSME;
23
25{
26 if (!state)
27 return nullptr;
28
29 const auto childStates = state->childStates();
30 for (State *child : childStates) {
31 if (auto *pseudoState = qobject_cast<PseudoState *>(child)) {
32 if (pseudoState->kind() == PseudoState::InitialState) {
33 const Transition *transition = pseudoState->transitions().value(0);
34 return transition ? transition->targetState() : nullptr;
35 }
36 }
37 }
38 return nullptr;
39}
40
41void ElementUtil::setInitialState(State *state, State *initialState)
42{
43 if (!state)
44 return;
45
46 QString pseudoStateName;
47 QString transitionName;
48 const auto childStates = state->childStates();
49 for (State *child : childStates) {
50 if (auto *pseudoState = qobject_cast<PseudoState *>(child)) {
51 if (pseudoState->kind() == PseudoState::InitialState) {
52 pseudoStateName = pseudoState->label();
53 const Transition *transition = pseudoState->transitions().value(0);
54 if (transition)
55 transitionName = transition->label();
56 delete pseudoState;
57 }
58 }
59 }
60
61 if (!initialState)
62 return;
63
64 if (pseudoStateName.isEmpty()) {
65 pseudoStateName = QStringLiteral("initalState_%1_%2").arg(state->label()).arg(initialState->label()); // clazy:exclude=qstring-arg
66 }
67
68 if (transitionName.isEmpty()) {
69 transitionName = QStringLiteral("transitionInitalState_%1_%2").arg(state->label()).arg(initialState->label()); // clazy:exclude=qstring-arg
70 }
71
73 ps->setLabel(pseudoStateName);
74
75 auto *tr = new Transition(ps);
76 tr->setLabel(transitionName);
77 tr->setTargetState(initialState);
78}
79
80State *ElementUtil::findState(State *root, const QString &label)
81{
82 if (!root || root->label().isEmpty())
83 return nullptr;
84
85 if (label == root->label())
86 return root;
87
88 const auto childStates = root->childStates();
89 for (State *state : childStates) {
90 if (State *st = findState(state, label)) { // cppcheck-suppress useStlAlgorithm
91 return st;
92 }
93 }
94
95 return nullptr;
96}
97
99{
100 QObject *current = const_cast<Element *>(element);
101 while (current != nullptr) {
102 if (auto *machine = qobject_cast<StateMachine *>(current))
103 return machine;
104 current = current->parent();
105 }
106 return nullptr;
107}
void setLabel(const QString &label)
Definition element.cpp:63
QString label
Definition element.h:40
KDSME::Element * parent
Definition element.h:37
QList< State * > childStates() const
Definition state.cpp:93
KDSME::State * targetState
Definition transition.h:27
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 State * findState(State *root, const QString &label)

© 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