diff options
-rw-r--r-- | sources/pyside-tools/metaobjectdump.py | 3 | ||||
-rw-r--r-- | sources/pyside6/doc/extras/QtQml.QmlForeign.rst | 33 | ||||
-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 | ||||
-rw-r--r-- | sources/pyside6/tests/QtQml/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sources/pyside6/tests/QtQml/registerforeign.py | 77 | ||||
-rw-r--r-- | sources/pyside6/tests/QtQml/registerforeign.qml | 33 |
12 files changed, 349 insertions, 3 deletions
diff --git a/sources/pyside-tools/metaobjectdump.py b/sources/pyside-tools/metaobjectdump.py index b1c7c559e..914a52b09 100644 --- a/sources/pyside-tools/metaobjectdump.py +++ b/sources/pyside-tools/metaobjectdump.py @@ -248,6 +248,9 @@ class MetaObjectDumpVisitor(ast.NodeVisitor): elif name == "ClassInfo" and node.keywords: kw = node.keywords[0] class_decorators.append(_decorator(kw.arg, kw.value.value)) + elif name == "QmlForeign" and len(node.args) == 1: + d = _decorator("QML.Foreign", node.args[0].id) + class_decorators.append(d) elif name == "QmlNamedElement" and node.args: name = node.args[0].value class_decorators.append(_decorator("QML.Element", name)) diff --git a/sources/pyside6/doc/extras/QtQml.QmlForeign.rst b/sources/pyside6/doc/extras/QtQml.QmlForeign.rst new file mode 100644 index 000000000..c58be3cb9 --- /dev/null +++ b/sources/pyside6/doc/extras/QtQml.QmlForeign.rst @@ -0,0 +1,33 @@ +.. currentmodule:: PySide6.QtQml +.. _QmlForeign: + +QmlForeign +********** + +.. py:decorator:: QmlForeign + + This decorator can be used to change the type that is created by QML. + + This is useful for registering types that cannot be amended by adding the + QmlElement decorator, for example because they belong to 3rdparty libraries. + + .. code-block:: python + + QML_IMPORT_NAME = "com.library.name" + QML_IMPORT_MAJOR_VERSION = 1 + QML_IMPORT_MINOR_VERSION = 0 # Optional + + @QmlNamedElement("QWidget") + @QmlForeign(QWidget) + class ForeignWidgetHelperClass(QObject): + ... + + Afterwards the class may be used in QML: + + .. code-block:: javascript + + import com.library.name 1.0 + + QWidget { + // ... + } 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 diff --git a/sources/pyside6/tests/QtQml/CMakeLists.txt b/sources/pyside6/tests/QtQml/CMakeLists.txt index 3fc443eab..2cf6de1c1 100644 --- a/sources/pyside6/tests/QtQml/CMakeLists.txt +++ b/sources/pyside6/tests/QtQml/CMakeLists.txt @@ -17,6 +17,7 @@ PYSIDE_TEST(qqmlnetwork_test.py) PYSIDE_TEST(qquickview_test.py) PYSIDE_TEST(connect_python_qml.py) PYSIDE_TEST(registertype.py) +PYSIDE_TEST(registerforeign.py) PYSIDE_TEST(registerqmlfile.py) PYSIDE_TEST(registeruncreatabletype.py) PYSIDE_TEST(registersingletontype.py) diff --git a/sources/pyside6/tests/QtQml/registerforeign.py b/sources/pyside6/tests/QtQml/registerforeign.py new file mode 100644 index 000000000..7d2d9ef18 --- /dev/null +++ b/sources/pyside6/tests/QtQml/registerforeign.py @@ -0,0 +1,77 @@ +############################################################################# +## +## Copyright (C) 2022 The Qt Company Ltd. +## Contact: https://www.qt.io/licensing/ +## +## This file is part of the test suite of Qt for Python. +## +## $QT_BEGIN_LICENSE:GPL-EXCEPT$ +## 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 General Public License Usage +## Alternatively, this file may be used under the terms of the GNU +## General Public License version 3 as published by the Free Software +## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +## 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-3.0.html. +## +## $QT_END_LICENSE$ +## +############################################################################# + +import os +import sys +import unittest + +from pathlib import Path +sys.path.append(os.fspath(Path(__file__).resolve().parents[1])) +from init_paths import init_test_paths +init_test_paths(False) + +from helper.helper import qmlcomponent_errorstring +from helper.timedqguiapplication import TimedQGuiApplication + +from PySide6.QtCore import Property, QObject, QUrl, qVersion +from PySide6.QtGui import QGuiApplication, QRasterWindow +from PySide6.QtQml import (QmlNamedElement, QmlForeign, QQmlEngine, + QQmlComponent) + + +"""Test the QmlForeign decorator, letting the QQmlEngine create a QRasterWindow.""" + + +QML_IMPORT_NAME = "Foreign" +QML_IMPORT_MAJOR_VERSION = 1 + + +@QmlNamedElement("QRasterWindow") +@QmlForeign(QRasterWindow) +class RasterWindowForeign(QObject): + def __init__(self, parent=None): + super().__init__(parent) + + +class TestQmlForeign(TimedQGuiApplication): + + def testIt(self): + engine = QQmlEngine() + file = Path(__file__).resolve().parent / 'registerforeign.qml' + self.assertTrue(file.is_file()) + component = QQmlComponent(engine, QUrl.fromLocalFile(file)) + window = component.create() + self.assertTrue(window, qmlcomponent_errorstring(component)) + self.assertEqual(type(window), QRasterWindow) + window.setTitle(f"Qt {qVersion()}") + window.show() + self.app.exec() + + +if __name__ == '__main__': + unittest.main() diff --git a/sources/pyside6/tests/QtQml/registerforeign.qml b/sources/pyside6/tests/QtQml/registerforeign.qml new file mode 100644 index 000000000..04d29d9f8 --- /dev/null +++ b/sources/pyside6/tests/QtQml/registerforeign.qml @@ -0,0 +1,33 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of Qt for Python. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import Foreign + +QRasterWindow { + id: rasterWindow +} |