/
gui_snaps.py
586 lines (411 loc) · 21.6 KB
/
gui_snaps.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# ***************************************************************************
# * (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
# * (c) 2009, 2010 Ken Cline <cline@frii.com> *
# * (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# * *
# * This file is part of the FreeCAD CAx development system. *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * FreeCAD 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with FreeCAD; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""Provides GUI tools to activate the different snapping methods."""
## @package gui_snaps
# \ingroup draftguitools
# \brief Provides GUI tools to activate the different snapping methods.
## \addtogroup draftguitools
# @{
import PySide.QtGui as QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCADGui as Gui
import draftguitools.gui_base as gui_base
from draftutils.translate import translate
# UTILITIES -----------------------------------------------------------------
def get_snap_statusbar_widget():
"""Return snap statusbar button."""
mw = Gui.getMainWindow()
if mw:
sb = mw.statusBar()
if sb:
return sb.findChild(QtGui.QToolBar,"draft_snap_widget")
return None
def sync_snap_toolbar_button(button, status):
"""Set snap toolbar button to given state."""
snap_toolbar = Gui.Snapper.get_snap_toolbar()
if not snap_toolbar:
return
for a in snap_toolbar.actions():
if a.objectName() == button:
if button == "Draft_Snap_Lock_Button":
# for lock button
snap_toolbar.actions()[0].setChecked(status)
for a in snap_toolbar.actions()[1:]:
if a.objectName()[:10] == "Draft_Snap":
a.setEnabled(status)
else:
# for every other button
a.setChecked(status)
if a.isChecked():
a.setToolTip(a.toolTip().replace("OFF","ON"))
else:
a.setToolTip(a.toolTip().replace("ON","OFF"))
def sync_snap_statusbar_button(button, status):
"""Set snap statusbar button to given state."""
ssw = get_snap_statusbar_widget()
if not ssw:
return
for child in ssw.children():
if child.objectName() == "Snap_Statusbutton":
ssb = child
actions = []
for a in ssb.menu().actions() + ssw.children()[-6:]:
actions.append(a)
if button == "Draft_Snap_Lock_Statusbutton":
ssb.setChecked(status)
for a in actions[1:]:
if a.objectName()[:10] == "Draft_Snap":
a.setEnabled(status)
else:
for a in actions:
if a.objectName() == button:
a.setChecked(status)
# SNAP GUI TOOLS ------------------------------------------------------------
class Draft_Snap_Lock(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Lock tool.
Activate or deactivate all snap methods at once.
"""
def __init__(self):
super(Draft_Snap_Lock, self).__init__(name=translate("draft","Main toggle snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Lock',
'Accel': "Shift+S",
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Lock", "Main snapping toggle On/Off"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Lock", "Activates or deactivates all snap methods at once.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Lock, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Lock')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Lock"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Lock"+"_Statusbutton", status)
Gui.addCommand('Draft_Snap_Lock', Draft_Snap_Lock())
class Draft_Snap_Midpoint(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Midpoint tool.
Set snapping to the midpoint of an edge.
"""
def __init__(self):
super(Draft_Snap_Midpoint, self).__init__(name=translate("draft","Midpoint snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Midpoint',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Midpoint", "Midpoint"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Midpoint", "Set snapping to the midpoint of an edge.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Midpoint, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Midpoint')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Midpoint"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Midpoint_Statusbutton", status)
Gui.addCommand('Draft_Snap_Midpoint', Draft_Snap_Midpoint())
class Draft_Snap_Perpendicular(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Perpendicular tool.
Set snapping to a direction that is perpendicular to an edge.
"""
def __init__(self):
super(Draft_Snap_Perpendicular, self).__init__(name=translate("draft","Perpendicular snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Perpendicular',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Perpendicular", "Perpendicular"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Perpendicular", "Set snapping to a direction that is perpendicular to an edge.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Perpendicular, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Perpendicular')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Perpendicular"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Perpendicular_Statusbutton", status)
Gui.addCommand('Draft_Snap_Perpendicular', Draft_Snap_Perpendicular())
class Draft_Snap_Grid(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Grid tool.
Set snapping to the intersection of grid lines.
"""
def __init__(self):
super(Draft_Snap_Grid, self).__init__(name=translate("draft","Grid snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Grid',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Grid", "Grid"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Grid", "Set snapping to the intersection of grid lines.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Grid, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Grid')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Grid"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Grid_Statusbutton", status)
Gui.addCommand('Draft_Snap_Grid', Draft_Snap_Grid())
class Draft_Snap_Intersection(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Intersection tool.
Set snapping to the intersection of edges.
"""
def __init__(self):
super(Draft_Snap_Intersection, self).__init__(name=translate("draft","Intersection snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Intersection',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Intersection","Intersection"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Intersection","Set snapping to the intersection of edges.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Intersection, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Intersection')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Intersection"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Intersection_Statusbutton", status)
Gui.addCommand('Draft_Snap_Intersection', Draft_Snap_Intersection())
class Draft_Snap_Parallel(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Parallel tool.
Set snapping to a direction that is parallel to an edge.
"""
def __init__(self):
super(Draft_Snap_Parallel, self).__init__(name=translate("draft","Parallel snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Parallel',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Parallel", "Parallel"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Parallel", "Set snapping to a direction that is parallel to an edge.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Parallel, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Parallel')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Parallel"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Parallel_Statusbutton", status)
Gui.addCommand('Draft_Snap_Parallel', Draft_Snap_Parallel())
class Draft_Snap_Endpoint(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Endpoint tool.
Set snapping to endpoints of an edge.
"""
def __init__(self):
super(Draft_Snap_Endpoint, self).__init__(name=translate("draft","Endpoint snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Endpoint',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Endpoint", "Endpoint"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Endpoint", "Set snapping to endpoints of an edge.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Endpoint, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Endpoint')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Endpoint"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Endpoint_Statusbutton", status)
Gui.addCommand('Draft_Snap_Endpoint', Draft_Snap_Endpoint())
class Draft_Snap_Angle(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Angle tool.
Set snapping to points in a circular arc located at multiples
of 30 and 45 degree angles.
"""
def __init__(self):
super(Draft_Snap_Angle, self).__init__(name=translate("draft","Angle snap (30 and 45 degrees)"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Angle',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Angle", "Angle"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Angle", "Set snapping to points in a circular arc located at multiples of 30 and 45 degree angles.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Angle, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Angle')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Angle"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Angle_Statusbutton", status)
Gui.addCommand('Draft_Snap_Angle', Draft_Snap_Angle())
class Draft_Snap_Center(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Center tool.
Set snapping to the center of a circular arc.
"""
def __init__(self):
super(Draft_Snap_Center, self).__init__(name=translate("draft","Arc center snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Center',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Center", "Center"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Center", "Set snapping to the center of a circular arc.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Center, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Center')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Center"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Center_Statusbutton", status)
Gui.addCommand('Draft_Snap_Center', Draft_Snap_Center())
class Draft_Snap_Extension(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Extension tool.
Set snapping to the extension of an edge.
"""
def __init__(self):
super(Draft_Snap_Extension, self).__init__(name=translate("draft","Edge extension snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Extension',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Extension", "Extension"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Extension", "Set snapping to the extension of an edge.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Extension, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Extension')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Extension"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Extension_Statusbutton", status)
Gui.addCommand('Draft_Snap_Extension', Draft_Snap_Extension())
class Draft_Snap_Near(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Near tool.
Set snapping to the nearest point of an edge.
"""
def __init__(self):
super(Draft_Snap_Near, self).__init__(name=translate("draft","Near snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Near',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Near", "Nearest"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Near", "Set snapping to the nearest point of an edge.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Near, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Near')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Near"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Near_Statusbutton", status)
Gui.addCommand('Draft_Snap_Near', Draft_Snap_Near())
class Draft_Snap_Ortho(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Ortho tool.
Set snapping to a direction that is a multiple of 45 degrees
from a point.
"""
def __init__(self):
super(Draft_Snap_Ortho, self).__init__(name=translate("draft","Orthogonal snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Ortho',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Ortho", "Orthogonal"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Ortho", "Set snapping to a direction that is a multiple of 45 degrees from a point.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Ortho, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Ortho')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Ortho"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Ortho"+"_Statusbutton", status)
Gui.addCommand('Draft_Snap_Ortho', Draft_Snap_Ortho())
class Draft_Snap_Special(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Special tool.
Set snapping to the special points defined inside an object.
"""
def __init__(self):
super(Draft_Snap_Special, self).__init__(name=translate("draft","Special point snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Special',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Special", "Special"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Special", "Set snapping to the special points defined inside an object.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Special, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Special')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Special"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Special_Statusbutton", status)
Gui.addCommand('Draft_Snap_Special', Draft_Snap_Special())
class Draft_Snap_Dimensions(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_Dimensions tool.
Show temporary linear dimensions when editing an object
and using other snapping methods.
"""
def __init__(self):
super(Draft_Snap_Dimensions, self).__init__(name=translate("draft","Dimension display"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_Dimensions',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_Dimensions", "Show dimensions"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_Dimensions", "Show temporary linear dimensions when editing an object and using other snapping methods.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_Dimensions, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('Dimensions')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_Dimensions"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_Dimensions"+"_Statusbutton", status)
Gui.addCommand('Draft_Snap_Dimensions', Draft_Snap_Dimensions())
class Draft_Snap_WorkingPlane(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_Snap_WorkingPlane tool.
Restricts snapping to a point in the current working plane.
If you select a point outside the working plane, for example,
by using other snapping methods, it will snap to that point's
projection in the current working plane.
"""
def __init__(self):
super(Draft_Snap_WorkingPlane, self).__init__(name=translate("draft","Working plane snap"))
def GetResources(self):
"""Set icon, menu and tooltip."""
return {'Pixmap': 'Snap_WorkingPlane',
'MenuText': QT_TRANSLATE_NOOP("Draft_Snap_WorkingPlane","Working plane"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_Snap_WorkingPlane","Restricts snapping to a point in the current working plane.\nIf you select a point outside the working plane, for example, by using other snapping methods,\nit will snap to that point's projection in the current working plane.")}
def Activated(self):
"""Execute when the command is called."""
super(Draft_Snap_WorkingPlane, self).Activated()
if hasattr(Gui, "Snapper"):
status = Gui.Snapper.toggle_snap('WorkingPlane')
# change interface consistently
sync_snap_toolbar_button("Draft_Snap_WorkingPlane"+"_Button", status)
sync_snap_statusbar_button("Draft_Snap_WorkingPlane_Statusbutton", status)
Gui.addCommand('Draft_Snap_WorkingPlane', Draft_Snap_WorkingPlane())
class ShowSnapBar(gui_base.GuiCommandSimplest):
"""GuiCommand for the Draft_ShowSnapBar tool.
Show the snap toolbar if it is hidden.
"""
def __init__(self):
super(ShowSnapBar, self).__init__(name=translate("draft","Show snap toolbar"))
def GetResources(self):
"""Set icon, menu and tooltip."""
_tip = ""
return {'Pixmap': 'Draft_Snap',
'MenuText': QT_TRANSLATE_NOOP("Draft_ShowSnapBar","Show snap toolbar"),
'ToolTip': QT_TRANSLATE_NOOP("Draft_ShowSnapBar","Show the snap toolbar if it is hidden.")}
def Activated(self):
"""Execute when the command is called."""
super(ShowSnapBar, self).Activated()
if hasattr(Gui, "Snapper"):
Gui.Snapper.show()
Gui.addCommand('Draft_ShowSnapBar', ShowSnapBar())
## @}