KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
qsmwatcher.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: 2015 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 "qsmwatcher_p.h"
17
18#include <QAbstractTransition>
19#include <QFinalState>
20#include <QState>
21#include <QStateMachine>
22
23QSMWatcher::QSMWatcher(QObject *parent)
24 : QObject(parent)
25 , m_watchedStateMachine(nullptr)
26 , m_lastEnteredState(nullptr)
27 , m_lastExitedState(nullptr)
28{
29}
30
34
35void QSMWatcher::setWatchedStateMachine(QStateMachine *machine)
36{
37 if (m_watchedStateMachine == machine) {
38 return;
39 }
40
41 m_watchedStateMachine = machine;
42
43 clearWatchedStates();
44 const auto children = machine->findChildren<QAbstractState *>();
45 for (QAbstractState *state : children) {
46 watchState(state);
47 }
48
49 Q_EMIT watchedStateMachineChanged(machine);
50}
51
52QStateMachine *QSMWatcher::watchedStateMachine() const
53{
54 return m_watchedStateMachine;
55}
56
57void QSMWatcher::watchState(QAbstractState *state)
58{
59 if (state->machine() != m_watchedStateMachine) {
60 return;
61 }
62
63 connect(state, SIGNAL(entered()),
64 this, SLOT(handleStateEntered()), Qt::UniqueConnection);
65 connect(state, SIGNAL(exited()),
66 this, SLOT(handleStateExited()), Qt::UniqueConnection);
67 connect(state, SIGNAL(destroyed(QObject *)),
68 this, SLOT(handleStateDestroyed()), Qt::UniqueConnection);
69
70 Q_FOREACH (QAbstractTransition *transition, state->findChildren<QAbstractTransition *>()) {
71 connect(transition, SIGNAL(triggered()),
72 this, SLOT(handleTransitionTriggered()), Qt::UniqueConnection);
73 }
74 m_watchedStates << state;
75}
76
77void QSMWatcher::clearWatchedStates()
78{
79 Q_FOREACH (QAbstractState *state, m_watchedStates) {
80 disconnect(state, SIGNAL(entered()), this, SLOT(handleStateEntered()));
81 disconnect(state, SIGNAL(exited()), this, SLOT(handleStateExited()));
82 disconnect(state, SIGNAL(destroyed(QObject *)), this, SLOT(handleStateDestroyed()));
83
84 Q_FOREACH (QAbstractTransition *transition, state->findChildren<QAbstractTransition *>()) {
85 disconnect(transition, SIGNAL(triggered()), this, SLOT(handleTransitionTriggered()));
86 }
87 }
88 m_watchedStates.clear();
89}
90
91void QSMWatcher::handleTransitionTriggered()
92{
93 QAbstractTransition *transition = qobject_cast<QAbstractTransition *>(QObject::sender());
94 Q_ASSERT(transition);
95
96 Q_EMIT transitionTriggered(transition);
97}
98
99void QSMWatcher::handleStateEntered()
100{
101 QAbstractState *state = qobject_cast<QAbstractState *>(QObject::sender());
102 Q_ASSERT(state);
103 if (state->machine() != m_watchedStateMachine) {
104 return;
105 }
106
107 if (state == m_lastEnteredState) {
108 return;
109 }
110
111 m_lastEnteredState = state;
112 Q_EMIT stateEntered(state);
113}
114
115void QSMWatcher::handleStateExited()
116{
117 QAbstractState *state = qobject_cast<QAbstractState *>(QObject::sender());
118 Q_ASSERT(state);
119
120 if (state->machine() != m_watchedStateMachine) {
121 return;
122 }
123
124 if (state == m_lastExitedState) {
125 return;
126 }
127
128 m_lastExitedState = state;
129 Q_EMIT stateExited(state);
130}
131
132void QSMWatcher::handleStateDestroyed()
133{
134 QAbstractState *state = static_cast<QAbstractState *>(QObject::sender());
135 Q_ASSERT(state);
136
137 const int index = m_watchedStates.indexOf(state);
138 Q_ASSERT(index != -1);
139 m_watchedStates.remove(index);
140}
void setWatchedStateMachine(QStateMachine *machine)
void stateExited(QAbstractState *state)
QStateMachine * watchedStateMachine() const
QSMWatcher(QObject *parent=nullptr)
void transitionTriggered(QAbstractTransition *)
void watchedStateMachineChanged(QStateMachine *)
void stateEntered(QAbstractState *state)

© 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