From 7099709625f0904ddc60f8ed33ecdd97344d66ed Mon Sep 17 00:00:00 2001 From: Andrew Sazonov Date: Thu, 29 Aug 2024 14:37:50 +0200 Subject: [PATCH] Replace setContextProperty with qmlRegisterSingletonType --- .../src/IntermediatePy.pyproject | 23 +++---- .../IntermediatePy/Gui/Globals/Backend.qml | 62 +++++++++++++++---- .../BackendProxy.qml => MockBackendProxy.qml} | 3 +- .../Logic/{Mock => MockQml}/Project.qml | 0 .../Logic/{Mock => MockQml}/Report.qml | 0 .../Logic/{Mock => MockQml}/Status.qml | 0 .../Logic/{Mock => MockQml}/qmldir | 3 +- .../Logic/{Py => RealPy}/connections.py | 0 .../Logic/{Py => RealPy}/helpers.py | 0 .../Logic/{Py => RealPy}/project.py | 0 .../Logic/{Py => RealPy}/report.py | 0 .../Logic/{Py => RealPy}/status.py | 0 .../src/IntermediatePy/Logic/qmldir | 3 + ...backend_proxy.py => real_backend_proxy.py} | 8 +-- .../IntermediatePy/src/IntermediatePy/main.py | 9 ++- 15 files changed, 76 insertions(+), 35 deletions(-) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Mock/BackendProxy.qml => MockBackendProxy.qml} (91%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Mock => MockQml}/Project.qml (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Mock => MockQml}/Report.qml (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Mock => MockQml}/Status.qml (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Mock => MockQml}/qmldir (60%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Py => RealPy}/connections.py (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Py => RealPy}/helpers.py (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Py => RealPy}/project.py (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Py => RealPy}/report.py (100%) rename examples/IntermediatePy/src/IntermediatePy/Logic/{Py => RealPy}/status.py (100%) create mode 100644 examples/IntermediatePy/src/IntermediatePy/Logic/qmldir rename examples/IntermediatePy/src/IntermediatePy/Logic/{Py/backend_proxy.py => real_backend_proxy.py} (84%) diff --git a/examples/IntermediatePy/src/IntermediatePy.pyproject b/examples/IntermediatePy/src/IntermediatePy.pyproject index d360d5b..dd655d3 100644 --- a/examples/IntermediatePy/src/IntermediatePy.pyproject +++ b/examples/IntermediatePy/src/IntermediatePy.pyproject @@ -29,16 +29,17 @@ "IntermediatePy/Gui/Pages/Report/Sidebar/Basic/Groups/Export.qml", "IntermediatePy/Gui/Pages/Report/Sidebar/Extra/Layout.qml", "IntermediatePy/Gui/Pages/Report/Sidebar/Extra/Groups/Empty.qml", - "IntermediatePy/Logic/Mock/qmldir", - "IntermediatePy/Logic/Mock/BackendProxy.qml", - "IntermediatePy/Logic/Mock/Project.qml", - "IntermediatePy/Logic/Mock/Report.qml", - "IntermediatePy/Logic/Mock/Status.qml", - "IntermediatePy/Logic/Py/backend_proxy.py", - "IntermediatePy/Logic/Py/connections.py", - "IntermediatePy/Logic/Py/helpers.py", - "IntermediatePy/Logic/Py/project.py", - "IntermediatePy/Logic/Py/report.py", - "IntermediatePy/Logic/Py/status.py" + "IntermediatePy/Logic/qmldir", + "IntermediatePy/Logic/MockBackendProxy.qml", + "IntermediatePy/Logic/MockQml/Project.qml", + "IntermediatePy/Logic/MockQml/Report.qml", + "IntermediatePy/Logic/MockQml/Status.qml", + "IntermediatePy/Logic/MockQml/qmldir", + "IntermediatePy/Logic/real_backend_proxy.py", + "IntermediatePy/Logic/RealPy/connections.py", + "IntermediatePy/Logic/RealPy/helpers.py", + "IntermediatePy/Logic/RealPy/project.py", + "IntermediatePy/Logic/RealPy/report.py", + "IntermediatePy/Logic/RealPy/status.py" ] } diff --git a/examples/IntermediatePy/src/IntermediatePy/Gui/Globals/Backend.qml b/examples/IntermediatePy/src/IntermediatePy/Gui/Globals/Backend.qml index d22418b..5b3f3e7 100644 --- a/examples/IntermediatePy/src/IntermediatePy/Gui/Globals/Backend.qml +++ b/examples/IntermediatePy/src/IntermediatePy/Gui/Globals/Backend.qml @@ -6,24 +6,62 @@ pragma Singleton import QtQuick -import Logic.Mock as MockLogic - - -// If the backend_proxy_py object is created in main.py and exposed to qml, it is used as to access -// the necessary backend properties and methods. Otherwise, the mock proxy defined in -// MockLogic/BackendProxy.qml with hardcoded data is used. -// The assumption here is that the real backend proxy and the mock proxy have the same API. +// This module is registered in the main.py file and allows access to the properties +// and backend methods of the singleton object of the ‘PyBackendProxy’ class. +// If ‘PyBackendProxy’ is not defined, then MockBackendProxy from org/easyscience/easydiffraction is used. +// It is required to be able to run the GUI frontend via the qml runtime tool without a Python backend. +import Logic QtObject { + //////////////// + // Backend proxy + //////////////// + readonly property var proxy: { - if (typeof backend_proxy_py !== 'undefined' && backend_proxy_py !== null) { - console.debug('Currently, the real python backend proxy is in use') - return backend_proxy_py + if (typeof PyBackendProxy !== 'undefined' && PyBackendProxy !== null) { + console.debug('Currently, the REAL python backend proxy is in use') + return PyBackendProxy } else { - console.debug('Currently, the mock backend proxy is in use') - return MockLogic.BackendProxy + console.debug('Currently, the MOCK backend proxy is in use') + return MockBackendProxy } } + ///////////// + // Status bar + ///////////// + + readonly property var status: QtObject { + readonly property string project: proxy.status.project + readonly property string phases_count: proxy.status.phases_count + readonly property string experiments_count: proxy.status.experiments_count + readonly property string calculator: proxy.status.calculator + readonly property string minimizer: proxy.status.minimizer + readonly property string variables: proxy.status.variables + } + + /////////////// + // Project page + /////////////// + + readonly property var project: QtObject { + readonly property bool created: proxy.project.created + readonly property string name: proxy.project.name + readonly property var info: proxy.project.info + readonly property var examples: proxy.project.examples + + function create() { proxy.project.create() } + function save() { proxy.project.save() } + } + + /////////////// + // Summary page + /////////////// + + readonly property var summary: QtObject { + readonly property bool created: proxy.report.created + readonly property string as_html: proxy.report.as_html + } + } diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/BackendProxy.qml b/examples/IntermediatePy/src/IntermediatePy/Logic/MockBackendProxy.qml similarity index 91% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Mock/BackendProxy.qml rename to examples/IntermediatePy/src/IntermediatePy/Logic/MockBackendProxy.qml index e0f1d51..b3b1b6e 100644 --- a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/BackendProxy.qml +++ b/examples/IntermediatePy/src/IntermediatePy/Logic/MockBackendProxy.qml @@ -6,7 +6,8 @@ pragma Singleton import QtQuick -import Logic.Mock as MockLogic +import Logic.MockQml as MockLogic + QtObject { diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/Project.qml b/examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/Project.qml similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Mock/Project.qml rename to examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/Project.qml diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/Report.qml b/examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/Report.qml similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Mock/Report.qml rename to examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/Report.qml diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/Status.qml b/examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/Status.qml similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Mock/Status.qml rename to examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/Status.qml diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/qmldir b/examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/qmldir similarity index 60% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Mock/qmldir rename to examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/qmldir index f183065..7fb0562 100644 --- a/examples/IntermediatePy/src/IntermediatePy/Logic/Mock/qmldir +++ b/examples/IntermediatePy/src/IntermediatePy/Logic/MockQml/qmldir @@ -1,6 +1,5 @@ -module MockLogic +module MockQml -singleton BackendProxy BackendProxy.qml singleton Project Project.qml singleton Report Report.qml singleton Status Status.qml diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/connections.py b/examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/connections.py similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Py/connections.py rename to examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/connections.py diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/helpers.py b/examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/helpers.py similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Py/helpers.py rename to examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/helpers.py diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/project.py b/examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/project.py similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Py/project.py rename to examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/project.py diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/report.py b/examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/report.py similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Py/report.py rename to examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/report.py diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/status.py b/examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/status.py similarity index 100% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Py/status.py rename to examples/IntermediatePy/src/IntermediatePy/Logic/RealPy/status.py diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/qmldir b/examples/IntermediatePy/src/IntermediatePy/Logic/qmldir new file mode 100644 index 0000000..f5b82f2 --- /dev/null +++ b/examples/IntermediatePy/src/IntermediatePy/Logic/qmldir @@ -0,0 +1,3 @@ +module Logic + +singleton MockBackendProxy MockBackendProxy.qml diff --git a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/backend_proxy.py b/examples/IntermediatePy/src/IntermediatePy/Logic/real_backend_proxy.py similarity index 84% rename from examples/IntermediatePy/src/IntermediatePy/Logic/Py/backend_proxy.py rename to examples/IntermediatePy/src/IntermediatePy/Logic/real_backend_proxy.py index 0fac05e..cb2fb8e 100644 --- a/examples/IntermediatePy/src/IntermediatePy/Logic/Py/backend_proxy.py +++ b/examples/IntermediatePy/src/IntermediatePy/Logic/real_backend_proxy.py @@ -6,10 +6,10 @@ from EasyApp.Logic.Logging import LoggerLevelHandler -from .connections import ConnectionsHandler -from .project import Project -from .status import Status -from .report import Report +from .RealPy.connections import ConnectionsHandler +from .RealPy.project import Project +from .RealPy.status import Status +from .RealPy.report import Report class BackendProxy(QObject): diff --git a/examples/IntermediatePy/src/IntermediatePy/main.py b/examples/IntermediatePy/src/IntermediatePy/main.py index cf4048a..6f84f43 100644 --- a/examples/IntermediatePy/src/IntermediatePy/main.py +++ b/examples/IntermediatePy/src/IntermediatePy/main.py @@ -6,7 +6,7 @@ import sys from PySide6.QtGui import QGuiApplication -from PySide6.QtQml import QQmlApplicationEngine +from PySide6.QtQml import QQmlApplicationEngine, qmlRegisterSingletonType from PySide6.QtCore import qInstallMessageHandler # It is usually assumed that the EasyApp package is already installed in the desired python environment. @@ -18,7 +18,7 @@ from EasyApp.Logic.Logging import console -from Logic.Py.backend_proxy import BackendProxy +from Logic.real_backend_proxy import BackendProxy if __name__ == '__main__': qInstallMessageHandler(console.qmlMessageHandler) @@ -30,9 +30,8 @@ engine = QQmlApplicationEngine() console.debug(f'QML application engine created {engine}') - backend_proxy = BackendProxy() - engine.rootContext().setContextProperty('backend_proxy_py', backend_proxy) - console.debug('backend_proxy object exposed to QML as backend_proxy_py') + qmlRegisterSingletonType(BackendProxy, 'Logic', 1, 0, 'PyBackendProxy') + console.debug('BackendProxy class is registered to be accessible from QML via the name PyBackendProxy') engine.addImportPath(EASYAPP_DIR) engine.addImportPath(CURRENT_DIR)