18#include <QAbstractTransition>
21#include <QStateMachine>
25 , m_watchedStateMachine(nullptr)
26 , m_lastEnteredState(nullptr)
27 , m_lastExitedState(nullptr)
37 if (m_watchedStateMachine == machine) {
41 m_watchedStateMachine = machine;
44 const auto children = machine->findChildren<QAbstractState *>();
45 for (QAbstractState *state : children) {
54 return m_watchedStateMachine;
57void QSMWatcher::watchState(QAbstractState *state)
59 if (state->machine() != m_watchedStateMachine) {
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);
70 Q_FOREACH (QAbstractTransition *transition, state->findChildren<QAbstractTransition *>()) {
71 connect(transition, SIGNAL(triggered()),
72 this, SLOT(handleTransitionTriggered()), Qt::UniqueConnection);
74 m_watchedStates << state;
77void QSMWatcher::clearWatchedStates()
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()));
84 Q_FOREACH (QAbstractTransition *transition, state->findChildren<QAbstractTransition *>()) {
85 disconnect(transition, SIGNAL(triggered()),
this, SLOT(handleTransitionTriggered()));
88 m_watchedStates.clear();
91void QSMWatcher::handleTransitionTriggered()
93 QAbstractTransition *transition = qobject_cast<QAbstractTransition *>(QObject::sender());
99void QSMWatcher::handleStateEntered()
101 QAbstractState *state = qobject_cast<QAbstractState *>(QObject::sender());
103 if (state->machine() != m_watchedStateMachine) {
107 if (state == m_lastEnteredState) {
111 m_lastEnteredState = state;
115void QSMWatcher::handleStateExited()
117 QAbstractState *state = qobject_cast<QAbstractState *>(QObject::sender());
120 if (state->machine() != m_watchedStateMachine) {
124 if (state == m_lastExitedState) {
128 m_lastExitedState = state;
132void QSMWatcher::handleStateDestroyed()
134 QAbstractState *state =
static_cast<QAbstractState *
>(QObject::sender());
137 const int index = m_watchedStates.indexOf(state);
138 Q_ASSERT(index != -1);
139 m_watchedStates.remove(index);
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)