1 | from PyQt5 import QtGui |
---|
2 | |
---|
3 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
4 | |
---|
5 | class SlicerModel(object): |
---|
6 | def __init__(self): |
---|
7 | # Model representation of local parameters |
---|
8 | self._model = QtGui.QStandardItemModel() |
---|
9 | |
---|
10 | self.update_model = True |
---|
11 | self._model.itemChanged.connect(self.setParamsFromModel) |
---|
12 | |
---|
13 | def setModelFromParams(self): |
---|
14 | """ |
---|
15 | Set up the Qt model for data handling between controls |
---|
16 | """ |
---|
17 | parameters = self.getParams() |
---|
18 | self._model.removeRows( 0, self._model.rowCount() ) |
---|
19 | # Crete/overwrite model items |
---|
20 | for parameter in list(parameters.keys()): |
---|
21 | item1 = QtGui.QStandardItem(parameter) |
---|
22 | item2 = QtGui.QStandardItem(GuiUtils.formatNumber(parameters[parameter])) |
---|
23 | self._model.appendRow([item1, item2]) |
---|
24 | self._model.setHeaderData(0, QtCore.Qt.Horizontal, "Parameter") |
---|
25 | self._model.setHeaderData(1, QtCore.Qt.Horizontal, "Value") |
---|
26 | |
---|
27 | def setParamsFromModel(self, item): |
---|
28 | """ |
---|
29 | Set up the params dictionary based on the model content. |
---|
30 | """ |
---|
31 | params = self.getParams() |
---|
32 | index = self._model.indexFromItem(item) |
---|
33 | row_index = index.row() |
---|
34 | param_name = str(self._model.item(row_index, 0).text()) |
---|
35 | params[param_name] = float(self._model.item(row_index, 1).text()) |
---|
36 | |
---|
37 | self.update_model = False |
---|
38 | self.setParams(params) |
---|
39 | self.update_model = True |
---|
40 | |
---|
41 | def model(self): |
---|
42 | '''getter for the model''' |
---|
43 | return self._model |
---|
44 | |
---|
45 | def getParams(self): |
---|
46 | ''' pure virtual ''' |
---|
47 | raise NotImplementedError("Parameter getter must be implemented in derived class.") |
---|
48 | |
---|
49 | def setParams(self): |
---|
50 | ''' pure virtual ''' |
---|
51 | raise NotImplementedError("Parameter setter must be implemented in derived class.") |
---|
52 | |
---|
53 | def validate(self): |
---|
54 | ''' pure virtual ''' |
---|
55 | raise NotImplementedError("Validator must be implemented in derived class.") |
---|
56 | |
---|