-
Notifications
You must be signed in to change notification settings - Fork 2
/
BoM_ManageColumns.py
529 lines (442 loc) · 20.7 KB
/
BoM_ManageColumns.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
# ***************************************************************************
# * Copyright (c) 2023 Paul Ebbers [email protected] *
# * *
# * 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 Lesser 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 *
# * *
# ***************************************************************************/
import FreeCAD as App
import FreeCADGui as Gui
import os
from inspect import getsourcefile
import General_BOM_Functions
import Standard_Functions_BOM_WB as Standard_Functions
from PySide.QtGui import QPalette, QIcon
from PySide.QtWidgets import QListWidgetItem, QDialogButtonBox
from PySide.QtCore import SIGNAL, Qt
import Settings_BoM
from Settings_BoM import ENABLE_DEBUG
import BoM_WB_Locator
import sys
# get the path of the current python script
PATH_TB = os.path.dirname(BoM_WB_Locator.__file__)
# Get the paths for the ,recoures, icons and ui
PATH_TB_RESOURCES = os.path.join(PATH_TB, "Resources")
PATH_TB_ICONS = os.path.join(PATH_TB, PATH_TB_RESOURCES, "Icons")
PATH_TB_UI = os.path.join(PATH_TB, PATH_TB_RESOURCES, "UI")
sys.path.append(PATH_TB_UI)
# import graphical created Ui. (With QtDesigner or QtCreator)
import Add_RemoveColumns_ui as Add_RemoveColumns_ui
# Define the translation
translate = App.Qt.translate
# get the path of the current python script
PATH_TB = os.path.dirname(BoM_WB_Locator.__file__)
# Get the paths for the ,recoures, icons and ui
PATH_TB_RESOURCES = os.path.join(PATH_TB, "Resources")
PATH_TB_ICONS = os.path.join(PATH_TB, PATH_TB_RESOURCES, "Icons")
PATH_TB_UI = os.path.join(PATH_TB, PATH_TB_RESOURCES, "UI")
# Get the initial headers. To use for the reset button.
initalHeaders = General_BOM_Functions.General_BOM.customHeaders
class LoadDialog(Add_RemoveColumns_ui.Ui_Dialog):
def __init__(self):
# Makes "self.on_CreateBOM_clicked" listen to the changed control values instead initial values
super(LoadDialog, self).__init__()
# # this will create a Qt widget from our ui file
self.form = Gui.PySideUic.loadUi(
os.path.join(PATH_TB_UI, "Add_RemoveColumns.ui")
)
self.form.setWindowIcon(QIcon(os.path.join(PATH_TB_ICONS, "SetColumns.svg")))
# Make sure that the dialog stays on top
self.form.setWindowFlags(Qt.WindowStaysOnTopHint)
# Get the style from the main window and use it for this form
mw = Gui.getMainWindow()
palette = mw.palette()
self.form.setPalette(palette)
Style = mw.style()
self.form.setStyle(Style)
# region - Add the connections
#
# Load ---------------------------------------------------------------------------------
def LoadProperties():
self.on_LoadProperties_clicked(self)
self.form.LoadProperties.connect(
self.form.LoadProperties, SIGNAL("clicked()"), LoadProperties
)
# -----------------------------------------------------------------------------------------
#
# AddItem ---------------------------------------------------------------------------------
def AddItem():
self.on_AddItem_clicked(self)
self.form.AddItem.connect(self.form.AddItem, SIGNAL("clicked()"), AddItem)
# -----------------------------------------------------------------------------------------
#
# RemoveItem ------------------------------------------------------------------------------
def RemoveItem():
self.on_RemoveItem_clicked(self)
self.form.RemoveItem.connect(
self.form.RemoveItem, SIGNAL("clicked()"), RemoveItem
)
# -----------------------------------------------------------------------------------------
#
# AddManual ------------------------------------------------------------------------------
def AddManual():
self.on_AddManual_clicked(self)
self.form.AddManual.connect(self.form.AddManual, SIGNAL("clicked()"), AddManual)
# -----------------------------------------------------------------------------------------
#
# SortAZ ------------------------------------------------------------------------------
def SortAZ():
self.on_Sort_AZ_clicked(self)
self.form.Sort_AZ.connect(self.form.Sort_AZ, SIGNAL("clicked()"), SortAZ)
# -----------------------------------------------------------------------------------------
#
# SortZA ------------------------------------------------------------------------------
def SortZA():
self.on_Sort_ZA_clicked(self)
self.form.Sort_ZA.connect(self.form.Sort_ZA, SIGNAL("clicked()"), SortZA)
# -----------------------------------------------------------------------------------------
#
# MoveUp ------------------------------------------------------------------------------
def MoveUp():
self.on_Move_Up_clicked(self)
self.form.Move_Up.connect(self.form.Move_Up, SIGNAL("clicked()"), MoveUp)
# -----------------------------------------------------------------------------------------
#
# MoveDown ------------------------------------------------------------------------------
def MoveDown():
self.on_Move_Down_clicked(self)
self.form.Move_Down.connect(self.form.Move_Down, SIGNAL("clicked()"), MoveDown)
# -----------------------------------------------------------------------------------------
#
# ButtonBox -------------------------------------------------------------------------------
#
# Cancel ----------------------------------------------------------------------------------
def Cancel():
self.on_ButtonBox_Rejected(self)
self.form.buttonBox.rejected.connect(Cancel)
# -----------------------------------------------------------------------------------------
#
# Ok --------------------------------------------------------------------------------------
def Ok():
self.on_ButtonBox_Accepted(self)
self.form.buttonBox.accepted.connect(Ok)
# -----------------------------------------------------------------------------------------
#
# Apply -----------------------------------------------------------------------------------
def Apply():
self.on_ButtonBox_Applied(self)
self.form.buttonBox.button(QDialogButtonBox.Apply).clicked.connect(Apply)
# -----------------------------------------------------------------------------------------
#
# Apply -----------------------------------------------------------------------------------
def Reset():
self.on_ButtonBox_Resetted(self)
self.form.buttonBox.button(QDialogButtonBox.Reset).clicked.connect(Reset)
# -----------------------------------------------------------------------------------------
# endregion
# region - Set the correct icons depending on the color of the main window
BackGround_AddAll = (
Gui.getMainWindow().palette().color(QPalette.ColorRole.Window).getRgb()
)
if Standard_Functions.LightOrDark(rgbColor=BackGround_AddAll) == "dark":
# Add/Remove buttons
self.form.AddItem.setIcon(QIcon(os.path.join(PATH_TB_ICONS, "+ sign.svg")))
self.form.RemoveItem.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "- sign.svg"))
)
self.form.AddManual.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "SingleArrow_Up_Light.svg"))
)
# Sort buttons
self.form.Sort_AZ.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "Sort_AZ_Light.png"))
)
self.form.Sort_ZA.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "Sort_ZA_Light.png"))
)
# Move up/down buttons
self.form.Move_Up.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "SingleArrow_Up_Light.svg"))
)
self.form.Move_Down.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "SingleArrow_Down_Light.svg"))
)
if Standard_Functions.LightOrDark(rgbColor=BackGround_AddAll) != "dark":
# Add/Remove buttons
self.form.AddItem.setIcon(QIcon(os.path.join(PATH_TB_ICONS, "+ sign.svg")))
self.form.RemoveItem.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "- sign.svg"))
)
self.form.AddManual.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "SingleArrow_Up_Dark.svg"))
)
# Sort buttons
self.form.Sort_AZ.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "Sort_AZ_Dark.png"))
)
self.form.Sort_ZA.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "Sort_ZA_Dark.png"))
)
# Move up/down buttons
self.form.Move_Up.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "SingleArrow_Up_Dark.svg"))
)
self.form.Move_Down.setIcon(
QIcon(os.path.join(PATH_TB_ICONS, "SingleArrow_Down_Dark.svg"))
)
# endregion
return
@staticmethod
def on_LoadProperties_clicked(self):
# Get the properties from the active document
doc = App.ActiveDocument
sel = Gui.Selection.getSelection()
if sel is not None:
try:
doc = sel[0]
except Exception:
doc = App.ActiveDocument
PropertyList = doc.PropertiesList
# Get the currently applied custom columns
CustomHeaders = General_BOM_Functions.General_BOM.customHeaders.split(";")
# Fill the list "Columns_Present" with the custom headers that are currently present
self.form.Columns_Present.clear()
for Header in CustomHeaders:
if Header != "":
self.form.Columns_Present.addItem(Header)
# Clear the list first.
self.form.Columns_To_Add.clear()
# Fill the List "Columns_To_Add" with the properties that are not already a custom header.
for Property in PropertyList:
IsInList = False
for CurrentProperty in CustomHeaders:
if Property == CurrentProperty:
IsInList = True
break
if IsInList is False:
if Property != "Shape":
self.form.Columns_To_Add.addItem(Property)
if Property == "Shape":
self.form.Columns_To_Add.addItem("Shape - Length")
self.form.Columns_To_Add.addItem("Shape - Width")
self.form.Columns_To_Add.addItem("Shape - Height")
self.form.Columns_To_Add.addItem("Shape - Volume")
self.form.Columns_To_Add.addItem("Shape - Area")
self.form.Columns_To_Add.addItem("Shape - CenterOfGravity")
if Property == "ShapeMaterial":
self.form.Columns_To_Add.addItem("Shape - Mass")
return
@staticmethod
def on_AddItem_clicked(self):
# Get the selected item(s)
Values = self.form.Columns_To_Add.selectedItems()
# Go through the items
for Value in Values:
# Get the item text
itemText = QListWidgetItem(Value).text()
# Add the item to the list with current items
self.form.Columns_Present.addItem(itemText)
# If debug is enabled, log the action.
if ENABLE_DEBUG is True:
Text = translate("BoM Workbench", f"{itemText} added to the columns.")
Standard_Functions.Print(Text, "Log")
# Go through the items on the list with items to add.
for i in range(self.form.Columns_To_Add.count()):
# Get the item
item = self.form.Columns_To_Add.item(i)
# If the item is not none and the item text is equeal to itemText,
# remove it from the columns to add list.
if item is not None:
if item.text() == itemText:
self.form.Columns_To_Add.takeItem(i)
# Remove the focus from the control
self.form.AddItem.clearFocus()
return
@staticmethod
def on_RemoveItem_clicked(self):
# Get the selected item(s)
Values = self.form.Columns_Present.selectedItems()
# Go through the items
for Value in Values:
# Get the item text
itemText = QListWidgetItem(Value).text()
# Add the item to the lsit with current items
self.form.Columns_To_Add.addItem(itemText)
# If debug is enabled, log the action.
if ENABLE_DEBUG is True:
Text = translate(
"BoM Workbench", f"{itemText} removed from the columns."
)
Standard_Functions.Print(Text, "Log")
# Go through the items on the list with items to add.
for i in range(self.form.Columns_Present.count()):
# Get the item
item = self.form.Columns_Present.item(i)
# If the item is not none and the item text is equeal to itemText,
# remove it from the columns to add list.
if item is not None:
if item.text() == itemText:
self.form.Columns_Present.takeItem(i)
# Remove the focus from the control
self.form.RemoveItem.clearFocus()
return
@staticmethod
def on_AddManual_clicked(self):
Value = self.form.ManualProperty.text()
if Value != "":
self.form.Columns_Present.addItem(Value)
@staticmethod
def on_Sort_AZ_clicked(self):
# Sort the items in ascending order
self.form.Columns_Present.sortItems(Qt.SortOrder.AscendingOrder)
# Remove the focus from the control
self.form.Sort_AZ.clearFocus()
return
@staticmethod
def on_Sort_ZA_clicked(self):
# Sort the items in descending order
self.form.Columns_Present.sortItems(Qt.SortOrder.DescendingOrder)
# Remove the focus from the control
self.form.Sort_ZA.clearFocus()
return
@staticmethod
def on_Move_Up_clicked(self):
# Get the current row
Row = self.form.Columns_Present.currentRow()
# remove the current row
Item = self.form.Columns_Present.takeItem(Row)
# Add the just removed row, one row higher on the list
self.form.Columns_Present.insertItem(Row - 1, Item)
# Set the moved row, to the current row
self.form.Columns_Present.setCurrentRow(Row - 1)
# Remove the focus from the control
self.form.Move_Up.clearFocus()
return
@staticmethod
def on_Move_Down_clicked(self):
# Get the current row
Row = self.form.Columns_Present.currentRow()
# remove the current row
Item = self.form.Columns_Present.takeItem(Row)
# Add the just removed row, one row lower on the list
self.form.Columns_Present.insertItem(Row + 1, Item)
# Set the moved row, to the current row
self.form.Columns_Present.setCurrentRow(Row + 1)
# Remove the focus from the control
self.form.Move_Down.clearFocus()
return
@staticmethod
def on_ButtonBox_Rejected(self):
self.form.close()
return
@staticmethod
def on_ButtonBox_Accepted(self):
# Create the result string from the items present
result = ""
for i in range(self.form.Columns_Present.count()):
result = result + ";" + self.form.Columns_Present.item(i).text()
if result[:1] == ";":
result = result[1:]
# Set the custom headers for the current session
General_BOM_Functions.General_BOM.customHeaders = result
# Write the custom headers to preferences, for next time
Settings_BoM.SetStringSetting("CustomHeader", result)
# If debug is enabled, log the action.
# If debug is enabled, log the action.
if ENABLE_DEBUG is True:
if result == "":
result = "None"
Text = translate(
"BoM Workbench", f"The extra columns are:{result.replace(';', ', ')}."
)
Standard_Functions.Print(Text, "Log")
# Close the form
self.form.close()
return
@staticmethod
def on_ButtonBox_Applied(self):
# Create the result string from the items present
result = ""
for i in range(self.form.Columns_Present.count()):
result = result + ";" + self.form.Columns_Present.item(i).text()
if result[:1] == ";":
result = result[1:]
# Set the custom headers for the current session
General_BOM_Functions.General_BOM.customHeaders = result
# Write the custom headers to preferences, for next time
Settings_BoM.SetStringSetting("CustomHeader", result)
# If debug is enabled, log the action.
if ENABLE_DEBUG is True:
if result == "":
result = "None"
Text = translate(
"BoM Workbench", f"The extra columns are: {result.replace(';', ', ')}."
)
Standard_Functions.Print(Text, "Log")
# Remove the focus from the control
self.form.buttonBox.button(QDialogButtonBox.Apply).clearFocus()
return
@staticmethod
def on_ButtonBox_Resetted(self):
# clear the listviews, so you can repopulate them.
self.form.Columns_Present.clear()
self.form.Columns_To_Add.clear()
# Get the currently applied custom columns
CustomHeaders = initalHeaders.split(";")
# Add the headers to the list of present columns
for Header in CustomHeaders:
if Header != "":
self.form.Columns_Present.addItem(Header)
# Get the properties from the active document
doc = App.ActiveDocument
sel = Gui.Selection.getSelection()
if sel is not None:
try:
doc = sel[0]
except Exception:
doc = App.ActiveDocument
PropertyList = doc.PropertiesList
# Fill the List "Columns_To_Add" with the properties that are not already a custom header.
for Property in PropertyList:
IsInList = False
for CurrentProperty in CustomHeaders:
if Property == CurrentProperty:
IsInList = True
break
if IsInList is False:
self.form.Columns_To_Add.addItem(Property)
# Set the headers to the initial state in the current session
General_BOM_Functions.General_BOM.customHeaders = initalHeaders
# Set the headers to the initial state in the preferences, for next time
Settings_BoM.SetStringSetting("CustomHeader", initalHeaders)
# If debug is enabled, log the action.
if ENABLE_DEBUG is True:
Text = translate(
"BoM Workbench",
f"The extra columns resetted to: {initalHeaders.replace(';', ', ')}.",
)
Standard_Functions.Print(Text, "Log")
# Remove the focus from the control
self.form.buttonBox.button(QDialogButtonBox.Apply).clearFocus()
return
def main():
# Get the form
Dialog = LoadDialog().form
# Show the form
Dialog.show()
return