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

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 7969b9c was 4992ff2, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Initial, in-progress version. Not really working atm. SASVIEW-787

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