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>
173 int timeoutMsecs = 0);
188 int retryDelayMsecs = 1000,
192 int timeoutMsecs = 0);
208 void postEvent(
const QString& eventName,
const QVariant& data = QVariant(),
209 EventPriority priority = NormalPriority);
216 int postDelayedEvent(
const QString& eventName,
int delayMsecs,
const QVariant& data = QVariant());
252 template<
typename Func>
254 const QObject* sender,
256 int timeoutMsecs = 0)
275 const QString& eventName,
276 int timeoutMsecs = 0);
300 std::function<
void()>
onEntry =
nullptr,
301 std::function<
void()>
onExit =
nullptr);
319 QAbstractState*
findState(
const QString& stateName)
const;
325 return findChildren<T*>();
337 QAbstractState* nextState,
338 const QString& message = QString());
342 const QString& message = QString());
398 template<
typename Func>
399 QSignalTransition*
addTransition(QState* from, Func signal, QAbstractState* to,
400 QAbstractAnimation* animation =
nullptr)
402 auto* transition = from->addTransition(from, signal, to);
404 transition->addAnimation(animation);
415 template<
typename Func>
418 QAbstractAnimation* animation =
nullptr)
421 from->addTransition(transition);
423 transition->addAnimation(animation);
434 QAbstractAnimation* animation =
nullptr);
442 QAbstractAnimation* animation =
nullptr);
450 template<
typename SenderType,
typename Func>
453 std::function<
void()> retryAction,
456 auto* transition =
new RetryTransition(from, signal, to, std::move(retryAction), maxRetries);
457 from->addTransition(transition);
467 template<
typename SenderType,
typename Func>
470 std::function<
bool()> guard)
473 from->addTransition(transition);
499 template<
typename Sender,
typename Signal>
503 std::function<
void()> action)
505 auto* transition = state->addTransition(sender, signal, state);
507 connect(transition, &QAbstractTransition::triggered,
this, std::move(action));
521 template<
typename Sender,
typename Signal>
525 std::function<
void()> action)
528 state->addTransition(transition);
539 QList<QAbstractTransition*>
transitionsFrom(QAbstractState* state)
const;
544 QList<QAbstractTransition*>
transitionsTo(QAbstractState* state)
const;
549 QList<QAbstractState*>
reachableFrom(QAbstractState* state)
const;
554 QList<QAbstractState*>
predecessorsOf(QAbstractState* state)
const;
713 void _onStateEntered();
715 float _calculateProgress()
const;
718 QAbstractState* _globalErrorState =
nullptr;
719 QAbstractState* _recoveryState =
nullptr;
720 bool _deleteOnStop =
false;
721 bool _inProgress =
false;
727 QList<QAbstractState*> _progressStates;
728 QList<int> _progressWeights;
729 int _progressTotalWeight = 0;
730 int _progressCurrentIndex = -1;
731 float _progressSubProgress = 0.0f;
732 float _progressLastEmitted = 0.0f;
738 QStringList _stateHistory;
739 int _stateHistoryLimit = 20;
742 QHash<QString, int> _timeoutOverrides;
743 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.
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 inProgressChanged()
Emitted when inProgress() 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.
bool isRunning() const =delete
Use inProgress() instead: isRunning() is false until Qt processes the queued start.
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.
QGCStateMachine(const QString &machineName, Vehicle *vehicle, QObject *parent=nullptr)
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 in progress.
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)
void setRunning(bool running)=delete
Use start() and stop() instead: setRunning() calls QStateMachine::start() and bypasses start().
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.
std::function< bool(QEvent *)> EventHandler
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.