source: sasview/src/sas/qtgui/Calculators/SldPanel.py @ 12dfce6

ESS_GUIESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_opencl
Last change on this file since 12dfce6 was 33c0561, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 6 years ago

Replace Apply button menu driven functionality with additional button.
Removed Cancel.
Removed the window system context help button from all affected widgets.
SASVIEW-1239

  • Property mode set to 100644
File size: 8.2 KB
Line 
1# global
2import logging
3from PyQt5 import QtCore
4from PyQt5 import QtGui
5from PyQt5 import QtWidgets
6
7from periodictable import formula as Formula
8from periodictable.xsf import xray_energy, xray_sld
9from periodictable.nsf import neutron_scattering
10
11import sas.qtgui.Utilities.GuiUtils as GuiUtils
12
13from sas.qtgui.UI import main_resources_rc
14
15# Local UI
16from sas.qtgui.Calculators.UI.SldPanel import Ui_SldPanel
17
18from sas.qtgui.Utilities.GuiUtils import enum
19
20MODEL = enum(
21    'MOLECULAR_FORMULA',
22    'MASS_DENSITY',
23    'NEUTRON_WAVELENGTH',
24    'NEUTRON_SLD_REAL',
25    'NEUTRON_SLD_IMAG',
26    'XRAY_WAVELENGTH',
27    'XRAY_SLD_REAL',
28    'XRAY_SLD_IMAG',
29    'NEUTRON_INC_XS',
30    'NEUTRON_ABS_XS',
31    'NEUTRON_LENGTH',
32)
33
34class SldResult(object):
35    def __init__(self, molecular_formula, mass_density,
36        neutron_wavelength, neutron_sld_real, neutron_sld_imag,
37        xray_wavelength, xray_sld_real, xray_sld_imag,
38        neutron_inc_xs, neutron_abs_xs, neutron_length):
39
40        self.molecular_formula = molecular_formula
41        self.mass_density = mass_density
42        self.neutron_wavelength = neutron_wavelength
43        self.neutron_sld_real = neutron_sld_real
44        self.neutron_sld_imag = neutron_sld_imag
45        self.xray_wavelength = xray_wavelength
46        self.xray_sld_real = xray_sld_real
47        self.xray_sld_imag = xray_sld_imag
48        self.neutron_inc_xs = neutron_inc_xs
49        self.neutron_abs_xs = neutron_abs_xs
50        self.neutron_length = neutron_length
51
52def sldAlgorithm(molecular_formula, mass_density, neutron_wavelength, xray_wavelength):
53
54    xray_sld_real, xray_sld_imag = xray_sld(
55            compound=molecular_formula,
56            density=mass_density,
57            wavelength=xray_wavelength)
58
59    (neutron_sld_real, neutron_sld_imag, _), (_, neutron_abs_xs, neutron_inc_xs), neutron_length = \
60        neutron_scattering(
61            compound=molecular_formula,
62            density=mass_density,
63            wavelength=neutron_wavelength)
64
65    SCALE = 1e-6
66
67    # neutron sld
68    scaled_neutron_sld_real = SCALE * neutron_sld_real
69    scaled_neutron_sld_imag = SCALE * abs(neutron_sld_imag)
70
71    # xray sld
72    scaled_xray_sld_real = SCALE * xray_sld_real
73    scaled_xray_sld_imag = SCALE * abs(xray_sld_imag)
74
75
76    return SldResult(
77        molecular_formula, mass_density,
78        neutron_wavelength, scaled_neutron_sld_real, scaled_neutron_sld_imag,
79        xray_wavelength, scaled_xray_sld_real, scaled_xray_sld_imag,
80        neutron_inc_xs, neutron_abs_xs, neutron_length)
81
82
83class SldPanel(QtWidgets.QDialog):
84
85    def __init__(self, parent=None):
86        super(SldPanel, self).__init__()
87
88        self.manager = parent
89
90        self.setupUi()
91        # disable the context help icon
92        self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint)
93
94        self.setupModel()
95        self.setupMapper()
96
97    def _getOutputs(self):
98        return {
99            MODEL.NEUTRON_SLD_REAL: self.ui.editNeutronSldReal,
100            MODEL.NEUTRON_SLD_IMAG: self.ui.editNeutronSldImag,
101            MODEL.XRAY_SLD_REAL: self.ui.editXraySldReal,
102            MODEL.XRAY_SLD_IMAG: self.ui.editXraySldImag,
103            MODEL.NEUTRON_INC_XS: self.ui.editNeutronIncXs,
104            MODEL.NEUTRON_ABS_XS: self.ui.editNeutronAbsXs,
105            MODEL.NEUTRON_LENGTH: self.ui.editNeutronLength
106        }
107
108    def setupUi(self):
109        self.ui = Ui_SldPanel()
110        self.ui.setupUi(self)
111
112        # set validators
113        # TODO: GuiUtils.FormulaValidator() crashes with Qt5 - fix
114        #self.ui.editMolecularFormula.setValidator(GuiUtils.FormulaValidator(self.ui.editMolecularFormula))
115
116        rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
117        self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity))
118        self.ui.editNeutronWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editNeutronWavelength))
119        self.ui.editXrayWavelength.setValidator(QtGui.QRegExpValidator(rx, self.ui.editXrayWavelength))
120
121        # signals
122        self.ui.helpButton.clicked.connect(self.displayHelp)
123        self.ui.closeButton.clicked.connect(self.closePanel)
124        self.ui.recalculateButton.clicked.connect(self.calculateSLD)
125
126    def calculateSLD(self):
127        self.recalculateSLD()
128
129    def setupModel(self):
130        self.model = QtGui.QStandardItemModel(self)
131        self.model.setItem(MODEL.MOLECULAR_FORMULA , QtGui.QStandardItem())
132        self.model.setItem(MODEL.MASS_DENSITY      , QtGui.QStandardItem())
133        self.model.setItem(MODEL.NEUTRON_WAVELENGTH, QtGui.QStandardItem())
134        self.model.setItem(MODEL.XRAY_WAVELENGTH   , QtGui.QStandardItem())
135
136        for key in list(self._getOutputs().keys()):
137            self.model.setItem(key, QtGui.QStandardItem())
138
139        self.model.dataChanged.connect(self.dataChanged)
140
141        self.ui.editMassDensity.textChanged.connect(self.recalculateSLD)
142        self.ui.editMolecularFormula.textChanged.connect(self.recalculateSLD)
143        self.ui.editNeutronWavelength.textChanged.connect(self.recalculateSLD)
144        self.ui.editXrayWavelength.textChanged.connect(self.recalculateSLD)
145
146        self.modelReset()
147
148    def setupMapper(self):
149        self.mapper = QtWidgets.QDataWidgetMapper(self)
150        self.mapper.setModel(self.model)
151        self.mapper.setOrientation(QtCore.Qt.Vertical)
152        self.mapper.addMapping(self.ui.editMolecularFormula , MODEL.MOLECULAR_FORMULA)
153        self.mapper.addMapping(self.ui.editMassDensity      , MODEL.MASS_DENSITY)
154        self.mapper.addMapping(self.ui.editNeutronWavelength, MODEL.NEUTRON_WAVELENGTH)
155        self.mapper.addMapping(self.ui.editXrayWavelength   , MODEL.XRAY_WAVELENGTH)
156
157        for key, edit in self._getOutputs().items():
158            self.mapper.addMapping(edit, key)
159
160        self.mapper.toFirst()
161
162    def dataChanged(self, top, bottom):
163        update = False
164        for index in range(top.row(), bottom.row() + 1):
165            if (index == MODEL.MOLECULAR_FORMULA) or (index == MODEL.MASS_DENSITY) or (index == MODEL.NEUTRON_WAVELENGTH) or (index == MODEL.XRAY_WAVELENGTH):
166                update = True
167
168        # calculation
169        if update:
170            self.recalculateSLD()
171
172    def recalculateSLD(self):
173        formula = self.ui.editMolecularFormula.text()
174        density = self.ui.editMassDensity.text()
175        neutronWavelength = self.ui.editNeutronWavelength.text()
176        xrayWavelength = self.ui.editXrayWavelength.text()
177
178        if len(formula) > 0 and len(density) > 0 and len(neutronWavelength) > 0 and len(xrayWavelength) > 0:
179            try:
180                results = sldAlgorithm(str(formula), float(density), float(neutronWavelength), float(xrayWavelength))
181
182                def format(value):
183                    return ("%-5.3g" % value).strip()
184
185                self.model.item(MODEL.NEUTRON_SLD_REAL).setText(format(results.neutron_sld_real))
186                self.model.item(MODEL.NEUTRON_SLD_IMAG).setText(format(results.neutron_sld_imag))
187
188                self.model.item(MODEL.XRAY_SLD_REAL).setText(format(results.xray_sld_real))
189                self.model.item(MODEL.XRAY_SLD_IMAG).setText(format(results.xray_sld_imag))
190
191                self.model.item(MODEL.NEUTRON_INC_XS).setText(format(results.neutron_inc_xs))
192                self.model.item(MODEL.NEUTRON_ABS_XS).setText(format(results.neutron_abs_xs))
193                self.model.item(MODEL.NEUTRON_LENGTH).setText(format(results.neutron_length))
194
195                return
196
197            except Exception as e:
198                pass
199
200        for key in list(self._getOutputs().keys()):
201            self.model.item(key).setText("")
202
203    def modelReset(self):
204        #self.model.beginResetModel()
205        try:
206            self.model.item(MODEL.MOLECULAR_FORMULA ).setText("H2O")
207            self.model.item(MODEL.MASS_DENSITY      ).setText("1.0")
208            self.model.item(MODEL.NEUTRON_WAVELENGTH).setText("6.0")
209            self.model.item(MODEL.XRAY_WAVELENGTH   ).setText("1.0")
210            self.recalculateSLD()
211        finally:
212            pass
213        #self.model.endResetModel()
214
215    def displayHelp(self):
216        location = "/user/qtgui/Calculators/sld_calculator_help.html"
217        self.manager.showHelp(location)
218
219
220    def closePanel(self):
221        """
222        close the window containing this panel
223        """
224        self.close()
225
Note: See TracBrowser for help on using the repository browser.