/
fr_three_arrows_widget.py
1305 lines (1113 loc) · 54 KB
/
fr_three_arrows_widget.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
#
# ***************************************************************************
# * *
# * This file is a part of the Open Source Design456 Workbench - FreeCAD. *
# * *
# * Copyright (C) 2022 *
# * *
# * *
# * This library is free software; you can redistribute it and/or *
# * modify it under the terms of the GNU Lesser General Public *
# * License as published by the Free Software Foundation; either *
# * version 2 of the License, or (at your option) any later version. *
# * *
# * This library 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 *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with this library; if not, If not, see *
# * <http://www.gnu.org/licenses/>. *
# * *
# * Author : Mariwan Jalal mariwan.jalal@gmail.com *
# **************************************************************************
import os
import sys
import FreeCAD as App
import FreeCADGui as Gui
import pivy.coin as coin
from ThreeDWidgets import fr_widget
from ThreeDWidgets import constant
from typing import List
import FACE_D as faced
from dataclasses import dataclass
from ThreeDWidgets.fr_draw import draw_2Darrow
from ThreeDWidgets import fr_label_draw
from ThreeDWidgets.constant import FR_EVENTS
from ThreeDWidgets.constant import FR_COLOR
from ThreeDWidgets.fr_draw1 import draw_RotationPad
import math
__updated__ = '2022-10-16 07:44:07'
"""
Example how to use this widget.
# show the window and it's widgets.
import ThreeDWidgets.fr_one_arrow_widget as wd
import ThreeDWidgets.fr_coinwindow as wnn
from ThreeDWidgets.constant import FR_COLOR
class test:
global runOunce
def __init__(self):
self.runOunce = None
def callback_move(self,userData : wd.userDataObject = None):
PadObj = userData.PadObj # Arrow object
click=App.Vector(PadObj.w_parent.w_lastEventXYZ.Coin_x,
PadObj.w_parent.w_lastEventXYZ.Coin_y,
PadObj.w_parent.w_lastEventXYZ.Coin_z)
if self.runOunce is None:
self.runOunce = click.sub(PadObj.w_vector[0])
PadObj.w_vector[0]=click.sub(self.runOunce)
PadObj.redraw()
print(self.runOunce,"self.runOunce")
def runme(self):
mywin = wnn.Fr_CoinWindow()
vectors=[App.Vector(0.0, 0.0, 0.0), App.Vector(0.0, 0.0, 0.0)]
rot=[0,0,0,0]
root=wd.Fr_OneArrow_Widget(vectors, "X-Axis")
root.w_move_callback_=self.callback_move
root.enableDisc()
mywin.addWidget(root)
mywin.show()
a=test()
a.runme()
a=test()
OR another example :
import ThreeDWidgets.fr_one_arrow_widget as wd
import ThreeDWidgets.fr_coinwindow as wnn
from ThreeDWidgets.constant import FR_COLOR
mywin = wnn.Fr_CoinWindow()
v1=[App.Vector(0.0, 0.0, 0.0), App.Vector(0.0, 0.0, 0.0)]
v2=[App.Vector(20,0,0), App.Vector(0.0, 0.0, 0.0)]
r1=wd.Fr_OneArrow_Widget(v1, "X-Axis")
r1.enableDisc()
r2=wd.Fr_OneArrow_Widget(v2, "y-Axis")
r1.enableDisc()
r2.enableDisc()
mywin.addWidget(r1)
mywin.addWidget(r2)
mywin.show()
"""
@dataclass
class userDataObject:
__slots__ = ['discObj', 'events', 'callerObject', 'Axis_cb',
'Disc_cb', 'axisType']
def __init__(self):
self.discObj = None # Class/Tool uses
self.events = None # events - save handle events here
self.callerObject = None #
self.Axis_cb = False # Disallow running callback - Arrows
self.Disc_cb = False # Disallow running callback - discs.
self.axisType = None
# *******************************CALLBACKS - DEMO *****************************
def xAxis_cb(userData: userDataObject = None):
"""
This function executes when the XAxis
event callback.
"""
# Subclass this and impalement the callback or
# just change the callback function
print("dummy XAxis -3Arrows callback")
def xDisc_cb(userData: userDataObject = None):
"""
This function executes when the rotary disc
angel changed event callback.
"""
# Subclass this and impalement the callback or
# just change the callback function
print("dummy Xangle changed callback", userData.discObj.w_discAngle)
def yAxis_cb(userData: userDataObject = None):
"""
This function executes when the XAxis
event callback.
"""
# Subclass this and impalement the callback or
# just change the callback function
print("dummy YAxis -3Arrows callback")
def yDisc_cb(userData: userDataObject = None):
"""
This function executes when the rotary disc
angel changed event callback.
"""
# Subclass this and impalement the callback or
# just change the callback function
print("dummy Yangle changed callback", userData.discObj.w_discAngle)
def zAxis_cb(userData: userDataObject = None):
"""
This function executes when the XAxis
event callback.
"""
# Subclass this and impalement the callback or
# just change the callback function
print("dummy ZAxis -3Arrows callback")
def zDisc_cb(userData: userDataObject = None):
"""
This function executes when the rotary disc
angel changed event callback.
"""
# Subclass this and impalement the callback or
# just change the callback function
print("dummy Zangle changed callback", userData.discObj.w_discAngle)
# *************************
# Whole widget callback - Might not be used
# *************************************************************
def callback(userData: userDataObject = None):
"""
This function executes when lblCallbak (double click callback)
or general widget callback occurs
"""
# Subclass the widget and impalement the callback or just change the callback function
print("dummy callback")
# *************************************************************
class Fr_ThreeArrows_Widget(fr_widget.Fr_Widget):
"""
This class is for drawing a one Degrees disc
This will be used later to create 3D disc.
Or it can be used as a single rotate/move widget
"""
def __init__(self, vectors: List[App.Vector] = [],
label: str = [[]],
_lblColor=FR_COLOR.FR_WHITE,
_axisColor=[FR_COLOR.FR_RED,
FR_COLOR.FR_GREEN,
FR_COLOR.FR_BLUE],
# Whole widget rotation
_rotation: List[float] = [0.0, 0.0, 0.0, 0.0],
# Pre-rotation
_setupRotation: List[float] = [0.0, 0.0, 0.0],
_scale: List[float] = [3.0, 3.0, 3.0],
_type: int = 1,
_opacity: float = 0.0,
_distanceBetweenThem: float = 5.0):
super().__init__(vectors, label)
self.w_lbluserData = fr_widget.propertyValues() # Only for label
self.w_widgetType = constant.FR_WidgetType.FR_THREE_DISC
# General widget callback (mouse-button) function - External function
self.w_callback_ = callback
self.w_lbl_calback_ = callback # Label callback
self.w_KB_callback_ = callback # Keyboard
# Dummy callback Axis
self.w_xAxis_cb_ = xAxis_cb
self.w_yAxis_cb_ = yAxis_cb
self.w_zAxis_cb_ = zAxis_cb
# Dummy callback disc
self.w_discXAxis_cb_ = xDisc_cb
self.w_discYAxis_cb_ = yDisc_cb
self.w_discZAxis_cb_ = zDisc_cb
self.Opacity = _opacity
self.DrawingType = _type
# Use this to separate the arrows/lbl from the origin of the widget
self.distanceBetweenThem = _distanceBetweenThem
self.w_wdgsoSwitch = coin.SoSwitch()
self.w_XarrowSeparator = None
self.w_XdiscSeparator = None
self.w_YarrowSeparator = None
self.w_YdiscSeparator = None
self.w_ZarrowSeparator = None
self.w_ZdiscSeparator = None
self.w_color = _axisColor
self.w_rotaryDisc_color = _axisColor
self.w_selColor = [[i * 1.5 for i in self.w_color[0]],
[j * 1.5 for j in self.w_color[1]],
[k * 1.5 for k in self.w_color[2]]]
self.w_Scale = _scale
self.w_inactiveColor = [[i * 0.9 for i in self.w_color[0]],
[j * 0.9 for j in self.w_color[1]],
[k * 0.9 for k in self.w_color[2]]]
self.w_userData = userDataObject() # Keep info about the whole widget
self.w_userData.discObj = self
self.w_discAngle = [0.0, 0.0, 0.0] # Only disc rotation.
self.oldAngle = [0.0, 0.0, 0.0]
self.rotationDirection = [1, 1, 1] # +1 CCW , -1 ACCW
# This affect only the Widget label - nothing else
self.w_lbluserData.linewidth = self.w_lineWidth
self.w_lbluserData.vectors = self.w_vector
# We must make it higher or it will intersect the object and won't be visible
# TODO:Check if this works always?
self.w_lbluserData.vectors[0].x = self.w_lbluserData.vectors[0].x + \
self.distanceBetweenThem
self.w_lbluserData.vectors[0].y = self.w_lbluserData.vectors[0].y + \
self.distanceBetweenThem
self.w_lbluserData.vectors[0].z = self.w_lbluserData.vectors[0].z + \
self.distanceBetweenThem
self.w_lbluserData.labelcolor = _lblColor
# Use this to save rotation degree of the disk which is the whole widget angle.
self.w_WidgetDiskRotation = [0.0, 0.0, 0.0]
self.w_rotation = _rotation # Whole object Rotation
self.w_discEnabled = [False, False, False] # Affect redrawing, disabling/enabling
self.w_discHided = [False, False, False] # Affect redrawing, hiding
self.w_arrowEnabled = [True, True, True]
# Used to avoid running drag code while it is in drag mode
self.XreleaseDragAxis = -1
self.YreleaseDragAxis = -1
self.ZreleaseDragAxis = -1
# -1 no click, 0 mouse clicked, 1 mouse dragging
# Used to avoid running drag code while it is in drag mode
self.XreleaseDragDisc = -1
self.YreleaseDragDisc = -1
self.ZreleaseDragDisc = -1
self.run_Once = [False, False, False]
self.startVector = [0.0, 0.0, 0.0]
self.endVector = [0.0, 0.0, 0.0]
self.axisType = None
def handle(self, event):
"""
This function is responsible for taking events and processing
the actions required. If the object != targeted,
the function will skip the events. But if the widget was
targeted, it returns 1. Returning 1 means that the widget
processed the event and no other widgets needs to get the
event. Window object is responsible for distributing the events.
"""
if type(event) == int:
if event == FR_EVENTS.FR_NO_EVENT:
return 1 # we treat this event. Nothing to do
self.w_userData.events = event # Keep the event always here
# This is for the widgets label - Not the axes label - be aware.
clickwdglblNode = self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_widgetlblSoNodes)
# X Axis
XclickwdgdNode = [False, False]
YclickwdgdNode = [False, False]
ZclickwdgdNode = [False, False]
# In this widget, we have 2 coin drawings that we need to capture event for them
if self.w_discEnabled[0] is True or self.w_arrowEnabled[0] is True:
# ------------- X DISC / Axis -------------
if(self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_XarrowSeparator) is not None):
XclickwdgdNode[0] = True
self.enableOnlyMe("XA")
if self.w_discEnabled[0] is True:
if (self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_XdiscSeparator) is not None):
XclickwdgdNode[1] = True
self.enableOnlyMe("XD")
if self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_LEFT_DOUBLECLICK:
if (clickwdglblNode is not None):
# Double click event.
print("Double click detected")
# if not self.has_focus():
# self.take_focus()
self.do_lblcallback()
self.enable_all()
return 1
elif self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_LEFT_RELEASE:
# disc's part
# When the discs rotates, we don't accept
# the arrow dragging. Disk rotation has priority
self.XreleaseDragDisc = -1
self.XreleaseDragAxis = -1
self.enable_all()
# Release callback should be activated
if XclickwdgdNode[1] is True:
if self.XreleaseDragDisc == 1 or self.XreleaseDragDisc == 0:
self.do_callbacks(0) # Generic callback
return 1
# Axis's part
elif XclickwdgdNode[0] is True:
if self.XreleaseDragAxis == 1 or self.XreleaseDragAxis == 0:
self.do_callbacks(1)
return 1
else: # None of them -- remove the focus
self.remove_focus()
# Mouse first click and then mouse with movement is here
if self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_DRAG:
# X-DISC
if((XclickwdgdNode[1] is True) and (self.XreleaseDragDisc == -1)):
# This part will be active only once when for the first time user click on the coin drawing.
# Later DRAG should be used
self.XreleaseDragDisc = 0
self.XreleaseDragAxis = -1
self.take_focus()
return 1
# X-DISC
elif ((XclickwdgdNode[1] is True) and (self.XreleaseDragDisc == 0)):
self.XreleaseDragDisc = 1 # Rotation will continue it will be a drag always
self.XreleaseDragAxis = -1
self.take_focus()
self.cb_XdiscRotate()
self.do_callbacks(2) # disc callback
return 1
# X-DISC
elif self.XreleaseDragDisc == 1 and (XclickwdgdNode[1] is True):
# As far as we had DRAG before, we will continue run callback.
# This is because if the mouse is not exactly on the widget, it should still take the drag.
# Continue run the callback as far as it releaseDrag=1
self.cb_XdiscRotate()
self.do_callbacks(2) # We use the same callback,
return 1
if self.w_arrowEnabled[0] is True:
# X-Axis
if ((((clickwdglblNode is not None)
or (XclickwdgdNode[0] is True))
and (self.XreleaseDragAxis == -1))):
# This part will be active only once when for the first time user click on the coin drawing.
# Later DRAG should be used
self.XreleaseDragAxis = 0
self.XreleaseDragDisc = -1 # Not possible to have rotation while arrow is active
self.take_focus()
return 1
# X-Axis
elif ((
(XclickwdgdNode[0] is True) or (clickwdglblNode is not None))
and (self.XreleaseDragAxis == 0)):
self.XreleaseDragAxis = 1 # Drag will continue, it will be a drag always
self.XreleaseDragDisc = -1
self.take_focus()
self.do_callbacks(1)
return 1
# X-Axis
elif self.XreleaseDragAxis == 1:
# As far as we had DRAG before, we will continue run callback.
# This is because if the mouse is not exactly on the widget, it should still take the drag.
# Continue run the callback as far as it releaseDrag=1
self.do_callbacks(1) # We use the same callback,
return 1
if self.w_discEnabled[1] is True or self.w_arrowEnabled[1] is True:
# ------------- Y DISC / Axis -------------
if(self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_YarrowSeparator) is not None):
YclickwdgdNode[0] = True
self.enableOnlyMe("YA")
if self.w_discEnabled[1] is True:
if (self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_YdiscSeparator) is not None):
YclickwdgdNode[1] = True
self.enableOnlyMe("YD")
if self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_LEFT_DOUBLECLICK:
if (clickwdglblNode is not None):
# Double click event.
print("Double click detected")
# if not self.has_focus():
# self.take_focus()
self.do_lblcallback()
self.enable_all()
return 1
elif self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_LEFT_RELEASE:
# disc's part
# When the discs rotates, we don't accept
# the arrow dragging. Disk rotation has priority
self.YreleaseDragDisc = -1
self.YreleaseDragAxis = -1
self.enable_all()
# Release callback should be activated
if YclickwdgdNode[1] is True:
if self.YreleaseDragDisc == 1 or self.YreleaseDragDisc == 0:
self.do_callbacks(0) # Generic callback
return 1
# Axis's part
elif YclickwdgdNode[0] is True:
if self.YreleaseDragAxis == 1 or self.YreleaseDragAxis == 0:
self.do_callbacks(1)
return 1
else: # None of them -- remove the focus
self.remove_focus()
# Mouse first click and then mouse with movement is here
if self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_DRAG:
# Y-DISC
if((YclickwdgdNode[1] is True) and (self.YreleaseDragDisc == -1)):
# This part will be active only once when for the first time user click on the coin drawing.
# Later DRAG should be used
self.YreleaseDragDisc = 0
self.YreleaseDragAxis = -1
self.take_focus()
return 1
# Y-DISC
elif ((YclickwdgdNode[1] is True) and (self.YreleaseDragDisc == 0)):
self.YreleaseDragDisc = 1 # Rotation will continue it will be a drag always
self.YreleaseDragAxis = -1
self.take_focus()
self.cb_YdiscRotate()
self.do_callbacks(4) # disc callback
return 1
# Y-DISC
elif self.YreleaseDragDisc == 1 and (YclickwdgdNode[1] is True):
# As far as we had DRAG before, we will continue run callback.
# This is because if the mouse is not exactly on the widget, it should still take the drag.
# Continue run the callback as far as it releaseDrag=1
self.cb_YdiscRotate()
self.do_callbacks(4) # We use the same callback,
return 1
if self.w_arrowEnabled[1] is True:
# Y-Axis
if ((((clickwdglblNode is not None)
or (YclickwdgdNode[0] is True))
and (self.YreleaseDragAxis == -1))):
# This part will be active only once when for the first time user click on the coin drawing.
# Later DRAG should be used
self.YreleaseDragAxis = 0
self.YreleaseDragDisc = -1 # Not possible to have rotation while arrow is active
self.take_focus()
return 1
# Y-Axis
elif ((
(YclickwdgdNode[0] is True) or (clickwdglblNode is not None))
and (self.YreleaseDragAxis == 0)):
self.YreleaseDragAxis = 1 # Drag will continue, it will be a drag always
self.YreleaseDragDisc = -1
self.take_focus()
self.do_callbacks(3)
return 1
# Y-Axis
elif self.YreleaseDragAxis == 1:
# As far as we had DRAG before, we will continue run callback.
# This is because if the mouse is not exactly on the widget, it should still take the drag.
# Continue run the callback as far as it releaseDrag=1
self.do_callbacks(3) # We use the same callback,
return 1
if self.w_discEnabled[2] is True or self.w_arrowEnabled[2] is True:
# ------------- Z DISC / Axis -------------
if(self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_ZarrowSeparator) is not None):
ZclickwdgdNode[0] = True
self.enableOnlyMe("ZA")
if self.w_discEnabled[2] is True:
if (self.w_parent.objectMouseClick_Coin3d(self.w_parent.w_lastEventXYZ.pos,
self.w_pick_radius, self.w_ZdiscSeparator) is not None):
ZclickwdgdNode[1] = True
self.enableOnlyMe("ZD")
if self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_LEFT_DOUBLECLICK:
if (clickwdglblNode is not None):
# Double click event.
print("Double click detected")
# if not self.has_focus():
# self.take_focus()
self.do_lblcallback()
self.enable_all()
return 1
elif self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_LEFT_RELEASE:
# disc's part
# When the discs rotates, we don't accept
# the arrow dragging. Disk rotation has priority
self.ZreleaseDragDisc = -1
self.ZreleaseDragAxis = -1
self.enable_all()
# Release callback should be activated
if ZclickwdgdNode[1] is True:
if self.ZreleaseDragDisc == 1 or self.ZreleaseDragDisc == 0:
self.do_callbacks(0) # Generic callback
return 1
# Axis's part
elif ZclickwdgdNode[0] is True:
if self.ZreleaseDragAxis == 1 or self.ZreleaseDragAxis == 0:
self.do_callbacks(1)
return 1
else: # None of them -- remove the focus
self.remove_focus()
# Mouse first click and then mouse with movement is here
if self.w_parent.w_lastEvent == FR_EVENTS.FR_MOUSE_DRAG:
# Z-DISC
if((ZclickwdgdNode[1] is True) and (self.ZreleaseDragDisc == -1)):
# This part will be active only once when for the first time user click on the coin drawing.
# Later DRAG should be used
self.ZreleaseDragDisc = 0
self.ZreleaseDragAxis = -1
self.take_focus()
return 1
# Z-DISC
elif ((ZclickwdgdNode[1] is True) and (self.ZreleaseDragDisc == 0)):
self.ZreleaseDragDisc = 1 # Rotation will continue it will be a drag always
self.ZreleaseDragAxis = -1
self.take_focus()
self.cb_ZdiscRotate()
self.do_callbacks(6) # disc callback
return 1
# Z-DISC
elif self.ZreleaseDragDisc == 1 and (ZclickwdgdNode[1] is True):
# As far as we had DRAG before, we will continue run callback.
# This is because if the mouse is not exactly on the widget, it should still take the drag.
# Continue run the callback as far as it releaseDrag=1
self.cb_ZdiscRotate()
self.do_callbacks(6) # We use the same callback,
return 1
if self.w_arrowEnabled[2] is True:
# Z-Axis
if ((((clickwdglblNode is not None)
or (ZclickwdgdNode[0] is True))
and (self.ZreleaseDragAxis == -1))):
# This part will be active only once when for the first time user click on the coin drawing.
# Later DRAG should be used
self.ZreleaseDragAxis = 0
self.ZreleaseDragDisc = -1 # Not possible to have rotation while arrow is active
self.take_focus()
return 1
# Z-Axis
elif ((
(ZclickwdgdNode[0] is True) or (clickwdglblNode is not None))
and (self.ZreleaseDragAxis == 0)):
self.ZreleaseDragAxis = 1 # Drag will continue, it will be a drag always
self.ZreleaseDragDisc = -1
self.take_focus()
self.do_callbacks(5)
return 1
# Z-Axis
elif self.ZreleaseDragAxis == 1:
# As far as we had DRAG before, we will continue run callback.
# This is because if the mouse is not exactly on the widget, it should still take the drag.
# Continue run the callback as far as it releaseDrag=1
self.do_callbacks(5) # We use the same callback,
return 1
# Don't care events, return the event to other widgets
return 0 # We couldn't use the event .. so return 0
def draw(self):
"""
Main draw function. It is responsible for creating the node,
and draw the disc on the screen. It creates a node for each
element and for each disc.
"""
try:
if (len(self.w_vector) < 2):
raise ValueError('Must be 2 vector at least')
usedColor = self.w_color
if self.is_active() and self.has_focus():
usedColor = self.w_selColor
elif self.is_active() and (self.has_focus() != 1):
pass # usedColor = self.w_color we did that already ..just for reference
elif self.is_active() != 1:
usedColor = self.w_inactiveColor
# TODO: FIXME:
XpreRotVal = [0.0, 90.0, 0.0] # pre-Rotation
YpreRotVal = [0.0, 90.0, 90.0] # pre-Rotation
ZpreRotVal = [0.0, 0.0, 0.0]
if not self.is_visible():
return
self.w_XarrowSeparator = draw_2Darrow(App.Vector(self.w_vector[0].x + self.distanceBetweenThem, self.w_vector[0].y, self.w_vector[0].z),
# default FR_COLOR.FR_RED
usedColor[0], self.w_Scale, self.DrawingType, self.Opacity, XpreRotVal)
self.w_YarrowSeparator = draw_2Darrow(App.Vector(self.w_vector[0].x, self.w_vector[0].y + self.distanceBetweenThem, self.w_vector[0].z),
# default FR_COLOR.FR_GREEN
usedColor[1], self.w_Scale, self.DrawingType, self.Opacity, YpreRotVal)
self.w_ZarrowSeparator = draw_2Darrow(App.Vector(self.w_vector[0].x, self.w_vector[0].y, self.w_vector[0].z + self.distanceBetweenThem),
# default FR_COLOR.FR_BLUE
usedColor[2], self.w_Scale, self.DrawingType, self.Opacity, ZpreRotVal)
XpreRotValdisc = [self.w_discAngle[0], 0.0, 90.0]
YpreRotValdisc = [self.w_discAngle[1], 0.0, 0.0]
ZpreRotValdisc = [0.0, 270.0, (-1*self.w_discAngle[2])]
self.w_XdiscSeparator = draw_RotationPad(self.w_vector[0],
self.w_rotaryDisc_color[0],
self.w_Scale, self.Opacity,
XpreRotValdisc) # RED
self.w_YdiscSeparator = draw_RotationPad(self.w_vector[0],
self.w_rotaryDisc_color[1],
self.w_Scale, self.Opacity,
YpreRotValdisc) # GREEN
self.w_ZdiscSeparator = draw_RotationPad(self.w_vector[0],
self.w_rotaryDisc_color[2],
self.w_Scale, self.Opacity,
ZpreRotValdisc) # BLUE
# self.w_userData.RotaryDisc = [self.w_XdiscSeparator,
# self.w_YdiscSeparator,
# self.w_ZdiscSeparator]
transformRot = coin.SoTransform()
separtorAll = coin.SoSeparator()
tR = coin.SbVec3f()
tR.setValue(
self.w_rotation[0], self.w_rotation[1], self.w_rotation[2])
transformRot.rotation.setValue(
tR, math.radians(self.w_rotation[3]))
separtorAll.addChild(transformRot)
if self.w_arrowEnabled[0] is True:
separtorAll.addChild(self.w_XarrowSeparator)
if self.w_arrowEnabled[1] is True:
separtorAll.addChild(self.w_YarrowSeparator)
if self.w_arrowEnabled[2] is True:
separtorAll.addChild(self.w_ZarrowSeparator)
if self.w_discEnabled[0] is True:
if self.w_discHided[0] is False:
separtorAll.addChild(self.w_XdiscSeparator)
if self.w_discEnabled[1] is True:
if self.w_discHided[1] is False:
separtorAll.addChild(self.w_YdiscSeparator)
if self.w_discEnabled[2] is True:
if self.w_discHided[2] is False:
separtorAll.addChild(self.w_ZdiscSeparator)
self.draw_label()
self.saveSoNodesToWidget(separtorAll)
# add SoSeparator to the switch
# We can put them in a tuple but it is better not doing so
self.addSoNodeToSoSwitch(self.w_widgetSoNodes)
self.addSoNodeToSoSwitch(self.w_widgetlblSoNodes)
except Exception as err:
App.Console.PrintError("'draw Three Arrows Widget' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def draw_label(self):
"""[Draw 3D text in the scenegraph]
Returns:
[type]: [Use w_lbluserData variable to setup the label shape, size, font ..etc
w_lebluserData is of type propertyValues
@dataclass
class propertyValues:
'''
Property-holder class for drawing labels
'''
__slots__ = ['vectors', 'linewidth', 'fontName', 'fontsize',
'labelcolor', 'alignment', 'rotation', setupRotation,
'scale']
vectors: VECTOR # List[App.Vector] two vectors must be provided
linewidth: int
fontName: str
fontsize: int
labelcolor: tuple
alignment: int # This will not be used .. not good
rotation: tuple # three float value and an angle
setupRotation: Three angle values for the xyz axis
scale: tuple # Three float numbers for scaling
]
"""
try:
self.w_lbluserData.SetupRotation = App.Vector(0, 0, 0)
#self.w_lbluserData.SetupRotation[1] = App.Vector(0, 0, 90)
#self.w_lbluserData.SetupRotation[2] = App.Vector(0, -90, 180)
#self.w_lbluserData.labelcolor = self.w_lblcolor
lbl = fr_label_draw.draw_label(self.w_label, self.w_lbluserData)
self.saveSoNodeslblToWidget(lbl)
except Exception as err:
App.Console.PrintError("'draw label Three Arrows Widget' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def move(self, newVecPos=App.Vector(0, 0, 0)):
"""[Move the widget to a new location referenced by the
left-top corner of the object. Or the start of the disc
if it is an disc.]
Args:
newVecPos ([App.Vector], optional): [Move the label to a new position]. Defaults to App.Vector(0.0, 0.0, 0.0).
"""
self.w_lbluserData.vectors = [newVecPos, ]
def show(self):
"""[This function will show the widget. But it doesn't draw it. ]
"""
self.w_visible = 1
self.w_wdgsoSwitch.whichChild = coin.SO_SWITCH_ALL # Show all children
def take_focus(self):
"""
Set focus to the widget. Which should redraw it also.
"""
if self.w_hasFocus == 1:
return # nothing to do here
self.w_hasFocus = 1
self.redraw()
def activate(self):
"""[Activate the widget so it can take events]
"""
if self.w_active:
return # nothing to do
self.w_active = 1
self.redraw()
def deactivate(self):
"""
Deactivate the widget. which causes that no handle comes to the widget
"""
self.w_active = 0
def ChangeScale(self, newScale=[1, 1, 1]):
"""[Scale the whole widget default is no scaling ]
Args:
newScale (list, optional): [New scale to apply to the widget]. Defaults to [1.0,1.0,1.0].
"""
self.w_Scale = newScale
def __del__(self):
"""
Class Destructor.
This will remove the widget totally.
"""
self.hide()
try:
if self.w_parent is not None:
# Parent should be the windows widget.
self.w_parent.removeWidget(self)
if self.w_parent is not None:
self.w_parent.removeSoSwitchFromSceneGraph(self.w_wdgsoSwitch)
self.removeSoNodeFromSoSwitch()
self.removeSoNodes()
self.removeSoSwitch()
except Exception as err:
App.Console.PrintError("'del Fr_disc_Widget' Failed. "
"{err}\n".format(err=str(err)))
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, fname, exc_tb.tb_lineno)
def hide(self):
"""[Hide the widget - but the widget is not destroyed]
Returns:
[type]: [description]
"""
if self.w_visible == 0:
return # nothing to do
self.w_visible = 0
self.w_wdgsoSwitch.whichChild = coin.SO_SWITCH_NONE # hide all children
self.redraw()
def remove_focus(self):
"""
Remove the focus from the widget.
This happens by clicking anything
else than the widget itself
"""
if self.w_hasFocus == 0:
return # nothing to do
else:
self.w_hasFocus = 0
self.redraw()
def resize(self, _scale: tuple = [1.0, 1.0, 1.0]):
"""Resize the widget by using the new vectors"""
self.w_Scale = _scale
self.redraw()
def size(self, _scale: tuple = [1.0, 1.0, 1.0]):
"""[Scale the widget for the three directions]
Args:
_scale (tuple, Three float values): [Scale the widget using three float values for x,y,z axis].
Defaults to [1.0, 1.0, 1.0].
"""
self.resize(_scale)
def label(self, newlabel):
self.w_label = newlabel
# Keep in mind you must run lblRedraw
def label_font(self, name="sans"):
"""[Change Label Font]
Args:
name (str, optional): [Change label font]. Defaults to "sans".
"""
self.w_lbluserData.fontName = name
# Keep in mind you must run lblRedraw
def label_scale(self, newsize: tuple = [1.0, 1.0, 1.0]):
"""[Scale the font using three float values. This is not the font size.
You must redraw the label to get this scaling done.
]
Args:
newsize (int, optional): [Change font label ]. Defaults to 1.
"""
self.w_lbluserData.scale = newsize
# Keep in mind you must run lblRedraw
def label_fontsize(self, newsize=1):
"""[Change label font size ]
Args:
newsize (int, optional): [Change fontsize of the label ]. Defaults to 1.
"""
self.w_lbluserData.fontsize = newsize
# Must be App.Vector
def label_move(self, newPos=App.Vector(0.0, 0.0, 0.0)):
"""[Move location of the label]
Args:
newPos ([App.Vector], optional): [Change placement of the label]. Defaults to App.Vector(0.0, 0.0, 0.0).
"""
self.w_lbluserData.vectors = [newPos, ]
def setRotationAngle(self, axis_and_angle):
'''
Set the rotation axis and the angle. This is for the whole widget.
Axis is coin.SbVec3f((x,y,z)
angle=float number
'''
self.w_rotation = axis_and_angle
def enableDisc(self, disctype:str=""):
"""[Enable rotation disc. You need to redraw the widget]
"""
if disctype == "X":
self.w_discEnabled[0] = True
elif disctype == "Y":
self.w_discEnabled[1] = True
elif disctype == "Z":
self.w_discEnabled[2] = True
def setDistanceBetweenThem(self, newvalue):
"""[Change distance between the arrows to the origin of the widget. ]
Args:
newvalue ([float]): [new distance in mm]
"""
self.distanceBetweenThem = newvalue
def disableDisc(self, disctype:str=""):
"""[Disable rotation disc. You need to redraw the widget]
Args:
disctype (str, axis name): [Disc name to disable]. Defaults to "".
"""
if disctype == "X":
self.w_discEnabled[0] = False
elif disctype == "Y":
self.w_discEnabled[1] = False
elif disctype == "Z":
self.w_discEnabled[2] = False
# def calculateMouseAngle(self, val1, val2):
# """[Calculate Angle of two coordinates ( xy, yz or xz).
# This function is useful to calculate mouse position
# in Angle depending on the mouse position.
# ]
# Args:
# val1 ([Horizontal coordinate]): [x, y]
# val2 ([Vertical coordinate ]): [y or z]
# Returns:
# [int]: [Calculated value in degrees]
# """
# if(val2 == 0):
# return None # divide by zero
# result = 0
# if (val1 > 0 and val2 > 0):
# result = int(math.degrees(math.atan2(float(val1),
# float(val2))))
# if (val1 < 0 and val2 > 0):
# result = int(math.degrees(math.atan2(float(val1),
# float(val2))))+360
# if (val1 > 0 and val2 < 0):
# result = int(math.degrees(math.atan2(float(val1),
# float(val2))))
# if (val1 < 0 and val2 < 0):
# result = int(math.degrees(math.atan2(float(val1),
# float(val2))))+360
# return result
def cb_XdiscRotate(self, userData: userDataObject = None):
"""