[0849aec] | 1 | from PyQt5 import QtGui |
---|
[412e069e] | 2 | from PyQt5 import QtCore |
---|
[3bdbfcc] | 3 | |
---|
[83eb5208] | 4 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[3bdbfcc] | 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 |
---|
[b0b09b9] | 21 | for parameter in list(parameters.keys()): |
---|
[3bdbfcc] | 22 | item1 = QtGui.QStandardItem(parameter) |
---|
| 23 | item2 = QtGui.QStandardItem(GuiUtils.formatNumber(parameters[parameter])) |
---|
| 24 | self._model.appendRow([item1, item2]) |
---|
[895e7359] | 25 | self._model.setHeaderData(0, QtCore.Qt.Horizontal, "Parameter") |
---|
| 26 | self._model.setHeaderData(1, QtCore.Qt.Horizontal, "Value") |
---|
[3bdbfcc] | 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 | |
---|
[161713c] | 54 | def validate(self): |
---|
| 55 | ''' pure virtual ''' |
---|
| 56 | raise NotImplementedError("Validator must be implemented in derived class.") |
---|
| 57 | |
---|