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