-
Notifications
You must be signed in to change notification settings - Fork 3
/
appTool.py
353 lines (274 loc) · 11.3 KB
/
appTool.py
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# ########################################################## ##
# FlatCAM: 2D Post-processing for Manufacturing #
# http://flatcam.org #
# Author: Juan Pablo Caram (c) #
# Date: 2/5/2014 #
# MIT Licence #
# ########################################################## ##
from PyQt6 import QtGui, QtWidgets, QtCore
from shapely import Polygon, LineString
import gettext
import appTranslation as fcTranslate
import builtins
fcTranslate.apply_language('strings')
if '_' not in builtins.__dict__:
_ = gettext.gettext
class AppTool(QtWidgets.QWidget):
pluginName = "FlatCAM Generic Tool"
def __init__(self, app, parent=None):
"""
:param app: The application this tool will run in.
:type app: appMain.App
:param parent: Qt Parent
:return: AppTool
"""
QtWidgets.QWidget.__init__(self, parent)
self.app = app
self.decimals = self.app.decimals
# self.setSizePolicy(QtWidgets.QSizePolicy.Policy.Maximum, QtWidgets.QSizePolicy.Policy.Maximum)
self.layout = QtWidgets.QVBoxLayout()
self.setLayout(self.layout)
self.menuAction = None
def install(self, icon=None, separator=None, shortcut=None, **kwargs):
before = None
# 'pos' is the menu where the Action has to be installed
# if no 'pos' kwarg is provided then by default our Action will be installed in the menu_plugins
# as it previously was
if 'pos' in kwargs:
pos = kwargs['pos']
else:
pos = self.app.ui.menu_plugins
# 'before' is the Action in the menu stated by 'pos' kwarg, before which we want our Action to be installed
# if 'before' kwarg is not provided, by default our Action will be added in the last place.
if 'before' in kwargs:
before = (kwargs['before'])
# create the new Action
self.menuAction = QtGui.QAction(self)
# if provided, add an icon to this Action
if icon is not None:
self.menuAction.setIcon(icon)
# set the text name of the Action, which will be displayed in the menu
if shortcut is None:
self.menuAction.setText(self.pluginName)
else:
self.menuAction.setText(self.pluginName + '\t%s' % shortcut)
# add a ToolTip to the new Action
# self.menuAction.setToolTip(self.toolTip) # currently not available
# insert the action in the position specified by 'before' and 'pos' kwargs
pos.insertAction(before, self.menuAction)
# if separator parameter is True add a Separator after the newly created Action
if separator is True:
pos.addSeparator()
self.menuAction.triggered.connect(lambda: self.run(toggle=True))
def run(self):
if self.app.plugin_tab_locked is True:
return
# Remove anything else in the appGUI
self.app.ui.plugin_scroll_area.takeWidget()
# Put ourselves in the appGUI
self.app.ui.plugin_scroll_area.setWidget(self)
# Switch notebook to tool page
self.app.ui.notebook.setCurrentWidget(self.app.ui.plugin_tab)
# Set the tool name as the widget object name
self.app.ui.plugin_scroll_area.widget().setObjectName(self.pluginName)
self.show()
def clear_ui(self, layout):
# for item in reversed(range(layout.count())):
# lay_item = layout.itemAt(item)
#
# # in case that the widget is None
# try:
# widget = lay_item.widget()
# widget.setParent(None)
# except AttributeError:
# pass
if layout is not None:
while layout.count():
item = layout.takeAt(0)
widget = item.widget()
if widget is not None:
widget.setParent(None)
else:
self.clear_ui(item.layout())
def draw_tool_selection_shape(self, old_coords, coords, **kwargs):
"""
:param old_coords: old coordinates
:param coords: new coordinates
:param kwargs:
:return:
"""
if 'shapes_storage' in kwargs:
s_storage = kwargs['shapes_storage']
else:
s_storage = self.app.tool_shapes
if 'color' in kwargs:
color = kwargs['color']
else:
color = self.app.options['global_sel_line']
if 'face_color' in kwargs:
face_color = kwargs['face_color']
else:
face_color = self.app.options['global_sel_fill']
if 'face_alpha' in kwargs:
face_alpha = kwargs['face_alpha']
else:
face_alpha = 0.3
x0, y0 = old_coords
x1, y1 = coords
pt1 = (x0, y0)
pt2 = (x1, y0)
pt3 = (x1, y1)
pt4 = (x0, y1)
sel_rect = Polygon([pt1, pt2, pt3, pt4])
# color_t = Color(face_color)
# color_t.alpha = face_alpha
color_t = face_color[:-2] + str(hex(int(face_alpha * 255)))[2:]
s_storage.add(sel_rect, color=color, face_color=color_t, update=True, layer=0, tolerance=None)
if self.app.use_3d_engine:
s_storage.redraw()
def draw_selection_shape_polygon(self, points, **kwargs):
"""
:param points: a list of points from which to create a Polygon
:param kwargs:
:return:
"""
if 'shapes_storage' in kwargs:
s_storage = kwargs['shapes_storage']
else:
s_storage = self.app.tool_shapes
if 'color' in kwargs:
color = kwargs['color']
else:
color = self.app.options['global_sel_line']
if 'face_color' in kwargs:
face_color = kwargs['face_color']
else:
face_color = self.app.options['global_sel_fill']
if 'face_alpha' in kwargs:
face_alpha = kwargs['face_alpha']
else:
face_alpha = 0.3
if len(points) < 3:
sel_rect = LineString(points)
else:
sel_rect = Polygon(points)
# color_t = Color(face_color)
# color_t.alpha = face_alpha
color_t = face_color[:-2] + str(hex(int(face_alpha * 255)))[2:]
s_storage.add(sel_rect, color=color, face_color=color_t, update=True, layer=0, tolerance=None)
if self.app.use_3d_engine:
s_storage.redraw()
def delete_tool_selection_shape(self, **kwargs):
"""
:param kwargs:
:return:
"""
if 'shapes_storage' in kwargs:
s_storage = kwargs['shapes_storage']
else:
s_storage = self.app.tool_shapes
s_storage.clear()
s_storage.redraw()
def draw_moving_selection_shape_poly(self, points, data, **kwargs):
"""
:param points:
:param data:
:param kwargs:
:return:
"""
if 'shapes_storage' in kwargs:
s_storage = kwargs['shapes_storage']
else:
s_storage = self.app.sel_shapes
if 'color' in kwargs:
color = kwargs['color']
else:
color = self.app.options['global_sel_line']
if 'face_color' in kwargs:
face_color = kwargs['face_color']
else:
face_color = self.app.options['global_sel_fill']
if 'face_alpha' in kwargs:
face_alpha = kwargs['face_alpha']
else:
face_alpha = 0.3
temp_points = [x for x in points]
try:
if data != temp_points[-1]:
temp_points.append(data)
except IndexError:
return
l_points = len(temp_points)
if l_points == 2:
geo = LineString(temp_points)
elif l_points > 2:
geo = Polygon(temp_points)
else:
return
color_t = face_color[:-2] + str(hex(int(face_alpha * 255)))[2:]
color_t_error = "#00000000"
if geo.is_valid and not geo.is_empty:
s_storage.add(geo, color=color, face_color=color_t, update=True, layer=0, tolerance=None)
elif not geo.is_valid:
s_storage.add(geo, color="red", face_color=color_t_error, update=True, layer=0, tolerance=None)
if self.app.use_3d_engine:
s_storage.redraw()
def delete_moving_selection_shape(self, **kwargs):
"""
:param kwargs:
:return:
"""
if 'shapes_storage' in kwargs:
s_storage = kwargs['shapes_storage']
else:
s_storage = self.app.sel_shapes
s_storage.clear()
s_storage.redraw()
def on_plugin_mouse_click_release(self, pos):
# this should be implemented in the descendants, the Plugin classes
pass
def on_plugin_mouse_move(self, pos):
# this should be implemented in the descendants, the Plugin classes
pass
def on_plugin_cleanup(self):
# this should be implemented in the descendants, the Plugin classes
pass
def confirmation_message(self, accepted, minval, maxval):
if accepted is False:
self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%.*f, %.*f]' % (_("Edited value is out of range"),
self.decimals,
minval,
self.decimals,
maxval), False)
else:
self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
def confirmation_message_int(self, accepted, minval, maxval):
if accepted is False:
self.app.inform[str, bool].emit('[WARNING_NOTCL] %s: [%d, %d]' %
(_("Edited value is out of range"), minval, maxval), False)
else:
self.app.inform[str, bool].emit('[success] %s' % _("Edited value is within limits."), False)
def sizeHint(self):
"""
I've overloaded this just in case I will need to make changes in the future to enforce dimensions
:return:
"""
default_hint_size = super(AppTool, self).sizeHint()
return QtCore.QSize(default_hint_size.width(), default_hint_size.height())
class AppToolEditor(AppTool):
def run(self):
super(AppToolEditor, self).run()
# TODO Hack, should find the root cause and fix
# for whatever reason the stylesheet for dark mode is lost at some point here, so we should reapply it for the
# QWidget
if self.app.options['global_theme'] not in ['default', 'light']:
super(AppTool, self).setStyleSheet(
'''
QWidget {
background-color: rgba(32.000, 33.000, 36.000, 1.000);
color: rgba(170.000, 170.000, 170.000, 1.000);
selection-background-color: rgba(138.000, 180.000, 247.000, 1.000);
selection-color: rgba(32.000, 33.000, 36.000, 1.000);
}
'''
)