Commit 3028684d authored by Fabrice Salvaire's avatar Fabrice Salvaire

QMl Application: cleanup and fixed for qml error

parent 91ca6883
####################################################################################################
#
# Patro - A Python library to make patterns for fashion design
# Copyright (C) 2017 Fabrice Salvaire
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
####################################################################################################
"""Module to implement argparse actions.
"""
####################################################################################################
__all__ = [
'PathAction',
]
####################################################################################################
import argparse
from pathlib import Path
####################################################################################################
class PathAction(argparse.Action):
"""Class to implement argparse action for path."""
##############################################
def __call__(self, parser, namespace, values, option_string=None):
if values is not None:
if isinstance(values, list):
path = [Path(x) for x in values]
else:
path = Path(values)
else:
path = None
setattr(namespace, self.dest, path)
......@@ -18,6 +18,12 @@
#
####################################################################################################
"""Module to implement a Qt Application.
"""
####################################################################################################
__all__ = [
'QmlApplication',
]
......@@ -44,6 +50,7 @@ from QtShim.QtQuick import QQuickPaintedItem, QQuickView
# from QtShim.QtQuickControls2 import QQuickStyle
from Patro.Common.Platform import QtPlatform
from Patro.Common.ArgparseAction import PathAction
from Patro.GraphicEngine.Painter.QtPainter import QtScene, QtQuickPaintedSceneItem
from .rcc import PatroRessource
......@@ -56,6 +63,8 @@ _module_logger = logging.getLogger(__name__)
class QmlApplication(QObject):
"""Class to implement a Qt QML Application."""
_logger = _module_logger.getChild('QmlApplication')
##############################################
......@@ -78,32 +87,16 @@ class QmlApplication(QObject):
@scene.setter
def scene(self, scene):
if self._scene is not scene:
# print('QmlApplication set scene', scene)
self._logger.info('set scene') # Fixme: don't print ???
self._logger.info('set scene {}'.format(scene))
self._scene = scene
self.sceneChanged.emit()
####################################################################################################
class PathAction(argparse.Action):
##############################################
def __call__(self, parser, namespace, values, option_string=None):
if values is not None:
if isinstance(values, list):
path = [Path(x) for x in values]
else:
path = Path(values)
else:
path = None
setattr(namespace, self.dest, path)
####################################################################################################
class Application(QObject):
"""Class to implement a Qt Application."""
instance = None
_logger = _module_logger.getChild('Application')
......@@ -182,8 +175,12 @@ class Application(QObject):
# local_msg = msg.toLocal8Bit()
# localMsg.constData()
file_path = Path(context.file)
method('{1} {3} — {0}'.format(msg, file_path.name, context.line, context.function))
context_file = context.file
if context_file is not None:
file_path = Path(context_file).name
else:
file_path = ''
method('{1} {3} — {0}'.format(msg, file_path, context.line, context.function))
##############################################
......@@ -250,7 +247,6 @@ class Application(QObject):
##############################################
def _set_context_properties(self):
context = self._engine.rootContext()
context.setContextProperty('application', self._qml_application)
......@@ -261,25 +257,29 @@ class Application(QObject):
# self._engine.addImportPath('qrc:///qml')
qml_path = Path(__file__).parent.joinpath('qml', 'main.qml')
qml_url = QUrl.fromLocalFile(str(qml_path))
self._qml_url = QUrl.fromLocalFile(str(qml_path))
# QUrl('qrc:/qml/main.qml')
self._engine.load(qml_url)
self._engine.objectCreated.connect(self._check_qml_is_loaded)
self._engine.load(self._qml_url)
##############################################
def exec_(self):
def _check_qml_is_loaded(self, obj, url):
# See https://bugreports.qt.io/browse/QTBUG-39469
if (obj is None and url == self._qml_url):
sys.exit(-1)
##############################################
def exec_(self):
# self._view.show()
sys.exit(self._appplication.exec_())
##############################################
def _post_init(self):
# Fixme: ui refresh ???
self._logger.info('post init')
if self._args.user_script is not None:
self.execute_user_script(self._args.user_script)
......@@ -287,8 +287,8 @@ class Application(QObject):
def execute_user_script(self, script_path):
"""Execute an user script provided by file *script_path* in a context where is defined a variable
*application* that is a reference to the application instance.
"""Execute an user script provided by file *script_path* in a context where is defined a
variable *application* that is a reference to the application instance.
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment