Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
####################################################################################################
#
# 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/>.
#
####################################################################################################
__all__ = [
'QmlApplication',
]
####################################################################################################
import argparse
import logging
import sys
from pathlib import Path
from PyQt5.QtCore import (
pyqtProperty, pyqtSignal, QObject,
Qt, QTimer, QUrl,
)
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQml import qmlRegisterType, qmlRegisterUncreatableType, QQmlApplicationEngine
from PyQt5.QtQuick import QQuickPaintedItem, QQuickView
from Patro.GraphicEngine.Painter.QtPainter import QtScene, QtQuickPaintedSceneItem
####################################################################################################
_module_logger = logging.getLogger(__name__)
####################################################################################################
class QmlApplication(QObject):
_logger = _module_logger.getChild('QmlApplication')
##############################################
def __init__(self, application):
super().__init__()
self._application = application
self._scene = None
##############################################
sceneChanged = pyqtSignal()
@pyqtProperty(QtScene, notify=sceneChanged)
def scene(self):
return self._scene
@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._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):
instance = None
_logger = _module_logger.getChild('Application')
##############################################
# Fixme: Singleton
@classmethod
def create(cls, *args, **kwargs):
if cls.instance is not None:
raise NameError('Instance exists')
cls.instance = cls(*args, **kwargs)
return cls.instance
##############################################
def __init__(self):
super().__init__()
self._parse_arguments()
self._appplication = QGuiApplication(sys.argv)
self._engine = QQmlApplicationEngine()
self._qml_application = QmlApplication(self)
self._scene = None
# self._load_translation()
self._register_qml_types()
self._set_context_properties()
self._load_qml_main()
# self._run_before_event_loop()
QTimer.singleShot(0, self._post_init)
# self._view = QQuickView()
# self._view.setResizeMode(QQuickView.SizeRootObjectToView)
# self._view.setSource(qml_url)
##############################################
@property
def args(self):
return self._args
@property
def qml_application(self):
return self._qml_application
##############################################
@classmethod
def setup_gui_application(cls):
# QGuiApplication.setApplicationName(APPLICATION_NAME)
# QGuiApplication.setOrganizationName(ORGANISATION_NAME)
QGuiApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
##############################################
def _parse_arguments(self):
parser = argparse.ArgumentParser(
description='Patro',
)
# parser.add_argument(
# '--version',
# action='store_true', default=False,
# help="show version and exit",
# )
parser.add_argument(
'--user-script',
action=PathAction,
default=None,
help='user script to execute',
)
parser.add_argument(
'--user-script-args',
default='',
help="user script args (don't forget to quote)",
)
self._args = parser.parse_args()
##############################################
# def _load_translationt(self):
# locale = QLocale()
# if m_translator.load(locale, '...', '.', ':/translations', '.qm'):
# m_application.installTranslator(m_translator)
# else:
# raise "No translator for locale" locale.name()
##############################################
def _register_qml_types(self):
qmlRegisterUncreatableType(QmlApplication, 'Patro', 1, 0, 'QmlApplication', 'Cannot create QmlApplication')
qmlRegisterUncreatableType(QtScene, 'Patro', 1, 0, 'QtScene', 'Cannot create QtScene')
qmlRegisterType(QtQuickPaintedSceneItem, 'Patro', 1, 0, 'PaintedSceneItem')
##############################################
def _set_context_properties(self):
context = self._engine.rootContext()
context.setContextProperty('application', self._qml_application)
##############################################
def _load_qml_main(self):
# self._engine.addImportPath('qrc:///qml')
qml_path = Path(__file__).parent.joinpath('qml', 'main.qml')
qml_url = QUrl.fromLocalFile(str(qml_path))
# QUrl('qrc:/qml/main.qml')
self._engine.load(qml_url)
##############################################
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)
##############################################
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.
"""
script_path = Path(script_path).absolute()
self._logger.info('Execute user script: {}'.format(script_path))
try:
source = open(script_path).read()
except FileNotFoundError:
self._logger.info('File {} not found'.format(script_path))
sys.exit(1)
bytecode = compile(source, script_path, 'exec')
exec(bytecode, {'application':self})
self._logger.info('User script done')