1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSTATEMACHINE_H
5#define QSTATEMACHINE_H
6
7#include <QtCore/qcoreevent.h>
8#include <QtCore/qlist.h>
9#include <QtCore/qobject.h>
10#include <QtCore/qset.h>
11#include <QtCore/qvariant.h>
12
13#include <QtStateMachine/qstate.h>
14
15#if __has_include(<chrono>)
16# include <chrono>
17#endif
18
19QT_REQUIRE_CONFIG(statemachine);
20
21QT_BEGIN_NAMESPACE
22
23class QStateMachinePrivate;
24class QAbstractAnimation;
25class Q_STATEMACHINE_EXPORT QStateMachine : public QState
26{
27 Q_OBJECT
28 Q_PROPERTY(QString errorString READ errorString BINDABLE bindableErrorString)
29 Q_PROPERTY(QState::RestorePolicy globalRestorePolicy READ globalRestorePolicy
30 WRITE setGlobalRestorePolicy BINDABLE bindableGlobalRestorePolicy)
31 Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
32#if QT_CONFIG(animation)
33 Q_PROPERTY(bool animated READ isAnimated WRITE setAnimated BINDABLE bindableAnimated)
34#endif
35public:
36 class Q_STATEMACHINE_EXPORT SignalEvent : public QEvent
37 {
38 public:
39 SignalEvent(QObject *sender, int signalIndex,
40 const QList<QVariant> &arguments);
41 ~SignalEvent();
42
43 inline QObject *sender() const { return m_sender; }
44 inline int signalIndex() const { return m_signalIndex; }
45 inline QList<QVariant> arguments() const { return m_arguments; }
46
47 private:
48 QObject *m_sender;
49 int m_signalIndex;
50 QList<QVariant> m_arguments;
51
52 friend class QSignalTransitionPrivate;
53 };
54
55 class Q_STATEMACHINE_EXPORT WrappedEvent : public QEvent
56 {
57 public:
58 WrappedEvent(QObject *object, QEvent *event);
59 ~WrappedEvent();
60
61 inline QObject *object() const { return m_object; }
62 inline QEvent *event() const { return m_event; }
63
64 private:
65 QObject *m_object;
66 QEvent *m_event;
67 };
68
69 enum EventPriority {
70 NormalPriority,
71 HighPriority
72 };
73
74 enum Error {
75 NoError,
76 NoInitialStateError,
77 NoDefaultStateInHistoryStateError,
78 NoCommonAncestorForTransitionError,
79 StateMachineChildModeSetToParallelError
80 };
81
82 explicit QStateMachine(QObject *parent = nullptr);
83 explicit QStateMachine(QState::ChildMode childMode, QObject *parent = nullptr);
84 ~QStateMachine();
85
86 void addState(QAbstractState *state);
87 void removeState(QAbstractState *state);
88
89 Error error() const;
90
91 QString errorString() const;
92 void clearError();
93 QBindable<QString> bindableErrorString() const;
94
95 bool isRunning() const;
96
97#if QT_CONFIG(animation)
98 bool isAnimated() const;
99 void setAnimated(bool enabled);
100 QBindable<bool> bindableAnimated();
101
102 void addDefaultAnimation(QAbstractAnimation *animation);
103 QList<QAbstractAnimation *> defaultAnimations() const;
104 void removeDefaultAnimation(QAbstractAnimation *animation);
105#endif // animation
106
107 QState::RestorePolicy globalRestorePolicy() const;
108 void setGlobalRestorePolicy(QState::RestorePolicy restorePolicy);
109 QBindable<QState::RestorePolicy> bindableGlobalRestorePolicy();
110
111 void postEvent(QEvent *event, EventPriority priority = NormalPriority);
112 int postDelayedEvent(QEvent *event, int delay);
113 bool cancelDelayedEvent(int id);
114
115 QSet<QAbstractState*> configuration() const;
116
117#if QT_CONFIG(qeventtransition)
118 bool eventFilter(QObject *watched, QEvent *event) override;
119#endif
120
121#if __has_include(<chrono>) || defined(Q_QDOC)
122 int postDelayedEvent(QEvent *event, std::chrono::milliseconds delay)
123 {
124 return postDelayedEvent(event, delay: int(delay.count()));
125 }
126#endif
127
128public Q_SLOTS:
129 void start();
130 void stop();
131 void setRunning(bool running);
132
133Q_SIGNALS:
134 void started(QPrivateSignal);
135 void stopped(QPrivateSignal);
136 void runningChanged(bool running);
137
138
139protected:
140 void onEntry(QEvent *event) override;
141 void onExit(QEvent *event) override;
142
143 virtual void beginSelectTransitions(QEvent *event);
144 virtual void endSelectTransitions(QEvent *event);
145
146 virtual void beginMicrostep(QEvent *event);
147 virtual void endMicrostep(QEvent *event);
148
149 bool event(QEvent *e) override;
150
151protected:
152 QStateMachine(QStateMachinePrivate &dd, QObject *parent);
153
154private:
155 Q_DISABLE_COPY(QStateMachine)
156 Q_DECLARE_PRIVATE(QStateMachine)
157 Q_PRIVATE_SLOT(d_func(), void _q_start())
158 Q_PRIVATE_SLOT(d_func(), void _q_process())
159#if QT_CONFIG(animation)
160 Q_PRIVATE_SLOT(d_func(), void _q_animationFinished())
161#endif
162 Q_PRIVATE_SLOT(d_func(), void _q_startDelayedEventTimer(int, int))
163 Q_PRIVATE_SLOT(d_func(), void _q_killDelayedEventTimer(int, int))
164};
165
166QT_END_NAMESPACE
167
168#endif
169

source code of qtscxml/src/statemachine/qstatemachine.h