-
Notifications
You must be signed in to change notification settings - Fork 2
/
operations_tableview.py
973 lines (756 loc) · 28.1 KB
/
operations_tableview.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
from typing import List
from typing import Any
from typing import Dict
from typing import cast
from PySide6 import QtCore
from PySide6 import QtGui
from PySide6 import QtWidgets
from svgviewer import SvgViewer
class OpItem:
def __init__(self, data):
self.name = data.get("name", "op")
self.cam_op = data.get("type", "Pocket")
self.cut_depth = data.get("cut_depth", 3.175)
self.paths = data.get("paths", [])
self.ramp_plunge = data.get("ramp_plunge", False)
self.combinaison = data.get("combinaison", "Union")
self.direction = data.get("direction", "Conventional")
self.units = data.get("units", "mm")
self.margin = data.get("margin", 0.0)
self.width = data.get("width", 0.0)
# not in the data
self.enabled = False
def put_value(self, attr, value):
""" """
setattr(self, attr, value)
def __str__(self):
""" """
return "op: %s %s [%f] %s %s %s %f" % (
self.name,
self.cam_op,
self.cut_depth,
self.ramp_plunge,
self.enabled,
self.combinaison,
self.cut_depth,
)
def to_dict(self) -> Dict[str, Any]:
""" """
return {
"name": self.name,
"type": self.cam_op,
"cut_depth": self.cut_depth,
"paths": self.paths,
"ramp_plunge": self.ramp_plunge,
"combinaison": self.combinaison,
"direction": self.direction,
"units": self.units,
"margin": self.margin,
"width": self.width,
}
class PyCutDoubleSpinBox(QtWidgets.QDoubleSpinBox):
""" """
def __init__(self, parent):
""" """
QtWidgets.QDoubleSpinBox.__init__(self, parent)
self.o = None
self.attribute = ""
self.setMinimum(0)
self.setMaximum(100)
self.setSingleStep(0.1)
self.setDecimals(3)
self.valueChanged.connect(self.cb_spinbox)
def cb_disconnect(self):
""" """
self.valueChanged.disconnect(self.cb_spinbox)
def cb_connect(self):
""" """
self.valueChanged.connect(self.cb_spinbox)
def assign_object(self, o):
""" """
self.o = o
def assign_object_attribute(self, attribute):
""" """
self.attribute = attribute
def set_value(self):
""" """
self.cb_disconnect()
try:
val = getattr(self.o, self.attribute)
except Exception:
val = 0
self.setValue(val)
self.cb_connect()
def cb_spinbox(self):
""" """
val = self.value()
self.o.put_value(self.attribute, val)
class PyCutCheckBox(QtWidgets.QCheckBox):
""" """
def __init__(self, parent):
""" """
QtWidgets.QCheckBox.__init__(self, parent)
self.o = None
self.attribute = ""
self.stateChanged.connect(self.cb_checkbox)
def cb_disconnect(self):
""" """
self.stateChanged.disconnect(self.cb_checkbox)
def cb_connect(self):
""" """
self.stateChanged.connect(self.cb_checkbox)
def assign_object(self, o):
""" """
self.o = o
def assign_object_attribute(self, attribute):
""" """
self.attribute = attribute
def set_value(self):
""" """
self.cb_disconnect()
uival = {True: QtCore.Qt.Checked, False: QtCore.Qt.Unchecked}[
getattr(self.o, self.attribute)
]
self.setCheckState(uival)
self.cb_connect()
def cb_checkbox(self, index):
""" """
val = {QtCore.Qt.Checked: True, QtCore.Qt.Unchecked: False}[self.checkState()]
self.o.put_value(self.attribute, val)
class PyCutComboBox(QtWidgets.QComboBox):
""" """
def __init__(self, parent, items):
""" """
QtWidgets.QComboBox.__init__(self, parent)
self.o = None
self.attribute = ""
self.items = items
self.currentIndexChanged.connect(self.cb_combobox)
def cb_disconnect(self):
""" """
self.currentIndexChanged.disconnect(self.cb_combobox)
def cb_connect(self):
""" """
self.currentIndexChanged.connect(self.cb_combobox)
def assign_object(self, o):
""" """
self.o = o
def assign_object_attribute(self, attribute):
""" """
self.attribute = attribute
def fill_control(self):
""" """
self.cb_disconnect()
self.clear()
self.addItems(self.items)
self.cb_connect()
def set_value(self):
""" """
self.cb_disconnect()
# values can be strings or integers or of any other type -> use only strings in control
val = str(getattr(self.o, self.attribute))
# basic check
if val not in self.items:
idx = 0 # to avoid an exception
else:
idx = self.items.index(val)
self.setCurrentIndex(idx)
self.cb_connect()
def cb_combobox(self, index):
""" """
val = self.itemText(index)
self.o.put_value(self.attribute, val)
class PyCutDoubleSpinBoxDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent):
super().__init__(parent)
self.xeditors = {}
def createEditor(
self, parent, option, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex
):
editor = PyCutDoubleSpinBox(parent)
model = cast(PyCutSimpleTableModel, index.model())
op = model.get_operation(index)
attr = model.get_operation_attr(index)
editor.assign_object(op)
editor.assign_object_attribute(attr)
# to flush an "setModelData" in place - it works!
editor.valueChanged.connect(self.onEditorValueChanged)
self.xeditors[(index.row(), index.column())] = editor
return editor
def onEditorValueChanged(self):
editor = self.sender()
if editor:
self.commitData.emit(editor)
def setEditorData(
self,
spinBox: PyCutDoubleSpinBox, # type: ignore [override]
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
spinBox.set_value()
def setModelData(
self,
spinBox: PyCutDoubleSpinBox, # type: ignore [override]
model,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
model.handleNewvalue(index, spinBox.value())
def updateEditorGeometry(
self,
editor: PyCutDoubleSpinBox, # type: ignore [override]
option,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
editor.setGeometry(option.rect)
class PyCutCheckBoxDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(
self, parent, option, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex
):
editor = PyCutCheckBox(parent)
model = cast(PyCutSimpleTableModel, index.model())
op = model.get_operation(index)
attr = model.get_operation_attr(index)
editor.assign_object(op)
editor.assign_object_attribute(attr)
# return editor # -> ugly checkbox on the left of the cell
# -> for checkboxes to be centered: embed into a widget
checkWidget = QtWidgets.QWidget(parent)
checkLayout = QtWidgets.QHBoxLayout(checkWidget)
checkLayout.addWidget(editor)
checkLayout.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
checkLayout.setContentsMargins(0, 0, 0, 0)
# to flush an "setModelData" in place - it works!
editor.stateChanged.connect(self.onEditorStateChanged)
return checkWidget
def onEditorStateChanged(self):
editor = self.sender()
if editor:
checkWidget = editor.parent()
self.commitData.emit(checkWidget)
def setEditorData(
self,
checkWidget: QtWidgets.QWidget,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
checkBoxItem = checkWidget.layout().itemAt(0)
checkBox = cast(PyCutCheckBox, checkBoxItem.widget())
checkBox.set_value()
def setModelData(
self,
checkWidget: QtWidgets.QWidget,
model,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
checkBoxItem = checkWidget.layout().itemAt(0)
checkBox = cast(PyCutCheckBox, checkBoxItem.widget())
model.handleNewvalue(index, checkBox.isChecked())
def updateEditorGeometry(
self, editor, option, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex
):
editor.setGeometry(option.rect)
class PyCutComboBoxDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent):
super().__init__(parent)
self.xeditors = {}
def createEditor(
self, parent, option, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex
):
col = index.column()
self.items = []
if col == 1:
self.items = [
"Pocket",
"Inside",
"Outside",
"Engrave",
"Drill",
"Peck",
"Helix",
]
if col == 4:
self.items = ["inch", "mm"]
if col == 7:
self.items = ["Union", "Intersection", "Difference", "Xor"]
if col == 8:
self.items = ["Conventional", "Climb"]
editor = PyCutComboBox(parent, self.items)
model = cast(PyCutSimpleTableModel, index.model())
op = model.get_operation(index)
attr = model.get_operation_attr(index)
editor.assign_object(op)
editor.assign_object_attribute(attr)
editor.fill_control()
# to flush a "setModelData" in place - it works! but model still has old value -
editor.currentIndexChanged.connect(self.onEditorCurrentIndexChanged)
self.xeditors[(index.row(), index.column())] = editor
return editor
def onEditorCurrentIndexChanged(self, idx):
editor = self.sender()
if editor:
self.commitData.emit(editor)
def setEditorData(
self,
comboBox: PyCutComboBox, # type: ignore [override]
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
comboBox.set_value()
def setModelData(
self,
comboBox: PyCutComboBox, # type: ignore [override]
model,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
model.handleNewvalue(index, comboBox.currentText())
return
def updateEditorGeometry(
self,
comboBox: PyCutComboBox, # type: ignore [override]
option,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
):
comboBox.setGeometry(option.rect)
class PyCutOperationsTableViewManager(QtWidgets.QWidget):
def __init__(self, parent):
""" """
super().__init__(parent)
self.mainwindow = None
self.svg_viewer: SvgViewer = None
self.model = None
# main section of the window
vbox = self.vbox = QtWidgets.QVBoxLayout()
vbox.setContentsMargins(0, 0, 0, 0)
# let's add two views of the same data source we just created:
self.table = PyCutSimpleTableView(self)
# bottom section of the window:
# let's have a text input and a pushbutton that add an item to our model.
hbox_add = QtWidgets.QHBoxLayout()
# create the button, and hook it up to the slot below.
self._button_add = QtWidgets.QPushButton("Create Operation")
self._button_add.clicked.connect(self.add_item)
self._button_add.setIcon(
QtGui.QIcon(":/images/tango/22x22/categories/applications-system.png")
)
hbox_add.addWidget(self._button_add)
hbox_gen = QtWidgets.QHBoxLayout()
self._button_gen = QtWidgets.QPushButton("Generate GCode")
self._button_gen.clicked.connect(self.gen_gcode)
self._button_gen.setIcon(QtGui.QIcon(":/images/milling-machine-op.png"))
hbox_gen.addWidget(self._button_gen)
# add bottom to main window layout
vbox.addLayout(hbox_add)
vbox.addLayout(hbox_gen)
vbox.addWidget(self.table)
vbox.setStretch(2, 1)
# set layout on the window
self.setLayout(vbox)
def set_svg_viewer(self, svg_viewer: SvgViewer):
""" """
self.svg_viewer = svg_viewer
self.mainwindow = svg_viewer.mainwindow
def set_operations(self, operations: List[Dict[str, str]]):
""" """
cnc_ops = []
for op in operations:
cnc_op = OpItem(op)
cnc_ops.append(cnc_op)
self.model = PyCutSimpleTableModel(cnc_ops, self.mainwindow)
# so that the model known the view
self.model.set_view(self.table)
self.table.setModel(self.model)
self.table.setup()
self.vbox.addWidget(self.table)
def get_operations(self) -> List[Dict]:
"""
returns the list of operations ready to br saved as json data
"""
ops = []
for operation in self.get_model_operations():
op = operation.to_dict()
ops.append(op)
return ops
def set_model(self, model):
""" """
self.table.setModel(model)
def get_model(self) -> "PyCutSimpleTableModel":
""" """
return self.table.model()
def get_model_operations(self) -> List[OpItem]:
""" """
return self.get_model().operations
def add_item(self):
"""
instruct the model to add an item
"""
paths = self.svg_viewer.get_selected_items_ids()
self.table.add_item({"paths": paths})
print("ADD")
for op in self.get_model_operations():
print(op)
def gen_gcode(self):
""" """
# instruct the model to generate the g-code for all selected items
self.model.generate_gcode()
class PyCutSimpleTableView(QtWidgets.QTableView):
""" """
def __init__(self, parent: PyCutOperationsTableViewManager):
""" """
super().__init__(parent)
self.manager = parent
# Fixes the width of columns and the height of rows.
try:
# self.horizontalHeader().setResizeMode(QtWidgets.QHeaderView.Fixed)
# self.verticalHeader().setResizeMode(QtWidgets.QHeaderView.Fixed)
pass
except Exception:
pass # PySide
self.setAlternatingRowColors(True)
self.resizeColumnsToContents()
self.setMinimumWidth(800)
def setup(self):
"""
self.header = [
"name", # [0] str
"cam_op",
"enabled", # [2] checkbox
"paths", # [3] str
"units",
"cut_depth", # [5] float
"ramp_plunge", # [6] checkbox
"combinaison",
"direction",
"margin", # [9] float
"width", # [10] float
"del", # [11] button
"up", # [12] button
"down", # [13] button
]
"""
self.delegate_col_op = delegate = PyCutComboBoxDelegate(self)
self.setItemDelegateForColumn(1, delegate)
delegate = PyCutCheckBoxDelegate(self)
self.setItemDelegateForColumn(2, delegate)
delegate = PyCutComboBoxDelegate(self)
self.setItemDelegateForColumn(4, delegate)
delegate = PyCutDoubleSpinBoxDelegate(self)
self.setItemDelegateForColumn(5, delegate)
delegate = PyCutCheckBoxDelegate(self)
self.setItemDelegateForColumn(6, delegate)
delegate = PyCutComboBoxDelegate(self)
self.setItemDelegateForColumn(7, delegate)
delegate = PyCutComboBoxDelegate(self)
self.setItemDelegateForColumn(8, delegate)
self.delegate_col_margin = delegate = PyCutDoubleSpinBoxDelegate(self)
self.setItemDelegateForColumn(9, delegate)
self.delegate_col_width = delegate = PyCutDoubleSpinBoxDelegate(self)
self.setItemDelegateForColumn(10, delegate)
self.setup_persistent_editors()
def setup_persistent_editors(self):
""" """
# Make the combo boxes / check boxes / others specials always displayed.
for k in range(self.model().rowCount(None)):
self.openPersistentEditor(self.model().index(k, 1)) # cam_op
self.openPersistentEditor(self.model().index(k, 2)) # enabled
# self.openPersistentEditor(self.model().index(k, 3)) # paths
self.openPersistentEditor(self.model().index(k, 4)) # units
self.openPersistentEditor(self.model().index(k, 5)) # cut_depth
self.openPersistentEditor(self.model().index(k, 6)) # ramp
self.openPersistentEditor(self.model().index(k, 7)) # combinaison
self.openPersistentEditor(self.model().index(k, 8)) # direction
self.openPersistentEditor(self.model().index(k, 9)) # margin
self.openPersistentEditor(self.model().index(k, 10)) # width
for row in range(self.model().rowCount(None)):
btn_del_op = QtWidgets.QPushButton()
btn_del_op.setText("")
btn_del_op.setIcon(
QtGui.QIcon(":/images/tango/22x22/actions/edit-clear.png")
)
btn_del_op.setToolTip("Delete Op")
btn_del_op.clicked.connect(self.cb_delete_op)
self.setIndexWidget(self.model().index(row, 11), btn_del_op)
btn_up_op = QtWidgets.QPushButton()
btn_up_op.setText("")
btn_up_op.setIcon(QtGui.QIcon(":/images/tango/22x22/actions/go-up.png"))
btn_up_op.setToolTip("Up")
btn_up_op.clicked.connect(self.cb_move_up_op)
self.setIndexWidget(self.model().index(row, 12), btn_up_op)
btn_dw_op = QtWidgets.QPushButton()
btn_dw_op.setText("")
btn_dw_op.setIcon(QtGui.QIcon(":/images/tango/22x22/actions/go-down.png"))
btn_dw_op.setToolTip("Down")
btn_dw_op.clicked.connect(self.cb_move_down_op)
self.setIndexWidget(self.model().index(row, 13), btn_dw_op)
self.resizeColumnsToContents() # now!
self.setColumnWidth(3, 90) # paths
self.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
self.enable_disable_cells()
self.enable_disable_drill_and_peck_ops()
def cb_delete_op(self):
index = self.currentIndex()
idx = index.row()
# instruct the model to del an item
self.model().del_item(idx)
print("DEL")
for op in self.model().operations:
print(op)
def cb_move_down_op(self):
index = self.currentIndex()
idx = index.row()
if idx < self.model().rowCount(None) - 1:
self.model().swap_items(idx, idx + 1)
# to be sure to update the table... and all its delegates
self.setup_persistent_editors()
print("DOWN")
for op in self.model().operations:
print(op)
def cb_move_up_op(self):
index = self.currentIndex()
idx = index.row()
if idx > 0:
self.model().swap_items(idx, idx - 1)
# to be sure to update the table... and all its delegates
self.setup_persistent_editors()
print("UP")
for op in self.model().operations:
print(op)
def add_item(self, op_data):
self.model().add_item(op_data)
self.setup_persistent_editors() # to show the editors on a new item
def enable_disable_cells(self):
margin = {
"Pocket": True,
"Inside": True,
"Outside": True,
"Engrave": False,
"Drill": False,
"Peck": False,
"Helix": False,
}
width = {
"Pocket": False,
"Inside": True,
"Outside": True,
"Engrave": False,
"Drill": False,
"Peck": False,
"Helix": True,
}
for row in range(self.model().rowCount(None)):
op = self.model().operations[row]
cam_op = op.cam_op
self.delegate_col_margin.xeditors[(row, 9)].setEnabled(margin[cam_op])
self.delegate_col_width.xeditors[(row, 10)].setEnabled(width[cam_op])
# HOW to do that : "Drill" and "Peck" and "Helix" only for "circle" shapes
# HOW to do that : "Pocket" not for "line" and "polyline" shapes
def enable_disable_drill_and_peck_ops(self):
""" """
for row in range(self.model().rowCount(None)):
op = self.model().operations[row]
paths = op.paths
for path_id in paths:
if not path_id in self.manager.svg_viewer.svg_shapes:
continue
tag = self.manager.svg_viewer.svg_shapes[path_id].shape_tag
comboxbox = self.delegate_col_op.xeditors[(row, 1)]
model = comboxbox.model()
item_pocket = model.item(0)
# item_inside = model.item(1)
# item_outside = model.item(2)
# item_engrave = model.item(3)
item_drill = model.item(4)
item_peck = model.item(5)
item_helix = model.item(6)
if tag == "circle":
item_drill.setEnabled(True)
item_peck.setEnabled(True)
else:
item_drill.setEnabled(False)
item_peck.setEnabled(False)
if (
tag == "circle"
or tag == "ellipse"
or tag == "rect"
or tag == "polygon"
or (tag == "path" and True) # FIXME "is closed"
):
item_helix.setEnabled(True)
else:
item_helix.setEnabled(False)
if tag == "line" or tag == "polyline":
item_pocket.setEnabled(False)
else:
item_pocket.setEnabled(True)
class PyCutSimpleTableModel(QtCore.QAbstractTableModel):
"""
model for the table view
"""
def __init__(self, operations: List[OpItem], mainwindow):
super().__init__()
self.operations = operations
self.mainwindow = mainwindow
self.view: PyCutSimpleTableView | None = None
self.header = [
"name", # [0] str
"cam_op",
"enabled", # [2] checkbox
"paths", # [3] str
"units",
"cut_depth", # [5] float
"ramp_plunge", # [6] checkbox
"combinaison",
"direction",
"margin", # [9] float
"width", # [10] float
"del", # [11] button
"up", # [12] button
"down", # [13] button
]
self.cnt = 0
def set_view(self, view: PyCutSimpleTableView):
""" """
self.view = view
def generate_gcode(self):
""" """
self.mainwindow.cb_generate_gcode()
def handleNewvalue(self, index: QtCore.QModelIndex, value: Any):
row = index.row()
col = index.column()
attrib = self.header[col]
# update pycut GUI
if attrib in [
"cam_op",
"enabled",
"paths",
"units",
"cut_depth",
"ramp_plunge",
"combinaison",
"margin",
"width",
]:
cnc_op = self.operations[row]
setattr(cnc_op, attrib, value)
self.mainwindow.display_cnc_ops_geometry(self.operations)
if self.view is not None:
self.view.enable_disable_cells()
self.view.enable_disable_drill_and_peck_ops()
def __str__(self):
self.cnt += 1
data = "--------------------------------\n"
for op in self.operations:
data += str(op) + "\n"
return data
def dump(self):
self.cnt += 1
print("--------------------------------", self.cnt)
for op in self.operations:
print(op)
def headerData(
self,
col: int,
orientation: QtCore.Qt.Orientation,
role: int = QtCore.Qt.ItemDataRole.EditRole,
):
if (
orientation == QtCore.Qt.Orientation.Horizontal
and role == QtCore.Qt.ItemDataRole.DisplayRole
):
return self.header[col]
return None
def rowCount(self, parent):
return len(self.operations)
def columnCount(self, parent):
return len(self.header)
def setData(
self,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
value,
role: int = QtCore.Qt.ItemDataRole.EditRole,
):
"""
for the cells without delegate
"""
op = self.get_operation(index)
attr = self.get_operation_attr(index)
if role == QtCore.Qt.ItemDataRole.EditRole:
if index.column() != 3:
setattr(op, attr, value)
else:
items = eval(value) # string to list
setattr(op, attr, items)
def data(
self,
index: QtCore.QModelIndex | QtCore.QPersistentModelIndex,
role: int = QtCore.Qt.ItemDataRole.EditRole,
):
op = self.get_operation(index)
attr = self.get_operation_attr(index)
# for check box, data is displayed in the "editor"
col = index.column()
if col == 11: # button
return None
if col == 12: # button
return None
if col == 13: # button
return None
# for checkboxes only
if col == 2: # checkbox
return None
if col == 6: # checkbox
return None
if role == QtCore.Qt.ItemDataRole.DisplayRole:
val = getattr(op, attr)
if col == 3: # make a string of the list
# list of svg paths ids
val = str(val)
return val
if role == QtCore.Qt.ItemDataRole.EditRole:
val = getattr(op, attr)
if col == 3:
# list of svg paths ids
val = str(val)
return val
# if role == QtCore.Qt.ToolTipRole:
# if col == 3:
# val = getattr(op, attr)
# return val
if role == QtCore.Qt.ItemDataRole.BackgroundRole:
# non-existant paths marked as red
paths = op.paths
for path_id in paths:
view = cast(PyCutSimpleTableView, self.view)
if not path_id in view.manager.svg_viewer.svg_shapes:
return QtGui.QBrush(QtCore.Qt.GlobalColor.red) # red background
return None
def flags(
self, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex
) -> QtCore.Qt.ItemFlag:
flags = super().flags(index)
flags |= QtCore.Qt.ItemFlag.ItemIsEditable
flags |= QtCore.Qt.ItemFlag.ItemIsSelectable
flags |= QtCore.Qt.ItemFlag.ItemIsEnabled
flags |= QtCore.Qt.ItemFlag.ItemIsDragEnabled
flags |= QtCore.Qt.ItemFlag.ItemIsDropEnabled
return flags
def add_item(self, op_data):
op = OpItem(op_data)
idx = len(self.operations)
self.beginInsertRows(QtCore.QModelIndex(), idx, idx)
self.operations.append(op)
self.endInsertRows()
def del_item(self, idx: int):
op = self.operations[idx]
self.beginRemoveRows(QtCore.QModelIndex(), idx, idx)
self.operations.remove(op)
self.endRemoveRows()
def swap_items(self, idx1: int, idx2: int):
self.beginResetModel()
self.operations[idx1], self.operations[idx2] = (
self.operations[idx2],
self.operations[idx1],
)
self.endResetModel()
def get_operation(self, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex):
return self.operations[index.row()]
def get_operation_attr(
self, index: QtCore.QModelIndex | QtCore.QPersistentModelIndex
):
return self.header[index.column()]