diff --git a/Patro/Common/ArgparseAction.py b/Patro/Common/ArgparseAction.py new file mode 100644 index 0000000000000000000000000000000000000000..28538698d1b93707aec22b4569595e56dcfd6558 --- /dev/null +++ b/Patro/Common/ArgparseAction.py @@ -0,0 +1,53 @@ +#################################################################################################### +# +# 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 . +# +#################################################################################################### + +"""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) diff --git a/Patro/QtApplication/QmlApplication.py b/Patro/QtApplication/QmlApplication.py index 4428ade1a3a4c004effba0b93580d099edb49011..b189a34ed332b9bc0dad3d84ef3333b3e84f9eaa 100644 --- a/Patro/QtApplication/QmlApplication.py +++ b/Patro/QtApplication/QmlApplication.py @@ -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. """