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

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 d1955d67 was d4881f6a, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Initial implementation of Adam Washington's Corfunc perspective.
Converted to py3/Qt5.

  • 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
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(QtWidgets.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(QtWidgets.QDialogButtonBox.Reset).clicked.connect(self.modelReset)
71        self.ui.buttonBox.button(QtWidgets.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 = QtWidgets.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 range(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        try:
140            self.setMode(None)
141            self.model.item(MODEL.MOLECULAR_FORMULA).setText("H2O")
142            self.model.item(MODEL.MOLAR_VOLUME     ).setText("")
143            self.model.item(MODEL.MASS_DENSITY     ).setText("")
144        finally:
145            pass
146
147    def displayHelp(self):
148        try:
149            location = HELP_DIRECTORY_LOCATION + \
150                "/user/sasgui/perspectives/calculator/density_calculator_help.html"
151
152            self.manager._helpView.load(QtCore.QUrl(location))
153            self.manager._helpView.show()
154        except AttributeError:
155            # No manager defined - testing and standalone runs
156            pass
Note: See TracBrowser for help on using the repository browser.