45#include <QtStateMachine/QStateMachine>
46#include <QtStateMachine/QFinalState>
47#include <QtStateMachine/QHistoryState>
48#include <QtCore/QAbstractAnimation>
49#include <QtCore/QJsonArray>
50#include <QtCore/QString>
51#include <QtCore/QVariant>
96 bool active()
const {
return isRunning(); }
167 int timeoutMsecs = 0);
182 int retryDelayMsecs = 1000,
186 int timeoutMsecs = 0);
202 void postEvent(
const QString& eventName,
const QVariant& data = QVariant(),
203 EventPriority priority = NormalPriority);
210 int postDelayedEvent(
const QString& eventName,
int delayMsecs,
const QVariant& data = QVariant());
246 template<
typename Func>
248 const QObject* sender,
250 int timeoutMsecs = 0)
269 const QString& eventName,
270 int timeoutMsecs = 0);
294 std::function<
void()>
onEntry =
nullptr,
295 std::function<
void()>
onExit =
nullptr);
313 QAbstractState*
findState(
const QString& stateName)
const;
319 return findChildren<T*>();
331 QAbstractState* nextState,
332 const QString& message = QString());
336 const QString& message = QString());
392 template<
typename Func>
393 QSignalTransition*
addTransition(QState* from, Func signal, QAbstractState* to,
394 QAbstractAnimation* animation =
nullptr)
396 auto* transition = from->addTransition(from, signal, to);
398 transition->addAnimation(animation);
409 template<
typename Func>
412 QAbstractAnimation* animation =
nullptr)
415 from->addTransition(transition);
417 transition->addAnimation(animation);
428 QAbstractAnimation* animation =
nullptr);
436 QAbstractAnimation* animation =
nullptr);
444 template<
typename SenderType,
typename Func>
447 std::function<
void()> retryAction,
450 auto* transition =
new RetryTransition(from, signal, to, std::move(retryAction), maxRetries);
451 from->addTransition(transition);
461 template<
typename SenderType,
typename Func>
464 std::function<
bool()> guard)
467 from->addTransition(transition);
493 template<
typename Sender,
typename Signal>
497 std::function<
void()> action)
499 auto* transition = state->addTransition(sender, signal, state);
501 connect(transition, &QAbstractTransition::triggered,
this, std::move(action));
515 template<
typename Sender,
typename Signal>
519 std::function<
void()> action)
522 state->addTransition(transition);
533 QList<QAbstractTransition*>
transitionsFrom(QAbstractState* state)
const;
538 QList<QAbstractTransition*>
transitionsTo(QAbstractState* state)
const;
543 QList<QAbstractState*>
reachableFrom(QAbstractState* state)
const;
548 QList<QAbstractState*>
predecessorsOf(QAbstractState* state)
const;
707 void _onStateEntered();
708 float _calculateProgress()
const;
711 QAbstractState* _globalErrorState =
nullptr;
712 QAbstractState* _recoveryState =
nullptr;
713 bool _deleteOnStop =
false;
719 QList<QAbstractState*> _progressStates;
720 QList<int> _progressWeights;
721 int _progressTotalWeight = 0;
722 int _progressCurrentIndex = -1;
723 float _progressSubProgress = 0.0f;
724 float _progressLastEmitted = 0.0f;
730 QStringList _stateHistory;
731 int _stateHistoryLimit = 20;
734 QHash<QString, int> _timeoutOverrides;
735 QHash<QString, int> _timeoutStats;
Calls a function when entered and waits for an external trigger to advance.
std::function< void(AsyncFunctionState *state)> SetupFunction
A state that evaluates a predicate on entry and either executes or skips.
std::function< void()> Action
std::function< bool()> Predicate
Delays that state machine for the specified time in milliseconds.
ExhaustedBehavior
What to do when all recovery options are exhausted.
@ EmitError
Emit error() signal (default)
std::function< bool()> Action
std::function< void()> VoidAction
Drives the MAVLink events protocol for a single component.
A state that waits for one or more named events before advancing.
A transition that only fires if a guard predicate returns true.
std::function< bool()> Guard
Transition that fires without exiting/re-entering the current state.
Transition that fires when a named event is posted to the state machine.
State that executes all child states in parallel.
Lightweight base class for simple states.
Final state for a QGCStateMachine with logging support.
QGroundControl specific state machine with enhanced error handling.
QAbstractState * recoveryState() const
Get the recovery state.
QList< T * > findStates() const
Find all states of a specific type.
FunctionState * addFunctionState(const QString &stateName, std::function< void()> function)
GuardedTransition * addGuardedTransition(QState *from, Func signal, QAbstractState *to, GuardedTransition::Guard guard, QAbstractAnimation *animation=nullptr)
void recordTimeout(const QString &stateName)
Record a timeout for statistics.
void stopMachine(bool deleteOnStop=true)
bool structuredLoggingEnabled() const
void clearError(bool restart=false)
QAbstractState * globalErrorState() const
Get the global error state.
MachineEventTransition * addEventTransition(QState *from, const QString &eventName, QAbstractState *to, QAbstractAnimation *animation=nullptr)
FunctionState * addLogAndStopErrorState(const QString &stateName, const QString &message=QString())
Create a logging error handler state that stops the machine.
void runningChanged()
Emitted when the running state changes (for QML binding)
void enablePropertyRestore()
int timeoutOverride(const QString &stateName) const
Get the timeout override for a state, or -1 if not set.
void start()
Start the state machine with debug logging.
void progressUpdate(float progress)
void setEventHandler(EventHandler handler)
bool isPropertyRestoreEnabled() const
Check if property restoration is enabled.
QList< QAbstractTransition * > transitionsFrom(QAbstractState *state) const
TimeoutTransition * addTimeoutTransition(QState *from, int timeoutMsecs, QAbstractState *to, QAbstractAnimation *animation=nullptr)
void logConfiguration() const
Log the full configuration to debug output.
bool profilingEnabled() const
ParallelState * addParallelState(const QString &stateName)
void setGlobalErrorState(QAbstractState *errorState)
QList< QAbstractState * > reachableFrom(QAbstractState *state) const
void onExit(QEvent *event) override
bool resetToState(QAbstractState *state)
QString dumpRecordedHistory() const
QGCState * addErrorRecoveryState(const QString &stateName, ErrorRecoveryBuilder::Action action, int maxRetries=0, int retryDelayMsecs=1000, ErrorRecoveryBuilder::ExhaustedBehavior exhaustedBehavior=ErrorRecoveryBuilder::EmitError, ErrorRecoveryBuilder::Action fallback=nullptr, ErrorRecoveryBuilder::VoidAction rollback=nullptr, int timeoutMsecs=0)
QList< QAbstractState * > predecessorsOf(QAbstractState *state) const
QSet< QAbstractState * > activeStates() const
std::function< void()> ExitCallback
QString exportAsDot() const
void setStructuredLoggingEnabled(bool enabled)
Enable/disable structured state-machine logger.
bool historyRecordingEnabled() const
std::function< void()> EntryCallback
Name of the currently active state (for QML binding)
void clearTimeoutOverrides()
Clear all timeout overrides.
void currentStateNameChanged()
Emitted when the current state changes (for QML binding)
EventQueuedState * addEventQueuedState(const QString &stateName, const QString &eventName, int timeoutMsecs=0)
void logCurrentState() const
Log the current state to debug output.
QJsonArray recordedHistoryJson() const
void setSubProgress(float subProgress)
QList< QAbstractState * > deadEndStates() const
const StateContext & context() const
int postDelayedEvent(const QString &eventName, int delayMsecs, const QVariant &data=QVariant())
QList< QAbstractState * > unreachableStates() const
void setStateHistoryLimit(int limit)
Set the maximum number of state history entries to keep.
QString machineName() const
QSignalTransition * addSelfLoopTransition(QState *state, Sender *sender, Signal signal, std::function< void()> action)
AsyncFunctionState * addAsyncFunctionState(const QString &stateName, AsyncFunctionState::SetupFunction setupFunction, int timeoutMsecs=0)
bool isStateActive(QAbstractState *state) const
void machineEvent(const QString &eventName)
SubMachineState * addSubMachineState(const QString &stateName, SubMachineState::MachineFactory factory)
int maxPathLength() const
QAbstractState * findState(const QString &stateName) const
void setOnEntry(EntryCallback callback)
Set a callback to be invoked when the machine starts.
QHash< QString, int > timeoutStats() const
Get timeout statistics (how often each state timed out)
void setHistoryRecordingEnabled(bool enabled, int maxEntries=1000)
Enable/disable structured transition history recording.
bool event(QEvent *event) override
void setProfilingEnabled(bool enabled)
Enable/disable state timing profiler.
bool cancelDelayedEvent(int eventId)
Vehicle * vehicle() const
void setInitialState(QAbstractState *state, bool autoStart=false)
void setOnExit(ExitCallback callback)
Set a callback to be invoked when the machine stops.
void setProgressWeights(const QList< QPair< QAbstractState *, int > > &stateWeights)
void setRecoveryState(QAbstractState *state)
DelayState * addDelayState(int delayMsecs)
void ensureRunning()
Start the machine if not already running.
QString dumpConfiguration() const
void clearTimeoutStats()
Clear timeout statistics.
QString lastError() const
Get the last error message (from Qt's internal error handling)
QList< QAbstractTransition * > transitionsTo(QAbstractState *state) const
QString currentStateName() const
InternalTransition * addInternalTransition(QState *state, Sender *sender, Signal signal, std::function< void()> action)
WaitForSignalState * addWaitForSignalState(const QString &stateName, const QObject *sender, Func signal, int timeoutMsecs=0)
QGCFinalState * addFinalState(const QString &stateName=QString())
void resetProgress()
Reset progress tracking (call when restarting the machine)
QStringList stateHistory() const
bool isInErrorState() const
Check if the machine is in an error state.
QHash< QString, int > allTimeoutOverrides() const
Get all timeout overrides.
void setTimeoutOverride(const QString &stateName, int timeoutMsecs)
QState * createTimedActionState(const QString &stateName, int durationMsecs, std::function< void()> onEntry=nullptr, std::function< void()> onExit=nullptr)
void onEntry(QEvent *event) override
void postEvent(const QString &eventName, const QVariant &data=QVariant(), EventPriority priority=NormalPriority)
virtual void onLeave()
Override to perform actions when machine stops.
RetryTransition * addRetryTransition(SenderType *from, Func signal, QAbstractState *to, std::function< void()> retryAction, int maxRetries=1)
void stateHistoryChanged()
Emitted when the state history changes (for QML binding)
QSignalTransition * addTransition(QState *from, Func signal, QAbstractState *to, QAbstractAnimation *animation=nullptr)
void setCallbacks(EntryCallback onEntry, ExitCallback onExit=nullptr)
Set both entry and exit callbacks.
QString profilingSummary() const
void removeTimeoutOverride(const QString &stateName)
GuardedTransition * addConditionalTransition(SenderType *from, Func signal, QAbstractState *to, std::function< bool()> guard)
float progress() const
Get the current overall progress (0.0 to 1.0)
virtual void onEnter()
Override to perform actions when machine starts.
void registerState(QGCState *state)
ConditionalState * addConditionalState(const QString &stateName, ConditionalState::Predicate predicate, ConditionalState::Action action=nullptr)
void restart()
Restart the machine (stop then start)
FunctionState * addLogAndContinueErrorState(const QString &stateName, QAbstractState *nextState, const QString &message=QString())
Create a logging error handler state that advances to nextState.
QString dumpCurrentState() const
Full-featured base class for QGroundControl state machine states.
Transition that retries an action N times before advancing to target state.
Type-safe context for passing data between states in a state machine.
Records state machine transitions for debugging and analysis.
Extended logging for state machines.
Performance profiler for state machines.
State that invokes a child state machine.
std::function< QGCStateMachine *(SubMachineState *parent)> MachineFactory
Transition that fires automatically after a specified delay.
Waits for a signal from a QObject before advancing.