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
qqmlpropertytopropertybinding.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
5
6#include <private/qqmlanybinding_p.h>
7#include <private/qqmlengine_p.h>
8#include <private/qqmlvmemetaobject_p.h>
9#include <private/qv4alloca_p.h>
10
11#include <QtQml/qqmlinfo.h>
12
14
15/*!
16 * \internal
17 * \class QQmlPropertyToPropertyBinding
18 *
19 * This class can be used to create a direct binding from a source property to
20 * a target property, without going through QQmlJavaScriptExpression and
21 * QV4::Function. In particular you don't need a compilation unit or byte code
22 * to set this up.
23 *
24 * \note The target cannot be a group property, but the source can.
25 */
26
27QQmlAnyBinding QQmlPropertyToPropertyBinding::create(
28 QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
29{
30 QQmlAnyBinding result;
31 if (target.isBindable() && !QQmlPropertyPrivate::get(target)->isValueType()) {
32 if (source.isBindable()) {
33 result = QUntypedPropertyBinding(new QQmlBindableToBindablePropertyBinding(
34 engine, source, target));
35 return result;
36 }
37
38 result = QUntypedPropertyBinding(new QQmlUnbindableToBindablePropertyBinding(
39 engine, source, target));
40 return result;
41 }
42
43 if (source.isBindable()) {
44 result = new QQmlBindableToUnbindablePropertyBinding(engine, source, target);
45 return result;
46 }
47
48 result = new QQmlUnbindableToUnbindablePropertyBinding(engine, source, target);
49 return result;
50}
51
52QQmlPropertyToPropertyBinding::QQmlPropertyToPropertyBinding(
53 QQmlEngine *engine, const QQmlProperty &source)
54 : engine(engine)
55 , sourceObject(source.object())
56 , sourcePropertyIndex(QQmlPropertyPrivate::get(source)->encodedIndex())
57{
58}
59
61 QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
63 , QQmlPropertyToUnbindablePropertyBinding(engine, source, target)
64{
65}
66
68{
69 return PropertyToPropertyBinding;
70}
71
73 bool e, QQmlPropertyData::WriteFlags flags)
74{
75 const bool wasEnabled = enabledFlag();
76 setEnabledFlag(e);
77 updateCanUseAccessor();
78 if (e && !wasEnabled)
79 update(flags);
80}
81
82void QQmlPropertyToUnbindablePropertyBinding::update(QQmlPropertyData::WriteFlags flags)
83{
84 if (!enabledFlag())
85 return;
86
87 // Check that the target has not been deleted
88 QObject *target = targetObject();
89 if (QQmlData::wasDeleted(target))
90 return;
91
92 const QQmlPropertyData *d = nullptr;
93 QQmlPropertyData vtd;
94 getPropertyData(&d, &vtd);
95 Q_ASSERT(d);
96
97 // Check for a binding update loop
98 if (Q_UNLIKELY(updatingFlag()))
99 return;
100
101 setUpdatingFlag(true);
102
103 if (canUseAccessor())
104 flags.setFlag(QQmlPropertyData::BypassInterceptor);
105
106 QVariant value = m_binding.readSourceValue(
107 [&](const QMetaObject *sourceMetaObject, const QMetaProperty &property) {
108 captureProperty(sourceMetaObject, property);
109 });
110
111 QQmlPropertyPrivate::writeValueProperty(target, *d, vtd, value, {}, flags);
112 setUpdatingFlag(false);
113}
114
116 QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
118{
119 setTarget(target);
120}
121
123 QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
125 , QQmlPropertyToUnbindablePropertyBinding(engine, source, target)
126{
127}
128
130 QPropertyObserver *observer, QUntypedPropertyData *)
131{
132 static_cast<QQmlBindableToUnbindablePropertyBinding *>(observer)
133 ->QQmlPropertyToUnbindablePropertyBinding::update();
134}
135
137 const QMetaObject *sourceMetaObject, const QMetaProperty &sourceProperty)
138{
139 Q_UNUSED(sourceMetaObject);
140 m_binding.doConnectNotify(this, sourceProperty);
141}
142
144 const QMetaObject *sourceMetaObject, const QMetaProperty &sourceProperty)
145{
146 Q_UNUSED(sourceProperty);
147
148 // We have already captured.
149 if (m_isObserving)
150 return;
151
152 QUntypedBindable bindable;
153 void *argv[] = { &bindable };
154 sourceMetaObject->metacall(
155 m_binding.sourceObject, QMetaObject::BindableProperty,
156 m_binding.sourcePropertyIndex.coreIndex(), argv);
157 bindable.observe(this);
158 m_isObserving = true;
159}
160
161namespace QtPrivate {
162template<typename Binding>
163inline constexpr BindingFunctionVTable
166 [](void *qpropertyBinding) { delete reinterpret_cast<Binding *>(qpropertyBinding); },
167 [](void *, void *){},
168 0
169 };
170}
171
173 QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target,
174 const QtPrivate::BindingFunctionVTable *vtable)
178{
179}
180
190
192{
193 PendingBindingObserverList bindingObservers;
194 evaluateRecursive(bindingObservers);
195
196 if (const QPropertyBindingError error = bindingError();
197 Q_UNLIKELY(error.type() == QPropertyBindingError::BindingLoop)) {
198 return;
199 }
200
201 notifyNonRecursive(bindingObservers);
202}
203
212
213void QQmlUnbindableToUnbindableGuard_callback(QQmlNotifierEndpoint *e, void **)
214{
215 static_cast<QQmlUnbindableToUnbindablePropertyBinding *>(e)->update();
216}
217
218void QQmlUnbindableToBindableGuard_callback(QQmlNotifierEndpoint *e, void **)
219{
221}
222
223QT_END_NAMESPACE
QQmlBindableToBindablePropertyBinding(QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
void captureProperty(const QMetaObject *sourceMetaObject, const QMetaProperty &sourceProperty) final
QQmlBindableToUnbindablePropertyBinding(QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
QQmlPropertyToBindablePropertyBinding(QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target, const QtPrivate::BindingFunctionVTable *vtable)
void setEnabled(bool e, QQmlPropertyData::WriteFlags flags) final
QQmlPropertyToUnbindablePropertyBinding(QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
void update(QQmlPropertyData::WriteFlags flags=QQmlPropertyData::DontRemoveBinding)
QQmlUnbindableToBindablePropertyBinding(QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
QQmlUnbindableToUnbindablePropertyBinding(QQmlEngine *engine, const QQmlProperty &source, const QQmlProperty &target)
void captureProperty(const QMetaObject *sourceMetaObject, const QMetaProperty &sourceProperty) final
constexpr BindingFunctionVTable bindingFunctionVTableForQQmlPropertyToBindablePropertyBinding
void QQmlUnbindableToBindableGuard_callback(QQmlNotifierEndpoint *e, void **)
void QQmlUnbindableToUnbindableGuard_callback(QQmlNotifierEndpoint *e, void **)