-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLoadDesign_Ribbon.py
4520 lines (3859 loc) · 195 KB
/
LoadDesign_Ribbon.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
# *************************************************************************
# * *
# * Copyright (c) 2019-2024 Hakan Seven, Geolta, Paul Ebbers *
# * *
# * 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 3 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * 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 Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# *************************************************************************
import FreeCAD as App
import FreeCADGui as Gui
import os
from PySide.QtGui import QIcon, QPixmap, QAction
from PySide.QtWidgets import (
QListWidgetItem,
QTableWidgetItem,
QListWidget,
QTableWidget,
QToolBar,
QToolButton,
QComboBox,
QPushButton,
QMenu,
QWidget,
QLineEdit,
QSizePolicy,
QRadioButton,
)
from PySide.QtCore import Qt, SIGNAL, Signal, QObject, QThread, QSize
import sys
import json
from datetime import datetime
import shutil
import Standard_Functions_RIbbon as StandardFunctions
from Standard_Functions_RIbbon import CommandInfoCorrections
import Parameters_Ribbon
import Serialize_Ribbon
import webbrowser
import time
import math
# Get the resources
pathIcons = Parameters_Ribbon.ICON_LOCATION
pathStylSheets = Parameters_Ribbon.STYLESHEET_LOCATION
pathUI = Parameters_Ribbon.UI_LOCATION
pathBackup = Parameters_Ribbon.BACKUP_LOCATION
sys.path.append(pathIcons)
sys.path.append(pathStylSheets)
sys.path.append(pathUI)
sys.path.append(pathBackup)
# import graphical created Ui. (With QtDesigner or QtCreator)
import Design_ui as Design_ui
# Define the translation
translate = App.Qt.translate
# Get the main window of FreeCAD
mw = Gui.getMainWindow()
class LoadDialog(Design_ui.Ui_Form):
ReproAdress: str = ""
Restart = False
# Set the data file version. Triggeres an question if an update is needed
DataFileVersion = "1.0"
# Define list of the workbenches, toolbars and commands on class level
List_Workbenches = []
StringList_Toolbars = []
List_WorkBenchToolBarItems = []
List_Commands = []
# Create lists for the several list in the json file.
List_IgnoredToolbars = []
List_IconOnly_Toolbars = []
List_QuickAccessCommands = []
List_IgnoredWorkbenches = []
Dict_RibbonCommandPanel = {}
Dict_CustomToolbars = {}
Dict_DropDownButtons = {}
Dict_NewPanels = {}
ShowText_Small = False
ShowText_Medium = False
ShowText_Large = False
List_IgnoredToolbars_internal = []
# Create the lists for the deserialized icons
List_CommandIcons = []
List_WorkBenchIcons = []
# Create a tomporary list for newly added dropdown buttons
newDDBList = []
def __init__(self):
# Makes "self.on_CreateBOM_clicked" listen to the changed control values instead initial values
super(LoadDialog, self).__init__()
# Get the main window from FreeCAD
mw = Gui.getMainWindow()
# # this will create a Qt widget from our ui file
self.form = Gui.PySideUic.loadUi(os.path.join(pathUI, "Design.ui"))
# Get the address of the repository address
PackageXML = os.path.join(os.path.dirname(__file__), "package.xml")
self.ReproAdress = StandardFunctions.ReturnXML_Value(PackageXML, "url", "type", "repository")
# Make sure that the dialog stays on top
self.form.raise_()
self.form.setWindowFlags(Qt.WindowType.WindowStaysOnTopHint)
# self.form.setWindowFlags(Qt.WindowType.Tool)
# self.form.setWindowModality(Qt.WindowModality.WindowModal)
# Position the dialog in front of FreeCAD
centerPoint = mw.geometry().center()
Rectangle = self.form.frameGeometry()
Rectangle.moveCenter(centerPoint)
self.form.move(Rectangle.topLeft())
# Set the size of the window to the previous state
#
# Get the previous values
LayoutDialog_Height = Parameters_Ribbon.Settings.GetIntSetting("LayoutDialog_Height")
if LayoutDialog_Height == 0 or LayoutDialog_Height is None:
LayoutDialog_Height = 800
LayoutDialog_Width = Parameters_Ribbon.Settings.GetIntSetting("LayoutDialog_Width")
if LayoutDialog_Width == 0 or LayoutDialog_Height is None:
LayoutDialog_Width = 990
# set a fixed size to force the form in to shape
self.form.setFixedSize(LayoutDialog_Width, LayoutDialog_Height)
# Set the size policy to fixed
self.form.setSizePolicy(QSizePolicy.Policy.Fixed, QSizePolicy.Policy.Fixed)
# Set a minimum and maximum size
self.form.setMinimumSize(600, 600)
self.form.setMaximumSize(120000, 120000)
# change the sizepolicy to preferred, to allow stretching
self.form.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
# Set the window title
self.form.setWindowTitle(translate("FreeCAD Ribbon", "Ribbon design"))
# Get the style from the main window and use it for this form
palette = mw.palette()
self.form.setPalette(palette)
Style = mw.style()
self.form.setStyle(Style)
# load the RibbonStructure.json
self.ReadJson()
# Check if there is a datafile. if not, ask the user to create one.
DataFile = os.path.join(os.path.dirname(__file__), "RibbonDataFile.dat")
if os.path.exists(DataFile) is False:
Question = translate(
"FreeCAD Ribbon",
"The first time, a data file must be generated!\n" "This can take a while! Do you want to proceed?",
)
Answer = StandardFunctions.Mbox(Question, "FreeCAD Ribbon", 1, "Question")
if Answer == "yes":
self.on_ReloadWB_clicked()
# region - Load data------------------------------------------------------------------
#
Data = {}
# read ribbon structure from JSON file
with open(DataFile, "r") as file:
Data.update(json.load(file))
file.close()
DataUpdateNeeded = False
try:
FileVersion = Data["dataVersion"]
if FileVersion != self.DataFileVersion:
DataUpdateNeeded = True
except Exception:
DataUpdateNeeded = True
if DataUpdateNeeded is True:
Question = translate(
"FreeCAD Ribbon",
"The current data file is based on an older format!\n"
"It is important to update the data!\n"
"Do you want to proceed?\n"
"This can take a while!",
)
Answer = StandardFunctions.Mbox(Question, "FreeCAD Ribbon", 1, "Question")
if Answer == "yes":
self.on_ReloadWB_clicked()
# get the system language
FreeCAD_preferences = App.ParamGet("User parameter:BaseApp/Preferences/General")
try:
FCLanguage = FreeCAD_preferences.GetString("Language")
# Check if the language in the data file machtes the system language
IsSystemLanguage = True
if FCLanguage != Data["Language"]:
IsSystemLanguage = False
# If the languguage doesn't match, ask the user to update the data
if IsSystemLanguage is False:
Question = translate(
"FreeCAD Ribbon",
"The data was generated for a differernt language!\n"
"Do you want to update the data?\n"
"This can take a while!",
)
Answer = StandardFunctions.Mbox(Question, "FreeCAD Ribbon", 1, "Question")
if Answer == "yes":
self.on_ReloadWB_clicked()
except Exception:
pass
# Load the standard lists for Workbenches, toolbars and commands
self.List_Workbenches = Data["List_Workbenches"]
self.StringList_Toolbars = Data["StringList_Toolbars"]
self.List_Commands = Data["List_Commands"]
# test if List_Commands is correct
i = 5
if len(self.List_Commands) > 0:
for item in self.List_Commands:
if len(item) < 5:
i = len(item)
break
if i < 5:
Question = translate(
"FreeCAD Ribbon",
"It seems that the data file is not up-to-date.\n"
"Do you want to update the data?\n"
"This can take a while!",
)
Answer = StandardFunctions.Mbox(Question, "FreeCAD Ribbon", 1, "Question")
if Answer == "yes":
self.on_ReloadWB_clicked()
# Load the lists for the deserialized icons
try:
for IconItem in Data["WorkBench_Icons"]:
Icon: QIcon = Serialize_Ribbon.deserializeIcon(IconItem[1])
item = [IconItem[0], Icon]
self.List_WorkBenchIcons.append(item)
# Load the lists for the deserialized icons
for IconItem in Data["Command_Icons"]:
Icon: QIcon = Serialize_Ribbon.deserializeIcon(IconItem[1])
item = [IconItem[0], Icon]
self.List_CommandIcons.append(item)
except Exception as e:
StandardFunctions.Print(f"{e.with_traceback(e.__traceback__)}", "Warning")
pass
# check if the list with workbenches is up-to-date
missingWB = []
for WorkBenchName in Gui.listWorkbenches():
for j in range(len(self.List_Workbenches)):
if WorkBenchName == self.List_Workbenches[j][0] or WorkBenchName == "NoneWorkbench":
break
if j == len(self.List_Workbenches) - 1:
missingWB.append(WorkBenchName)
if len(missingWB) > 0:
ListWB = " "
for WB in missingWB:
ListWB = ListWB + WB + "\n" + " "
Question = translate(
"FreeCAD Ribbon",
"The following workbenches were installed after the last data update: \n"
"{}\n\n"
"Do you want to update the data?\n"
"This can take a while!",
).format(ListWB)
Answer = StandardFunctions.Mbox(Question, "FreeCAD Ribbon", 1, "Question")
if Answer == "yes":
self.on_ReloadWB_clicked()
# Add dropdownbuttons to the list of commands
try:
for DropDownCommand, Commands in self.Dict_DropDownButtons["dropdownButtons"].items():
if isinstance(Commands, list):
CommandName = Commands[0][0]
IconName = ""
for CommandItem in self.List_Commands:
if CommandItem[0] == CommandName:
IconName = StandardFunctions.CommandInfoCorrections(CommandItem[1])["pixmap"]
self.List_Commands.append(
[
DropDownCommand,
IconName,
DropDownCommand.split("_")[0],
"General",
DropDownCommand.split("_")[0],
]
)
else:
del self.Dict_DropDownButtons["dropdownButtons"]
StandardFunctions.Print(
"dropdownbuttons have wrong format. Please create them again!",
"Warning",
)
except Exception as e:
if Parameters_Ribbon.DEBUG_MODE is True:
StandardFunctions.Print(f"{e.with_traceback(e.__traceback__)}", "Warning")
pass
# add commands from newpanels to the list of commands
try:
for NewPanelWorkBench in self.Dict_NewPanels["newPanels"]:
for NewPanel in self.Dict_NewPanels["newPanels"][NewPanelWorkBench]:
for NewPanelCommand in self.Dict_NewPanels["newPanels"][NewPanelWorkBench][NewPanel]:
# get the icon for this command
if CommandInfoCorrections(NewPanelCommand[0])["pixmap"] != "":
IconName = CommandInfoCorrections(NewPanelCommand[0])["pixmap"]
else:
IconName = ""
MenuName = CommandInfoCorrections(NewPanelCommand[0])["menuText"].replace("&", "")
MenuNameTranslated = CommandInfoCorrections(NewPanelCommand[0])["ActionText"].replace("&", "")
self.List_Commands.append(
[
NewPanelCommand[0],
IconName,
MenuName,
NewPanelWorkBench,
MenuNameTranslated,
]
)
except Exception:
pass
# endregion
# region - Load all controls------------------------------------------------------------------
#
# laod all controls
self.LoadControls()
# endregion-----------------------------------------------------------------------------------
# region - connect controls with functions----------------------------------------------------
#
#
# --- Reload function -------------------
#
self.form.LoadWB.connect(self.form.LoadWB, SIGNAL("clicked()"), self.on_ReloadWB_clicked)
# --- Initial setup functions -----------
#
# Connect the import functions
self.form.Importlayout_IS.connect(
self.form.Importlayout_IS,
SIGNAL("clicked()"),
self.on_Importlayout_IS_clicked,
)
self.form.ImportCustomPanels_IS.connect(
self.form.ImportCustomPanels_IS,
SIGNAL("clicked()"),
self.on_ImportCustomPanels_IS_clicked,
)
self.form.ImportDropDownButtons_IS.connect(
self.form.ImportDropDownButtons_IS,
SIGNAL("clicked()"),
self.on_ImportDropDownButtons_IS_clicked,
)
self.form.ExportLayout_IS.connect(
self.form.ExportLayout_IS,
SIGNAL("clicked()"),
self.on_ExportLayout_IS_clicked,
)
self.form.ImportWorkbench_IS.connect(
self.form.ImportWorkbench_IS,
SIGNAL("clicked()"),
self.on_ImportWorkbench_IS_clicked,
)
# Connect the workbench generator
self.form.GenerateSetup_IS_WorkBenches.connect(
self.form.GenerateSetup_IS_WorkBenches,
SIGNAL("clicked()"),
self.on_GenerateSetup_IS_WorkBenches_clicked,
)
# Connect the panel generator
self.form.GenerateSetup_IS_Panels.connect(
self.form.GenerateSetup_IS_Panels,
SIGNAL("clicked()"),
self.on_GenerateSetup_IS_Panels_clicked,
)
# Connect the buttons for the default panel position for custom panels
self.form.CustomPanelPositionLeft.clicked.connect(self.on_CustomPanelPositionLeft_IS_clicked)
self.form.CustomPanelPositionRight.clicked.connect(self.on_CustomPanelPositionRight_IS_clicked)
# --- QuickCommandsTab ------------------
#
# Connect Add/Remove and move events to the buttons on the QuickAccess Tab
self.form.Add_Command_QC.connect(self.form.Add_Command_QC, SIGNAL("clicked()"), self.on_AddCommand_QC_clicked)
self.form.Remove_Command_QC.connect(
self.form.Remove_Command_QC,
SIGNAL("clicked()"),
self.on_RemoveCommand_QC_clicked,
)
self.form.MoveUp_Command_QC.connect(
self.form.MoveUp_Command_QC,
SIGNAL("clicked()"),
self.on_MoveUpCommand_QC_clicked,
)
self.form.MoveDown_Command_QC.connect(
self.form.MoveDown_Command_QC,
SIGNAL("clicked()"),
self.on_MoveDownCommand_QC_clicked,
)
# Connect the filter for the quick commands on the quickcommands tab
def FilterQuickCommands_QC():
self.on_ListCategory_QC_TextChanged()
# Connect the filter for the quick commands on the quickcommands tab
self.form.ListCategory_QC.currentTextChanged.connect(FilterQuickCommands_QC)
# Connect the searchbar for the quick commands on the quick commands tab
self.form.SearchBar_QC.textChanged.connect(self.on_SearchBar_QC_TextChanged)
#
# --- ExcludePanelsTab ------------------
#
# Connect LoadToolbars with the dropdown PanelList_RD on the Ribbon design tab
def FilterPanels_EP():
self.on_ListCategory_EP_TextChanged()
# Connect the filter for the toolbars on the toolbar tab
self.form.ListCategory_EP.currentTextChanged.connect(FilterPanels_EP)
# Connect the searchbar for the toolbars on the toolbar tab
self.form.SearchBar_EP.textChanged.connect(self.on_SearchBar_EP_TextChanged)
# Connect Add/Remove events to the buttons on the Toolbars Tab
self.form.AddPanel_EP.connect(self.form.AddPanel_EP, SIGNAL("clicked()"), self.on_AddToolbar_EP_clicked)
self.form.RemovePanel_EP.connect(
self.form.RemovePanel_EP,
SIGNAL("clicked()"),
self.on_RemoveToolbar_EP_clicked,
)
#
# --- IncludeWorkbenchTab ------------------
#
# Connect Add/Remove events to the buttons on the Workbench Tab
self.form.AddWorkbench_IW.connect(
self.form.AddWorkbench_IW,
SIGNAL("clicked()"),
self.on_AddWorkbench_IW_clicked,
)
self.form.RemoveWorkbench_IW.connect(
self.form.RemoveWorkbench_IW,
SIGNAL("clicked()"),
self.on_RemoveWorkbench_IW_clicked,
)
#
# --- CustomPanelsTab ------------------
#
# Connect move and events to the buttons on the Custom Panels Tab
self.form.MoveUpPanelCommand_CP.connect(
self.form.MoveUpPanelCommand_CP,
SIGNAL("clicked()"),
self.on_MoveUpPanelCommand_CP_clicked,
)
self.form.MoveDownPanelCommand_CP.connect(
self.form.MoveDownPanelCommand_CP,
SIGNAL("clicked()"),
self.on_MoveDownPanelCommand_CP_clicked,
)
# Connect Add events to the buttons on the Custom Panels Tab for adding commands to the panel
self.form.AddPanel_CP.connect(self.form.AddPanel_CP, SIGNAL("clicked()"), self.on_AddPanel_CP_clicked)
self.form.AddCustomPanel_CP.connect(
self.form.AddCustomPanel_CP,
SIGNAL("clicked()"),
self.on_AddCustomPanel_CP_clicked,
)
# Connect LoadWorkbenches with the dropdown WorkbenchList on the Ribbon design tab
def LoadWorkbenches_CP():
self.on_WorkbenchList_CP__activated()
self.form.WorkbenchList_CP.activated.connect(LoadWorkbenches_CP)
# Connect custom toolbar selector on the Custom Panels Tab
def CommandList_CP():
self.on_CustomToolbarSelector_CP_activated()
self.form.CustomToolbarSelector_CP.activated.connect(CommandList_CP)
self.form.RemovePanel_CP.connect(
self.form.RemovePanel_CP,
SIGNAL("clicked()"),
self.on_RemovePanel_CP_clicked,
)
#
# --- CreateNewPanelTab ----------------
#
# Connect custom toolbar selector on the Custom Panels Tab
def CommandList_NP():
self.on_CustomToolbarSelector_NP_activated()
self.form.CustomToolbarSelector_NP.activated.connect(CommandList_NP)
self.form.RemovePanel_NP.connect(
self.form.RemovePanel_NP,
SIGNAL("clicked()"),
self.on_RemovePanel_NP_clicked,
)
self.form.AddCustomToolbar_NP.connect(
self.form.AddCustomToolbar_NP,
SIGNAL("clicked()"),
self.on_AddCustomToolbar_NP_clicked,
)
# Connect Add/Remove and move events to the buttons on the QuickAccess Tab
self.form.AddPanelCommand_NP.connect(
self.form.AddPanelCommand_NP,
SIGNAL("clicked()"),
self.on_AddCommand_NP_clicked,
)
self.form.RemovePanelCommand_NP.connect(
self.form.RemovePanelCommand_NP,
SIGNAL("clicked()"),
self.on_RemoveCommand_NP_clicked,
)
self.form.MoveUpPanelCommand_NP.connect(
self.form.MoveUpPanelCommand_NP,
SIGNAL("clicked()"),
self.on_MoveUpCommand_NP_clicked,
)
self.form.MoveDownPanelCommand_NP.connect(
self.form.MoveDownPanelCommand_NP,
SIGNAL("clicked()"),
self.on_MoveDownCommand_NP_clicked,
)
# Connect the filter for the quick commands on the quickcommands tab
def FilterWorkbench_NP():
self.on_ListCategory_NP_TextChanged()
# Connect the filter for the quick commands on the quickcommands tab
self.form.ListCategory_NP.currentTextChanged.connect(FilterWorkbench_NP)
# Connect the searchbar for the quick commands on the quick commands tab
self.form.SearchBar_NP.textChanged.connect(self.on_SearchBar_NP_TextChanged)
#
# --- CreateDropDownButtonTab ----------------
#
# Connect the Create dropdown button
self.form.CreateControl_DDB.connect(
self.form.CreateControl_DDB,
SIGNAL("clicked()"),
self.on_CreateControl_DDB_clicked,
)
# Connect dropdownselector on the create dropdown button Tab
def CommandList_DDB():
self.on_CommandList_DDB_activated()
self.form.CommandList_DDB.activated.connect(CommandList_DDB)
# Connect the remove dropdown button
self.form.RemoveControl_DDB.connect(
self.form.RemoveControl_DDB,
SIGNAL("clicked()"),
self.on_RemoveControl_DDB_clicked,
)
# Connect Add/Remove and move events to the buttons on the QuickAccess Tab
self.form.AddCommand_DDB.connect(
self.form.AddCommand_DDB,
SIGNAL("clicked()"),
self.on_AddCommand_DDB_clicked,
)
self.form.RemoveCommand_DDB.connect(
self.form.RemoveCommand_DDB,
SIGNAL("clicked()"),
self.on_RemoveCommand_DDB_clicked,
)
self.form.MoveUpCommand_DDB.connect(
self.form.MoveUpCommand_DDB,
SIGNAL("clicked()"),
self.on_MoveUpCommand_DDB_clicked,
)
self.form.MoveDownCommand_DDB.connect(
self.form.MoveDownCommand_DDB,
SIGNAL("clicked()"),
self.on_MoveDownCommand_DDB_clicked,
)
# Connect the filter for the quick commands on the quickcommands tab
def FilterWorkbench_DDB():
self.on_ListCategory_DDB_TextChanged()
# Connect the filter for the quick commands on the quickcommands tab
self.form.ListCategory_DDB.currentTextChanged.connect(FilterWorkbench_DDB)
# Connect the searchbar for the quick commands on the quick commands tab
self.form.SearchBar_DDB.textChanged.connect(self.on_SearchBar_DDB_TextChanged)
#
# --- RibbonDesignTab ------------------
#
# Connect LoadWorkbenches with the dropdown WorkbenchList on the Ribbon design tab
def LoadWorkbenches_RD():
self.on_WorkbenchList_RD__TextChanged()
self.form.WorkbenchList_RD.currentTextChanged.connect(LoadWorkbenches_RD)
# Connect LoadToolbars with the dropdown PanelList_RD on the Ribbon design tab
def LoadPanels_RD():
self.on_PanelList_RD__TextChanged()
self.form.PanelList_RD.currentTextChanged.connect(LoadPanels_RD)
# Connect the icon only checkbox
self.form.IconOnly_RD.clicked.connect(self.on_IconOnly_RD_clicked)
# Connect a click event on the tablewidgit on the Ribbon design tab
self.form.CommandTable_RD.itemClicked.connect(self.on_TableCell_RD_clicked)
# Connect a change event on the tablewidgit on the Ribbon design tab to change the button text.
self.form.CommandTable_RD.itemChanged.connect(self.on_TableCell_RD_changed)
# Connect move events to the buttons on the Ribbon design Tab
self.form.MoveUp_RibbonCommand_RD.connect(
self.form.MoveUp_RibbonCommand_RD,
SIGNAL("clicked()"),
self.on_MoveUpCommandTable_RD_clicked,
)
self.form.MoveDown_RibbonCommand_RD.connect(
self.form.MoveDown_RibbonCommand_RD,
SIGNAL("clicked()"),
self.on_MoveDownCommandTable_RD_clicked,
)
self.form.MoveUpPanel_RD.connect(
self.form.MoveUpPanel_RD,
SIGNAL("clicked()"),
self.on_MoveUpPanel_RD_clicked,
)
self.form.MoveDownPanel_RD.connect(
self.form.MoveDownPanel_RD,
SIGNAL("clicked()"),
self.on_MoveDownPanel_RD_clicked,
)
self.form.PanelOrder_RD.indexesMoved.connect(self.on_PanelOrder_RD_changed)
self.form.AddSeparator_RD.connect(
self.form.AddSeparator_RD,
SIGNAL("clicked()"),
self.on_AddSeparator_RD_clicked,
)
self.form.RemoveSeparator_RD.connect(
self.form.RemoveSeparator_RD,
SIGNAL("clicked()"),
self.on_RemoveSeparator_RD_clicked,
)
# --- Form controls ------------------
#
# Connect the button UpdateJson with the function on_UpdateJson_clicked
def UpdateJson():
self.on_UpdateJson_clicked(self)
self.form.UpdateJson.connect(self.form.UpdateJson, SIGNAL("clicked()"), UpdateJson)
# Connect the button Close with the function on_Close_clicked
def Close():
self.on_Close_clicked(self)
self.form.Close.connect(self.form.Close, SIGNAL("clicked()"), Close)
self.form.RestoreJson.connect(self.form.RestoreJson, SIGNAL("clicked()"), self.on_RestoreJson_clicked)
self.form.ResetJson.connect(self.form.ResetJson, SIGNAL("clicked()"), self.on_ResetJson_clicked)
# # connect the change of the current tab event to a function to set the size per tab
# self.form.tabWidget.currentChanged.connect(self.on_tabBar_currentIndexChanged)
# Connect the cancel button
def Cancel():
self.on_Cancel_clicked(self)
self.form.Cancel.connect(self.form.Cancel, SIGNAL("clicked()"), Cancel)
# Connect the help buttons
def Help():
self.on_Helpbutton_clicked(self)
self.form.HelpButton.connect(self.form.HelpButton, SIGNAL("clicked()"), Help)
# endregion
# region - Modify controls--------------------------------------------------------------------
#
# -- TabWidget
# Set the first tab activated
self.form.tabWidget.setCurrentWidget(self.form.tabWidget.widget(0))
#
# -- Initial setup tab --
self.form.DefaultButtonSize_IS_Workbenches.setItemData(0, "small", Qt.ItemDataRole.UserRole)
self.form.DefaultButtonSize_IS_Workbenches.setItemData(1, "medium", Qt.ItemDataRole.UserRole)
self.form.DefaultButtonSize_IS_Workbenches.setItemData(2, "large", Qt.ItemDataRole.UserRole)
self.form.DefaultButtonSize_IS_Panels.setItemData(0, "small", Qt.ItemDataRole.UserRole)
self.form.DefaultButtonSize_IS_Panels.setItemData(1, "medium", Qt.ItemDataRole.UserRole)
self.form.DefaultButtonSize_IS_Panels.setItemData(2, "large", Qt.ItemDataRole.UserRole)
# -- Ribbon design tab --
# Settings for the table widget
self.form.CommandTable_RD.setEnabled(True)
self.form.CommandTable_RD.horizontalHeader().setVisible(True)
self.form.CommandTable_RD.setColumnWidth(0, 300)
self.form.CommandTable_RD.resizeColumnToContents(1)
self.form.CommandTable_RD.resizeColumnToContents(2)
self.form.CommandTable_RD.resizeColumnToContents(3)
# -- Form buttons --
# Get the icon from the FreeCAD help
helpMenu = mw.findChildren(QMenu, "&Help")[0]
helpAction = helpMenu.actions()[0]
helpIcon = helpAction.icon()
if helpIcon is not None:
self.form.HelpButton.setIcon(helpIcon)
self.form.HelpButton.setMinimumHeight(self.form.Close.minimumHeight())
# Disable and hide the restore button if the backup function is disabled
if Parameters_Ribbon.ENABLE_BACKUP is False:
self.form.RestoreJson.setDisabled(True)
self.form.RestoreJson.setHidden(True)
else:
self.form.RestoreJson.setEnabled(True)
self.form.RestoreJson.setVisible(True)
# Set the icon and size for the refresh button
self.form.LoadWB.setIcon(Gui.getIcon("view-refresh"))
self.form.LoadWB.setIconSize(QSize(20, 20))
return
def on_ReloadWB_clicked(self):
# minimize the dialog
self.form.hide()
# clear the lists first
self.List_Workbenches.clear()
self.StringList_Toolbars.clear()
self.List_Commands.clear()
# get the system language
FreeCAD_preferences = App.ParamGet("User parameter:BaseApp/Preferences/General")
FCLanguage = FreeCAD_preferences.GetString("Language")
# --- Workbenches ----------------------------------------------------------------------------------------------
#
# Create a list of all workbenches with their icon
self.List_Workbenches.clear()
List_Workbenches = Gui.listWorkbenches().copy()
for WorkBenchName in List_Workbenches:
if str(WorkBenchName) != "" or WorkBenchName is not None:
if str(WorkBenchName) != "NoneWorkbench":
Gui.activateWorkbench(WorkBenchName)
WorkBench = Gui.getWorkbench(WorkBenchName)
# Get the toolbar items
ToolbarItems: dict = WorkBench.getToolbarItems()
# Update the toolbar items with corrections
ToolbarItems: dict = StandardFunctions.CorrectGetToolbarItems(ToolbarItems)
IconName = ""
IconName = str(Gui.getWorkbench(WorkBenchName).Icon)
WorkbenchTitle = Gui.getWorkbench(WorkBenchName).MenuText
WorkbenchTitleTranslated = StandardFunctions.TranslationsMapping(WorkBenchName, WorkbenchTitle)
self.List_Workbenches.append(
[
str(WorkBenchName),
IconName,
WorkbenchTitle,
ToolbarItems,
WorkbenchTitleTranslated,
]
)
# --- Toolbars ----------------------------------------------------------------------------------------------
#
# Store the current active workbench
ActiveWB = Gui.activeWorkbench().name()
# Go through the list of workbenches
i = 0
for WorkBench in self.List_Workbenches:
wbToolbars = []
if WorkBench[0] != "General" and WorkBench[0] != "" and WorkBench[0] is not None:
Gui.activateWorkbench(WorkBench[0])
wbToolbars = Gui.getWorkbench(WorkBench[0]).listToolbars()
# Go through the toolbars
for Toolbar in wbToolbars:
ToolBarTtranslated = StandardFunctions.TranslationsMapping(WorkBench[0], Toolbar)
self.StringList_Toolbars.append([Toolbar, WorkBench[2], WorkBench[0], ToolBarTtranslated])
# Add the custom toolbars
CustomToolbars = self.List_ReturnCustomToolbars()
for Customtoolbar in CustomToolbars:
self.StringList_Toolbars.append(Customtoolbar)
CustomToolbars = self.List_ReturnCustomToolbars_Global()
for Customtoolbar in CustomToolbars:
self.StringList_Toolbars.append(Customtoolbar)
# --- Commands ----------------------------------------------------------------------------------------------
#
# Create a list of all commands with their icon
self.List_Commands.clear()
# Create a list of command names
CommandNames = []
for i in range(len(self.List_Workbenches)):
Gui.activateWorkbench(self.List_Workbenches[i][0])
WorkBench = Gui.getWorkbench(self.List_Workbenches[i][0])
# Get the toolbar items
ToolbarItems: dict = WorkBench.getToolbarItems()
# Update the toolbar items with corrections
ToolbarItems: dict = StandardFunctions.CorrectGetToolbarItems(ToolbarItems)
for key, value in list(ToolbarItems.items()):
for j in range(len(value)):
Item = [value[j], self.List_Workbenches[i][0]]
CommandNames.append(Item)
# Go through the list
for CommandName in CommandNames:
# get the command with this name
command = Gui.Command.get(CommandName[0])
WorkBenchName = CommandName[1]
if command is not None:
# get the icon for this command
if CommandInfoCorrections(CommandName[0])["pixmap"] != "":
IconName = CommandInfoCorrections(CommandName[0])["pixmap"]
else:
IconName = ""
MenuName = CommandInfoCorrections(CommandName[0])["menuText"].replace("&", "")
MenuNameTranslated = CommandInfoCorrections(CommandName[0])["ActionText"].replace("&", "")
self.List_Commands.append(
[
CommandName[0],
IconName,
MenuName,
WorkBenchName,
MenuNameTranslated,
]
)
# add also custom commands
Toolbars = self.List_ReturnCustomToolbars()
for Toolbar in Toolbars:
WorkbenchTitle = Toolbar[1]
for WorkBench in self.List_Workbenches:
if WorkbenchTitle == WorkBench[2]:
WorkBenchName = WorkBench[0]
for CustomCommand in Toolbar[2]:
command = Gui.Command.get(CustomCommand)
if CommandInfoCorrections(CustomCommand)["pixmap"] != "":
IconName = CommandInfoCorrections(CustomCommand)["pixmap"]
else:
IconName = ""
MenuName = CommandInfoCorrections(CustomCommand)["menuText"].replace("&", "")
MenuNameTranslated = CommandInfoCorrections(CustomCommand)["ActionText"].replace("&", "")
self.List_Commands.append(
[
CustomCommand,
IconName,
MenuName,
WorkBenchName,
MenuNameTranslated,
]
)
Toolbars = self.List_ReturnCustomToolbars_Global()
for Toolbar in Toolbars:
for CustomCommand in Toolbar[2]:
command = Gui.Command.get(CustomCommand)
if CommandInfoCorrections(CustomCommand)["pixmap"] != "":
IconName = CommandInfoCorrections(CustomCommand)["pixmap"]
else:
IconName = None
MenuName = CommandInfoCorrections(CustomCommand)["menuText"].replace("&", "")
MenuNameTranslated = CommandInfoCorrections(CustomCommand)["ActionText"].replace("&", "")
self.List_Commands.append([CustomCommand, IconName, MenuName, Toolbar[1], MenuNameTranslated])
# Add general commands
if int(App.Version()[0]) > 0:
command = Gui.Command.get("Std_Measure")
if CommandInfoCorrections("Std_Measure")["pixmap"] != "":
IconName = CommandInfoCorrections("Std_Measure")["pixmap"]
else:
IconName = ""
MenuName = CommandInfoCorrections("Std_Measure")["menuText"].replace("&", "")
MenuNameTranslated = CommandInfoCorrections("Std_Measure")["ActionText"].replace("&", "")
self.List_Commands.append(["Std_Measure", IconName, MenuName, "General", MenuNameTranslated])
# re-activate the workbench that was stored.
Gui.activateWorkbench(ActiveWB)
# --- Serialize Icons ------------------------------------------------------------------------------------------
#
WorkbenchIcon = []
for WorkBenchItem in self.List_Workbenches:
WorkBenchName = WorkBenchItem[0]
Icon = Gui.getIcon(WorkBenchItem[1])
if Icon is not None and Icon.isNull() is False:
try:
SerializedIcon = Serialize_Ribbon.serializeIcon(Icon)
WorkbenchIcon.append([WorkBenchName, SerializedIcon])
# add the icons also to the deserialized list
self.List_WorkBenchIcons.append([WorkBenchName, Icon])
except Exception as e:
if Parameters_Ribbon.DEBUG_MODE is True:
StandardFunctions.Print(f"{e.with_traceback(e.__traceback__)}", "Warning")
CommandIcons = []
for CommandItem in self.List_Commands:
CommandName = CommandItem[0]
Icon = StandardFunctions.returnQiCons_Commands(CommandName, CommandItem[1])
if Icon is not None and Icon.isNull() is False:
try:
SerializedIcon = Serialize_Ribbon.serializeIcon(Icon)
CommandIcons.append([CommandName, SerializedIcon])
# add the icons also to the deserialized list
self.List_CommandIcons.append([CommandName, Icon])
except Exception as e:
if Parameters_Ribbon.DEBUG_MODE is True:
StandardFunctions.Print(f"{e.with_traceback(e.__traceback__)}", "Warning")
# Write the lists to a data file
#
# clear the data file. If not exists, create it
DataFile = os.path.join(os.path.dirname(__file__), "RibbonDataFile.dat")
open(DataFile, "w").close()
# Open de data file, load it as json and then close it again
Data = {}
# Update the data
Data["dataVersion"] = self.DataFileVersion
Data["Language"] = FCLanguage
Data["List_Workbenches"] = self.List_Workbenches
Data["StringList_Toolbars"] = self.StringList_Toolbars
Data["List_Commands"] = self.List_Commands
Data["WorkBench_Icons"] = WorkbenchIcon
Data["Command_Icons"] = CommandIcons
# Write to the data file
DataFile = os.path.join(os.path.dirname(__file__), "RibbonDataFile.dat")
with open(DataFile, "w") as outfile:
json.dump(Data, outfile, indent=4)
outfile.close()
# Write a second data file with the list of commands only
Data2 = {}
Data2["List_Commands"] = self.List_Commands
# Write to the data file
DataFile2 = os.path.join(os.path.dirname(__file__), "RibbonDataFile2.dat")
with open(DataFile2, "w") as outfile:
json.dump(Data2, outfile, indent=4)
outfile.close()
# run init again
self.__init__()
# Set the first tab active
self.form.tabWidget.setCurrentIndex(0)
# Show the dialog again
self.form.show()
return
# region - Control functions----------------------------------------------------------------------
# Add all toolbars of the selected workbench to the toolbar list(QComboBox)
#
# region - Initial setup tab
def on_Importlayout_IS_clicked(self):
JsonFile = StandardFunctions.GetFileDialog(
Filter="RibbonStructure (*.json)",
parent=self.form,
DefaultPath=Parameters_Ribbon.IMPORT_LOCATION,
SaveAs=False,
)
if JsonFile != "":
self.ReadJson(Section="All", JsonFile=JsonFile)
self.LoadControls()
# Enable the apply button