aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlpropertybinding.cpp
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2020-03-23 21:32:38 +0100
committerSimon Hausmann <simon.hausmann@qt.io>2020-03-30 14:18:18 +0200
commitdff7689a016d63fbaf1abc6f00a768dbfb8ec095 (patch)
tree6f0b67ce5d6dec279c0bbf62f014955cd0f25e32 /src/qml/qml/qqmlpropertybinding.cpp
parent9f2c5aa2efd24507f4508889911873bc1c9d301e (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/qml/qml/qqmlpropertybinding.cpp')
-rw-r--r--src/qml/qml/qqmlpropertybinding.cpp101
1 files changed, 101 insertions, 0 deletions
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