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)
7 : _timeoutMsecs(timeoutMsecs)
8{
9 // Wire the QSignalTransition sender/signal after the base ctor returns.
10 // Passing `this` to the base ctor while members are still uninitialized
11 // trips GCC's `-Wmaybe-uninitialized` analyzer (false positive).
12 setSenderObject(this);
13 setSignal(SIGNAL(timeout()));
14
15 if (target) {
16 setTargetState(target);
17 }
18
19 _timer.setSingleShot(true);
20 connect(&_timer, &QTimer::timeout, this, &TimeoutTransition::timeout);
21}
22
24{
25 if (!source || _sourceAttached) {
26 return;
27 }
28
29 connect(source, &QState::entered, this, &TimeoutTransition::_onSourceStateEntered, Qt::UniqueConnection);
30 connect(source, &QState::exited, this, &TimeoutTransition::_onSourceStateExited, Qt::UniqueConnection);
31 _sourceAttached = true;
32}
33
34void TimeoutTransition::_onAddedToState()
35{
36 attachToSourceState(qobject_cast<QState*>(sourceState()));
37}
38
39void TimeoutTransition::_onSourceStateEntered()
40{
41 _timer.start(_timeoutMsecs);
42}
43
44void TimeoutTransition::_onSourceStateExited()
45{
46 _timer.stop();
47}
48
50{
51 if (e->type() == QEvent::ParentChange) {
52 _onAddedToState();
53
54 // sourceState() may not be finalized during the immediate ParentChange
55 // callback in some addTransition paths; retry once on the next turn.
56 if (!_sourceAttached) {
57 QTimer::singleShot(0, this, [this]() { _onAddedToState(); });
58 }
59 }
60 return QGCSignalTransition::event(e);
61}
62
64{
65 Q_UNUSED(event);
66}
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)