QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ConditionalState.cc
Go to the documentation of this file.
1#include "ConditionalState.h"
3
4#include <QtCore/QMetaObject>
5
6ConditionalState::ConditionalState(const QString& stateName, QState* parent, Predicate predicate, Action action)
7 : QGCState(stateName, parent)
8 , _predicate(std::move(predicate))
9 , _action(std::move(action))
10{
11 connect(this, &QState::entered, this, &ConditionalState::_onEntered);
12}
13
14void ConditionalState::_onEntered()
15{
16 if (_predicate && _predicate()) {
17 qCDebug(QGCStateMachineLog) << "Condition met, executing" << stateName();
18 if (_action) {
19 _action();
20 }
21 // Defer signal emission to avoid firing during state entry
22 QMetaObject::invokeMethod(this, &ConditionalState::advance, Qt::QueuedConnection);
23 } else {
24 qCDebug(QGCStateMachineLog) << "Condition not met, skipping" << stateName();
25 // Defer signal emission to avoid firing during state entry
26 QMetaObject::invokeMethod(this, &ConditionalState::skipped, Qt::QueuedConnection);
27 }
28}
ConditionalState(const QString &stateName, QState *parent, Predicate predicate, Action action=Action())
void skipped()
Emitted when the predicate returns false and the state is skipped.
std::function< void()> Action
std::function< bool()> Predicate
QString stateName() const