QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
TimeoutTransition.cc
Go to the documentation of this file.
1#include "TimeoutTransition.h"
2
3#include <QtStateMachine/QState>
4#include <QtStateMachine/QStateMachine>
5
6TimeoutTransition::TimeoutTransition(int timeoutMsecs, QAbstractState* target)
8 , _timeoutMsecs(timeoutMsecs)
9{
10 if (target) {
11 setTargetState(target);
12 }
13
14 _timer.setSingleShot(true);
15 connect(&_timer, &QTimer::timeout, this, &TimeoutTransition::timeout);
16}
17
19{
20 if (!source || _sourceAttached) {
21 return;
22 }
23
24 connect(source, &QState::entered, this, &TimeoutTransition::_onSourceStateEntered, Qt::UniqueConnection);
25 connect(source, &QState::exited, this, &TimeoutTransition::_onSourceStateExited, Qt::UniqueConnection);
26 _sourceAttached = true;
27}
28
29void TimeoutTransition::_onAddedToState()
30{
31 // Connect to source state's entered/exited signals for timer control
32 attachToSourceState(qobject_cast<QState*>(sourceState()));
33}
34
35void TimeoutTransition::_onSourceStateEntered()
36{
37 _timer.start(_timeoutMsecs);
38}
39
40void TimeoutTransition::_onSourceStateExited()
41{
42 _timer.stop();
43}
44
46{
47 // Detect when transition is added to a state (parent changes)
48 if (e->type() == QEvent::ParentChange) {
49 _onAddedToState();
50
51 // In some addTransition paths sourceState() is not finalized yet during
52 // the immediate ParentChange callback. Retry once on the next turn.
53 if (!_sourceAttached) {
54 QTimer::singleShot(0, this, [this]() { _onAddedToState(); });
55 }
56 }
57 return QGCSignalTransition::event(e);
58}
59
61{
62 Q_UNUSED(event);
63 // Timer already stopped by _onSourceStateExited or will be stopped when we leave
64}
Base class for signal-based transitions that need access to QGCStateMachine and Vehicle.
void onTransition(QEvent *event) override
TimeoutTransition(int timeoutMsecs, QAbstractState *target=nullptr)
bool event(QEvent *e) override
void timeout()
Emitted when the timeout expires (internal use - triggers transition)
void attachToSourceState(QState *source)