diff options
| author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-02-09 08:12:41 +0100 |
|---|---|---|
| committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2022-02-11 11:07:26 +0100 |
| commit | 9827a47135d8e0ac7262c6c13188e387bf034b3b (patch) | |
| tree | 41c0ceb2f2719435deebbf8b45581d72bf48603b /sources/pyside6/libpysideqml | |
| parent | 12c9791e7c2be1feffd87a9dc2bdf159afeb9c95 (diff) | |
Add the @QmlForeign decorator
[ChangeLog][PySide6] The @QmlForeign decorator has been added.
Task-number: PYSIDE-1709
Change-Id: I7dbba47923efb6a1c3ac4accb2b3adcd3fb6fd9b
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/pyside6/libpysideqml')
| -rw-r--r-- | sources/pyside6/libpysideqml/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqml.cpp | 2 | ||||
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqmlforeign.cpp | 129 | ||||
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqmlforeign_p.h | 53 | ||||
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqmlregistertype.cpp | 9 | ||||
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqmltypeinfo.cpp | 9 | ||||
| -rw-r--r-- | sources/pyside6/libpysideqml/pysideqmltypeinfo_p.h | 2 |
7 files changed, 202 insertions, 3 deletions
diff --git a/sources/pyside6/libpysideqml/CMakeLists.txt b/sources/pyside6/libpysideqml/CMakeLists.txt index 2b12cfe30..aa07a5077 100644 --- a/sources/pyside6/libpysideqml/CMakeLists.txt +++ b/sources/pyside6/libpysideqml/CMakeLists.txt @@ -2,6 +2,7 @@ set(libpysideqml_libraries Qt::Core Qt::CorePrivate Qt::Qml Qt::QmlPrivate) set(libpysideqml_SRC pysideqml.cpp + pysideqmlforeign.cpp pysideqmlregistertype.cpp pysideqmlmetacallerror.cpp pysideqmllistproperty.cpp diff --git a/sources/pyside6/libpysideqml/pysideqml.cpp b/sources/pyside6/libpysideqml/pysideqml.cpp index c6bcad549..06646a6e8 100644 --- a/sources/pyside6/libpysideqml/pysideqml.cpp +++ b/sources/pyside6/libpysideqml/pysideqml.cpp @@ -39,6 +39,7 @@ #include "pysideqml.h" #include "pysideqmllistproperty_p.h" +#include "pysideqmlforeign_p.h" #include "pysideqmlnamedelement_p.h" #include "pysideqmluncreatable.h" #include "pysideqmlmetacallerror_p.h" @@ -51,6 +52,7 @@ namespace PySide::Qml void init(PyObject *module) { initQtQmlListProperty(module); + initQmlForeign(module); initQmlNamedElement(module); initQmlUncreatable(module); PySide::SignalManager::setQmlMetaCallErrorHandler(PySide::Qml::qmlMetaCallErrorHandler); diff --git a/sources/pyside6/libpysideqml/pysideqmlforeign.cpp b/sources/pyside6/libpysideqml/pysideqmlforeign.cpp new file mode 100644 index 000000000..fb257f044 --- /dev/null +++ b/sources/pyside6/libpysideqml/pysideqmlforeign.cpp @@ -0,0 +1,129 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $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 "pysideqmlforeign_p.h" +#include "pysideqmltypeinfo_p.h" + +#include <signalmanager.h> +#include <pysideclassdecorator_p.h> + +#include <shiboken.h> +#include <signature.h> +#include <sbkstring.h> + +#include <QtCore/QtGlobal> +#include <QtCore/QDebug> + +// The QmlForeign decorator modifies QmlElement to create a different type +// QmlElement. +class PySideQmlForeignPrivate : public PySide::ClassDecorator::TypeDecoratorPrivate +{ +public: + PyObject *tp_call(PyObject *self, PyObject *args, PyObject * /* kw */) override; + const char *name() const override; +}; + +// The call operator is passed the class type and registers the type +// in QmlTypeInfo. +PyObject *PySideQmlForeignPrivate::tp_call(PyObject *self, PyObject *args, PyObject * /* kw */) +{ + PyObject *klass = tp_call_check(args, CheckMode::WrappedType); + if (klass == nullptr) + return nullptr; + + auto *data = DecoratorPrivate::get<PySideQmlForeignPrivate>(self); + const auto info = PySide::Qml::ensureQmlTypeInfo(klass); + info->foreignType = data->type(); + // Insert an alias to be used by the factory functions of Decorators like + // @QmlExtended and @QmlAttached. + auto *foreignObj = reinterpret_cast<const PyObject *>(info->foreignType); + PySide::Qml::insertQmlTypeInfoAlias(foreignObj, info); + + Py_INCREF(klass); + return klass; +} + +const char *PySideQmlForeignPrivate::name() const +{ + return "QmlForeign"; +} + +extern "C" { + +static PyTypeObject *createPySideQmlForeignType(void) +{ + auto typeSlots = + PySide::ClassDecorator::Methods<PySideQmlForeignPrivate>::typeSlots(); + + PyType_Spec PySideQmlForeignType_spec = { + "2:PySide6.QtCore.qmlForeign", + sizeof(PySideClassDecorator), + 0, + Py_TPFLAGS_DEFAULT, + typeSlots.data() + }; + return SbkType_FromSpec(&PySideQmlForeignType_spec); +} + +PyTypeObject *PySideQmlForeign_TypeF(void) +{ + static auto *type = createPySideQmlForeignType(); + return type; +} + +} // extern "C" + +static const char *qmlAttached_SignatureStrings[] = { + "PySide6.QtQml.QmlForeign(self,type:type)", + nullptr // Sentinel +}; + +namespace PySide::Qml { + +void initQmlForeign(PyObject *module) +{ + if (InitSignatureStrings(PySideQmlForeign_TypeF(), qmlAttached_SignatureStrings) < 0) + return; + + Py_INCREF(PySideQmlForeign_TypeF()); + PyModule_AddObject(module, "QmlForeign", + reinterpret_cast<PyObject *>(PySideQmlForeign_TypeF())); +} + +} // namespace PySide::Qml diff --git a/sources/pyside6/libpysideqml/pysideqmlforeign_p.h b/sources/pyside6/libpysideqml/pysideqmlforeign_p.h new file mode 100644 index 000000000..99f1de339 --- /dev/null +++ b/sources/pyside6/libpysideqml/pysideqmlforeign_p.h @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of Qt for Python. +** +** $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 PYSIDEQMLFOREIGN_P_H +#define PYSIDEQMLFOREIGN_P_H + +#include <sbkpython.h> + +namespace PySide::Qml { +struct QmlExtensionInfo; +struct QmlTypeInfo; + +void initQmlForeign(PyObject *module); + +} // namespace PySide::Qml + +#endif // PYSIDEQMLFOREIGN_P_H diff --git a/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp b/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp index 2abb8bfd2..30f8f2bfb 100644 --- a/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp +++ b/sources/pyside6/libpysideqml/pysideqmlregistertype.cpp @@ -439,16 +439,19 @@ PyObject *qmlElementMacro(PyObject *pyObj, const char *decoratorName, RegisterMode mode = RegisterMode::Normal; const char *noCreationReason = nullptr; const auto info = PySide::Qml::qmlTypeInfo(pyObj); + auto *registerObject = pyObj; if (!info.isNull()) { if (info->flags.testFlag(PySide::Qml::QmlTypeFlag::Singleton)) mode = RegisterMode::Singleton; else if (info->flags.testFlag(PySide::Qml::QmlTypeFlag::Uncreatable)) mode = RegisterMode::Uncreatable; noCreationReason = info->noCreationReason.c_str(); - + if (info->foreignType) + registerObject = reinterpret_cast<PyObject *>(info->foreignType); } - return qmlElementMacroHelper(pyObj, decoratorName, typeName, mode, - noCreationReason); + if (!qmlElementMacroHelper(registerObject, decoratorName, typeName, mode, noCreationReason)) + return nullptr; + return pyObj; } PyObject *qmlElementMacro(PyObject *pyObj) diff --git a/sources/pyside6/libpysideqml/pysideqmltypeinfo.cpp b/sources/pyside6/libpysideqml/pysideqmltypeinfo.cpp index 0a080bfd0..2d5b48c98 100644 --- a/sources/pyside6/libpysideqml/pysideqmltypeinfo.cpp +++ b/sources/pyside6/libpysideqml/pysideqmltypeinfo.cpp @@ -42,6 +42,8 @@ #include <QtCore/QDebug> #include <QtCore/QHash> +#include <algorithm> + namespace PySide::Qml { using QmlTypeInfoHash = QHash<const PyObject *, QmlTypeInfoPtr>; @@ -57,6 +59,11 @@ QmlTypeInfoPtr ensureQmlTypeInfo(const PyObject *o) return it.value(); } +void insertQmlTypeInfoAlias(const PyObject *o, const QmlTypeInfoPtr &value) +{ + qmlTypeInfoHashStatic()->insert(o, value); +} + QmlTypeInfoPtr qmlTypeInfo(const PyObject *o) { auto *hash = qmlTypeInfoHashStatic(); @@ -73,6 +80,8 @@ QDebug operator<<(QDebug d, const QmlTypeInfo &i) d << "QmlTypeInfo(" << i.flags; if (!i.noCreationReason.empty()) d << ", noCreationReason=\"" << i.noCreationReason.c_str() << '"'; + if (i.foreignType) + d << ", foreignType=" << i.foreignType->tp_name; d << ')'; return d; } diff --git a/sources/pyside6/libpysideqml/pysideqmltypeinfo_p.h b/sources/pyside6/libpysideqml/pysideqmltypeinfo_p.h index 15bf465df..26ea782d3 100644 --- a/sources/pyside6/libpysideqml/pysideqmltypeinfo_p.h +++ b/sources/pyside6/libpysideqml/pysideqmltypeinfo_p.h @@ -65,11 +65,13 @@ struct QmlTypeInfo { QmlTypeFlags flags; std::string noCreationReason; + PyTypeObject *foreignType = nullptr; }; using QmlTypeInfoPtr = QSharedPointer<QmlTypeInfo>; QmlTypeInfoPtr ensureQmlTypeInfo(const PyObject *o); +void insertQmlTypeInfoAlias(const PyObject *o, const QmlTypeInfoPtr &value); QmlTypeInfoPtr qmlTypeInfo(const PyObject *o); #ifndef QT_NO_DEBUG_STREAM |
