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

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

Show help pages in default browser. Fixed some help links and modified unit tests. SASVIEW-800

  • Property mode set to 100644
File size: 5.1 KB
RevLine 
[6083d6e]1# global
2import logging
[1d85b5e]3import functools
[4992ff2]4from PyQt5 import QtCore
5from PyQt5 import QtGui
6from PyQt5 import QtWidgets
[1d85b5e]7
[6083d6e]8from periodictable import formula as Formula
9
[83eb5208]10from sas.qtgui.Utilities.GuiUtils import FormulaValidator
[cd2cc745]11from sas.qtgui.UI import main_resources_rc
[b0c5e8c]12from sas.qtgui.Utilities.GuiUtils import HELP_DIRECTORY_LOCATION
[6083d6e]13
14# Local UI
[83eb5208]15from sas.qtgui.Calculators.UI.DensityPanel import Ui_DensityPanel
[6083d6e]16
[d4881f6a]17from sas.qtgui.Utilities.GuiUtils import enum
[6083d6e]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
[4992ff2]41class DensityPanel(QtWidgets.QDialog):
[6083d6e]42
43    def __init__(self, parent=None):
[1d85b5e]44        super(DensityPanel, self).__init__()
[6083d6e]45
46        self.mode = None
[1d85b5e]47        self.manager = parent
[6083d6e]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
[f140169]56        # no reason to have this widget resizable
57        self.setFixedSize(self.minimumSizeHint())
58
[6083d6e]59        # set validators
[fbfc488]60        #self.ui.editMolecularFormula.setValidator(FormulaValidator(self.ui.editMolecularFormula))
[6083d6e]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
[1d85b5e]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
[4992ff2]70        self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Reset).clicked.connect(self.modelReset)
71        self.ui.buttonBox.button(QtWidgets.QDialogButtonBox.Help).clicked.connect(self.displayHelp)
[6083d6e]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
[1d85b5e]80        self.model.dataChanged.connect(self.dataChanged)
[6083d6e]81
82        self.modelReset()
83
84    def setupMapper(self):
[4992ff2]85        self.mapper = QtWidgets.QDataWidgetMapper(self)
[6083d6e]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
[fbfc488]94        self.mapper.toFirst()
[6083d6e]95
96    def dataChanged(self, top, bottom):
[b3e8629]97        for index in range(top.row(), bottom.row() + 1):
[6083d6e]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
[9968d6a]124        except (ArithmeticError, ValueError):
[6083d6e]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
[9968d6a]135        except (ArithmeticError, ValueError):
[6083d6e]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
[1d85b5e]146
147    def displayHelp(self):
[e90988c]148        location =  "/user/sasgui/perspectives/calculator/density_calculator_help.html"
149        self.manager.showHelp(location)
150
[9968d6a]151
Note: See TracBrowser for help on using the repository browser.