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