Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlabstractbinding_p.h
Go to the documentation of this file.
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// Qt-Security score:significant
4
5#ifndef QQMLABSTRACTBINDING_P_H
6#define QQMLABSTRACTBINDING_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtCore/qsharedpointer.h>
20#include <QtCore/qshareddata.h>
21#include <private/qtqmlglobal_p.h>
22#include <private/qqmlproperty_p.h>
23
24QT_BEGIN_NAMESPACE
25
26class QQmlObjectCreator;
27class QQmlAnyBinding;
28
29class Q_QML_EXPORT QQmlAbstractBinding
30{
31 friend class QQmlAnyBinding;
32protected:
33 QQmlAbstractBinding();
34public:
35 enum Kind {
36 ValueTypeProxy,
37 QmlBinding,
38 PropertyToPropertyBinding,
39 };
40
41 virtual ~QQmlAbstractBinding();
42
43 typedef QExplicitlySharedDataPointer<QQmlAbstractBinding> Ptr;
44
45 virtual QString expression() const;
46
47 virtual Kind kind() const = 0;
48
49 // Should return the encoded property index for the binding. Should return this value
50 // even if the binding is not enabled or added to an object.
51 // Encoding is: coreIndex | (valueTypeIndex << 16)
52 QQmlPropertyIndex targetPropertyIndex() const { return m_targetIndex; }
53
54 // Should return the object for the binding. Should return this object even if the
55 // binding is not enabled or added to the object.
56 QObject *targetObject() const { return m_target.data(); }
57
58 void setTarget(const QQmlProperty &);
59 bool setTarget(QObject *, const QQmlPropertyData &, const QQmlPropertyData *valueType);
60 bool setTarget(QObject *, int coreIndex, bool coreIsAlias, int valueTypeIndex);
61
62 virtual void setEnabled(bool e, QQmlPropertyData::WriteFlags f = QQmlPropertyData::DontRemoveBinding) = 0;
63
64 void addToObject();
65 void removeFromObject();
66
67 virtual void printBindingLoopError(const QQmlProperty &prop);
68
69 inline QQmlAbstractBinding *nextBinding() const;
70
71 inline bool canUseAccessor() const
72 { return m_target.tag().testFlag(CanUseAccessor); }
73 void setCanUseAccessor(bool canUseAccessor)
74 { m_target.setTag(m_target.tag().setFlag(CanUseAccessor, canUseAccessor)); }
75
76 bool isSticky() const { return m_target.tag().testFlag(IsSticky); }
77 void setSticky(bool isSticky) { m_target.setTag(m_target.tag().setFlag(IsSticky, isSticky)); }
78
79 struct RefCount {
80 RefCount() {}
81 int refCount = 0;
82 void ref() { ++refCount; }
83 int deref() { return --refCount; }
84 operator int() const { return refCount; }
85 };
86 RefCount ref;
87
88 // A binding can only be enabled if it's added to an object,
89 // and it can only be updating if it's enabled.
90 enum State {
91 Disabled = 0,
92 AddedToObject = 1,
93 BindingEnabled = 2,
94 UpdatingBinding = 3,
95 };
96
97 enum TargetTag {
98 NoTargetTag = 0x0,
99 CanUseAccessor = 0x1,
100 IsSticky = 0x2,
101 };
102 Q_DECLARE_FLAGS(TargetTags, TargetTag)
103
104protected:
105 friend class QQmlData;
106 friend class QQmlValueTypeProxyBinding;
107 friend class QQmlObjectCreator;
108
109 inline void setAddedToObject(bool v);
110 inline bool isAddedToObject() const;
111
112 inline void setNextBinding(QQmlAbstractBinding *);
113
114 void getPropertyData(
115 const QQmlPropertyData **propertyData, QQmlPropertyData *valueTypeData) const;
116
117 inline bool updatingFlag() const;
118 inline void setUpdatingFlag(bool);
119 inline bool enabledFlag() const;
120 inline void setEnabledFlag(bool);
121 void updateCanUseAccessor();
122
123 QQmlPropertyIndex m_targetIndex;
124
125 // Pointer is the target object to which the binding binds
126 QTaggedPointer<QObject, TargetTags> m_target;
127
128 // Pointer to the next binding in the linked list of bindings.
129 QTaggedPointer<QQmlAbstractBinding, State> m_nextBinding;
130
131private:
132 void setState(State state, bool enabled)
133 {
134 if (enabled) {
135 m_nextBinding.setTag(std::max(m_nextBinding.tag(), state));
136 return;
137 }
138
139 Q_ASSERT(state > 0);
140 m_nextBinding.setTag(std::min(m_nextBinding.tag(), State(state - 1)));
141 }
142
143 bool hasState(State state) const
144 {
145 return m_nextBinding.tag() >= state;
146 }
147};
148
149Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlAbstractBinding::TargetTags)
150
151void QQmlAbstractBinding::setAddedToObject(bool v)
152{
153 setState(AddedToObject, v);
154}
155
156bool QQmlAbstractBinding::isAddedToObject() const
157{
158 return hasState(AddedToObject);
159}
160
161QQmlAbstractBinding *QQmlAbstractBinding::nextBinding() const
162{
163 return m_nextBinding.data();
164}
165
166void QQmlAbstractBinding::setNextBinding(QQmlAbstractBinding *b)
167{
168 if (b)
169 b->ref.ref();
170 if (m_nextBinding.data() && !m_nextBinding->ref.deref())
171 delete m_nextBinding.data();
172 m_nextBinding = b;
173}
174
175bool QQmlAbstractBinding::updatingFlag() const
176{
177 return hasState(UpdatingBinding);
178}
179
180void QQmlAbstractBinding::setUpdatingFlag(bool v)
181{
182 setState(UpdatingBinding, v);
183}
184
185bool QQmlAbstractBinding::enabledFlag() const
186{
187 return hasState(BindingEnabled);
188}
189
190void QQmlAbstractBinding::setEnabledFlag(bool v)
191{
192 setState(BindingEnabled, v);
193}
194
195QT_END_NAMESPACE
196
197#endif // QQMLABSTRACTBINDING_P_H
QQmlAnyBinding is an abstraction over the various bindings in QML.
Combined button and popup list for selecting options.
static const QQmlPropertyData * getObjectPropertyData(QObject *object, int coreIndex)