diff options
| author | Simon Hausmann <simon.hausmann@qt.io> | 2020-03-23 21:32:38 +0100 |
|---|---|---|
| committer | Simon Hausmann <simon.hausmann@qt.io> | 2020-03-30 14:18:18 +0200 |
| commit | dff7689a016d63fbaf1abc6f00a768dbfb8ec095 (patch) | |
| tree | 6f0b67ce5d6dec279c0bbf62f014955cd0f25e32 /src | |
| parent | 9f2c5aa2efd24507f4508889911873bc1c9d301e (diff) | |
Add support for binding directly to QProperty instances
Avoid going through externally managed bindings and instead allocate a
more lightweight property binding. It's basically a
QQmlJavaScriptExpression and one pointer plus the overhead of
QPropertyBindingPrivate.
Change-Id: I1530330926d351b61f2b3bbad39301c628a8bef1
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/qml/.prev_CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/qml/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/qml/qml/qml.pri | 2 | ||||
| -rw-r--r-- | src/qml/qml/qqmlobjectcreator.cpp | 7 | ||||
| -rw-r--r-- | src/qml/qml/qqmlpropertybinding.cpp | 101 | ||||
| -rw-r--r-- | src/qml/qml/qqmlpropertybinding_p.h | 83 | ||||
| -rw-r--r-- | src/qml/qml/qqmlpropertycache.cpp | 1 | ||||
| -rw-r--r-- | src/qml/qml/qqmlpropertydata_p.h | 17 |
8 files changed, 207 insertions, 6 deletions
diff --git a/src/qml/.prev_CMakeLists.txt b/src/qml/.prev_CMakeLists.txt index 0f25337573..147419711f 100644 --- a/src/qml/.prev_CMakeLists.txt +++ b/src/qml/.prev_CMakeLists.txt @@ -229,6 +229,7 @@ qt_add_module(Qml qml/qqmlplatform.cpp qml/qqmlplatform_p.h qml/qqmlprivate.h qml/qqmlproperty.cpp qml/qqmlproperty.h qml/qqmlproperty_p.h + qml/qqmlpropertybinding.cpp qml/qqmlpropertybinding_p.h qml/qqmlpropertycache.cpp qml/qqmlpropertycache_p.h qml/qqmlpropertycachecreator.cpp qml/qqmlpropertycachecreator_p.h qml/qqmlpropertycachemethodarguments_p.h diff --git a/src/qml/CMakeLists.txt b/src/qml/CMakeLists.txt index f6ab914b9a..cb49452a26 100644 --- a/src/qml/CMakeLists.txt +++ b/src/qml/CMakeLists.txt @@ -231,6 +231,7 @@ qt_add_module(Qml qml/qqmlplatform.cpp qml/qqmlplatform_p.h qml/qqmlprivate.h qml/qqmlproperty.cpp qml/qqmlproperty.h qml/qqmlproperty_p.h + qml/qqmlpropertybinding.cpp qml/qqmlpropertybinding_p.h qml/qqmlpropertycache.cpp qml/qqmlpropertycache_p.h qml/qqmlpropertycachecreator.cpp qml/qqmlpropertycachecreator_p.h qml/qqmlpropertycachemethodarguments_p.h diff --git a/src/qml/qml/qml.pri b/src/qml/qml/qml.pri index f937d4d0b3..6cca897e08 100644 --- a/src/qml/qml/qml.pri +++ b/src/qml/qml/qml.pri @@ -54,6 +54,7 @@ SOURCES += \ $$PWD/qqmlfile.cpp \ $$PWD/qqmlplatform.cpp \ $$PWD/qqmlbinding.cpp \ + $$PWD/qqmlpropertybinding.cpp \ $$PWD/qqmlabstracturlinterceptor.cpp \ $$PWD/qqmlapplicationengine.cpp \ $$PWD/qqmllistwrapper.cpp \ @@ -148,6 +149,7 @@ HEADERS += \ $$PWD/qqmlfile.h \ $$PWD/qqmlplatform_p.h \ $$PWD/qqmlbinding_p.h \ + $$PWD/qqmlpropertybinding_p.h \ $$PWD/qqmlextensionplugin_p.h \ $$PWD/qqmlabstracturlinterceptor.h \ $$PWD/qqmlapplicationengine_p.h \ diff --git a/src/qml/qml/qqmlobjectcreator.cpp b/src/qml/qml/qqmlobjectcreator.cpp index 213e15e173..2ee2969d43 100644 --- a/src/qml/qml/qqmlobjectcreator.cpp +++ b/src/qml/qml/qqmlobjectcreator.cpp @@ -60,6 +60,7 @@ #include <private/qjsvalue_p.h> #include <private/qv4generatorobject_p.h> #include <private/qv4resolvedtypereference_p.h> +#include <private/qqmlpropertybinding_p.h> #include <QScopedValueRollback> @@ -917,6 +918,12 @@ bool QQmlObjectCreator::setPropertyBinding(const QQmlPropertyData *bindingProper _scopeObject, runtimeFunction, currentQmlContext()); bs->takeExpression(expr); + } else if (bindingProperty->isQProperty()) { + // ### TODO: support binding->isTranslationBinding() + QV4::Function *runtimeFunction = compilationUnit->runtimeFunctions[binding->value.compiledScriptIndex]; + auto qmlBinding = QQmlPropertyBinding::create(bindingProperty, runtimeFunction, _scopeObject, context, currentQmlContext()); + void *argv[] = { &qmlBinding }; + _bindingTarget->qt_metacall(QMetaObject::SetQPropertyBinding, bindingProperty->coreIndex(), argv); } else { // When writing bindings to grouped properties implemented as value types, // such as point.x: { someExpression; }, then the binding is installed on diff --git a/src/qml/qml/qqmlpropertybinding.cpp b/src/qml/qml/qqmlpropertybinding.cpp new file mode 100644 index 0000000000..ca64cf412a --- /dev/null +++ b/src/qml/qml/qqmlpropertybinding.cpp @@ -0,0 +1,101 @@ +/**************************************************************************** +** +** Copyright (C) 2020 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$ +** +****************************************************************************/ + +#include "qqmlpropertybinding_p.h" + +#include <private/qpropertybinding_p.h> + +QT_BEGIN_NAMESPACE + +QUntypedPropertyBinding QQmlPropertyBinding::create(const QQmlPropertyData *pd, QV4::Function *function, + QObject *obj, const QQmlRefPointer<QQmlContextData> &ctxt, + QV4::ExecutionContext *scope) +{ + auto binding = new QQmlPropertyBinding(QMetaType(pd->propType())); + binding->setNotifyOnValueChanged(true); + binding->setContext(ctxt); + binding->setScopeObject(obj); + binding->setupFunction(scope, function); + return QUntypedPropertyBinding(QPropertyBindingPrivatePtr(binding)); +} + +void QQmlPropertyBinding::expressionChanged() +{ + setDirty(true); +} + +QQmlPropertyBinding::QQmlPropertyBinding(const QMetaType &mt) + : QPropertyBindingPrivate(mt, + [this](const QMetaType &metaType, void *dataPtr) -> QUntypedPropertyBinding::BindingEvaluationResult { + return evaluateAndReturnTrueIfChanged(metaType, dataPtr); + }, QPropertyBindingSourceLocation()) +{ +} + +QUntypedPropertyBinding::BindingEvaluationResult QQmlPropertyBinding::evaluateAndReturnTrueIfChanged(const QMetaType &metaType, void *dataPtr) +{ + QQmlEngine *engine = context()->engine(); + QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine); + ep->referenceScarceResources(); + + bool isUndefined = false; + + QV4::Scope scope(engine->handle()); + QV4::ScopedValue result(scope, QQmlJavaScriptExpression::evaluate(&isUndefined)); + + ep->dereferenceScarceResources(); + + if (hasError()) { + QPropertyBindingError error(QPropertyBindingError::UnknownError); + error.setDescription(delayedError()->error().description()); + return error; + } + + QVariant resultVariant(scope.engine->toVariant(result, metaType.id())); + + int compareResult = 0; + if (QMetaType::compare(dataPtr, resultVariant.constData(), metaType.id(), &compareResult) + && compareResult == 0) + return false; + QMetaType::destruct(metaType.id(), dataPtr); + QMetaType::construct(metaType.id(), dataPtr, resultVariant.constData()); + return true; +} + +QT_END_NAMESPACE diff --git a/src/qml/qml/qqmlpropertybinding_p.h b/src/qml/qml/qqmlpropertybinding_p.h new file mode 100644 index 0000000000..3bd6772413 --- /dev/null +++ b/src/qml/qml/qqmlpropertybinding_p.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2020 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$ +** +****************************************************************************/ + +#ifndef QQMLPROPERTYBINDING_P_H +#define QQMLPROPERTYBINDING_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qproperty.h> +#include <private/qpropertybinding_p.h> + +#include "qqmlpropertydata_p.h" +#include "qqmljavascriptexpression_p.h" + +#include <memory> + +QT_BEGIN_NAMESPACE + +class QQmlPropertyBinding final : public QQmlJavaScriptExpression, + public QPropertyBindingPrivate +{ +public: + static QUntypedPropertyBinding create(const QQmlPropertyData *pd, QV4::Function *function, + QObject *obj, const QQmlRefPointer<QQmlContextData> &ctxt, + QV4::ExecutionContext *scope); + + virtual void expressionChanged(); + +private: + QQmlPropertyBinding(const QMetaType &metaType); + + QUntypedPropertyBinding::BindingEvaluationResult evaluateAndReturnTrueIfChanged(const QMetaType &metaType, + void *dataPtr); +}; + +QT_END_NAMESPACE + +#endif // QQMLPROPERTYBINDING_P_H diff --git a/src/qml/qml/qqmlpropertycache.cpp b/src/qml/qml/qqmlpropertycache.cpp index 4b17760f8e..72226a452d 100644 --- a/src/qml/qml/qqmlpropertycache.cpp +++ b/src/qml/qml/qqmlpropertycache.cpp @@ -76,6 +76,7 @@ static QQmlPropertyData::Flags fastFlagsForProperty(const QMetaProperty &p) flags.setIsResettable(p.isResettable()); flags.setIsFinal(p.isFinal()); flags.setIsRequired(p.isRequired()); + flags.setIsQProperty(p.isQProperty()); if (p.isEnumType()) flags.type = QQmlPropertyData::Flags::EnumType; diff --git a/src/qml/qml/qqmlpropertydata_p.h b/src/qml/qml/qqmlpropertydata_p.h index 9fa272bcdf..697c995706 100644 --- a/src/qml/qml/qqmlpropertydata_p.h +++ b/src/qml/qml/qqmlpropertydata_p.h @@ -97,7 +97,7 @@ public: // b when type equals FunctionType. For that reason, the semantic meaning of the bit is // overloaded, and the accessor functions are used to get the correct value // - // Moreover, isSignalHandler, isOverload and isCloned and isConstructor make only sense + // Moreover, isSignalHandler, isOverload and isCloned make only sense // for functions, too (and could at a later point be reused for flags that only make sense // for non-functions) // @@ -111,7 +111,7 @@ public: unsigned isSignalHandler : 1; // Function is a signal handler unsigned isOverload : 1; // Function is an overload of another function unsigned isRequiredORisCloned : 1; // Has REQUIRED flag OR The function was marked as cloned - unsigned isConstructor : 1; // The function was marked is a constructor + unsigned isConstructorORisQProperty : 1; // The function was marked is a constructor OR property is backed by QProperty<T> unsigned isDirect : 1; // Exists on a C++ QMetaObject unsigned isOverridden : 1; // Is overridden by a extension property public: @@ -156,6 +156,10 @@ public: isOverridden = b; } + void setIsQProperty(bool b) { + isConstructorORisQProperty = b; + } + void setIsDirect(bool b) { isDirect = b; } @@ -204,7 +208,7 @@ public: void setIsConstructor(bool b) { Q_ASSERT(type == FunctionType); - isConstructor = b; + isConstructorORisQProperty = b; } }; @@ -249,7 +253,8 @@ public: bool isOverload() const { return m_flags.isOverload; } void setOverload(bool onoff) { m_flags.isOverload = onoff; } bool isCloned() const { return isFunction() && m_flags.isRequiredORisCloned; } - bool isConstructor() const { return m_flags.isConstructor; } + bool isConstructor() const { return m_flags.isConstructorORisQProperty; } + bool isQProperty() const { return m_flags.isConstructorORisQProperty; } bool hasOverride() const { return overrideIndex() >= 0; } bool hasRevision() const { return revision() != QTypeRevision::zero(); } @@ -441,7 +446,7 @@ QQmlPropertyData::Flags::Flags() , isSignalHandler(false) , isOverload(false) , isRequiredORisCloned(false) - , isConstructor(false) + , isConstructorORisQProperty(false) , isOverridden(false) , type(OtherType) , notFullyResolved(false) @@ -459,7 +464,7 @@ bool QQmlPropertyData::Flags::operator==(const QQmlPropertyData::Flags &other) c isSignalHandler == other.isSignalHandler && isRequiredORisCloned == other.isRequiredORisCloned && type == other.type && - isConstructor == other.isConstructor && + isConstructorORisQProperty == other.isConstructorORisQProperty && notFullyResolved == other.notFullyResolved && overrideIndexIsProperty == other.overrideIndexIsProperty; } |
