Skip to content

Commit

Permalink
Replace setContextProperty with qmlRegisterSingletonType
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSazonov committed Aug 29, 2024
1 parent 53deaed commit 7099709
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 35 deletions.
23 changes: 12 additions & 11 deletions examples/IntermediatePy/src/IntermediatePy.pyproject
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
62 changes: 50 additions & 12 deletions examples/IntermediatePy/src/IntermediatePy/Gui/Globals/Backend.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ pragma Singleton

import QtQuick

import Logic.Mock as MockLogic
import Logic.MockQml as MockLogic


QtObject {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module MockLogic
module MockQml

singleton BackendProxy BackendProxy.qml
singleton Project Project.qml
singleton Report Report.qml
singleton Status Status.qml
3 changes: 3 additions & 0 deletions examples/IntermediatePy/src/IntermediatePy/Logic/qmldir
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Logic

singleton MockBackendProxy MockBackendProxy.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 4 additions & 5 deletions examples/IntermediatePy/src/IntermediatePy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 7099709

Please sign in to comment.