diff options
Diffstat (limited to 'src/qml/qml/qqmlanybinding_p.h')
-rw-r--r-- | src/qml/qml/qqmlanybinding_p.h | 184 |
1 files changed, 119 insertions, 65 deletions
diff --git a/src/qml/qml/qqmlanybinding_p.h b/src/qml/qml/qqmlanybinding_p.h index 38c1023c79..f432d2abae 100644 --- a/src/qml/qml/qqmlanybinding_p.h +++ b/src/qml/qml/qqmlanybinding_p.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2021 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QQMLANYBINDINGPTR_P_H #define QQMLANYBINDINGPTR_P_H @@ -55,6 +19,8 @@ #include <private/qqmlpropertybinding_p.h> #include <private/qqmlbinding_p.h> +QT_BEGIN_NAMESPACE + // Fully inline so that subsequent prop.isBindable check might get ellided. /*! @@ -78,10 +44,9 @@ class QQmlAnyBinding { public: - QQmlAnyBinding() = default; + constexpr QQmlAnyBinding() noexcept = default; QQmlAnyBinding(std::nullptr_t) : d(static_cast<QQmlAbstractBinding *>(nullptr)) {} - /*! \internal Returns the binding of the property \a prop as a QQmlAnyBinding. @@ -100,6 +65,28 @@ public: } /*! + \overload + + \a object must be non-null + */ + static QQmlAnyBinding ofProperty(QObject *object, QQmlPropertyIndex index) + { + QQmlAnyBinding binding; + Q_ASSERT(object); + auto coreIndex = index.coreIndex(); + // we don't support bindable properties on value types so far + if (!index.hasValueTypeIndex() + && QQmlData::ensurePropertyCache(object)->property(coreIndex)->isBindable()) { + auto metaProp = object->metaObject()->property(coreIndex); + QUntypedBindable bindable = metaProp.bindable(object); + binding = bindable.binding(); + } else { + binding = QQmlPropertyPrivate::binding(object, index); + } + return binding; + } + + /*! Removes the binding from the property \a prop, and returns it as a QQmlAnyBinding if there was any. Otherwise returns a null QQmlAnyBinding. @@ -113,9 +100,9 @@ public: } else { auto qmlBinding = QQmlPropertyPrivate::binding(prop); if (qmlBinding) { + binding = qmlBinding; // this needs to run before removeFromObject, else the refcount might reach zero qmlBinding->setEnabled(false, QQmlPropertyData::DontRemoveBinding | QQmlPropertyData::BypassInterceptor); qmlBinding->removeFromObject(); - binding = qmlBinding; } } return binding; @@ -149,6 +136,30 @@ public: /*! \internal + Creates a binding for property \a prop from \a script. + \a obj is the scope object which shall be used for the function and \a ctxt its QML scope. + The binding is not installed on the property (but if a QQmlBinding is created, it has its + target set to \a prop). + */ + static QQmlAnyBinding createFromScriptString(const QQmlProperty &prop, const QQmlScriptString &script, + QObject *obj, QQmlContext *ctxt) + { + QQmlAnyBinding binding; + auto propPriv = QQmlPropertyPrivate::get(prop); + if (prop.isBindable()) { + auto index = QQmlPropertyIndex(propPriv->core.coreIndex(), -1); + binding = QQmlPropertyBinding::createFromScriptString(&propPriv->core, script, obj, ctxt, prop.object(), index); + } else { + auto qmlBinding = QQmlBinding::create(&propPriv->core, script, obj, ctxt); + qmlBinding->setTarget(prop); + binding = qmlBinding; + } + return binding; + } + + + /*! + \internal Removes the binding from \a prop if there is any. */ static void removeBindingFrom(QQmlProperty &prop) @@ -230,17 +241,39 @@ public: QQmlPropertyPrivate::setBinding(abstractBinding); } else { Q_ASSERT(target.isBindable()); - // TODO: QMetaProperty::bindable needs a mode to work on the dynamic metaobject - QUntypedBindable bindable = target.property().bindable(target.object()); + QUntypedBindable bindable; + void *argv[] = {&bindable}; + if (mode == IgnoreInterceptors) { + target.object()->qt_metacall(QMetaObject::BindableProperty, target.index(), argv); + } else { + QMetaObject::metacall(target.object(), QMetaObject::BindableProperty, target.index(), argv); + } bindable.setBinding(asUntypedPropertyBinding()); } } /*! + \internal + Returns true if the binding is in an error state (e.g. binding loop), false otherwise. + + \note For ValueTypeProxyBindings, this methods will always return false + */ + bool hasError() { + if (isAbstractPropertyBinding()) { + auto abstractBinding = asAbstractBinding(); + if (abstractBinding->kind() != QQmlAbstractBinding::QmlBinding) + return false; + return static_cast<QQmlBinding *>(abstractBinding)->hasError(); + } else { + return asUntypedPropertyBinding().error().hasError(); + } + } + + /*! Stores a null binding. For purpose of classification, the null bindings is treated as a QQmlAbstractPropertyBindings. */ - QQmlAnyBinding& operator=(std::nullptr_t ) + QQmlAnyBinding &operator=(std::nullptr_t) { clear(); return *this; @@ -293,9 +326,31 @@ public: /*! \internal + Reevaluates the binding. If the binding was disabled, + it gets enabled. + */ + void refresh() + { + if (d.isNull()) + return; + if (d.isT1()) { + auto binding = static_cast<QQmlBinding *>(d.asT1()); + binding->setEnabledFlag(true); + binding->refresh(); + } else { + auto bindingPriv = d.asT2(); + PendingBindingObserverList bindingObservers; + bindingPriv->evaluateRecursive(bindingObservers); + bindingPriv->notifyNonRecursive(bindingObservers); + } + + } + + /*! + \internal Stores \a binding and keeps a reference to it. */ - QQmlAnyBinding& operator=(QQmlAbstractBinding *binding) + QQmlAnyBinding &operator=(QQmlAbstractBinding *binding) { clear(); if (binding) { @@ -309,7 +364,7 @@ public: \internal Stores the binding stored in \a binding and keeps a reference to it. */ - QQmlAnyBinding& operator=(const QQmlAbstractBinding::Ptr &binding) + QQmlAnyBinding &operator=(const QQmlAbstractBinding::Ptr &binding) { clear(); if (binding) { @@ -323,7 +378,7 @@ public: \internal Stores \a binding's binding, taking ownership from \a binding. */ - QQmlAnyBinding& operator=(QQmlAbstractBinding::Ptr &&binding) + QQmlAnyBinding &operator=(QQmlAbstractBinding::Ptr &&binding) { clear(); if (binding) { @@ -336,7 +391,7 @@ public: \internal Stores the binding stored in \a untypedBinding and keeps a reference to it. */ - QQmlAnyBinding& operator=(const QUntypedPropertyBinding &untypedBinding) + QQmlAnyBinding &operator=(const QUntypedPropertyBinding &untypedBinding) { clear(); auto binding = QPropertyBindingPrivate::get(untypedBinding); @@ -352,7 +407,7 @@ public: \overload Stores the binding stored in \a untypedBinding, taking ownership from it. */ - QQmlAnyBinding& operator=(const QUntypedPropertyBinding &&untypedBinding) + QQmlAnyBinding &operator=(QUntypedPropertyBinding &&untypedBinding) { clear(); auto binding = QPropertyBindingPrivate::get(untypedBinding); @@ -363,17 +418,17 @@ public: return *this; } - QQmlAnyBinding(const QQmlAnyBinding &other) - { - *this = other; - } + QQmlAnyBinding(QQmlAnyBinding &&other) noexcept + : d(std::exchange(other.d, QBiPointer<QQmlAbstractBinding, QPropertyBindingPrivate>())) + {} - friend void swap(const QQmlAnyBinding &a, const QQmlAnyBinding &b) - { - qSwap(a.d, b.d); - } + QQmlAnyBinding(const QQmlAnyBinding &other) noexcept { *this = other; } + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QQmlAnyBinding) + + void swap(QQmlAnyBinding &other) noexcept { d.swap(other.d); } + friend void swap(QQmlAnyBinding &lhs, QQmlAnyBinding &rhs) noexcept { lhs.swap(rhs); } - QQmlAnyBinding& operator=(const QQmlAnyBinding &other) + QQmlAnyBinding &operator=(const QQmlAnyBinding &other) noexcept { clear(); if (auto abstractBinding = other.asAbstractBinding()) @@ -393,11 +448,9 @@ public: return p1.d != p2.d; } - ~QQmlAnyBinding() { - clear(); - } + ~QQmlAnyBinding() noexcept { clear(); } private: - void clear() { + void clear() noexcept { if (d.isNull()) return; if (d.isT1()) { @@ -406,14 +459,15 @@ private: delete qqmlptr; } else if (d.isT2()) { QPropertyBindingPrivate *priv = d.asT2(); - priv->ref--; - if (!priv->ref) + if (!priv->deref()) QPropertyBindingPrivate::destroyAndFreeMemory(priv); } d = static_cast<QQmlAbstractBinding *>(nullptr); } - QBiPointer<QQmlAbstractBinding, QPropertyBindingPrivate> d = static_cast<QQmlAbstractBinding *>(nullptr); + QBiPointer<QQmlAbstractBinding, QPropertyBindingPrivate> d; }; +QT_END_NAMESPACE + #endif // QQMLANYBINDINGPTR_P_H |