Skip to content

Commit

Permalink
Simplify examples and add more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSazonov committed Aug 30, 2024
1 parent 2056dce commit e6b0fd2
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ QtObject {

readonly property var activeBackend: {
if (typeof Backends.PyBackend !== 'undefined') {
console.debug('Currently, the REAL python backend is in use')
console.debug('REAL python backend is in use')
return Backends.PyBackend
} else {
console.debug('Currently, the MOCK backend is in use')
console.debug('MOCK QML backend is in use')
return Backends.MockBackend
}
}
Expand Down
11 changes: 7 additions & 4 deletions examples/AdvancedPy/src/AdvancedPy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
qInstallMessageHandler(console.qmlMessageHandler)
console.debug('Custom Qt message handler defined')

# This singleton object will be accessible in QML as follows:
# import Backends 1.0 as Backends OR import Backends as Backends
# property var activeBackend: Backends.PyBackend
qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
console.debug('Backend class is registered as a singleton type for QML')

app = QGuiApplication(sys.argv)
console.debug(f'Qt Application created {app}')

engine = QQmlApplicationEngine()
console.debug(f'QML application engine created {engine}')

qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
console.debug('Backend class is registered to be accessible from QML via the name PyBackend')

engine.addImportPath(EASYAPP_DIR)
engine.addImportPath(CURRENT_DIR)
engine.addImportPath(EASYAPP_DIR)
console.debug('Paths added where QML searches for components')

engine.load(CURRENT_DIR / 'main.qml')
Expand Down
4 changes: 2 additions & 2 deletions examples/BasicC++/src/BasicC++/Gui/Globals/BackendWrapper.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ QtObject {

readonly property var activeBackend: {
if (typeof Backends.PyBackend !== 'undefined') {
console.debug('Currently, the REAL python backend is in use')
console.debug('REAL python backend is in use')
return Backends.PyBackend
} else {
console.debug('Currently, the MOCK backend is in use')
console.debug('MOCK QML backend is in use')
return Backends.MockBackend
}
}
Expand Down
16 changes: 9 additions & 7 deletions examples/BasicC++/src/BasicC++/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
#include <QQmlApplicationEngine>
#include <QString>

const QString CURRENT_DIR = "qrc:/"; // path to qml components of the easyapp module
const QString EASYAPP_DIR = "qrc:/../../../../src/EasyApp"; // path to qml components of the current project

int main(int argc, char *argv[])
{
// Create application
// Create Qt application
QGuiApplication app(argc, argv);

// Create QML application engine
// Create the QML application engine
QQmlApplicationEngine engine;
engine.addImportPath(CURRENT_DIR);
engine.addImportPath(EASYAPP_DIR);

// Add the paths where QML searches for components
// Use whatever is defined in the resources.qrc file
engine.addImportPath("qrc:/");

// Load the main QML component
engine.load("qrc:/main.qml");

// Event loop
// Start the application event loop
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
Expand Down
4 changes: 2 additions & 2 deletions examples/BasicPy/src/BasicPy/Gui/Globals/BackendWrapper.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ QtObject {

readonly property var activeBackend: {
if (typeof Backends.PyBackend !== 'undefined') {
console.debug('Currently, the REAL python backend is in use')
console.debug('REAL python backend is in use')
return Backends.PyBackend
} else {
console.debug('Currently, the MOCK backend is in use')
console.debug('MOCK QML backend is in use')
return Backends.MockBackend
}
}
Expand Down
12 changes: 8 additions & 4 deletions examples/BasicPy/src/BasicPy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@


if __name__ == '__main__':
# Create application
# Create Qt application
app = QGuiApplication(sys.argv)

# Create QML application engine
# Create the QML application engine
engine = QQmlApplicationEngine()
engine.addImportPath(EASYAPP_DIR)

# Add the paths where QML searches for components
engine.addImportPath(CURRENT_DIR)
engine.addImportPath(EASYAPP_DIR)

# Load the main QML component
engine.load(CURRENT_DIR / 'main.qml')

# Event loop
# Start the application event loop
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
4 changes: 2 additions & 2 deletions examples/BasicQml/src/BasicQml/Gui/Globals/BackendWrapper.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ QtObject {

readonly property var activeBackend: {
if (typeof Backends.PyBackend !== 'undefined') {
console.debug('Currently, the REAL python backend is in use')
console.debug('REAL python backend is in use')
return Backends.PyBackend
} else {
console.debug('Currently, the MOCK backend is in use')
console.debug('MOCK QML backend is in use')
return Backends.MockBackend
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def name(self):
def name(self, new_value):
if self._name == new_value:
return
console.debug(IO.format_msg('main', f"Changing project name from '{self.name}' to '{new_value}'"))
print(f" py: Changing project name from '{self.name}' to '{new_value}'")
self._name = new_value
self.nameChanged.emit()

Expand All @@ -113,19 +113,19 @@ def examples(self):

@Slot()
def create(self):
console.debug(IO.format_msg('main', f"Creating project '{self.name}'"))
print(f" py: Creating project '{self.name}'")
self.info['creationDate'] = time.strftime("%d %b %Y %H:%M", time.localtime())
self.infoChanged.emit()
self.created = True

@Slot()
def save(self):
console.debug(IO.format_msg('main', f"Saving project '{self.name}'"))
print(f" py: Saving project '{self.name}'")

@Slot(str, str)
def editInfo(self, path, new_value):
if DottyDict.get(self._info, path) == new_value:
return
console.debug(IO.format_msg('main', f"Changing project info.{path} from '{DottyDict.get(self._info, path)}' to '{new_value}'"))
print(f" py: Changing project info.{path} from '{DottyDict.get(self._info, path)}' to '{new_value}'")
DottyDict.set(self._info, path, new_value)
self.infoChanged.emit()
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ QtObject {

readonly property var activeBackend: {
if (typeof Backends.PyBackend !== 'undefined') {
console.debug('Currently, the REAL python backend is in use')
console.debug('REAL python backend is in use')
return Backends.PyBackend
} else {
console.debug('Currently, the MOCK backend is in use')
console.debug('MOCK QML backend is in use')
return Backends.MockBackend
}
}
Expand Down
25 changes: 11 additions & 14 deletions examples/IntermediatePy/src/IntermediatePy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from PySide6.QtGui import QGuiApplication
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.
# If this is not the case, and if the example is run from the EasyApp repository, one need to add the path to the
Expand All @@ -16,32 +15,30 @@
EASYAPP_DIR = CURRENT_DIR / '..' / '..' / '..' / '..' / 'src' # path to qml components of the easyapp module
sys.path.append(str(EASYAPP_DIR))

from EasyApp.Logic.Logging import console

from Backends.real_backend import Backend


if __name__ == '__main__':
qInstallMessageHandler(console.qmlMessageHandler)
console.debug('Custom Qt message handler defined')
# Register the Backend class as a singleton type for QML
# This singleton object will be accessible in QML as follows:
# import Backends 1.0 as Backends OR import Backends as Backends
# property var activeBackend: Backends.PyBackend
qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')

# Create Qt application
app = QGuiApplication(sys.argv)
console.debug(f'Qt Application created {app}')

# Create the QML application engine
engine = QQmlApplicationEngine()
console.debug(f'QML application engine created {engine}')

qmlRegisterSingletonType(Backend, 'Backends', 1, 0, 'PyBackend')
console.debug('Backend class is registered to be accessible from QML via the name PyBackend')

engine.addImportPath(EASYAPP_DIR)
# Add the paths where QML searches for components
engine.addImportPath(CURRENT_DIR)
console.debug('Paths added where QML searches for components')
engine.addImportPath(EASYAPP_DIR)

# Load the main QML component
engine.load(CURRENT_DIR / 'main.qml')
console.debug('Main QML component loaded')

console.debug('Application event loop is about to start')
# Start the application event loop
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())

0 comments on commit e6b0fd2

Please sign in to comment.