source: sasview/src/sas/qtgui/Perspectives/Fitting/SmearingWidget.py @ d6b8a1d

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since d6b8a1d was d6b8a1d, checked in by Piotr Rozyczko <rozyczko@…>, 4 years ago

More Qt5 related fixes

  • Property mode set to 100644
File size: 6.5 KB
Line 
1"""
2Widget/logic for smearing data.
3"""
4from PyQt5 import QtCore
5from PyQt5 import QtGui
6from PyQt5 import QtWidgets
7
8from sas.qtgui.Plotting.PlotterData import Data1D
9from sas.qtgui.Plotting.PlotterData import Data2D
10import sas.qtgui.Utilities.GuiUtils as GuiUtils
11
12# Local UI
13from sas.qtgui.Perspectives.Fitting.UI.SmearingWidgetUI import Ui_SmearingWidgetUI
14
15class DataWidgetMapper(QtWidgets.QDataWidgetMapper):
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
26        if isinstance(widget, QtWidgets.QComboBox):
27            delegate = self.itemDelegate()
28            widget.currentIndexChanged.connect(lambda: delegate.commitData.emit(widget))
29
30SMEARING_1D = ["Custom Pinhole Smear", "Custom Slit Smear"]
31SMEARING_2D = ["Custom Pinhole Smear"]
32
33MODEL = [
34    'SMEARING',
35    'PINHOLE_MIN',
36    'PINHOLE_MAX',
37    'ACCURACY']
38
39class SmearingWidget(QtWidgets.QWidget, Ui_SmearingWidgetUI):
40    def __init__(self, parent=None):
41        super(SmearingWidget, self).__init__()
42
43        self.setupUi(self)
44
45        # Have we loaded data yet? If so, what kind
46        self.have_data = None
47        # Local model for holding data
48        self.model = None
49        # Mapper for model update
50        self.mapper = None
51
52        self.parent = parent
53        # Let only floats in the line edits
54        self.txtSmearDown.setValidator(GuiUtils.DoubleValidator())
55        self.txtSmearUp.setValidator(GuiUtils.DoubleValidator())
56
57        # Attach slots
58        self.cbSmearing.currentIndexChanged.connect(self.onIndexChange)
59        self.cbSmearing.setCurrentIndex(0)
60
61        self.initModel()
62        self.initMapper()
63
64    def initModel(self):
65        """
66        Initialize the state
67        """
68        self.model = QtGui.QStandardItemModel()
69        for model_item in range(len(MODEL)):
70            self.model.setItem(model_item, QtGui.QStandardItem())
71        # Attach slot
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'))
87
88        # FIXME DOESNT WORK WITH QT5
89        #self.mapper.toFirst()
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)
103            self.have_data = Data1D
104        else:
105            self.cbSmearing.addItems(SMEARING_2D)
106            self.have_data = Data2D
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        """
125        Respond to model change by updating
126        """
127        #print "MODEL CHANGED for property: %s. The value is now: %s" % \
128        #    (MODEL[top.row()], str(self.model.item(top.row()).text()))
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        """
147        if self.have_data == Data2D and self.cbSmearing.currentIndex() == 1:
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        """
170        # or model-held values
171        smearing = str(self.model.item(MODEL.index('SMEARING')).text())
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
185
186        return (smearing, accuracy, d_down, d_up)
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
Note: See TracBrowser for help on using the repository browser.