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 QQUICKSTATE_P_H
5#define QQUICKSTATE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qquickstate_p.h"
19
20#include "qquicktransitionmanager_p_p.h"
21
22#include <private/qqmlproperty_p.h>
23#include <private/qqmlguard_p.h>
24
25#include <private/qqmlbinding_p.h>
26
27#include <private/qobject_p.h>
28
29QT_BEGIN_NAMESPACE
30
31class QQuickSimpleAction
32{
33public:
34 enum State { StartState, EndState };
35 QQuickSimpleAction(const QQuickStateAction &a, State state = StartState)
36 {
37 m_property = a.property;
38 m_specifiedObject = a.specifiedObject;
39 m_specifiedProperty = a.specifiedProperty;
40 m_event = a.event;
41 if (state == StartState) {
42 m_value = a.fromValue;
43 m_binding = QQmlAnyBinding::ofProperty(prop: m_property);
44 m_reverseEvent = true;
45 } else {
46 m_value = a.toValue;
47 m_binding = a.toBinding;
48 m_reverseEvent = false;
49 }
50 }
51
52 ~QQuickSimpleAction()
53 {
54 }
55
56 QQuickSimpleAction(const QQuickSimpleAction &other)
57 : m_property(other.m_property),
58 m_value(other.m_value),
59 m_binding(other.binding()),
60 m_specifiedObject(other.m_specifiedObject),
61 m_specifiedProperty(other.m_specifiedProperty),
62 m_event(other.m_event),
63 m_reverseEvent(other.m_reverseEvent)
64 {
65 }
66
67 QQuickSimpleAction &operator =(const QQuickSimpleAction &other)
68 {
69 m_property = other.m_property;
70 m_value = other.m_value;
71 m_binding = other.binding();
72 m_specifiedObject = other.m_specifiedObject;
73 m_specifiedProperty = other.m_specifiedProperty;
74 m_event = other.m_event;
75 m_reverseEvent = other.m_reverseEvent;
76
77 return *this;
78 }
79
80 void setProperty(const QQmlProperty &property)
81 {
82 m_property = property;
83 }
84
85 const QQmlProperty &property() const
86 {
87 return m_property;
88 }
89
90 void setValue(const QVariant &value)
91 {
92 m_value = value;
93 }
94
95 const QVariant &value() const
96 {
97 return m_value;
98 }
99
100 void setBinding(QQmlAnyBinding binding)
101 {
102 m_binding = binding;
103 }
104
105 QQmlAnyBinding binding() const
106 {
107 return m_binding;
108 }
109
110 QObject *specifiedObject() const
111 {
112 return m_specifiedObject;
113 }
114
115 const QString &specifiedProperty() const
116 {
117 return m_specifiedProperty;
118 }
119
120 QQuickStateActionEvent *event() const
121 {
122 return m_event;
123 }
124
125 bool reverseEvent() const
126 {
127 return m_reverseEvent;
128 }
129
130private:
131 QQmlProperty m_property;
132 QVariant m_value;
133 QQmlAnyBinding m_binding;
134 QObject *m_specifiedObject;
135 QString m_specifiedProperty;
136 QQuickStateActionEvent *m_event;
137 bool m_reverseEvent;
138};
139
140class QQuickRevertAction
141{
142public:
143 QQuickRevertAction() : event(nullptr) {}
144 QQuickRevertAction(const QQmlProperty &prop) : property(prop), event(nullptr) {}
145 QQuickRevertAction(QQuickStateActionEvent *e) : event(e) {}
146 QQmlProperty property;
147 QQuickStateActionEvent *event;
148};
149
150class QQuickStateOperationPrivate : public QObjectPrivate
151{
152 Q_DECLARE_PUBLIC(QQuickStateOperation)
153
154public:
155
156 QQuickStateOperationPrivate()
157 : m_state(nullptr) {}
158
159 QQuickState *m_state;
160};
161
162class QQuickStatePrivate : public QObjectPrivate
163{
164 Q_DECLARE_PUBLIC(QQuickState)
165
166public:
167 QQuickStatePrivate()
168 : when(false), whenKnown(false), named(false), inState(false), group(nullptr) {}
169
170 typedef QList<QQuickSimpleAction> SimpleActionList;
171
172 QString name;
173 bool when;
174 bool whenKnown;
175 bool named;
176
177 struct OperationGuard : public QQmlGuard<QQuickStateOperation>
178 {
179 OperationGuard(QObject *obj, QList<OperationGuard> *l) : QQmlGuard<QQuickStateOperation>(
180 OperationGuard::objectDestroyedImpl, nullptr)
181 ,list(l)
182 {
183 setObject(static_cast<QQuickStateOperation *>(obj));
184 }
185 QList<OperationGuard> *list;
186
187 private:
188 static void objectDestroyedImpl(QQmlGuardImpl *guard) {
189 auto This = static_cast<OperationGuard *>(guard);
190 // we assume priv will always be destroyed after objectDestroyed calls
191 This->list->removeOne(t: *This);
192 }
193 };
194 QList<OperationGuard> operations;
195
196 static void operations_append(QQmlListProperty<QQuickStateOperation> *prop, QQuickStateOperation *op) {
197 QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
198 op->setState(qobject_cast<QQuickState*>(object: prop->object));
199 list->append(t: OperationGuard(op, list));
200 }
201 static void operations_clear(QQmlListProperty<QQuickStateOperation> *prop) {
202 QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
203 for (auto &e : *list)
204 e->setState(nullptr);
205 list->clear();
206 }
207 static qsizetype operations_count(QQmlListProperty<QQuickStateOperation> *prop) {
208 QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
209 return list->size();
210 }
211 static QQuickStateOperation *operations_at(QQmlListProperty<QQuickStateOperation> *prop, qsizetype index) {
212 QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
213 return list->at(i: index);
214 }
215 static void operations_replace(QQmlListProperty<QQuickStateOperation> *prop, qsizetype index,
216 QQuickStateOperation *op) {
217 QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
218 auto &guard = list->at(i: index);
219 if (guard.object() == op) {
220 op->setState(qobject_cast<QQuickState*>(object: prop->object));
221 } else {
222 list->at(i: index)->setState(nullptr);
223 op->setState(qobject_cast<QQuickState*>(object: prop->object));
224 list->replace(i: index, t: OperationGuard(op, list));
225 }
226 }
227 static void operations_removeLast(QQmlListProperty<QQuickStateOperation> *prop) {
228 QList<OperationGuard> *list = static_cast<QList<OperationGuard> *>(prop->data);
229 list->last()->setState(nullptr);
230 list->removeLast();
231 }
232
233 QQuickTransitionManager transitionManager;
234
235 SimpleActionList revertList;
236 QList<QQuickRevertAction> reverting;
237 QString extends;
238 mutable bool inState;
239 QQuickStateGroup *group;
240
241 QQuickStateOperation::ActionList generateActionList() const;
242 void complete();
243};
244
245QT_END_NAMESPACE
246
247#endif // QQUICKSTATE_P_H
248

source code of qtdeclarative/src/quick/util/qquickstate_p_p.h