[e1e3e09] | 1 | """ |
---|
| 2 | Widget/logic for smearing data. |
---|
| 3 | """ |
---|
[4992ff2] | 4 | from PyQt5 import QtCore |
---|
| 5 | from PyQt5 import QtGui |
---|
| 6 | from PyQt5 import QtWidgets |
---|
[e1e3e09] | 7 | |
---|
[dc5ef15] | 8 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 9 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[d6b8a1d] | 10 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[e1e3e09] | 11 | |
---|
| 12 | # Local UI |
---|
[1bc27f1] | 13 | from sas.qtgui.Perspectives.Fitting.UI.SmearingWidgetUI import Ui_SmearingWidgetUI |
---|
[e1e3e09] | 14 | |
---|
[4992ff2] | 15 | class DataWidgetMapper(QtWidgets.QDataWidgetMapper): |
---|
[180bd54] | 16 | """ |
---|
| 17 | Custom version of the standard QDataWidgetMapper allowing for proper |
---|
| 18 | response to index change in comboboxes |
---|
| 19 | """ |
---|
| 20 | def addMapping(self, widget, section, propertyName=None): |
---|
| 21 | if propertyName is None: |
---|
| 22 | super(DataWidgetMapper, self).addMapping(widget, section) |
---|
| 23 | else: |
---|
| 24 | super(DataWidgetMapper, self).addMapping(widget, section, propertyName) |
---|
| 25 | |
---|
[4992ff2] | 26 | if isinstance(widget, QtWidgets.QComboBox): |
---|
[180bd54] | 27 | delegate = self.itemDelegate() |
---|
[e1e3e09] | 28 | widget.currentIndexChanged.connect(lambda: delegate.commitData.emit(widget)) |
---|
| 29 | |
---|
| 30 | SMEARING_1D = ["Custom Pinhole Smear", "Custom Slit Smear"] |
---|
| 31 | SMEARING_2D = ["Custom Pinhole Smear"] |
---|
| 32 | |
---|
| 33 | MODEL = [ |
---|
| 34 | 'SMEARING', |
---|
| 35 | 'PINHOLE_MIN', |
---|
| 36 | 'PINHOLE_MAX', |
---|
| 37 | 'ACCURACY'] |
---|
| 38 | |
---|
[4992ff2] | 39 | class SmearingWidget(QtWidgets.QWidget, Ui_SmearingWidgetUI): |
---|
[e1e3e09] | 40 | def __init__(self, parent=None): |
---|
| 41 | super(SmearingWidget, self).__init__() |
---|
| 42 | |
---|
| 43 | self.setupUi(self) |
---|
[98b13f72] | 44 | |
---|
| 45 | # Have we loaded data yet? If so, what kind |
---|
[180bd54] | 46 | self.have_data = None |
---|
[98b13f72] | 47 | # Local model for holding data |
---|
[e1e3e09] | 48 | self.model = None |
---|
[98b13f72] | 49 | # Mapper for model update |
---|
[e1e3e09] | 50 | self.mapper = None |
---|
| 51 | |
---|
[1bc27f1] | 52 | self.parent = parent |
---|
[98b13f72] | 53 | # Let only floats in the line edits |
---|
[d6b8a1d] | 54 | self.txtSmearDown.setValidator(GuiUtils.DoubleValidator()) |
---|
| 55 | self.txtSmearUp.setValidator(GuiUtils.DoubleValidator()) |
---|
[98b13f72] | 56 | |
---|
| 57 | # Attach slots |
---|
| 58 | self.cbSmearing.currentIndexChanged.connect(self.onIndexChange) |
---|
| 59 | self.cbSmearing.setCurrentIndex(0) |
---|
| 60 | |
---|
[e1e3e09] | 61 | self.initModel() |
---|
| 62 | self.initMapper() |
---|
| 63 | |
---|
| 64 | def initModel(self): |
---|
| 65 | """ |
---|
| 66 | Initialize the state |
---|
| 67 | """ |
---|
| 68 | self.model = QtGui.QStandardItemModel() |
---|
[b3e8629] | 69 | for model_item in range(len(MODEL)): |
---|
[e1e3e09] | 70 | self.model.setItem(model_item, QtGui.QStandardItem()) |
---|
[98b13f72] | 71 | # Attach slot |
---|
[e1e3e09] | 72 | self.model.dataChanged.connect(self.onModelChange) |
---|
| 73 | |
---|
| 74 | def initMapper(self): |
---|
| 75 | """ |
---|
| 76 | Initialize model item <-> UI element mapping |
---|
| 77 | """ |
---|
| 78 | self.mapper = DataWidgetMapper(self) |
---|
| 79 | |
---|
| 80 | self.mapper.setModel(self.model) |
---|
| 81 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
| 82 | |
---|
| 83 | self.mapper.addMapping(self.txtSmearUp, MODEL.index('PINHOLE_MIN')) |
---|
| 84 | self.mapper.addMapping(self.txtSmearDown, MODEL.index('PINHOLE_MAX')) |
---|
| 85 | self.mapper.addMapping(self.cbSmearing, MODEL.index('SMEARING')) |
---|
| 86 | self.mapper.addMapping(self.cbAccuracy, MODEL.index('ACCURACY')) |
---|
[4992ff2] | 87 | |
---|
| 88 | # FIXME DOESNT WORK WITH QT5 |
---|
| 89 | #self.mapper.toFirst() |
---|
[e1e3e09] | 90 | |
---|
| 91 | def updateSmearing(self, data=None): |
---|
| 92 | """ |
---|
| 93 | Update control elements based on data passed |
---|
| 94 | """ |
---|
| 95 | self.cbSmearing.clear() |
---|
| 96 | self.cbSmearing.addItem("None") |
---|
| 97 | self.cbAccuracy.setVisible(False) |
---|
| 98 | |
---|
| 99 | if data is None: |
---|
| 100 | self.setElementsVisibility(False) |
---|
| 101 | elif isinstance(data, Data1D): |
---|
| 102 | self.cbSmearing.addItems(SMEARING_1D) |
---|
[180bd54] | 103 | self.have_data = Data1D |
---|
[e1e3e09] | 104 | else: |
---|
| 105 | self.cbSmearing.addItems(SMEARING_2D) |
---|
[180bd54] | 106 | self.have_data = Data2D |
---|
[e1e3e09] | 107 | self.cbSmearing.setCurrentIndex(0) |
---|
| 108 | |
---|
| 109 | def onIndexChange(self, index): |
---|
| 110 | """ |
---|
| 111 | Callback for smearing combobox index change |
---|
| 112 | """ |
---|
| 113 | if index == 0: |
---|
| 114 | self.setElementsVisibility(False) |
---|
| 115 | return |
---|
| 116 | elif index == 1: |
---|
| 117 | self.setElementsVisibility(True) |
---|
| 118 | self.setPinholeLabels() |
---|
| 119 | elif index == 2: |
---|
| 120 | self.setElementsVisibility(True) |
---|
| 121 | self.setSlitLabels() |
---|
| 122 | |
---|
| 123 | def onModelChange(self, top, bottom): |
---|
| 124 | """ |
---|
[98b13f72] | 125 | Respond to model change by updating |
---|
[e1e3e09] | 126 | """ |
---|
[180bd54] | 127 | #print "MODEL CHANGED for property: %s. The value is now: %s" % \ |
---|
| 128 | # (MODEL[top.row()], str(self.model.item(top.row()).text())) |
---|
[e1e3e09] | 129 | pass |
---|
| 130 | |
---|
| 131 | def setElementsVisibility(self, visible): |
---|
| 132 | """ |
---|
| 133 | Labels and linedits visibility control |
---|
| 134 | """ |
---|
| 135 | self.lblSmearDown.setVisible(visible) |
---|
| 136 | self.lblSmearUp.setVisible(visible) |
---|
| 137 | self.txtSmearDown.setVisible(visible) |
---|
| 138 | self.txtSmearUp.setVisible(visible) |
---|
| 139 | self.label_14.setVisible(visible) |
---|
| 140 | self.label_15.setVisible(visible) |
---|
| 141 | self.setAccuracyVisibility() |
---|
| 142 | |
---|
| 143 | def setAccuracyVisibility(self): |
---|
| 144 | """ |
---|
| 145 | Accuracy combobox visibility |
---|
| 146 | """ |
---|
[180bd54] | 147 | if self.have_data == Data2D and self.cbSmearing.currentIndex() == 1: |
---|
[e1e3e09] | 148 | self.cbAccuracy.setVisible(True) |
---|
| 149 | else: |
---|
| 150 | self.cbAccuracy.setVisible(False) |
---|
| 151 | |
---|
| 152 | def setPinholeLabels(self): |
---|
| 153 | """ |
---|
| 154 | Use pinhole labels |
---|
| 155 | """ |
---|
| 156 | self.lblSmearUp.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">low</span></p></body></html>') |
---|
| 157 | self.lblSmearDown.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">high</span></p></body></html>') |
---|
| 158 | |
---|
| 159 | def setSlitLabels(self): |
---|
| 160 | """ |
---|
| 161 | Use pinhole labels |
---|
| 162 | """ |
---|
| 163 | self.lblSmearUp.setText('Slit height') |
---|
| 164 | self.lblSmearDown.setText('Slit width') |
---|
| 165 | |
---|
| 166 | def state(self): |
---|
| 167 | """ |
---|
| 168 | Returns current state of controls |
---|
| 169 | """ |
---|
[98b13f72] | 170 | # or model-held values |
---|
| 171 | smearing = str(self.model.item(MODEL.index('SMEARING')).text()) |
---|
[180bd54] | 172 | accuracy = "" |
---|
| 173 | d_down = None |
---|
| 174 | d_up = None |
---|
| 175 | if smearing != "None": |
---|
| 176 | accuracy = str(self.model.item(MODEL.index('ACCURACY')).text()) |
---|
| 177 | try: |
---|
| 178 | d_down = float(self.model.item(MODEL.index('PINHOLE_MIN')).text()) |
---|
| 179 | except ValueError: |
---|
| 180 | d_down = None |
---|
| 181 | try: |
---|
| 182 | d_up = float(self.model.item(MODEL.index('PINHOLE_MAX')).text()) |
---|
| 183 | except ValueError: |
---|
| 184 | d_up = None |
---|
[98b13f72] | 185 | |
---|
| 186 | return (smearing, accuracy, d_down, d_up) |
---|
[672b8ab] | 187 | |
---|
| 188 | def setState(self, smearing, accuracy, d_down, d_up): |
---|
| 189 | """ |
---|
| 190 | Sets new values for the controls |
---|
| 191 | """ |
---|
| 192 | # Update the model -> controls update automatically |
---|
| 193 | if smearing is not None: |
---|
| 194 | self.model.item(MODEL.index('SMEARING')).setText(smearing) |
---|
| 195 | if accuracy is not None: |
---|
| 196 | self.model.item(MODEL.index('ACCURACY')).setText(accuracy) |
---|
| 197 | if d_down is not None: |
---|
| 198 | self.model.item(MODEL.index('PINHOLE_MIN')).setText(d_down) |
---|
| 199 | if d_up is not None: |
---|
| 200 | self.model.item(MODEL.index('PINHOLE_MAX')).setText(d_up) |
---|
| 201 | |
---|