source: sasview/src/sas/qtgui/Calculators/DensityPanel.py @ 464cd07

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 464cd07 was b0c5e8c, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Unit tests for fitting options dialog SASVIEW-514

  • Property mode set to 100644
File size: 5.5 KB
Line 
1# global
2import logging
3import functools
4from PyQt4 import QtGui, QtCore
5
6from periodictable import formula as Formula
7
8from sas.qtgui.Utilities.GuiUtils import FormulaValidator
9from sas.qtgui.UI import main_resources_rc
10from sas.qtgui.Utilities.GuiUtils import HELP_DIRECTORY_LOCATION
11
12# Local UI
13from sas.qtgui.Calculators.UI.DensityPanel import Ui_DensityPanel
14
15def enum(*sequential, **named):
16    enums = dict(zip(sequential, range(len(sequential))), **named)
17    return type('Enum', (), enums)
18
19MODEL = enum(
20    'MOLECULAR_FORMULA',
21    'MOLAR_MASS',
22    'MOLAR_VOLUME',
23    'MASS_DENSITY',
24)
25
26MODES = enum(
27    'VOLUME_TO_DENSITY',
28    'DENSITY_TO_VOLUME',
29)
30
31def toMolarMass(formula):
32    AVOGADRO = 6.02214129e23
33
34    try:
35        f = Formula(str(formula))
36        return "%g" % (f.molecular_mass * AVOGADRO)
37    except:
38        return ""
39
40
41class DensityPanel(QtGui.QDialog):
42
43    def __init__(self, parent=None):
44        super(DensityPanel, self).__init__()
45
46        self.mode = None
47        self.manager = parent
48        self.setupUi()
49        self.setupModel()
50        self.setupMapper()
51
52    def setupUi(self):
53        self.ui = Ui_DensityPanel()
54        self.ui.setupUi(self)
55
56        # no reason to have this widget resizable
57        self.setFixedSize(self.minimumSizeHint())
58
59        # set validators
60        self.ui.editMolecularFormula.setValidator(FormulaValidator(self.ui.editMolecularFormula))
61
62        rx = QtCore.QRegExp("[+\-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+\-]?\d+)?")
63        self.ui.editMolarVolume.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMolarVolume))
64        self.ui.editMassDensity.setValidator(QtGui.QRegExpValidator(rx, self.ui.editMassDensity))
65
66        # signals
67        self.ui.editMolarVolume.textEdited.connect(functools.partial(self.setMode, MODES.VOLUME_TO_DENSITY))
68        self.ui.editMassDensity.textEdited.connect(functools.partial(self.setMode, MODES.DENSITY_TO_VOLUME))
69
70        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Reset).clicked.connect(self.modelReset)
71        self.ui.buttonBox.button(QtGui.QDialogButtonBox.Help).clicked.connect(self.displayHelp)
72
73    def setupModel(self):
74        self.model = QtGui.QStandardItemModel(self)
75        self.model.setItem(MODEL.MOLECULAR_FORMULA, QtGui.QStandardItem())
76        self.model.setItem(MODEL.MOLAR_MASS       , QtGui.QStandardItem())
77        self.model.setItem(MODEL.MOLAR_VOLUME     , QtGui.QStandardItem())
78        self.model.setItem(MODEL.MASS_DENSITY     , QtGui.QStandardItem())
79
80        self.model.dataChanged.connect(self.dataChanged)
81
82        self.modelReset()
83
84    def setupMapper(self):
85        self.mapper = QtGui.QDataWidgetMapper(self)
86        self.mapper.setModel(self.model)
87        self.mapper.setOrientation(QtCore.Qt.Vertical)
88
89        self.mapper.addMapping(self.ui.editMolecularFormula, MODEL.MOLECULAR_FORMULA)
90        self.mapper.addMapping(self.ui.editMolarMass       , MODEL.MOLAR_MASS)
91        self.mapper.addMapping(self.ui.editMolarVolume     , MODEL.MOLAR_VOLUME)
92        self.mapper.addMapping(self.ui.editMassDensity     , MODEL.MASS_DENSITY)
93
94        self.mapper.toFirst()
95
96    def dataChanged(self, top, bottom):
97        for index in xrange(top.row(), bottom.row() + 1):
98            if index == MODEL.MOLECULAR_FORMULA:
99                molarMass = toMolarMass(self.model.item(MODEL.MOLECULAR_FORMULA).text())
100                self.model.item(MODEL.MOLAR_MASS).setText(molarMass)
101
102                if self.mode == MODES.VOLUME_TO_DENSITY:
103                    self._updateDensity()
104                elif self.mode == MODES.DENSITY_TO_VOLUME:
105                    self._updateVolume()
106
107            elif index == MODEL.MOLAR_VOLUME and self.mode == MODES.VOLUME_TO_DENSITY:
108                self._updateDensity()
109
110            elif index == MODEL.MASS_DENSITY and self.mode == MODES.DENSITY_TO_VOLUME:
111                self._updateVolume()
112
113    def setMode(self, mode):
114        self.mode = mode
115
116    def _updateDensity(self):
117        try:
118            molarMass = float(toMolarMass(self.model.item(MODEL.MOLECULAR_FORMULA).text()))
119            molarVolume = float(self.model.item(MODEL.MOLAR_VOLUME).text())
120
121            molarDensity = molarMass / molarVolume
122            self.model.item(MODEL.MASS_DENSITY).setText(str(molarDensity))
123
124        except (ArithmeticError, ValueError):
125            self.model.item(MODEL.MASS_DENSITY).setText("")
126
127    def _updateVolume(self):
128        try:
129            molarMass = float(toMolarMass(self.model.item(MODEL.MOLECULAR_FORMULA).text()))
130            molarDensity = float(self.model.item(MODEL.MASS_DENSITY).text())
131
132            molarVolume = molarMass / molarDensity
133            self.model.item(MODEL.MOLAR_VOLUME).setText(str(molarVolume))
134
135        except (ArithmeticError, ValueError):
136            self.model.item(MODEL.MOLAR_VOLUME).setText("")
137
138    def modelReset(self):
139        #self.model.beginResetModel()
140        try:
141            self.setMode(None)
142            self.model.item(MODEL.MOLECULAR_FORMULA).setText("H2O")
143            self.model.item(MODEL.MOLAR_VOLUME     ).setText("")
144            self.model.item(MODEL.MASS_DENSITY     ).setText("")
145        finally:
146            pass
147            #self.model.endResetModel()
148
149    def displayHelp(self):
150        try:
151            location = HELP_DIRECTORY_LOCATION + \
152                "/user/sasgui/perspectives/calculator/density_calculator_help.html"
153
154            self.manager._helpView.load(QtCore.QUrl(location))
155            self.manager._helpView.show()
156        except AttributeError:
157            # No manager defined - testing and standalone runs
158            pass
Note: See TracBrowser for help on using the repository browser.