[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 | |
---|
[83eb5208] | 8 | from sas.qtgui.Utilities.GuiUtils import FormulaValidator |
---|
[cd2cc745] | 9 | from sas.qtgui.UI import main_resources_rc |
---|
[b0c5e8c] | 10 | from sas.qtgui.Utilities.GuiUtils import HELP_DIRECTORY_LOCATION |
---|
[6083d6e] | 11 | |
---|
| 12 | # Local UI |
---|
[83eb5208] | 13 | from sas.qtgui.Calculators.UI.DensityPanel import Ui_DensityPanel |
---|
[6083d6e] | 14 | |
---|
| 15 | def enum(*sequential, **named): |
---|
| 16 | enums = dict(zip(sequential, range(len(sequential))), **named) |
---|
| 17 | return type('Enum', (), enums) |
---|
| 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(QtGui.QDialog): |
---|
| 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 |
---|
| 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 |
---|
[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 | |
---|
| 70 | self.ui.buttonBox.button(QtGui.QDialogButtonBox.Reset).clicked.connect(self.modelReset) |
---|
| 71 | self.ui.buttonBox.button(QtGui.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): |
---|
| 85 | self.mapper = QtGui.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 xrange(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 | |
---|
[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 | #self.model.beginResetModel() |
---|
| 140 | try: |
---|
| 141 | self.setMode(None) |
---|
| 142 | self.model.item(MODEL.MOLECULAR_FORMULA).setText("H2O") |
---|
| 143 | self.model.item(MODEL.MOLAR_VOLUME ).setText("") |
---|
| 144 | self.model.item(MODEL.MASS_DENSITY ).setText("") |
---|
| 145 | finally: |
---|
| 146 | pass |
---|
| 147 | #self.model.endResetModel() |
---|
[1d85b5e] | 148 | |
---|
| 149 | def displayHelp(self): |
---|
| 150 | try: |
---|
[b0c5e8c] | 151 | location = HELP_DIRECTORY_LOCATION + \ |
---|
[1d85b5e] | 152 | "/user/sasgui/perspectives/calculator/density_calculator_help.html" |
---|
[9968d6a] | 153 | |
---|
[1d85b5e] | 154 | self.manager._helpView.load(QtCore.QUrl(location)) |
---|
| 155 | self.manager._helpView.show() |
---|
| 156 | except AttributeError: |
---|
| 157 | # No manager defined - testing and standalone runs |
---|
| 158 | pass |
---|