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

Last change on this file since c57ecca was dc5ef15, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Removed qtgui dependency on sasgui and wx SASVIEW-590

  • Property mode set to 100644
File size: 6.4 KB
RevLine 
[e1e3e09]1"""
2Widget/logic for smearing data.
3"""
4from PyQt4 import QtGui
5from PyQt4 import QtCore
6
[dc5ef15]7from sas.qtgui.Plotting.PlotterData import Data1D
8from sas.qtgui.Plotting.PlotterData import Data2D
[e1e3e09]9
10# Local UI
[1bc27f1]11from sas.qtgui.Perspectives.Fitting.UI.SmearingWidgetUI import Ui_SmearingWidgetUI
[e1e3e09]12
[180bd54]13class 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
28SMEARING_1D = ["Custom Pinhole Smear", "Custom Slit Smear"]
29SMEARING_2D = ["Custom Pinhole Smear"]
30
31MODEL = [
32    'SMEARING',
33    'PINHOLE_MIN',
34    'PINHOLE_MAX',
35    'ACCURACY']
36
37class 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
[1bc27f1]50        self.parent = parent
[98b13f72]51        # Let only floats in the line edits
52        self.txtSmearDown.setValidator(QtGui.QDoubleValidator())
53        self.txtSmearUp.setValidator(QtGui.QDoubleValidator())
54
55        # Attach slots
56        self.cbSmearing.currentIndexChanged.connect(self.onIndexChange)
57        self.cbSmearing.setCurrentIndex(0)
58
[e1e3e09]59        self.initModel()
60        self.initMapper()
61
62    def initModel(self):
63        """
64        Initialize the state
65        """
66        self.model = QtGui.QStandardItemModel()
67        for model_item in xrange(len(MODEL)):
68            self.model.setItem(model_item, QtGui.QStandardItem())
[98b13f72]69        # Attach slot
[e1e3e09]70        self.model.dataChanged.connect(self.onModelChange)
71
72    def initMapper(self):
73        """
74        Initialize model item <-> UI element mapping
75        """
76        self.mapper = DataWidgetMapper(self)
77
78        self.mapper.setModel(self.model)
79        self.mapper.setOrientation(QtCore.Qt.Vertical)
80
81        self.mapper.addMapping(self.txtSmearUp,   MODEL.index('PINHOLE_MIN'))
82        self.mapper.addMapping(self.txtSmearDown, MODEL.index('PINHOLE_MAX'))
83        self.mapper.addMapping(self.cbSmearing,   MODEL.index('SMEARING'))
84        self.mapper.addMapping(self.cbAccuracy,   MODEL.index('ACCURACY'))
85        self.mapper.toFirst()
86
87    def updateSmearing(self, data=None):
88        """
89        Update control elements based on data passed
90        """
91        self.cbSmearing.clear()
92        self.cbSmearing.addItem("None")
93        self.cbAccuracy.setVisible(False)
94
95        if data is None:
96            self.setElementsVisibility(False)
97        elif isinstance(data, Data1D):
98            self.cbSmearing.addItems(SMEARING_1D)
[180bd54]99            self.have_data = Data1D
[e1e3e09]100        else:
101            self.cbSmearing.addItems(SMEARING_2D)
[180bd54]102            self.have_data = Data2D
[e1e3e09]103        self.cbSmearing.setCurrentIndex(0)
104
105    def onIndexChange(self, index):
106        """
107        Callback for smearing combobox index change
108        """
109        if index == 0:
110            self.setElementsVisibility(False)
111            return
112        elif index == 1:
113            self.setElementsVisibility(True)
114            self.setPinholeLabels()
115        elif index == 2:
116            self.setElementsVisibility(True)
117            self.setSlitLabels()
118
119    def onModelChange(self, top, bottom):
120        """
[98b13f72]121        Respond to model change by updating
[e1e3e09]122        """
[180bd54]123        #print "MODEL CHANGED for property: %s. The value is now: %s" % \
124        #    (MODEL[top.row()], str(self.model.item(top.row()).text()))
[e1e3e09]125        pass
126
127    def setElementsVisibility(self, visible):
128        """
129        Labels and linedits visibility control
130        """
131        self.lblSmearDown.setVisible(visible)
132        self.lblSmearUp.setVisible(visible)
133        self.txtSmearDown.setVisible(visible)
134        self.txtSmearUp.setVisible(visible)
135        self.label_14.setVisible(visible)
136        self.label_15.setVisible(visible)
137        self.setAccuracyVisibility()
138
139    def setAccuracyVisibility(self):
140        """
141        Accuracy combobox visibility
142        """
[180bd54]143        if self.have_data == Data2D and self.cbSmearing.currentIndex() == 1:
[e1e3e09]144            self.cbAccuracy.setVisible(True)
145        else:
146            self.cbAccuracy.setVisible(False)
147
148    def setPinholeLabels(self):
149        """
150        Use pinhole labels
151        """
152        self.lblSmearUp.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">low</span></p></body></html>')
153        self.lblSmearDown.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">high</span></p></body></html>')
154
155    def setSlitLabels(self):
156        """
157        Use pinhole labels
158        """
159        self.lblSmearUp.setText('Slit height')
160        self.lblSmearDown.setText('Slit width')
161
162    def state(self):
163        """
164        Returns current state of controls
165        """
[98b13f72]166        # or model-held values
167        smearing = str(self.model.item(MODEL.index('SMEARING')).text())
[180bd54]168        accuracy = ""
169        d_down = None
170        d_up = None
171        if smearing != "None":
172            accuracy = str(self.model.item(MODEL.index('ACCURACY')).text())
173            try:
174                d_down = float(self.model.item(MODEL.index('PINHOLE_MIN')).text())
175            except ValueError:
176                d_down = None
177            try:
178                d_up = float(self.model.item(MODEL.index('PINHOLE_MAX')).text())
179            except ValueError:
180                d_up = None
[98b13f72]181
182        return (smearing, accuracy, d_down, d_up)
[672b8ab]183
184    def setState(self, smearing, accuracy, d_down, d_up):
185        """
186        Sets new values for the controls
187        """
188        # Update the model -> controls update automatically
189        if smearing is not None:
190            self.model.item(MODEL.index('SMEARING')).setText(smearing)
191        if accuracy is not None:
192            self.model.item(MODEL.index('ACCURACY')).setText(accuracy)
193        if d_down is not None:
194            self.model.item(MODEL.index('PINHOLE_MIN')).setText(d_down)
195        if d_up is not None:
196            self.model.item(MODEL.index('PINHOLE_MAX')).setText(d_up)
197
Note: See TracBrowser for help on using the repository browser.