source: sasview/src/sas/qtgui/DensityPanel.py @ 7b8485f

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 7b8485f was 31c5b58, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Refactored to allow running with run.py.
Minor fixes to plotting.

  • Property mode set to 100644
File size: 5.4 KB
Line 
1# global
2import logging
3import functools
4from PyQt4 import QtGui, QtCore
5
6from periodictable import formula as Formula
7
8from sas.qtgui.GuiUtils import FormulaValidator
9
10# Local UI
11from sas.qtgui.UI.DensityPanel import Ui_DensityPanel
12
13def enum(*sequential, **named):
14    enums = dict(zip(sequential, range(len(sequential))), **named)
15    return type('Enum', (), enums)
16
17MODEL = enum(
18    'MOLECULAR_FORMULA',
19    'MOLAR_MASS',
20    'MOLAR_VOLUME',
21    'MASS_DENSITY',
22)
23
24MODES = enum(
25    'VOLUME_TO_DENSITY',
26    'DENSITY_TO_VOLUME',
27)
28
29def 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
39class DensityPanel(QtGui.QDialog):
40
41    def __init__(self, parent=None):
42        super(DensityPanel, self).__init__()
43
44        self.mode = None
45        self.manager = parent
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
54        # no reason to have this widget resizable
55        self.setFixedSize(self.minimumSizeHint())
56
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
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)
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
78        self.model.dataChanged.connect(self.dataChanged)
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
122        except (ArithmeticError, ValueError):
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
133        except (ArithmeticError, ValueError):
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()
146
147    def displayHelp(self):
148        try:
149            location = self.manager.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
Note: See TracBrowser for help on using the repository browser.