1 | # global |
---|
2 | import logging |
---|
3 | import functools |
---|
4 | from PyQt5 import QtCore |
---|
5 | from PyQt5 import QtGui |
---|
6 | from PyQt5 import QtWidgets |
---|
7 | |
---|
8 | from periodictable import formula as Formula |
---|
9 | |
---|
10 | from sas.qtgui.Utilities.GuiUtils import FormulaValidator |
---|
11 | from sas.qtgui.UI import main_resources_rc |
---|
12 | from sas.qtgui.Utilities.GuiUtils import HELP_DIRECTORY_LOCATION |
---|
13 | |
---|
14 | # Local UI |
---|
15 | from sas.qtgui.Calculators.UI.DensityPanel import Ui_DensityPanel |
---|
16 | |
---|
17 | from sas.qtgui.Utilities.GuiUtils import enum |
---|
18 | |
---|
19 | MODEL = enum( |
---|
20 | 'MOLECULAR_FORMULA', |
---|
21 | 'MOLAR_MASS', |
---|
22 | 'MOLAR_VOLUME', |
---|
23 | 'MASS_DENSITY', |
---|
24 | ) |
---|
25 | |
---|
26 | MODES = enum( |
---|
27 | 'VOLUME_TO_DENSITY', |
---|
28 | 'DENSITY_TO_VOLUME', |
---|
29 | ) |
---|
30 | |
---|
31 | def 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 | |
---|
41 | class 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 |
---|