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

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since bfb5d9e was d813cf9a, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Fix presentation of results in the Density calculator

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