/
FieldTypeTableModel.py
72 lines (57 loc) · 2.4 KB
/
FieldTypeTableModel.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
from PyQt5.QtCore import QAbstractTableModel, Qt, QModelIndex
from urh.signalprocessing.ProtocoLabel import ProtocolLabel
from urh.signalprocessing.FieldType import FieldType
class FieldTypeTableModel(QAbstractTableModel):
header_labels = ["Caption", 'Function', "Default display type"]
def __init__(self, fieldtypes, parent=None):
"""
:type fieldtypes: list of FieldType
:param parent:
"""
self.field_types = fieldtypes
super().__init__(parent)
def update(self):
self.beginResetModel()
self.endResetModel()
def columnCount(self, parent=None, *args, **kwargs):
return len(self.header_labels)
def rowCount(self, parent=None, *args, **kwargs):
return len(self.field_types)
def headerData(self, section, orientation, role=Qt.DisplayRole):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.header_labels[section]
return super().headerData(section, orientation, role)
def data(self, index: QModelIndex, role=Qt.DisplayRole):
if not index.isValid():
return None
if role == Qt.DisplayRole or role == Qt.EditRole:
i = index.row()
j = index.column()
fieldtype = self.field_types[i]
if j == 0:
return fieldtype.caption
elif j == 1:
return fieldtype.function.name
elif j == 2:
return ProtocolLabel.DISPLAY_FORMATS[fieldtype.display_format_index]
def setData(self, index: QModelIndex, value, role=None):
if role == Qt.EditRole:
i, j = index.row(), index.column()
fieldtype = self.field_types[i]
try:
if j == 0:
present_captions = {ft.caption for ft in self.field_types}
if value not in present_captions:
fieldtype.caption = value
elif j == 1:
try:
fieldtype.function = FieldType.Function[value]
except KeyError:
return False
if j == 2:
fieldtype.display_format_index = int(value)
except ValueError:
return False
return True
def flags(self, index: QModelIndex):
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsEditable