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