-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_and_timers_model.py
More file actions
291 lines (258 loc) · 11.5 KB
/
device_and_timers_model.py
File metadata and controls
291 lines (258 loc) · 11.5 KB
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
from PySide6.QtCore import QAbstractTableModel, Qt, SIGNAL, Signal
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QStyledItemDelegate, QLineEdit, QComboBox
import value_function_classes
import data_types
class DevicesModel(QAbstractTableModel):
"""
[device name, variable path, timer, datatype, function, func_arg_1, ..., func_arg_5, writable, historize]
"""
def __init__(self, data):
super().__init__()
if data is None:
data = []
self.devices_data = data
self.timer_names = []
self.headers = ["Path", "Variable Name", "Datatype", "Timer", "Function", "Arg 1", "Arg 2", "Arg 3", "Arg 4",
"Arg 5"]
def data(self, index, role: int = ...):
if role == Qt.DisplayRole:
if index.column() == 2:
column_data = self.devices_data[index.row()][index.column()]
if isinstance(column_data, str):
return ''
return column_data.name
return self.devices_data[index.row()][index.column()]
elif role == Qt.ToolTipRole:
if index.column() == 4:
function_type = self.devices_data[index.row()][index.column()]
tool_tip = ''
if function_type == 'valuelist':
tool_tip = "Arg 1: Values to iterate through<br>" \
"Arg 2: Number of times to repeat through list.<br>" \
"Arg 3: Repeat forever (True/False)<br>" \
"Arg 4: Historize (not working)<br>" \
"Arg 5: Not used"
elif function_type == 'weightedlist':
tool_tip = "Arg 1: Values to iterate through [v_1, ..., v_n]<br>" \
"Arg 2: Weights [w_1, ...,w_n]<br>" \
"Arg 3: Number of times to call a random sample<br>" \
"Arg 4: Repeat forever (True/False)<br>" \
"Arg 5: Historize (not working)<br>" \
"Random sample is taken from w_1 copies of v_1, ..., w_n copes of v_n"
elif function_type == 'rampstep':
tool_tip = "Arg 1: Minimum y value<br>" \
"Arg 2: Maximum y value<br>" \
"Arg 3: Step added to y every evaluation call<br>" \
"Arg 4: Repeat forever (True/False)<br>" \
"Arg 5: Historize (not working)"
else:
tool_tip = "Arg 1: Minimum y value<br>" \
"Arg 2: Maximum y value<br>" \
"Arg 3: How long in seconds to repeat<br>" \
"Arg 4: Repeat forever (True/False)<br>" \
"Arg 5: Historize (not working)"
if 'square' in function_type:
tool_tip += "<br>Oscillates between min and max."
return tool_tip
def rowCount(self, index):
return len(self.devices_data)
def columnCount(self, index):
if len(self.devices_data) > 0:
return len(self.devices_data[0])
return 0
def update_timer_names(self, timer_names):
self.timer_names.extend(timer_names)
def flags(self, index):
if not index.isValid():
return 0
if index.column() in range(0, 11):
return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
else:
return Qt.ItemIsSelectable | Qt.ItemIsEnabled
def headerData(self, section: int, orientation, role: int = ...):
if role == Qt.TextAlignmentRole:
if orientation == Qt.Horizontal:
return int(Qt.AlignLeft|Qt.AlignCenter)
return int(Qt.AlignRight|Qt.AlignCenter)
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
if 0 <= section < len(self.headers):
return self.headers[section]
def setData(self, index, value, role: int = ...) -> bool:
if index.isValid() and 0 <= index.row() < len(self.devices_data):
if index.column() == 2:
self.devices_data[index.row()][index.column()] = data_types.data_types[value]
else:
self.devices_data[index.row()][index.column()] = value
return True
def add_device(self):
self.devices_data.append(['']*len(self.headers))
class DeviceDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(DeviceDelegate, self).__init__(parent)
# print(parent.timer_names)
self.timer_names = parent.timer_names
vf = value_function_classes.ValueFunction(None, None, None, None, None)
self.function_list = vf.functions()
def update_timer_names(self, timer_names):
self.timer_names = timer_names
def createEditor(self, parent, option, index):
if index.column() in [0, 1, 5, 6, 7, 8, 9]:
editor = QLineEdit(parent)
editor.setText("Timer Name")
return editor
elif index.column() == 2:
editor = QComboBox(parent)
editor.addItems(sorted([key for key in data_types.data_types]))
return editor
elif index.column() == 3:
editor = QComboBox(parent)
editor.addItems(sorted(self.timer_names))
return editor
elif index.column() == 4:
editor = QComboBox(parent)
editor.addItems(sorted(self.function_list))
return editor
def commit_and_close_editor(self):
editor = self.sender()
def setEditorData(self, editor, index):
text = index.model().data(index, Qt.DisplayRole)
if index.column() == 0:
editor.setText(text.replace('\\', '/'))
elif index.column() in [1, 5, 6, 7, 8, 9]:
editor.setText(text)
elif index.column() in [2, 3, 4]:
i = editor.findText(text)
if i == -1:
i = 0
editor.setCurrentIndex(i)
def setModelData(self, editor, model, index) -> None:
if index.column() == 0:
# Get that backslash nonsense out of here
model.setData(index, editor.text().replace('\\', '/'))
elif index.column() in [1, 5, 6, 7, 8, 9]:
model.setData(index, editor.text())
elif index.column() in [2, 3, 4]:
model.setData(index, editor.currentText())
class TimersModel(QAbstractTableModel):
timer_changed = Signal(int)
def __init__(self, data):
super().__init__()
if data is None:
data = []
self.timer_data = data
def flags(self, index):
if not index.isValid():
return 0
if index.column() in range(0, 5):
v = Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
return v
else:
v = Qt.ItemIsSelectable | Qt.ItemIsEnabled
return v
def data(self, index, role: int = ...):
if role == Qt.DisplayRole:
timer_type = self.timer_data[index.row()][1]
if timer_type == 'random':
if index.column() != 4:
return self.timer_data[index.row()][index.column()]
return ''
elif timer_type == 'periodic':
if index.column() in [0, 1, 4]:
return self.timer_data[index.row()][index.column()]
return ''
else:
if 0 <= index.column() <= 4:
return self.timer_data[index.row()][index.column()]
elif role == Qt.BackgroundRole:
timer_type = self.timer_data[index.row()][1]
if timer_type == 'random':
if index.column() == 4:
return QColor(30, 30, 30)
elif timer_type == 'periodic':
if index.column() in [2, 3]:
return QColor(30, 30, 30)
elif role == Qt.ToolTipRole:
if index.column() == 1:
return "Random timers evaluate at a random time in seconds between min and max. Periodic timers " \
"evaluate at a constant rate of time."
def rowCount(self, index) -> int:
return len(self.timer_data)
def columnCount(self, index):
if len(self.timer_data) > 0:
return len(self.timer_data[0])
return 0
def headerData(self, section: int, orientation, role: int = ...):
if role == Qt.TextAlignmentRole:
if orientation == Qt.Horizontal:
return int(Qt.AlignLeft|Qt.AlignCenter)
return int(Qt.AlignRight|Qt.AlignCenter)
if role == Qt.DisplayRole:
if orientation == Qt.Horizontal:
if section == 0:
return "Name"
elif section == 1:
return "Type"
elif section == 2:
return "Random Min (Sec)"
elif section == 3:
return "Random Max (Sec)"
elif section == 4:
return "Periodic Timeout (Sec)"
def setData(self, index, value, role: int = ...) -> bool:
if index.isValid() and 0 <= index.row() < len(self.timer_data):
if index.column() == 0:
self.timer_data[index.row()][0] = value
self.timer_changed.emit(index.row())
else:
self.timer_data[index.row()][index.column()] = value
return True
def add_timer(self):
# self.timer_data.append(['']*len(self.timer_data[0]))
self.timer_data.append(['']*5)
class TimerDelegate(QStyledItemDelegate):
def __init__(self, parent=None):
super(TimerDelegate, self).__init__(parent)
self.timer_data = parent.timers_model.timer_data
def createEditor(self, parent, option, index):
timer_type = ''
if index.row() < len(self.timer_data):
timer_type = self.timer_data[index.row()][1]
if index.column() == 0:
editor = QLineEdit(parent)
editor.setText("Timer Name")
self.connect(editor, SIGNAL("returnPressed()"), self.commit_and_close_editor)
return editor
elif index.column() == 1:
editor = QComboBox(parent)
editor.addItems(['periodic', 'random'])
return editor
elif index.column() == 2 and timer_type == 'random':
editor = QLineEdit(parent)
editor.setText("Min (sec)")
return editor
elif index.column() == 3 and timer_type == 'random':
editor = QLineEdit(parent)
editor.setText("Max (sec)")
return editor
elif index.column() == 4 and timer_type == 'periodic':
editor = QLineEdit(parent)
editor.setText("timeout (sec)")
return editor
def commit_and_close_editor(self):
pass
def setEditorData(self, editor, index):
text = index.model().data(index, Qt.DisplayRole)
if index.column() in [0, 2, 3, 4]:
editor.setText(f'{text}')
elif index.column() in [1]:
i = editor.findText(text)
if i == -1:
i = 0
editor.setCurrentIndex(i)
def setModelData(self, editor, model, index) -> None:
if index.column() in [0, 2, 3, 4]:
model.setData(index, editor.text())
elif index.column() == 1:
model.setData(index, editor.currentText())