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