1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
""" |
4
|
|
|
This file contains item delegates for the pulse editor QTableView/model. |
5
|
|
|
|
6
|
|
|
Qudi is free software: you can redistribute it and/or modify |
7
|
|
|
it under the terms of the GNU General Public License as published by |
8
|
|
|
the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
(at your option) any later version. |
10
|
|
|
|
11
|
|
|
Qudi is distributed in the hope that it will be useful, |
12
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
GNU General Public License for more details. |
15
|
|
|
|
16
|
|
|
You should have received a copy of the GNU General Public License |
17
|
|
|
along with Qudi. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
Copyright (c) the Qudi Developers. See the COPYRIGHT.txt file at the |
20
|
|
|
top-level directory of this distribution and at <https://github.com/Ulm-IQO/qudi/> |
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
from qtpy import QtCore, QtGui, QtWidgets |
24
|
|
|
from gui.pulsed.pulsed_custom_widgets import DigitalChannelsWidget, AnalogParametersWidget |
25
|
|
|
from qtwidgets.scientific_spinbox import ScienDSpinBox |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class CheckBoxItemDelegate(QtGui.QStyledItemDelegate): |
29
|
|
|
""" |
30
|
|
|
""" |
31
|
|
|
editingFinished = QtCore.Signal() |
32
|
|
|
|
33
|
|
|
def __init__(self, parent, data_access_role=QtCore.Qt.DisplayRole): |
34
|
|
|
""" |
35
|
|
|
@param QWidget parent: the parent QWidget which hosts this child widget |
36
|
|
|
""" |
37
|
|
|
super().__init__(parent) |
38
|
|
|
self._access_role = data_access_role |
39
|
|
|
return |
40
|
|
|
|
41
|
|
|
def createEditor(self, parent, option, index): |
42
|
|
|
""" |
43
|
|
|
Create for the display and interaction with the user an editor. |
44
|
|
|
|
45
|
|
|
@param QtGui.QWidget parent: The parent object (probably QTableView) |
46
|
|
|
@param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for |
47
|
|
|
style configuration. |
48
|
|
|
@param QtCore.QModelIndex index: That index will be passed by the model object of the |
49
|
|
|
QTableView to the delegated object. This index contains |
50
|
|
|
information about the selected current cell. |
51
|
|
|
|
52
|
|
|
An editor can be in principle any QWidget, which you want to use to display the current |
53
|
|
|
(model-)data. Therefore the editor is like a container, which handles the passed entries |
54
|
|
|
from the user interface and should save the data to the model object of the QTableWidget. |
55
|
|
|
The setEditorData function reads data from the model. |
56
|
|
|
|
57
|
|
|
Do not save the created editor as a class variable! This consumes a lot of unneeded memory. |
58
|
|
|
It is way better to create an editor if it is needed. The inherent function closeEditor() |
59
|
|
|
of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not |
60
|
|
|
needed any longer. |
61
|
|
|
""" |
62
|
|
|
editor = QtWidgets.QCheckBox(parent=parent) |
63
|
|
|
editor.setGeometry(option.rect) |
64
|
|
|
editor.stateChanged.connect(self.commitAndCloseEditor) |
65
|
|
|
return editor |
66
|
|
|
|
67
|
|
|
def commitAndCloseEditor(self): |
68
|
|
|
editor = self.sender() |
69
|
|
|
self.commitData.emit(editor) |
70
|
|
|
# self.closeEditor.emit(editor) |
71
|
|
|
self.editingFinished.emit() |
72
|
|
|
return |
73
|
|
|
|
74
|
|
|
def sizeHint(self): |
75
|
|
|
return QtCore.QSize(15, 50) |
76
|
|
|
|
77
|
|
|
def setEditorData(self, editor, index): |
78
|
|
|
""" |
79
|
|
|
Set the display of the current value of the used editor. |
80
|
|
|
|
81
|
|
|
@param ScienDSpinBox editor: QObject which was created in createEditor function, |
82
|
|
|
here a ScienDSpinBox. |
83
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
84
|
|
|
|
85
|
|
|
This function converts the passed data to an value, which can be |
86
|
|
|
understood by the editor. |
87
|
|
|
""" |
88
|
|
|
data = index.data(self._access_role) |
89
|
|
|
if not isinstance(data, bool): |
90
|
|
|
return |
91
|
|
|
editor.blockSignals(True) |
92
|
|
|
editor.setChecked(data) |
93
|
|
|
editor.blockSignals(False) |
94
|
|
|
return |
95
|
|
|
|
96
|
|
|
def setModelData(self, editor, model, index): |
97
|
|
|
""" |
98
|
|
|
Save the data of the editor to the model. |
99
|
|
|
|
100
|
|
|
@param ScienDSpinBox editor: QObject which was created in createEditor function, |
101
|
|
|
here a ScienDSpinBox. |
102
|
|
|
@param QtCore.QAbstractTableModel model: That is the object which contains the data model. |
103
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
104
|
|
|
|
105
|
|
|
Before the editor is destroyed the current selection should be saved in the underlying data |
106
|
|
|
model. The setModelData() function reads the content of the editor, and writes it to the |
107
|
|
|
model. Furthermore here the postprocessing of the data can happen, where the data can be |
108
|
|
|
manipulated for the model. |
109
|
|
|
""" |
110
|
|
|
data = editor.isChecked() |
111
|
|
|
# write the data to the model: |
112
|
|
|
model.setData(index, data, self._access_role) |
113
|
|
|
return |
114
|
|
|
|
115
|
|
|
def paint(self, painter, option, index): |
116
|
|
|
painter.save() |
117
|
|
|
r = option.rect |
118
|
|
|
painter.translate(r.topLeft()) |
119
|
|
|
widget = QtWidgets.QCheckBox() |
120
|
|
|
widget.setGeometry(r) |
121
|
|
|
widget.setChecked(index.data(self._access_role)) |
122
|
|
|
widget.render(painter) |
123
|
|
|
painter.restore() |
124
|
|
|
|
125
|
|
|
|
126
|
|
View Code Duplication |
class SpinBoxItemDelegate(QtGui.QStyledItemDelegate): |
|
|
|
|
127
|
|
|
""" |
128
|
|
|
""" |
129
|
|
|
editingFinished = QtCore.Signal() |
130
|
|
|
|
131
|
|
|
def __init__(self, parent, item_dict=None, data_access_role=QtCore.Qt.DisplayRole): |
132
|
|
|
""" |
133
|
|
|
@param QWidget parent: the parent QWidget which hosts this child widget |
134
|
|
|
@param dict item_dict: dict with the following keys which give informations about the |
135
|
|
|
current viewbox: 'unit', 'init_val', 'min', 'max', 'view_stepsize', |
136
|
|
|
'dec', 'unit_prefix' |
137
|
|
|
""" |
138
|
|
|
super().__init__(parent) |
139
|
|
|
if item_dict is None: |
140
|
|
|
item_dict = dict() |
141
|
|
|
self.item_dict = item_dict |
142
|
|
|
self._access_role = data_access_role |
143
|
|
|
return |
144
|
|
|
|
145
|
|
|
def createEditor(self, parent, option, index): |
146
|
|
|
""" |
147
|
|
|
Create for the display and interaction with the user an editor. |
148
|
|
|
|
149
|
|
|
@param QtGui.QWidget parent: The parent object (probably QTableView) |
150
|
|
|
@param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for |
151
|
|
|
style configuration. |
152
|
|
|
@param QtCore.QModelIndex index: That index will be passed by the model object of the |
153
|
|
|
QTableView to the delegated object. This index contains |
154
|
|
|
information about the selected current cell. |
155
|
|
|
|
156
|
|
|
An editor can be in principle any QWidget, which you want to use to display the current |
157
|
|
|
(model-)data. Therefore the editor is like a container, which handles the passed entries |
158
|
|
|
from the user interface and should save the data to the model object of the QTableWidget. |
159
|
|
|
The setEditorData function reads data from the model. |
160
|
|
|
|
161
|
|
|
Do not save the created editor as a class variable! This consumes a lot of unneeded memory. |
162
|
|
|
It is way better to create an editor if it is needed. The inherent function closeEditor() |
163
|
|
|
of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not |
164
|
|
|
needed any longer. |
165
|
|
|
""" |
166
|
|
|
editor = QtGui.QSpinBox(parent=parent) |
167
|
|
|
if 'min' in self.item_dict: |
168
|
|
|
editor.setMinimum(self.item_dict['min']) |
169
|
|
|
if 'max' in self.item_dict: |
170
|
|
|
editor.setMaximum(self.item_dict['max']) |
171
|
|
|
if 'unit' in self.item_dict: |
172
|
|
|
editor.setSuffix(self.item_dict['unit']) |
173
|
|
|
editor.setGeometry(option.rect) |
174
|
|
|
editor.editingFinished.connect(self.commitAndCloseEditor) |
175
|
|
|
return editor |
176
|
|
|
|
177
|
|
|
def commitAndCloseEditor(self): |
178
|
|
|
editor = self.sender() |
179
|
|
|
self.commitData.emit(editor) |
180
|
|
|
# self.closeEditor.emit(editor) |
181
|
|
|
self.editingFinished.emit() |
182
|
|
|
return |
183
|
|
|
|
184
|
|
|
def sizeHint(self): |
185
|
|
|
return QtCore.QSize(90, 50) |
186
|
|
|
|
187
|
|
|
def setEditorData(self, editor, index): |
188
|
|
|
""" |
189
|
|
|
Set the display of the current value of the used editor. |
190
|
|
|
|
191
|
|
|
@param ScienDSpinBox editor: QObject which was created in createEditor function, |
192
|
|
|
here a ScienDSpinBox. |
193
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
194
|
|
|
|
195
|
|
|
This function converts the passed data to an value, which can be |
196
|
|
|
understood by the editor. |
197
|
|
|
""" |
198
|
|
|
data = index.data(self._access_role) |
199
|
|
|
if not isinstance(data, int): |
200
|
|
|
data = self.item_dict['init_val'] |
201
|
|
|
editor.blockSignals(True) |
202
|
|
|
editor.setValue(data) |
203
|
|
|
editor.blockSignals(False) |
204
|
|
|
return |
205
|
|
|
|
206
|
|
|
def setModelData(self, editor, model, index): |
207
|
|
|
""" |
208
|
|
|
Save the data of the editor to the model. |
209
|
|
|
|
210
|
|
|
@param ScienDSpinBox editor: QObject which was created in createEditor function, |
211
|
|
|
here a ScienDSpinBox. |
212
|
|
|
@param QtCore.QAbstractTableModel model: That is the object which contains the data model. |
213
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
214
|
|
|
|
215
|
|
|
Before the editor is destroyed the current selection should be saved in the underlying data |
216
|
|
|
model. The setModelData() function reads the content of the editor, and writes it to the |
217
|
|
|
model. Furthermore here the postprocessing of the data can happen, where the data can be |
218
|
|
|
manipulated for the model. |
219
|
|
|
""" |
220
|
|
|
data = editor.value() |
221
|
|
|
# write the data to the model: |
222
|
|
|
model.setData(index, data, self._access_role) |
223
|
|
|
return |
224
|
|
|
|
225
|
|
|
def paint(self, painter, option, index): |
226
|
|
|
painter.save() |
227
|
|
|
r = option.rect |
228
|
|
|
painter.translate(r.topLeft()) |
229
|
|
|
widget = QtGui.QSpinBox() |
230
|
|
|
if 'min' in self.item_dict: |
231
|
|
|
widget.setMinimum(self.item_dict['min']) |
232
|
|
|
if 'max' in self.item_dict: |
233
|
|
|
widget.setMaximum(self.item_dict['max']) |
234
|
|
|
if 'unit' in self.item_dict: |
235
|
|
|
widget.setSuffix(self.item_dict['unit']) |
236
|
|
|
widget.setGeometry(r) |
237
|
|
|
widget.setValue(index.data(self._access_role)) |
238
|
|
|
widget.render(painter) |
239
|
|
|
painter.restore() |
240
|
|
|
|
241
|
|
|
|
242
|
|
View Code Duplication |
class ScienDSpinBoxItemDelegate(QtGui.QStyledItemDelegate): |
|
|
|
|
243
|
|
|
""" |
244
|
|
|
""" |
245
|
|
|
editingFinished = QtCore.Signal() |
246
|
|
|
|
247
|
|
|
def __init__(self, parent, item_dict, data_access_role=QtCore.Qt.DisplayRole): |
248
|
|
|
""" |
249
|
|
|
@param QWidget parent: the parent QWidget which hosts this child widget |
250
|
|
|
@param dict item_dict: dict with the following keys which give informations about the |
251
|
|
|
current viewbox: 'unit', 'init_val', 'min', 'max' |
252
|
|
|
""" |
253
|
|
|
super().__init__(parent) |
254
|
|
|
self.item_dict = item_dict |
255
|
|
|
self._access_role = data_access_role |
256
|
|
|
# Note, the editor used in this delegate creates the unit prefix by |
257
|
|
|
# itself, therefore no handling for that is implemented. |
258
|
|
|
return |
259
|
|
|
|
260
|
|
|
def createEditor(self, parent, option, index): |
261
|
|
|
""" |
262
|
|
|
Create for the display and interaction with the user an editor. |
263
|
|
|
|
264
|
|
|
@param QtGui.QWidget parent: The parent object (probably QTableView) |
265
|
|
|
@param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for |
266
|
|
|
style configuration. |
267
|
|
|
@param QtCore.QModelIndex index: That index will be passed by the model object of the |
268
|
|
|
QTableView to the delegated object. This index contains |
269
|
|
|
information about the selected current cell. |
270
|
|
|
|
271
|
|
|
An editor can be in principle any QWidget, which you want to use to display the current |
272
|
|
|
(model-)data. Therefore the editor is like a container, which handles the passed entries |
273
|
|
|
from the user interface and should save the data to the model object of the QTableWidget. |
274
|
|
|
The setEditorData function reads data from the model. |
275
|
|
|
|
276
|
|
|
Do not save the created editor as a class variable! This consumes a lot of unneeded memory. |
277
|
|
|
It is way better to create an editor if it is needed. The inherent function closeEditor() |
278
|
|
|
of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not |
279
|
|
|
needed any longer. |
280
|
|
|
""" |
281
|
|
|
editor = ScienDSpinBox(parent=parent) |
282
|
|
|
if 'min' in self.item_dict: |
283
|
|
|
editor.setMinimum(self.item_dict['min']) |
284
|
|
|
if 'max' in self.item_dict: |
285
|
|
|
editor.setMaximum(self.item_dict['max']) |
286
|
|
|
if 'dec' in self.item_dict: |
287
|
|
|
editor.setDecimals(self.item_dict['dec']) |
288
|
|
|
if 'unit' in self.item_dict: |
289
|
|
|
editor.setSuffix(self.item_dict['unit']) |
290
|
|
|
editor.setGeometry(option.rect) |
291
|
|
|
editor.editingFinished.connect(self.commitAndCloseEditor) |
292
|
|
|
return editor |
293
|
|
|
|
294
|
|
|
def commitAndCloseEditor(self): |
295
|
|
|
editor = self.sender() |
296
|
|
|
self.commitData.emit(editor) |
297
|
|
|
# self.closeEditor.emit(editor) |
298
|
|
|
self.editingFinished.emit() |
299
|
|
|
return |
300
|
|
|
|
301
|
|
|
def sizeHint(self): |
302
|
|
|
return QtCore.QSize(90, 50) |
303
|
|
|
|
304
|
|
|
def setEditorData(self, editor, index): |
305
|
|
|
""" |
306
|
|
|
Set the display of the current value of the used editor. |
307
|
|
|
|
308
|
|
|
@param ScienDSpinBox editor: QObject which was created in createEditor function, |
309
|
|
|
here a ScienDSpinBox. |
310
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
311
|
|
|
|
312
|
|
|
This function converts the passed data to an value, which can be |
313
|
|
|
understood by the editor. |
314
|
|
|
""" |
315
|
|
|
data = index.data(self._access_role) |
316
|
|
|
if not isinstance(data, float): |
317
|
|
|
data = self.item_dict['init_val'] |
318
|
|
|
editor.blockSignals(True) |
319
|
|
|
editor.setValue(data) |
320
|
|
|
editor.blockSignals(False) |
321
|
|
|
return |
322
|
|
|
|
323
|
|
|
def setModelData(self, editor, model, index): |
324
|
|
|
""" |
325
|
|
|
Save the data of the editor to the model. |
326
|
|
|
|
327
|
|
|
@param ScienDSpinBox editor: QObject which was created in createEditor function, |
328
|
|
|
here a ScienDSpinBox. |
329
|
|
|
@param QtCore.QAbstractTableModel model: That is the object which contains the data model. |
330
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
331
|
|
|
|
332
|
|
|
Before the editor is destroyed the current selection should be saved in the underlying data |
333
|
|
|
model. The setModelData() function reads the content of the editor, and writes it to the |
334
|
|
|
model. Furthermore here the postprocessing of the data can happen, where the data can be |
335
|
|
|
manipulated for the model. |
336
|
|
|
""" |
337
|
|
|
data = editor.value() |
338
|
|
|
# write the data to the model: |
339
|
|
|
model.setData(index, data, self._access_role) |
340
|
|
|
return |
341
|
|
|
|
342
|
|
|
def paint(self, painter, option, index): |
343
|
|
|
painter.save() |
344
|
|
|
r = option.rect |
345
|
|
|
painter.translate(r.topLeft()) |
346
|
|
|
widget = ScienDSpinBox() |
347
|
|
|
if 'dec' in self.item_dict: |
348
|
|
|
widget.setDecimals(self.item_dict['dec']) |
349
|
|
|
if 'unit' in self.item_dict: |
350
|
|
|
widget.setSuffix(self.item_dict['unit']) |
351
|
|
|
widget.setGeometry(r) |
352
|
|
|
widget.setValue(index.data(self._access_role)) |
353
|
|
|
widget.render(painter) |
354
|
|
|
painter.restore() |
355
|
|
|
|
356
|
|
|
|
357
|
|
|
class ComboBoxItemDelegate(QtGui.QStyledItemDelegate): |
358
|
|
|
""" |
359
|
|
|
""" |
360
|
|
|
editingFinished = QtCore.Signal() |
361
|
|
|
|
362
|
|
|
def __init__(self, parent, item_list, data_access_role=QtCore.Qt.DisplayRole, |
363
|
|
|
size=QtCore.QSize(80, 50)): |
364
|
|
|
super().__init__(parent) |
365
|
|
|
self._item_list = item_list |
366
|
|
|
self._access_role = data_access_role |
367
|
|
|
self._size = size |
368
|
|
|
return |
369
|
|
|
|
370
|
|
|
def createEditor(self, parent, option, index): |
371
|
|
|
""" |
372
|
|
|
Create for the display and interaction with the user an editor. |
373
|
|
|
|
374
|
|
|
@param QtGui.QWidget parent: The parent object (probably QTableView) |
375
|
|
|
@param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use for |
376
|
|
|
style configuration. |
377
|
|
|
@param QtCore.QModelIndex index: That index will be passed by the model object of the |
378
|
|
|
QTableView to the delegated object. This index contains |
379
|
|
|
information about the selected current cell. |
380
|
|
|
|
381
|
|
|
An editor can be in principle any QWidget, which you want to use to display the current |
382
|
|
|
(model-)data. Therefore the editor is also a container, which handles the passed entries |
383
|
|
|
from the user interface and should save the data in the model object of the QTableWidget. |
384
|
|
|
|
385
|
|
|
Do not save the created editor as a class variable! This consumes a lot of unneeded memory. |
386
|
|
|
It is way better to create an editor if it is needed. The inherent function closeEditor() |
387
|
|
|
of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not |
388
|
|
|
needed any longer. |
389
|
|
|
""" |
390
|
|
|
widget = QtGui.QComboBox(parent) |
391
|
|
|
widget.addItems(self._item_list) |
392
|
|
|
widget.setGeometry(option.rect) |
393
|
|
|
widget.currentIndexChanged.connect(self.commitAndCloseEditor) |
394
|
|
|
return widget |
395
|
|
|
|
396
|
|
|
def commitAndCloseEditor(self): |
397
|
|
|
editor = self.sender() |
398
|
|
|
self.commitData.emit(editor) |
399
|
|
|
# self.closeEditor.emit(editor) |
400
|
|
|
self.editingFinished.emit() |
401
|
|
|
return |
402
|
|
|
|
403
|
|
|
def sizeHint(self): |
404
|
|
|
return self._size |
405
|
|
|
|
406
|
|
|
def setEditorData(self, editor, index): |
407
|
|
|
""" |
408
|
|
|
Set the display of the current value of the used editor. |
409
|
|
|
|
410
|
|
|
@param QComboBox editor: QObject which was created in createEditor function, |
411
|
|
|
here a QCombobox. |
412
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
413
|
|
|
""" |
414
|
|
|
data = index.data(self._access_role) |
415
|
|
|
combo_index = editor.findText(data) |
416
|
|
|
editor.blockSignals(True) |
417
|
|
|
editor.setCurrentIndex(combo_index) |
418
|
|
|
editor.blockSignals(False) |
419
|
|
|
return |
420
|
|
|
|
421
|
|
|
def setModelData(self, editor, model, index): |
422
|
|
|
""" |
423
|
|
|
Save the data of the editor to the model. |
424
|
|
|
|
425
|
|
|
@param QComboBox editor: QObject which was created in createEditor function, |
426
|
|
|
here a QCombobox. |
427
|
|
|
@param QtCore.QAbstractTableModel model: That is the object which contains the data model. |
428
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
429
|
|
|
|
430
|
|
|
Before the editor is destroyed the current selection should be saved in the data model. |
431
|
|
|
The setModelData() function reads the content of the editor, and writes it to the model. |
432
|
|
|
Furthermore here the postprocessing of the data can happen, where the data can be |
433
|
|
|
manipulated for the model. |
434
|
|
|
""" |
435
|
|
|
data = editor.currentText() |
436
|
|
|
model.setData(index, data, self._access_role) |
437
|
|
|
return |
438
|
|
|
|
439
|
|
|
def paint(self, painter, option, index): |
440
|
|
|
painter.save() |
441
|
|
|
r = option.rect |
442
|
|
|
painter.translate(r.topLeft()) |
443
|
|
|
widget = QtGui.QComboBox() |
444
|
|
|
widget.addItem(index.data(self._access_role)) |
445
|
|
|
widget.setGeometry(r) |
446
|
|
|
widget.render(painter) |
447
|
|
|
painter.restore() |
448
|
|
|
|
449
|
|
|
|
450
|
|
|
class DigitalStatesItemDelegate(QtGui.QStyledItemDelegate): |
451
|
|
|
""" |
452
|
|
|
""" |
453
|
|
|
editingFinished = QtCore.Signal() |
454
|
|
|
|
455
|
|
|
def __init__(self, parent, data_access_role=QtCore.Qt.DisplayRole): |
456
|
|
|
|
457
|
|
|
super().__init__(parent) |
458
|
|
|
self._access_role = data_access_role |
459
|
|
|
return |
460
|
|
|
|
461
|
|
|
def createEditor(self, parent, option, index): |
462
|
|
|
""" |
463
|
|
|
Create for the display and interaction with the user an editor. |
464
|
|
|
|
465
|
|
|
@param QtGui.QWidget parent: The parent object, here QTableWidget |
466
|
|
|
@param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use |
467
|
|
|
for style configuration. |
468
|
|
|
@param QtCore.QModelIndex index: That index will be passed by the model object of the |
469
|
|
|
QTableWidget to the delegated object. This index contains |
470
|
|
|
information about the selected current cell. |
471
|
|
|
|
472
|
|
|
An editor can be in principle any QWidget, which you want to use to display the current |
473
|
|
|
(model-)data. Therefore the editor is also a container, which handles the passed entries |
474
|
|
|
from the user interface and should save the data in the model object of the QTableWidget. |
475
|
|
|
|
476
|
|
|
Do not save the created editor as a class variable! This consumes a lot of unneeded memory. |
477
|
|
|
It is way better to create an editor if it is needed. The inherent function closeEditor() |
478
|
|
|
of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not |
479
|
|
|
needed any longer. |
480
|
|
|
""" |
481
|
|
|
editor = DigitalChannelsWidget(parent, list(index.data(self._access_role))) |
482
|
|
|
editor.setData(index.data(self._access_role)) |
483
|
|
|
editor.stateChanged.connect(self.commitAndCloseEditor) |
484
|
|
|
return editor |
485
|
|
|
|
486
|
|
|
def commitAndCloseEditor(self): |
487
|
|
|
editor = self.sender() |
488
|
|
|
self.commitData.emit(editor) |
489
|
|
|
# self.closeEditor.emit(editor) |
490
|
|
|
self.editingFinished.emit() |
491
|
|
|
return |
492
|
|
|
|
493
|
|
|
def sizeHint(self, option, index): |
494
|
|
|
widget = DigitalChannelsWidget(None, list(index.data(self._access_role))) |
495
|
|
|
return widget.sizeHint() |
496
|
|
|
|
497
|
|
|
def setEditorData(self, editor, index): |
498
|
|
|
""" |
499
|
|
|
Set the display of the current value of the used editor. |
500
|
|
|
|
501
|
|
|
@param DigitalChannelsWidget editor: QObject which was created in createEditor function, |
502
|
|
|
here a DigitalChannelsWidget. |
503
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
504
|
|
|
|
505
|
|
|
This function converts the passed data to an value, which can be understood by the editor. |
506
|
|
|
""" |
507
|
|
|
data = index.data(self._access_role) |
508
|
|
|
editor.blockSignals(True) |
509
|
|
|
editor.setData(data) |
510
|
|
|
editor.blockSignals(False) |
511
|
|
|
return |
512
|
|
|
|
513
|
|
|
def setModelData(self, editor, model, index): |
514
|
|
|
""" |
515
|
|
|
Save the data of the editor to the model. |
516
|
|
|
|
517
|
|
|
@param DigitalChannelsWidget editor: QObject which was created in createEditor function, |
518
|
|
|
here a DigitalChannelsWidget. |
519
|
|
|
@param QtCore.QAbstractTableModel model: That is the object which contains the data model. |
520
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
521
|
|
|
|
522
|
|
|
Before the editor is destroyed the current selection should be saved in the underlying data |
523
|
|
|
model. The setModelData() function reads the content of the editor, and writes it to the |
524
|
|
|
model. Furthermore here the postprocessing of the data can happen, where the data can be |
525
|
|
|
manipulated for the model. |
526
|
|
|
""" |
527
|
|
|
data = editor.data() |
528
|
|
|
model.setData(index, data, self._access_role) |
529
|
|
|
return |
530
|
|
|
|
531
|
|
|
def paint(self, painter, option, index): |
532
|
|
|
painter.save() |
533
|
|
|
r = option.rect |
534
|
|
|
painter.translate(r.topLeft()) |
535
|
|
|
widget = DigitalChannelsWidget(None, list(index.data(self._access_role))) |
536
|
|
|
widget.setData(index.data(self._access_role)) |
537
|
|
|
widget.render(painter) |
538
|
|
|
painter.restore() |
539
|
|
|
|
540
|
|
|
|
541
|
|
|
class AnalogParametersItemDelegate(QtGui.QStyledItemDelegate): |
542
|
|
|
""" |
543
|
|
|
""" |
544
|
|
|
editingFinished = QtCore.Signal() |
545
|
|
|
|
546
|
|
|
def __init__(self, parent, data_access_roles=None): |
547
|
|
|
super().__init__(parent) |
548
|
|
|
if data_access_roles is None: |
549
|
|
|
self._access_role = [QtCore.Qt.DisplayRole, QtCore.Qt.DisplayRole] |
550
|
|
|
else: |
551
|
|
|
self._access_role = data_access_roles |
552
|
|
|
|
553
|
|
|
def createEditor(self, parent, option, index): |
554
|
|
|
""" |
555
|
|
|
Create for the display and interaction with the user an editor. |
556
|
|
|
|
557
|
|
|
@param QtGui.QWidget parent: The parent object, here QTableWidget |
558
|
|
|
@param QtGui.QStyleOptionViewItemV4 option: This is a setting option which you can use |
559
|
|
|
for style configuration. |
560
|
|
|
@param QtCore.QModelIndex index: That index will be passed by the model object of the |
561
|
|
|
QTableWidget to the delegated object. This index contains |
562
|
|
|
information about the selected current cell. |
563
|
|
|
|
564
|
|
|
An editor can be in principle any QWidget, which you want to use to display the current |
565
|
|
|
(model-)data. Therefore the editor is also a container, which handles the passed entries |
566
|
|
|
from the user interface and should save the data in the model object of the QTableWidget. |
567
|
|
|
|
568
|
|
|
Do not save the created editor as a class variable! This consumes a lot of unneeded memory. |
569
|
|
|
It is way better to create an editor if it is needed. The inherent function closeEditor() |
570
|
|
|
of QStyledItemDelegate takes care of closing and destroying the editor for you, if it is not |
571
|
|
|
needed any longer. |
572
|
|
|
""" |
573
|
|
|
parameters = index.data(self._access_role[0]).params |
574
|
|
|
editor = AnalogParametersWidget(parent, parameters) |
575
|
|
|
editor.setData(index.data(self._access_role[1])) |
576
|
|
|
editor.editingFinished.connect(self.commitAndCloseEditor) |
577
|
|
|
return editor |
578
|
|
|
|
579
|
|
|
def commitAndCloseEditor(self): |
580
|
|
|
editor = self.sender() |
581
|
|
|
self.commitData.emit(editor) |
582
|
|
|
# self.closeEditor.emit(editor) |
583
|
|
|
self.editingFinished.emit() |
584
|
|
|
return |
585
|
|
|
|
586
|
|
|
def sizeHint(self, option, index): |
587
|
|
|
parameters = index.data(self._access_role[0]).params |
588
|
|
|
widget = AnalogParametersWidget(None, parameters) |
589
|
|
|
return widget.sizeHint() |
590
|
|
|
|
591
|
|
|
def setEditorData(self, editor, index): |
592
|
|
|
""" |
593
|
|
|
Set the display of the current value of the used editor. |
594
|
|
|
|
595
|
|
|
@param AnalogParametersWidget editor: QObject which was created in createEditor function, |
596
|
|
|
here a AnalogParametersWidget. |
597
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
598
|
|
|
|
599
|
|
|
This function converts the passed data to an value, which can be understood by the editor. |
600
|
|
|
""" |
601
|
|
|
data = index.data(self._access_role[1]) |
602
|
|
|
editor.blockSignals(True) |
603
|
|
|
editor.setData(data) |
604
|
|
|
editor.blockSignals(False) |
605
|
|
|
return |
606
|
|
|
|
607
|
|
|
def setModelData(self, editor, model, index): |
608
|
|
|
""" |
609
|
|
|
Save the data of the editor to the model. |
610
|
|
|
|
611
|
|
|
@param AnalogParametersWidget editor: QObject which was created in createEditor function, |
612
|
|
|
here a AnalogParametersWidget. |
613
|
|
|
@param QtCore.QAbstractTableModel model: That is the object which contains the data model. |
614
|
|
|
@param QtCore.QModelIndex index: explained in createEditor function. |
615
|
|
|
|
616
|
|
|
Before the editor is destroyed the current selection should be saved in the underlying data |
617
|
|
|
model. The setModelData() function reads the content of the editor, and writes it to the |
618
|
|
|
model. Furthermore here the postprocessing of the data can happen, where the data can be |
619
|
|
|
manipulated for the model. |
620
|
|
|
""" |
621
|
|
|
data = editor.data() |
622
|
|
|
model.setData(index, data, self._access_role[1]) |
623
|
|
|
return |
624
|
|
|
|
625
|
|
|
def paint(self, painter, option, index): |
626
|
|
|
painter.save() |
627
|
|
|
r = option.rect |
628
|
|
|
painter.translate(r.topLeft()) |
629
|
|
|
parameters = index.data(self._access_role[0]).params |
630
|
|
|
widget = AnalogParametersWidget(None, parameters) |
631
|
|
|
widget.setData(index.data(self._access_role[1])) |
632
|
|
|
widget.render(painter) |
633
|
|
|
painter.restore() |
634
|
|
|
|