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 | #include "qabstractstate.h" |
5 | #include "qabstractstate_p.h" |
6 | #include "qstate.h" |
7 | #include "qstate_p.h" |
8 | #include "qstatemachine.h" |
9 | #include "qstatemachine_p.h" |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | /*! |
14 | \class QAbstractState |
15 | \inmodule QtStateMachine |
16 | |
17 | \brief The QAbstractState class is the base class of states of a QStateMachine. |
18 | |
19 | \since 4.6 |
20 | \ingroup statemachine |
21 | |
22 | The QAbstractState class is the abstract base class of states that are part |
23 | of a QStateMachine. It defines the interface that all state objects have in |
24 | common. QAbstractState is part of \l{Qt State Machine Overview}{Qt State Machine Framework}. |
25 | |
26 | The entered() signal is emitted when the state has been entered. The |
27 | exited() signal is emitted when the state has been exited. |
28 | |
29 | The parentState() function returns the state's parent state. The machine() |
30 | function returns the state machine that the state is part of. |
31 | |
32 | \section1 Subclassing |
33 | |
34 | The onEntry() function is called when the state is entered; reimplement this |
35 | function to perform custom processing when the state is entered. |
36 | |
37 | The onExit() function is called when the state is exited; reimplement this |
38 | function to perform custom processing when the state is exited. |
39 | */ |
40 | |
41 | /*! |
42 | \property QAbstractState::active |
43 | \since 5.4 |
44 | |
45 | \brief the active property of this state. A state is active between |
46 | entered() and exited() signals. |
47 | */ |
48 | |
49 | |
50 | QAbstractStatePrivate::QAbstractStatePrivate(StateType type) |
51 | : stateType(type), isMachine(false), active(false), parentState(nullptr) |
52 | { |
53 | } |
54 | |
55 | QStateMachine *QAbstractStatePrivate::machine() const |
56 | { |
57 | QObject *par = parent; |
58 | while (par != nullptr) { |
59 | if (QStateMachine *mach = qobject_cast<QStateMachine*>(object: par)) |
60 | return mach; |
61 | par = par->parent(); |
62 | } |
63 | return nullptr; |
64 | } |
65 | |
66 | void QAbstractStatePrivate::callOnEntry(QEvent *e) |
67 | { |
68 | Q_Q(QAbstractState); |
69 | q->onEntry(event: e); |
70 | } |
71 | |
72 | void QAbstractStatePrivate::callOnExit(QEvent *e) |
73 | { |
74 | Q_Q(QAbstractState); |
75 | q->onExit(event: e); |
76 | } |
77 | |
78 | void QAbstractStatePrivate::emitEntered() |
79 | { |
80 | Q_Q(QAbstractState); |
81 | emit q->entered(QAbstractState::QPrivateSignal()); |
82 | active.setValue(true); |
83 | } |
84 | |
85 | void QAbstractStatePrivate::emitExited() |
86 | { |
87 | Q_Q(QAbstractState); |
88 | active.setValue(false); |
89 | emit q->exited(QAbstractState::QPrivateSignal()); |
90 | } |
91 | |
92 | /*! |
93 | Constructs a new state with the given \a parent state. |
94 | */ |
95 | QAbstractState::QAbstractState(QState *parent) |
96 | : QObject(*new QAbstractStatePrivate(QAbstractStatePrivate::AbstractState), parent) |
97 | { |
98 | } |
99 | |
100 | /*! |
101 | \internal |
102 | */ |
103 | QAbstractState::QAbstractState(QAbstractStatePrivate &dd, QState *parent) |
104 | : QObject(dd, parent) |
105 | { |
106 | } |
107 | |
108 | /*! |
109 | Destroys this state. |
110 | */ |
111 | QAbstractState::~QAbstractState() |
112 | { |
113 | } |
114 | |
115 | /*! |
116 | Returns this state's parent state, or \nullptr if the state has no |
117 | parent state. |
118 | */ |
119 | QState *QAbstractState::parentState() const |
120 | { |
121 | Q_D(const QAbstractState); |
122 | if (d->parentState != parent()) |
123 | d->parentState = qobject_cast<QState*>(object: parent()); |
124 | return d->parentState; |
125 | } |
126 | |
127 | /*! |
128 | Returns the state machine that this state is part of, or \nullptr if |
129 | the state is not part of a state machine. |
130 | */ |
131 | QStateMachine *QAbstractState::machine() const |
132 | { |
133 | Q_D(const QAbstractState); |
134 | return d->machine(); |
135 | } |
136 | |
137 | /*! |
138 | Returns whether this state is active. |
139 | |
140 | \sa activeChanged(bool), entered(), exited() |
141 | */ |
142 | bool QAbstractState::active() const |
143 | { |
144 | Q_D(const QAbstractState); |
145 | return d->active; |
146 | } |
147 | |
148 | QBindable<bool> QAbstractState::bindableActive() |
149 | { |
150 | Q_D(QAbstractState); |
151 | return &d->active; |
152 | } |
153 | |
154 | /*! |
155 | \fn QAbstractState::onExit(QEvent *event) |
156 | |
157 | This function is called when the state is exited. The given \a event is what |
158 | caused the state to be exited. Reimplement this function to perform custom |
159 | processing when the state is exited. |
160 | */ |
161 | |
162 | /*! |
163 | \fn QAbstractState::onEntry(QEvent *event) |
164 | |
165 | This function is called when the state is entered. The given \a event is |
166 | what caused the state to be entered. Reimplement this function to perform |
167 | custom processing when the state is entered. |
168 | */ |
169 | |
170 | /*! |
171 | \fn QAbstractState::entered() |
172 | |
173 | This signal is emitted when the state has been entered (after onEntry() has |
174 | been called). |
175 | */ |
176 | |
177 | /*! |
178 | \fn QAbstractState::exited() |
179 | |
180 | This signal is emitted when the state has been exited (after onExit() has |
181 | been called). |
182 | */ |
183 | |
184 | /*! |
185 | \fn QAbstractState::activeChanged(bool active) |
186 | \since 5.4 |
187 | |
188 | This signal is emitted when the active property is changed with \a active as argument. |
189 | |
190 | \sa QAbstractState::active, entered(), exited() |
191 | */ |
192 | |
193 | /*! |
194 | \reimp |
195 | */ |
196 | bool QAbstractState::event(QEvent *e) |
197 | { |
198 | return QObject::event(event: e); |
199 | } |
200 | |
201 | QT_END_NAMESPACE |
202 | |
203 | #include "moc_qabstractstate.cpp" |
204 | |