-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass_table_widget_functions.py
1516 lines (1329 loc) · 59.3 KB
/
class_table_widget_functions.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
"""
Class to handle a QTableWidget Object
Programmed by F.Garcia
"""
import logging
import re
from PyQt5 import QtCore, QtWidgets
import class_check_restrictions
# set up logging to file - see previous section for more details
log = logging.getLogger("") # root logger
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s [%(levelname)s] (%(threadName)-10s) %(message)s", datefmt="%y-%m-%d %H:%M"
)
# define a Handler which writes INFO messages or higher to the sys.stderr
twconsole = logging.StreamHandler()
twconsole.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter("[%(levelname)s] (%(threadName)-10s) %(message)s")
# tell the handler to use this format
twconsole.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger("").addHandler(twconsole)
logging.getLogger("").propagate = False
class TableWidgetFunctions(QtWidgets.QWidget):
"""
Class to handle a QTableWidget Object
Receives:
data_struct: dictionary of with structure:
Basic data_struct is a Show_Struct:
Show_Struct= {
"row1":{"Col1":ValueCol1,"Col2":ValueCol2},
"row2":{"Col1":ValueCol1,"Col3":ValueCol3}, # No need to have all Columns
....
}
data_struct_mask must have same structure as the Show_struct,
It contains the restrictions of each column or item.
Notes:
- row,col names/keys must match to apply restriction
- if no mask no restriction.
data_struct_mask:{ # Same as show_Struct
"__any__":{ #For all rows
"Col1":{
'__m__':"desired_restriction1",'__mv__':restriction_value1,...
,'__m__N':"desired_restrictionN",'__mv__N':restriction_valueN}
}
"Col2":{
'__m__':"desired_restriction1",'__mv__':restriction_value1,...
,'__m__N':"desired_restrictionN",'__mv__N':restriction_valueN}
}
...
}
"row3": :{ #For row 3 only
"Col3":{
'__m__':"desired_restriction1",'__mv__':restriction_value1,...
,'__m__N':"desired_restrictionN",'__mv__N':restriction_valueN}
}
...
}
}
If the show_struct is contained in a data_struct, pass the reference_track
data_struct={
Item1:{
"Whatever":"",
...
"Data_you_want_to_show": Show_Struct
}
...
}
reference_track references the data_struct to the place you want to show the info and allows to
read/write values in the correspondent part of the structure, but just showing the
Data_you_want_to_show in the TableWidget
reference_track=["Item1" "Data_you_want_to_show"]
data_id=Is the root item of your data_struct. This is not used in dictionary structures.
If data_struct is a list like ->
[data_struct1,data_struct2...]
Each data_structX must contain an unique 'ID' key.
data_struct1={
'ID': "Id1"
Item1:{
"Whatever":"",
...
"Data_you_want_to_show": Show_Struct
}
...
Then
data_id= "Id1" -> For getting only information on a specific 'ID' for list structure only.
and
reference_track=["Id1" "Item1" "Data_you_want_to_show"]
"""
signal_data_change = QtCore.pyqtSignal(list, str, str, str)
signal_item_button_clicked = QtCore.pyqtSignal(list)
signal_item_button_right_clicked = QtCore.pyqtSignal(list,QtCore.QPoint)
signal_item_combobox_currentindexchanged = QtCore.pyqtSignal(int, str, list)
item_doubleclicked = QtCore.pyqtSignal(list)
signal_item_checkbox_checked = QtCore.pyqtSignal(bool, list)
def __init__(
self,
tablewidgetobj: QtWidgets.QTableWidget,
data_struct: any,
data_struct_mask: dict,
data_id: str = None,
reference_track: list[str] = None,
/,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.__name__ = "tableWidget Functions"
self.tablewidgetobj = tablewidgetobj
self.data_struct = data_struct # all info
self.data_struct_mask = data_struct_mask
self._last_value_selected = None
self.data_id = data_id
if reference_track:
self.reference_track = reference_track
else:
self.reference_track = []
# Set restriction checker
self.check_restrictions = class_check_restrictions.CheckRestrictions()
# displayed on tableWidget
self.show_dict = {}
self.set_show_dict()
# uses show dict
self.modelobj = self.create_data_model_tablewidget(self.tablewidgetobj, False)
self.data_struct_types = self.get_types_struct(self.data_struct)
self.show_dict_types = self.get_types_struct(self.show_dict)
self.set_items_icons()
self.set_items_background_colors()
self.set_items_tooltips()
# initialize list of registered widgets
self.widget_registered_list=[]
self.set_items_widgets()
self.set_items_rolevalues()
self.restore_column_list = []
self.restore_key_list = []
self.resizetocontents = True
# print(self.show_dict_types)
self.refresh_tablewidget(self.show_dict, self.modelobj, self.tablewidgetobj)
# connect action
self.tablewidgetobj.clicked.connect(self._tablewidget_onclick)
#Install event filter for right click
self.tablewidgetobj.viewport().installEventFilter(self)
def eventFilter(self, source, event):
"""Emits signal for right button clicked of item
Args:
source (QWidget): widget where the event is comming
event (QtCore.QEvent): event to be filtered
Returns:
Event call: overwrites event in widget
"""
if(event.type() == QtCore.QEvent.MouseButtonPress and
event.buttons() == QtCore.Qt.RightButton and
source is self.tablewidgetobj.viewport()):
item = self.tablewidgetobj.itemAt(event.pos())
#print('Right click Global Pos:', event.globalPos())
if item is not None:
# print('Right click Table Item:', item.row(), item.column())
track=self.get_track_of_item_in_table(item)
self.signal_item_button_right_clicked.emit(track,event.globalPos())
return super().eventFilter(source, event)
def _data_change(self, track: list[str], val: any, valtype: str, subtype: str):
"""Emits event data has changed and new value to parent
Args:
track (list[str]): Tracking list with path to follow in dictionary structure
val (any): value on item
valtype (str): string with value type
subtype (str): if list inner value type
"""
self.signal_data_change.emit(track, val, valtype, subtype)
def _doubleclick_on_item(self, itm: QtWidgets.QTableWidgetItem):
"""Emits doubleclick event on item, passes track list of item
Args:
itm (QtWidgets.QTableWidgetItem): item being doubleclicked
"""
track = self.get_track_of_item_in_table(itm)
self.item_doubleclicked.emit(track)
def get_standard_dict_model(self, fieldname: str = "Datafield_1") -> dict:
"""Make a typical show dictionay
Args:
fieldname (str, optional): First row content value. Defaults to 'Datafield_1'.
Returns:
dict: Contains 'Value','Units','Info','Type' example columns
"""
return {fieldname: {"Value": "", "Units": "", "Info": "", "Type": str(type(""))}}
def _set_restore_columns(self, colkey: QtWidgets.QTableWidgetItem):
"""Sets dictionaries for restoring a value
Args:
colkey (QtWidgets.QTableWidgetItem): item in table
"""
for iii, ti in enumerate(self.table_items):
if colkey == ti:
self.restore_key_list.append(colkey)
self.restore_column_list.append(iii)
def set_show_dict(self):
"""
Sets show_dict in accordance to the reference path and data struct.
"""
if len(self.reference_track) == 0:
showdict = self.get_dictionary_from_structlist(self.data_struct, self.data_id)
# print('From len 0 showdict:\n',showdict)
else:
showdict = self.get_tracked_value_in_struct(self.reference_track, self.data_struct)
# print('From else showdict:\n',showdict)
if isinstance(showdict, dict):
self.show_dict = showdict
else:
self.show_dict = self.data_struct
def get_types_struct(self, dict_struct):
"""Generates a copy of the structure (list or dictionary)
replacing values with a string of the value type
Args:
dict_struct (any): struct
Returns:
any: struct with types
"""
if isinstance(dict_struct, list):
type_struct = []
for a_data in dict_struct:
type_struct.append(self.get_types_struct(a_data))
elif isinstance(dict_struct, dict):
type_struct = {}
for a_data in dict_struct:
if isinstance(dict_struct[a_data], dict):
nts = self.get_types_struct(dict_struct[a_data])
type_struct.update({a_data: nts})
else:
type_struct.update({a_data: str(type(dict_struct[a_data]))})
else:
type_struct = {}
type_struct.update({str(dict_struct): str(type(dict_struct))})
return type_struct
def get_gentrack_from_localtrack(self, track: list) -> list:
"""Adds refence track to a local track
Args:
track (list): local track (show_dict)
Returns:
list: general track (data_struct) using reference_track
"""
gentrack = self.reference_track.copy()
for iii in track:
gentrack.append(iii)
return gentrack
def get_localtrack_from_gentrack(self, track: list) -> list:
"""Gets local track (show_dict) of a general track (data_struct) using reference_track
Args:
track (list): Track of Struct dict
Returns:
list: returns local track show_dict removing reference_track path
"""
gentrack = self.reference_track.copy()
trackc = track.copy()
for iii in track:
if track[iii] == gentrack[iii]:
trackc.pop(0)
return trackc
def get_dictionary_from_structlist(self, data_struct: any, data_id=None) -> dict:
"""get the struct in dictionary form
Args:
data_struct (any): Data can be a list of dictionaries with 'ID' keys. Or dictionary form.
data_id (_type_, optional): For getting only information on a specific 'ID' for list
structure only. Defaults to None.
Returns:
dict: _description_
"""
if isinstance(data_struct, list):
for adict in data_struct:
try:
if adict["ID"] == data_id:
# print('get my dict Found ID',data_id)
return adict.copy()
except KeyError:
break
elif isinstance(data_struct, dict) or data_id is None:
# print('get my dict nochange is dict',data_id)
return data_struct.copy()
return {}
def refresh_tablewidget(
self, data_dict: any, modelobj: QtCore.QAbstractItemModel, tablewidgetobj: QtWidgets.QTableWidget
):
"""Refresh the Table Widget
Before calling: set property resizetocontents (bool) to resize or not to contents
Args:
data_dict (any): Data to be shown in Table widget
modelobj (QtCore.QAbstractItemModel): model object (reflects the table structure)
tablewidgetobj (QtWidgets.QTableWidget): pointer to object
"""
self.set_show_dict()
tablewidgetobj.setRowCount(0)
modelobj = self.create_data_model_tablewidget(tablewidgetobj, False)
self.import_data_to_tablewidget(data_dict, modelobj, tablewidgetobj)
if self.resizetocontents:
tablewidgetobj.resizeColumnsToContents()
# self.set_tableWidget_styles(tablewidgetobj.model())
def _set_item_style(self, itm: QtWidgets.QTableWidgetItem):
"""Sets icon, background color and rolevalue to the item
Args:
itm (QtWidgets.QTableWidgetItem): item in table
"""
self._set_icon_to_item(itm)
self._set_backgroundcolor_to_item(itm)
self._set_rolevalue_to_item(itm)
def set_items_tooltips(self, tooltipdict: dict = {"track_list": [], "tooltip_list": []}) -> None:
"""Sets dictionary for tooltips of each item
Args:
tooltipdict (dict, optional): Lists must be of the same lengths. Dict must contain:
"track_list": [TrackListItem1, ... , TrackListItemN]
"tooltip_list": [TooltiptextItem1, ... , TooltiptextItemN]
Defaults to {"track_list": [], "tooltip_list": []}.
"""
self.tooltip_dict = tooltipdict
def set_items_icons(self, icondict: dict = {"track_list": [], "icon_list": []}) -> None:
"""Sets dictionary for icons of each item
Args:
icondict (_type_, optional):(dict, optional): Lists must be of the same lengths. Dict must contain:
"track_list": [TrackListItem1, ... , TrackListItemN]
"icon_list": [IconObject1, ... , IconObjectN]
IconObject is a QIcon object
Defaults to {"track_list": [], "icon_list": []}.
"""
self.icon_dict = icondict
def set_items_background_colors(self, backgroundcolor_dict: dict = {"track_list": [], "color_list": []}) -> None:
"""Sets dictionary for background colors of each item
Args:
backgroundcolor_dict (_type_, optional): Lists must be of the same lengths. Dict must contain:
"track_list": [TrackListItem1, ... , TrackListItemN].
"color_list": [ColorObject1, ... , ColorObjectN]
Color Object can be QBrush, QColor, GlobalColor or QGradient
Defaults to {"track_list": [], "color_list": []}.
"""
self.backgroundcolor_dict = backgroundcolor_dict
def set_items_widgets(self, itemwidget_dict: dict = {"track_list": [], "widget_list": []}) -> None:
"""Sets dictionary for widgets of each item
Args:
itemwidget_dict (_type_, optional): Lists must be of the same lengths. Dict must contain:
"track_list": [TrackListItem1, ... , TrackListItemN].
"widget_list": [WidgetObject1, ... , WidgetObjectN]
WidgetObject should be QtWidgets.QPushButton, QComboBox , QCheckBox, or QLabel objects
Defaults to {"track_list": [], "widget_list": []}.
"""
self.itemwidget_dict = itemwidget_dict
def set_items_rolevalues(
self, rolevalue_dict: dict = {"track_list": [], "role_list": [], "value_list": []}
) -> None:
"""Sets dictionary for role values of each item
see: https://doc.qt.io/archives/qt-4.8/qt.html#ItemDataRole-enum
Args:
rolevalue_dict (_type_, optional): Lists must be of the same lengths. Dict must contain:
"track_list": [TrackListItem1, ... , TrackListItemN].
"role_list": [Role1, ... , RoleN]
"value_list": [RoleValue1, ... , RoleValue1N]
Defaults to {"track_list": [], "role_list": [], "value_list": []}.
"""
self.itemrolevalue_dict = rolevalue_dict
def _is_same_list(self, list1: list, list2: list) -> bool:
"""Compares two lists
Args:
list1 (list): list1
list2 (list): list2
Returns:
bool: True if the same,False if different
"""
if len(list1) != len(list2):
return False
for iii, jjj in zip(list1, list2):
if iii != jjj:
return False
return True
def _item_button_clicked(self, track: list):
"""Emits signal for button clicked of item
Args:
track (list): track list of item clicked
"""
# print('entered click {}'.format(track))
self.signal_item_button_clicked.emit(track)
def _item_combobox_indexchanged(self, cbw: QtWidgets.QComboBox, track: list = []):
"""Emits signal for item combobox widget
Args:
cbw (QtWidgets.QComboBox): combobox widget
track (list, optional): track list of item . Defaults to [].
"""
currenttxt = cbw.currentText()
index = cbw.findText(currenttxt, QtCore.Qt.MatchFixedString)
self.signal_item_combobox_currentindexchanged.emit(index, currenttxt, track)
def _item_checkbox_checked(self, chb: QtWidgets.QCheckBox, track: list = []):
"""Emits signal for item checkbox
Args:
chb (QtWidgets.QCheckBox): checkbox widget
track (list, optional): track list of item. Defaults to [].
"""
currentstate = chb.isChecked()
self.signal_item_checkbox_checked.emit(currentstate, track)
def _is_registered_widget_track(self,track:list)->bool:
"""checks if track has been registered
Args:
track (list): track of item
Returns:
bool: if is on registered list or not
"""
for tr in self.widget_registered_list:
if self._is_same_list(track, tr):
return True
return False
def _remove_registered_widget_track_from_list(self,track):
"""removes track if has been registered
Args:
track (list): track of item
"""
if self._is_registered_widget_track(track):
new_reg_list=[]
for tr in self.widget_registered_list:
if not self._is_same_list(track, tr):
new_reg_list.append(tr)
self.widget_registered_list = new_reg_list
def _add_registered_widget_track_to_list(self,track):
"""adds track if has been registered
Args:
track (list): track of item
"""
if not self._is_registered_widget_track(track):
self.widget_registered_list.append(track)
def _remove_non_active_widgets_from_register(self):
"""Deletes the non existing widgets from track list
"""
track_list = self.itemwidget_dict["track_list"]
reg_list =self.widget_registered_list.copy()
for tr in reg_list:
in_track_list = False
for track in track_list:
if self._is_same_list(track, tr):
in_track_list = True
break
if not in_track_list:
self._remove_registered_widget_track_from_list(tr)
def _set_widget_to_item(self, itm: QtWidgets.QTableWidgetItem):
"""Sets a widget to the item and connects the functionality
works for QPushButton, QComboBox , QCheckBox, QLabel objects
Args:
itm (QtWidgets.QTableWidgetItem): Item to set the widget
"""
self._remove_non_active_widgets_from_register()
try:
track = self.get_track_of_item_in_table(itm)
track_list = self.itemwidget_dict["track_list"]
widget_list = self.itemwidget_dict["widget_list"]
for tr, iw in zip(track_list, widget_list):
if self._is_same_list(track, tr):
if not self._is_registered_widget_track(tr):
# Only set once the widget else will be deleted
if not isinstance(iw, QtWidgets.QComboBox):
self.tablewidgetobj.setCellWidget(itm.row(), itm.column(), iw)
self._add_registered_widget_track_to_list(tr)
# Delegated behavior
if isinstance(iw, QtWidgets.QComboBox):
delegate = Delegate(itm.row(), itm.column(), iw, parent=self.tablewidgetobj)
self.tablewidgetobj.setItemDelegateForColumn(itm.column(),delegate)
iw.currentIndexChanged.connect(lambda: self._item_combobox_indexchanged(iw,track))
itm.setFlags(itm.flags() ^ QtCore.Qt.ItemIsEditable)
if isinstance(iw, QtWidgets.QPushButton):
iw.clicked.connect(lambda: self._item_button_clicked(track))
elif isinstance(iw, QtWidgets.QCheckBox):
iw.stateChanged.connect(lambda: self._item_checkbox_checked(iw, track))
elif isinstance(iw, QtWidgets.QLabel):
# self.tablewidgetobj.itemDoubleClicked.connect(self._doubleclick_on_item)
if self.resizetocontents:
self.tablewidgetobj.resizeColumnToContents(itm.column())
self.tablewidgetobj.resizeRowToContents(itm.row())
break
except RuntimeError as err:
log.error("RuntimeError setting widget_list to item: %s",err)
except (AttributeError, TypeError) as err:
log.error("Setting widget_list to item: %s",err)
def _set_icon_to_item(self, itm: QtWidgets.QTableWidgetItem):
"""Sets icon in icon_dict to item
Args:
itm (QtWidgets.QTableWidgetItem): Item in Table
"""
try:
track = self.get_track_of_item_in_table(itm)
track_list = self.icon_dict["track_list"]
icon_list = self.icon_dict["icon_list"]
for tr, ic in zip(track_list, icon_list):
if self._is_same_list(track, tr):
itm.setIcon(ic)
except (AttributeError, TypeError):
log.error("Setting icon_list to item")
def _set_rolevalue_to_item(self, itm: QtWidgets.QTableWidgetItem):
"""Sets rolevalues in itemrolevalue_dict to item
Args:
itm (QtWidgets.QTableWidgetItem): Item in Table
"""
try:
track = self.get_track_of_item_in_table(itm)
track_list = self.itemrolevalue_dict["track_list"]
role_list = self.itemrolevalue_dict["role_list"]
value_list = self.itemrolevalue_dict["value_list"]
for tr, role, value in zip(track_list, role_list, value_list):
if self._is_same_list(track, tr):
itm.setData(role, value)
except (AttributeError, TypeError):
log.error("Setting rolevalue to item")
def _set_backgroundcolor_to_item(self, itm: QtWidgets.QTableWidgetItem):
"""Sets backgrounds in backgroundcolor_dict to item
Args:
itm (QtWidgets.QTableWidgetItem): Item in Table
"""
try:
track = self.get_track_of_item_in_table(itm)
track_list = self.backgroundcolor_dict["track_list"]
color_list = self.backgroundcolor_dict["color_list"]
for tr, ic in zip(track_list, color_list):
if self._is_same_list(track, tr):
itm.setBackground(ic)
except (AttributeError, TypeError):
log.error("Setting backgroundcolor to item")
def _set_tooltiptext_to_item(self, itm: QtWidgets.QTableWidgetItem):
"""Sets tooltiptext in tooltip_dict to item.
if limited_selection mask exists will write the selection options.
Selection options are overwritten with tooltip_dict.
Args:
itm (QtWidgets.QTableWidgetItem): Item in Table
"""
reslist, resvallist = self.get_item_restriction_resval(itm)
for res, resval in zip(reslist, resvallist):
if res in ["limited_selection", "is_list_item_limited_selection"]:
itm.setToolTip(f"Options: {resval}")
try:
track = self.get_track_of_item_in_table(itm)
track_list = self.tooltip_dict["track_list"]
tooltip_list = self.tooltip_dict["tooltip_list"]
for tr, itt in zip(track_list, tooltip_list):
if self._is_same_list(track, tr):
itm.setToolTip(itt)
except (AttributeError, TypeError):
log.error("Setting ToolTiptext to item")
def _tablewidget_onclick(self, index: QtCore.QModelIndex):
"""Onclick method on table widget restores or edits the item
Args:
index (QtCore.QModelIndex): Item being clicked
"""
mycol = index.column()
if self.resizetocontents:
self.tablewidgetobj.resizeColumnToContents(mycol)
itm = self.tablewidgetobj.itemFromIndex(index)
self._set_icon_to_item(itm)
self._set_widget_to_item(itm)
self._set_tooltiptext_to_item(index)
if mycol in self.restore_column_list:
self._restore_a_tablewidget_item(index)
else:
val_ = itm.text()
if self.check_restrictions.str_to_bool_or_none(val_) in [True, False]:
self._set_checkbox_value_to_item(itm)
self._edit_a_table_widget_item(index)
def _set_checkbox_value_to_item(self, valueitem: QtWidgets.QTableWidgetItem):
"""Sets checkstate on checkbox when item is a boolean
Args:
valueitem (QtWidgets.QTableWidgetItem): Item
"""
if self.check_restrictions.str_to_bool(valueitem.text()):
valueitem.setCheckState(True)
else:
valueitem.setCheckState(False)
def get_columnname_from_colpos(self, colpos: int) -> str:
"""Get an item from the column position
Args:
colpos (int): column position
Returns:
: item
"""
for iii in self.table_items:
cp = self.get_item_column_pos_in_table(iii)
if cp == colpos:
return iii
return None
def get_key_value_from_item(self, anitem: QtWidgets.QTableWidgetItem):
"""Gets the dictionary key for an item
Args:
anitem (QtWidgets.QTableWidgetItem): item in table
Returns:
_type_: key on dictionary related to the row the item is in
"""
myrow = anitem.row()
return self.get_key_value_from_row(myrow)
def get_key_value_from_row(self, myrow: int):
"""Gets the dictionary key located in the row
Args:
myrow (int): row number
Returns:
any: key on dictionary related to the row
"""
for iii, key in enumerate(self.show_dict):
if iii == myrow:
return key
return None
def get_track_of_item_in_table(self, anitem: QtWidgets.QTableWidgetItem) -> list:
"""Generates a track list of the item in the structure
Args:
anitem (QtWidgets.QTableWidgetItem): item object to be tracked
Returns:
list: track list of item
"""
track = self.reference_track.copy()
if isinstance(anitem, QtWidgets.QTableWidgetItem):
myrow = anitem.row()
mycol = anitem.column()
valkey = self.get_key_value_from_row(myrow)
track.append(valkey)
track.append(self.get_columnname_from_colpos(mycol))
return track
def set_value_and_trigger_data_change(self,track:list,value:any,the_type:str,subtype:str=""):
"""Check if value is conform. If conform then set it and refresh Table
Args:
track (list): Track list
value (any): Value (in the correct type)
the_type (str): type of the value as string
subtype (str, optional): if list, type type of the list items in list as string. Defaults to "".
"""
value = self.check_restrictions.set_type_to_value(value,the_type,subtype)
itm, itmindex, _, _ = self.get_item_from_track(track)
#print("Got: ",itm, itmindex, itmtrack, itmindextrack)
if itmindex:
if self.check_item_value_for_edit(itmindex,value,True):
self.set_tracked_value_to_dict(track,value,self.show_dict,subtype,True)
if the_type == str(bool):
self._set_checkbox_value_to_item(itm)
self.refresh_tablewidget(self.show_dict, self.modelobj, self.tablewidgetobj)
self._tablewidget_onclick(itmindex)
def _edit_a_table_widget_item(self, index: QtCore.QModelIndex):
"""Connects item to datachange fuction
Args:
index (QtCore.QModelIndex): item being edited
"""
# print('_edit_a_table_widget_item',index)
itm = self.tablewidgetobj.itemFromIndex(index)
val = itm.text()
# print('edit index set:',index.data())
self._last_value_selected = val
self.tablewidgetobj.itemChanged.connect(lambda: self._item_data_changed(index, val))
# def get_list_of_tracks_of_children(self, parenttrack):
# self.get_gentrack_from_localtrack
def get_item_from_track(self, track: list) -> None:
"""Get an item object and all related info
Args:
track (list): where is the item located track
Returns:
QtWidgets.QTableWidgetItem: Item
QtCore.QModelIndex: Item index
list: item track list
list: track list of indexes
"""
try:
itmtrack = []
itmindextrack = []
parent = None
modelobj=self.tablewidgetobj.model()
# Track is in tables [ rowname colname ]
if len(track) > 1:
col_pos = self.get_item_column_pos_in_table(track[1])
else:
col_pos = 0
for tr in track:
if parent is None:
log.debug('get_item_from_track if the size -> {}'.format(modelobj.rowCount()))
itm = None
for iii in range(modelobj.rowCount()):
itmindex = modelobj.index(iii, col_pos)
itm = self.tablewidgetobj.itemFromIndex(itmindex)
# itm = modelobj.itemFromIndex(itmindex)
if not self._is_item_text(itm, tr):
break # item None or found
# not found
if not self._is_item_not_text(itm, tr):
break # item none or not found
else:
log.info('get_item_from_track else the size -> {}'.format(modelobj.rowCount(parent)))
itm = None
for iii in range(modelobj.rowCount(parent)):
itmindex = modelobj.index(iii, col_pos, parent)
#itm = modelobj.itemFromIndex(itmindex)
itm = self.tablewidgetobj.itemFromIndex(itmindex)
if not self._is_item_text(itm, tr):
break # item None or found
# not found
if not self._is_item_not_text(itm, tr):
break # item none or not found
parent = itmindex
parentitm = self.tablewidgetobj.itemFromIndex(itmindex)
#parentitm = modelobj.itemFromIndex(parent)
if parentitm.text() == "ID":
# parenttxt = parentitm.text()
parent = None
continue
itmtrack.append(itm)
itmindextrack.append(itmindex)
return itm, itmindex, itmtrack, itmindextrack
except (ValueError, KeyError, TypeError, AttributeError) as e:
log.error("Get item from track: %s", e)
return None, None, None, None
def _is_item_not_text(self, itm, tr):
"""Helper function"""
try:
if tr != itm.text():
return False
return True
except (UnboundLocalError,AttributeError):
return False
def _is_item_text(self, itm, tr):
"""Helper function"""
try:
if tr == itm.text():
return False
return True
except (UnboundLocalError,AttributeError):
return False
def _item_data_changed(self, index: QtCore.QModelIndex, val: any):
"""If value has changed and item is editable, changes the value to new value
only when value check is conform to mask and type. If not, sets the old value.
Args:
index (QtCore.QModelIndex): index of item
val (any): new value to be set
"""
old_value = val # self._last_value_selected
itm = self.tablewidgetobj.itemFromIndex(index)
# when you click outside will be none
if not itm:
return
new_value = itm.text()
#print("item_data_changed")
# self._set_item_style(self.tablewidgetobj.item(itm.row(),icol)) # column item
if new_value != old_value and old_value is not None and index in self.tablewidgetobj.selectedIndexes():
# indextype=index.siblingAtColumn(tcol)
# typeitem=self.tablewidgetobj.itemFromIndex(indextype)
track = self.get_track_of_item_in_table(self.tablewidgetobj.itemFromIndex(index))
# Here check if value is ok if yes
valisok = self.check_item_value_for_edit(index, new_value)
log.info("Data changed -> New:%s Old:%s Track: %s isvalid: %s", new_value, old_value, track, valisok)
subtype = ""
if valisok:
thetype, subtype = self._get_item_supposed_type_subtype(itm)
new_valwt = self.check_restrictions.set_type_to_value(
new_value, thetype, subtype
) # Send value with correct type to dictionary
do_refresh_tablewidget, self.show_dict = self.set_tracked_value_to_dict(
track, new_valwt, self.show_dict, subtype
)
if not do_refresh_tablewidget:
itm.setText(new_value)
if thetype == str(bool):
self._set_checkbox_value_to_item(itm)
else: # need to refresh only if value is changed
self.refresh_tablewidget(self.show_dict, self.modelobj, self.tablewidgetobj)
# Here send signal data has changed
self._data_change(track, new_value, thetype, subtype)
else: # reset old value
thetype, subtype = self._get_item_supposed_type_subtype(itm)
typestr = thetype
if typestr == str(list):
gentrack = self.get_gentrack_from_localtrack(track) # <-track is local!
subtype = self._get_listitem_subtype(gentrack)
# Send value with correct type to dictionary
old_valwt = self.check_restrictions.set_type_to_value(old_value, thetype, subtype)
do_refresh_tablewidget, self.show_dict = self.set_tracked_value_to_dict(
track, old_valwt, self.show_dict, subtype
)
itm.setText(old_value)
if thetype == str(bool):
self._set_checkbox_value_to_item(itm)
self._last_value_selected = None
def _is_item_supposed_to_be_a_list(self, itm: QtWidgets.QTableWidgetItem) -> bool:
"""Responds if Item is supposed to be a list or not looking at the mask
Args:
itm (QtWidgets.QTableWidgetItem): Item
Returns:
bool: True if Item should be a List
"""
reslist, resvallist = self.get_item_restriction_resval(itm)
for res, resval in zip(reslist, resvallist):
if "is_list_item_" in res or (res == "is_value_type" and resval == str(list)):
return True
return False
def _horizontalheaderclicked(self, index):
"""
click test
"""
print("_horizontalheaderclicked", index)
def _verticalheaderclicked(self, index):
"""
click test
"""
print("_verticalheaderclicked", index)
def _get_item_supposed_type_subtype(self, itm: QtWidgets.QTableWidgetItem) -> tuple[str, str]:
"""Gets items type and subtype
Args:
itm (QtWidgets.QTableWidgetItem): item
Returns:
tuple[str,str]: type and subtype of item
"""
subtype = ""
thetype = str(str)
reslist, resvallist = self.get_item_restriction_resval(itm)
if not self._is_item_supposed_to_be_a_list(itm):
for res, resval in zip(reslist, resvallist):
if res == "is_value_type":
thetype = resval
break
else:
thetype = str(list)
for res, resval in zip(reslist, resvallist):
if res == "is_list_item_type":
subtype = resval
break
return thetype, subtype
def _get_listitem_subtype(self, track: list) -> str:
"""Finds subtype for item in track from mask
Args:
track (list): track of item
Returns:
str: subtype
"""
mask = self.get_mask_for_item(track)
# log.info('Got for subtype: {} {}'.format(track,mask))
for mmm in mask:
keymmm = str(mmm)
if "__m__" in keymmm:
keyval = keymmm.replace("__m__", "__mv__")
if mask[keymmm] == "is_list_item_type":
return mask[keyval]
return ""
def set_tracked_value_to_dict(
self, track: list, val: any, dict_struct: any, subtype: str, emitsignal: bool = True
) -> tuple[bool, any]:
"""Track in the dictionary the item and set the value
Args:
track (list): Track list for setting value
val (any): value to be set
dict_struct (any): the data struct
subtype (str): subtype of list values
emitsignal (bool, optional): Emit signal when value is set. Defaults to True.
Returns:
bool: data changed needs to refresh table widget
any: data struct with new data
"""
do_refresh_tablewidget = False
trlist = track.copy()
selected = {}
if isinstance(dict_struct, list):
for a_data in dict_struct:
if a_data["ID"] == trlist[0]:
trlist.pop(0)
selected = a_data # .copy() #select dictionary
selected, trlist = self._get_selected_tracklist_one_item(selected, trlist)
# last tracked is variable
if len(trlist) == 1:
selected.update({trlist[0]: val})
# Change title of Data special case
# log.debug('setvaltodict_struct Here {} set to {}'.format(trlist[0],val))
if trlist[0] == "ID" and len(track) == 2:
do_refresh_tablewidget = True
if emitsignal:
trackstruct = track
self._data_change(trackstruct, str(val), str(type(val)), subtype) # refresh on main
break
elif isinstance(dict_struct, dict):
selected = dict_struct # .copy() #select dictionary
selected, trlist = self._get_selected_tracklist_one_item(selected, trlist)
# last tracked is variable
#print("is dict instance")
if len(trlist) == 1:
selected.update({trlist[0]: val})
# log.debug('setvaltodict_dict Here {} set to {}'.format(trlist[0],val))
# update
trackstruct = track.copy()
#_, self.data_struct = self.set_tracked_value_to_dict(trackstruct, val, self.data_struct, subtype)
if emitsignal:
self._data_change(trackstruct, str(val), str(type(val)), subtype) # refresh on main
return do_refresh_tablewidget, dict_struct