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
####################################################################################################
#
# 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/>.
#
####################################################################################################
####################################################################################################
import logging
from PyQt5.QtCore import (
pyqtProperty, pyqtSignal, QObject,
QRectF, QSizeF, QPointF, Qt,
)
from PyQt5.QtGui import QColor, QPainter, QPainterPath, QPen
# from PyQt5.QtQml import qmlRegisterType
from PyQt5.QtQuick import QQuickPaintedItem
from Patro.GeometryEngine.Vector import Vector2D
from Patro.GraphicEngine.GraphicScene.Scene import GraphicScene
from .Painter import Painter
####################################################################################################
_module_logger = logging.getLogger(__name__)
####################################################################################################
class QtScene(QObject, GraphicScene):
_logger = _module_logger.getChild('QtScene')
##############################################
def __init__(self):
QObject.__init__(self)
GraphicScene.__init__(self)
####################################################################################################
class QtPainter(Painter):
__STROKE_STYLE__ = {
None: None,
'dashDotLine': Qt.DashLine,
'dotLine': Qt.DotLine,
'hair': Qt.SolidLine,
'none': None,
'solid': Qt.SolidLine,
}
__COLOR__ = {
None : None,
'black': QColor('black'),
}
_logger = _module_logger.getChild('QtPainter')
##############################################
def __init__(self, scene=None):
super().__init__(scene)
# self._paper = paper
self._coordinates = {}
##############################################
def paint(self, painter):
self._logger.info('paint')
self._painter = painter
if self._scene is not None:
bounding_box = self._scene.bounding_box
self._scene_offset = QPointF(-bounding_box.x.inf, bounding_box.y.sup)
self._view_offset = QPointF(30, 30)
# painter.resetTransform()
# painter.scale(1, -1) # filp font
# painter.scale(10, 10) # scale font
# bounding_box = self._scene.bounding_box
# painter.translate(-bounding_box.x.inf, bounding_box.y.sup)
super().paint()
##############################################
def _cast_vector(self, position):
point = QPointF(position.x, -position.y)
point += self._scene_offset
point *= 10
point += self._view_offset
return point
##############################################
def _cast_position(self, position):
if isinstance(position, str):
return self._coordinates[position]
elif isinstance(position, Vector2D):
return self._cast_vector(position)
##############################################
def paint_CoordinateItem(self, item):
self._coordinates[item.name] = self._cast_vector(item.position)
##############################################
def paint_TextItem(self, item):
position = self._cast_position(item.position)
# Fixme: anchor position
self._painter.drawText(position, item.text)
##############################################
def _set_pen(self, item):
path_syle = item.path_style
color = self.__COLOR__[path_syle.stroke_color]
line_style = self.__STROKE_STYLE__[path_syle.stroke_style]
line_width = float(path_syle.line_width.replace('pt', '')) / 3 # Fixme: pt ???
# pen = QPen(
# color,
# line_width,
# line_style,
# )
# self._painter.setPen(pen)
##############################################
def paint_CircleItem(self, item):
center = self._cast_position(item.position)
radius = 5
# rectangle = QRectF(
# center - QPointF(1, 1)*radius,
# QSizeF(1, 1)*2*radius,
# )
# self._painter.setPen(QPen(
# QColor('black'),
# 1,
# Qt::SolidLine,
# )
# self._painter.drawArc(rectangle, 0, 360)
self._painter.drawEllipse(center, radius, radius)
##############################################
def paint_SegmentItem(self, item):
self._set_pen(item)
vertices = [self._cast_position(position) for position in item.positions]
self._painter.drawLine(*vertices)
##############################################
def paint_CubicBezierItem(self, item):
self._set_pen(item)
vertices = [self._cast_position(position) for position in item.positions]
path = QPainterPath()
path.moveTo(vertices[0])
path.cubicTo(*vertices[1:])
self._painter.drawPath(path)
####################################################################################################
class QtQuickPaintedSceneItem(QQuickPaintedItem, QtPainter):
_logger = _module_logger.getChild('QtQuickPaintedSceneItem')
##############################################
def __init__(self, parent=None):
QQuickPaintedItem.__init__(self, parent)
QtPainter.__init__(self)
self.setAntialiasing(True)
# self.setRenderTarget(QQuickPaintedItem.Image) # high quality antialiasing
self.setRenderTarget(QQuickPaintedItem.FramebufferObject) # use OpenGL
##############################################
scene_changed = pyqtSignal()
@pyqtProperty(QtScene, notify=scene_changed)
def scene(self):
return self._scene
@scene.setter
def scene(self, scene):
print('set scene', scene)
self._scene = scene
self.update()
####################################################################################################
# qmlRegisterType(QtQuickPaintedSceneItem, 'Patro', 1, 0, 'PaintedSceneItem')