1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QSTATEMACHINE_H
41#define QSTATEMACHINE_H
42
43#include <QtCore/qstate.h>
44
45#include <QtCore/qcoreevent.h>
46#include <QtCore/qlist.h>
47#include <QtCore/qobject.h>
48#include <QtCore/qset.h>
49#include <QtCore/qvariant.h>
50
51#if __has_include(<chrono>)
52# include <chrono>
53#endif
54
55QT_REQUIRE_CONFIG(statemachine);
56
57QT_BEGIN_NAMESPACE
58
59class QStateMachinePrivate;
60class QAbstractAnimation;
61class Q_CORE_EXPORT QStateMachine : public QState
62{
63 Q_OBJECT
64 Q_PROPERTY(QString errorString READ errorString)
65 Q_PROPERTY(QState::RestorePolicy globalRestorePolicy READ globalRestorePolicy WRITE setGlobalRestorePolicy)
66 Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
67#if QT_CONFIG(animation)
68 Q_PROPERTY(bool animated READ isAnimated WRITE setAnimated)
69#endif
70public:
71 class Q_CORE_EXPORT SignalEvent : public QEvent
72 {
73 public:
74 SignalEvent(QObject *sender, int signalIndex,
75 const QList<QVariant> &arguments);
76 ~SignalEvent();
77
78 inline QObject *sender() const { return m_sender; }
79 inline int signalIndex() const { return m_signalIndex; }
80 inline QList<QVariant> arguments() const { return m_arguments; }
81
82 private:
83 QObject *m_sender;
84 int m_signalIndex;
85 QList<QVariant> m_arguments;
86
87 friend class QSignalTransitionPrivate;
88 };
89
90 class Q_CORE_EXPORT WrappedEvent : public QEvent
91 {
92 public:
93 WrappedEvent(QObject *object, QEvent *event);
94 ~WrappedEvent();
95
96 inline QObject *object() const { return m_object; }
97 inline QEvent *event() const { return m_event; }
98
99 private:
100 QObject *m_object;
101 QEvent *m_event;
102 };
103
104 enum EventPriority {
105 NormalPriority,
106 HighPriority
107 };
108
109 enum Error {
110 NoError,
111 NoInitialStateError,
112 NoDefaultStateInHistoryStateError,
113 NoCommonAncestorForTransitionError,
114 StateMachineChildModeSetToParallelError
115 };
116
117 explicit QStateMachine(QObject *parent = nullptr);
118 explicit QStateMachine(QState::ChildMode childMode, QObject *parent = nullptr);
119 ~QStateMachine();
120
121 void addState(QAbstractState *state);
122 void removeState(QAbstractState *state);
123
124 Error error() const;
125 QString errorString() const;
126 void clearError();
127
128 bool isRunning() const;
129
130#if QT_CONFIG(animation)
131 bool isAnimated() const;
132 void setAnimated(bool enabled);
133
134 void addDefaultAnimation(QAbstractAnimation *animation);
135 QList<QAbstractAnimation *> defaultAnimations() const;
136 void removeDefaultAnimation(QAbstractAnimation *animation);
137#endif // animation
138
139 QState::RestorePolicy globalRestorePolicy() const;
140 void setGlobalRestorePolicy(QState::RestorePolicy restorePolicy);
141
142 void postEvent(QEvent *event, EventPriority priority = NormalPriority);
143 int postDelayedEvent(QEvent *event, int delay);
144 bool cancelDelayedEvent(int id);
145
146 QSet<QAbstractState*> configuration() const;
147
148#if QT_CONFIG(qeventtransition)
149 bool eventFilter(QObject *watched, QEvent *event) override;
150#endif
151
152#if __has_include(<chrono>) || defined(Q_QDOC)
153 int postDelayedEvent(QEvent *event, std::chrono::milliseconds delay)
154 {
155 return postDelayedEvent(event, int(delay.count()));
156 }
157#endif
158
159public Q_SLOTS:
160 void start();
161 void stop();
162 void setRunning(bool running);
163
164Q_SIGNALS:
165 void started(QPrivateSignal);
166 void stopped(QPrivateSignal);
167 void runningChanged(bool running);
168
169
170protected:
171 void onEntry(QEvent *event) override;
172 void onExit(QEvent *event) override;
173
174 virtual void beginSelectTransitions(QEvent *event);
175 virtual void endSelectTransitions(QEvent *event);
176
177 virtual void beginMicrostep(QEvent *event);
178 virtual void endMicrostep(QEvent *event);
179
180 bool event(QEvent *e) override;
181
182protected:
183 QStateMachine(QStateMachinePrivate &dd, QObject *parent);
184
185private:
186 Q_DISABLE_COPY(QStateMachine)
187 Q_DECLARE_PRIVATE(QStateMachine)
188 Q_PRIVATE_SLOT(d_func(), void _q_start())
189 Q_PRIVATE_SLOT(d_func(), void _q_process())
190#if QT_CONFIG(animation)
191 Q_PRIVATE_SLOT(d_func(), void _q_animationFinished())
192#endif
193 Q_PRIVATE_SLOT(d_func(), void _q_startDelayedEventTimer(int, int))
194 Q_PRIVATE_SLOT(d_func(), void _q_killDelayedEventTimer(int, int))
195};
196
197QT_END_NAMESPACE
198
199#endif
200