Changeset 49ab5d7 in sasview for src/sas/perspectives/calculator/density_panel.py
- Timestamp:
- Mar 4, 2015 1:28:39 PM (9 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 2f732b0
- Parents:
- 76aed53
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/perspectives/calculator/density_panel.py
r58c125d r49ab5d7 8 8 from wx.lib.scrolledpanel import ScrolledPanel 9 9 from sas.guiframe.utils import check_float 10 from sas.guiframe.events import StatusEvent 10 from sas.guiframe.events import StatusEvent 11 11 from periodictable import formula as Formula 12 12 from sas.perspectives.calculator import calculator_widgets as widget 13 13 from sas.guiframe.documentation_window import DocumentationWindow 14 15 AVOGADRO = 14 15 AVOGADRO = 6.02214129e23 16 16 _INPUTS = ['Mass Density', 'Molar Volume'] 17 17 _UNITS = ['g/cm^(3) ', 'cm^(3)/mol '] … … 27 27 PANEL_SIZE = 460 28 28 FONT_VARIANT = 1 29 29 30 30 class DensityPanel(ScrolledPanel, PanelBase): 31 31 """ … … 38 38 ## Flag to tell the AUI manager to put this panel in the center pane 39 39 CENTER_PANE = True 40 40 41 41 def __init__(self, parent, base=None, *args, **kwds): 42 42 """ … … 70 70 self.SetAutoLayout(True) 71 71 self.Layout() 72 72 73 73 def _do_layout(self): 74 74 """ … … 78 78 unit_density = self._unit_list[0] 79 79 unit_volume = self._unit_list[1] 80 80 81 81 # sizers 82 82 sizer_input = wx.GridBagSizer(5, 5) … … 86 86 self.sizer2 = wx.BoxSizer(wx.HORIZONTAL) 87 87 sizer3 = wx.BoxSizer(wx.HORIZONTAL) 88 vbox 89 88 vbox = wx.BoxSizer(wx.VERTICAL) 89 90 90 # inputs 91 91 inputbox = wx.StaticBox(self, -1, "Inputs") … … 97 97 self.compound_eg2 = wx.StaticText(self, -1, 'e.g., D2O') 98 98 self.input_cb = wx.ComboBox(self, -1, style=wx.CB_READONLY) 99 wx.EVT_COMBOBOX(self.input_cb, -1, self.on_select_input) 99 wx.EVT_COMBOBOX(self.input_cb, -1, self.on_select_input) 100 100 hint_input_name_txt = 'Mass or volume.' 101 self.input_cb.SetToolTipString(hint_input_name_txt) 101 self.input_cb.SetToolTipString(hint_input_name_txt) 102 102 unit_density1 = " " + unit_density 103 103 self.input_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) … … 107 107 ix = 0 108 108 sizer_input.Add(compound_txt, (iy, ix), (1, 1), 109 wx.LEFT |wx.EXPAND|wx.ADJUST_MINSIZE, 15)109 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 110 110 ix += 1 111 111 sizer_input.Add(self.compound_ctl, (iy, ix), (1, 1), 112 wx.EXPAND |wx.ADJUST_MINSIZE, 0)112 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 113 113 ix += 1 114 114 sizer_input.Add(self.compound_eg1, (iy, ix), (1, 1), 115 wx.EXPAND |wx.ADJUST_MINSIZE, 0)116 115 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 116 117 117 ix += 1 118 118 sizer_input.Add(self.compound_eg2, (iy, ix), (1, 1), 119 wx.EXPAND |wx.ADJUST_MINSIZE, 0)119 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 120 120 self.compound_eg1.Show(False) 121 121 iy += 1 122 122 ix = 0 123 123 sizer_input.Add(self.input_cb, (iy, ix), (1, 1), 124 wx.LEFT |wx.EXPAND|wx.ADJUST_MINSIZE, 15)124 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 125 125 ix += 1 126 126 sizer_input.Add(self.input_ctl, (iy, ix), (1, 1), 127 wx.EXPAND |wx.ADJUST_MINSIZE, 0)128 ix += 1129 sizer_input.Add(self.unit_input_density, (iy, ix), (1, 1),130 wx.EXPAND |wx.ADJUST_MINSIZE, 0)131 ix += 1127 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 128 ix += 1 129 sizer_input.Add(self.unit_input_density, (iy, ix), (1, 1), 130 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 131 ix += 1 132 132 self.unit_input_density.Show(False) 133 sizer_input.Add(self.unit_input_volume, (iy, ix), (1, 1),134 wx.EXPAND |wx.ADJUST_MINSIZE, 0)133 sizer_input.Add(self.unit_input_volume, (iy, ix), (1, 1), 134 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 135 135 boxsizer1.Add(sizer_input) 136 136 self.sizer1.Add(boxsizer1, 0, wx.EXPAND | wx.ALL, 10) 137 137 138 138 # outputs 139 139 outputbox = wx.StaticBox(self, -1, "Outputs") 140 140 boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL) 141 141 boxsizer2.SetMinSize((_STATICBOX_WIDTH, -1)) 142 142 143 143 molar_mass_txt = wx.StaticText(self, -1, 'Molar Mass ') 144 144 self.molar_mass_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) … … 147 147 self.molar_mass_unit1 = wx.StaticText(self, -1, ' g/mol') 148 148 self.molar_mass_unit2 = wx.StaticText(self, -1, 'g/mol') 149 149 150 150 self.output_cb = wx.ComboBox(self, -1, style=wx.CB_READONLY) 151 wx.EVT_COMBOBOX(self.output_cb, -1, self.on_select_output) 151 wx.EVT_COMBOBOX(self.output_cb, -1, self.on_select_output) 152 152 hint_output_name_txt = 'Mass or volume.' 153 self.output_cb.SetToolTipString(hint_output_name_txt) 153 self.output_cb.SetToolTipString(hint_output_name_txt) 154 154 list = [] 155 155 for item in self._input_list: … … 158 158 list.sort() 159 159 for idx in range(len(list)): 160 self.input_cb.Append(list[idx], idx)161 self.output_cb.Append(list[idx], idx)162 self.input_cb.SetStringSelection("Molar Volume") 163 self.output_cb.SetStringSelection("Mass Density") 160 self.input_cb.Append(list[idx], idx) 161 self.output_cb.Append(list[idx], idx) 162 self.input_cb.SetStringSelection("Molar Volume") 163 self.output_cb.SetStringSelection("Mass Density") 164 164 unit_volume = " " + unit_volume 165 165 self.output_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, -1)) … … 171 171 ix = 0 172 172 sizer_output.Add(molar_mass_txt, (iy, ix), (1, 1), 173 wx.LEFT |wx.EXPAND|wx.ADJUST_MINSIZE, 15)173 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 174 174 ix += 1 175 175 sizer_output.Add(self.molar_mass_ctl, (iy, ix), (1, 1), 176 wx.EXPAND |wx.ADJUST_MINSIZE, 0)176 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 177 177 ix += 1 178 178 sizer_output.Add(self.molar_mass_unit1, (iy, ix), (1, 1), 179 wx.EXPAND |wx.ADJUST_MINSIZE, 0)179 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 180 180 ix += 1 181 181 sizer_output.Add(self.molar_mass_unit2, (iy, ix), (1, 1), 182 wx.EXPAND |wx.ADJUST_MINSIZE, 0)182 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 183 183 self.molar_mass_unit1.Show(False) 184 184 iy += 1 185 185 ix = 0 186 186 sizer_output.Add(self.output_cb, (iy, ix), (1, 1), 187 wx.LEFT |wx.EXPAND|wx.ADJUST_MINSIZE, 15)187 wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 188 188 ix += 1 189 189 sizer_output.Add(self.output_ctl, (iy, ix), (1, 1), 190 wx.EXPAND |wx.ADJUST_MINSIZE, 0)191 ix += 1190 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 191 ix += 1 192 192 sizer_output.Add(self.unit_output_volume, 193 (iy, ix), (1, 1), wx.EXPAND |wx.ADJUST_MINSIZE, 0)193 (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) 194 194 ix += 1 195 195 sizer_output.Add(self.unit_output_density, 196 (iy, ix), (1, 1), wx.EXPAND |wx.ADJUST_MINSIZE, 0)197 196 (iy, ix), (1, 1), wx.EXPAND | wx.ADJUST_MINSIZE, 0) 197 198 198 self.unit_output_volume.Show(False) 199 199 boxsizer2.Add(sizer_output) 200 self.sizer2.Add(boxsizer2, 0, wx.EXPAND |wx.ALL, 10)201 200 self.sizer2.Add(boxsizer2, 0, wx.EXPAND | wx.ALL, 10) 201 202 202 # buttons 203 203 id = wx.NewId() 204 204 self.button_calculate = wx.Button(self, id, "Calculate") 205 205 self.button_calculate.SetToolTipString("Calculate.") 206 self.Bind(wx.EVT_BUTTON, self.calculate, id=id) 207 206 self.Bind(wx.EVT_BUTTON, self.calculate, id=id) 207 208 208 id = wx.NewId() 209 209 self.button_help = wx.Button(self, id, "HELP") 210 210 self.button_help.SetToolTipString("Help for density calculator.") 211 self.Bind(wx.EVT_BUTTON, self.on_help, id=id) 212 213 sizer_button.Add((150, 20), 1, wx.EXPAND |wx.ADJUST_MINSIZE, 0)214 sizer_button.Add(self.button_calculate, 0, 215 wx.RIGHT |wx.ADJUST_MINSIZE, 20)216 sizer_button.Add(self.button_help, 0, 217 wx.RIGHT |wx.ADJUST_MINSIZE, 20)211 self.Bind(wx.EVT_BUTTON, self.on_help, id=id) 212 213 sizer_button.Add((150, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) 214 sizer_button.Add(self.button_calculate, 0, 215 wx.RIGHT | wx.ADJUST_MINSIZE, 20) 216 sizer_button.Add(self.button_help, 0, 217 wx.RIGHT | wx.ADJUST_MINSIZE, 20) 218 218 sizer3.Add(sizer_button) 219 219 220 220 # layout 221 221 vbox.Add(self.sizer1) 222 222 vbox.Add(self.sizer2) 223 223 vbox.Add(sizer3) 224 vbox.Fit(self) 224 vbox.Fit(self) 225 225 self.SetSizer(vbox) 226 226 227 227 def on_select_input(self, event): 228 228 """ 229 On selection of input combobox, 229 On selection of input combobox, 230 230 update units and output combobox 231 231 """ … … 233 233 return 234 234 event.Skip() 235 235 236 236 combo = event.GetEventObject() 237 237 self._input = combo.GetValue() … … 242 242 243 243 self.set_values() 244 244 245 245 def on_select_output(self, event): 246 246 """ 247 On selection of output combobox, 247 On selection of output combobox, 248 248 update units and input combobox 249 249 """ … … 251 251 return 252 252 event.Skip() 253 253 254 254 combo = event.GetEventObject() 255 255 self._output = combo.GetValue() … … 260 260 261 261 self.set_values() 262 262 263 263 def set_values(self): 264 264 """ … … 281 281 self.unit_output_volume.Show(True) 282 282 self.unit_input_volume.Show(False) 283 self.unit_output_density.Show(False) 283 self.unit_output_density.Show(False) 284 284 else: 285 285 self.molar_mass_unit1.Show(False) … … 293 293 # layout 294 294 self.clear_outputs() 295 self.sizer1.Layout() 296 self.sizer2.Layout() 297 295 self.sizer1.Layout() 296 self.sizer2.Layout() 297 298 298 def get_input(self): 299 299 """ … … 301 301 """ 302 302 return self._input, self._output 303 303 304 304 def check_inputs(self): 305 305 """ … … 313 313 flag = False 314 314 input_type = str(self.input_cb.GetValue()) 315 msg += "Error for %s value :expect float" % input_type316 315 msg += "Error for %s value :expect float" % input_type 316 317 317 self.compound = self.compound_ctl.GetValue().lstrip().rstrip() 318 318 if self.compound != "": … … 332 332 msg += "Enter Formula" 333 333 return flag, msg 334 334 335 335 336 336 def calculate(self, event): … … 346 346 wx.PostEvent(self.base, StatusEvent(status=msg)) 347 347 if not flag: 348 return 348 return 349 349 #get ready to compute 350 350 mol_formula = Formula(self.compound) … … 355 355 except: 356 356 if self.base is not None: 357 msg = "Density/Volume Calculator: %s" %(sys.exc_value)357 msg = "Density/Volume Calculator: %s" % (sys.exc_value) 358 358 wx.PostEvent(self.base, StatusEvent(status=msg)) 359 359 if event is not None: 360 360 event.Skip() 361 362 def on_help(self, event): 361 362 def on_help(self, event): 363 363 """ 364 364 Bring up the density/volume calculator Documentation whenever 365 the HELP button is clicked. 366 365 the HELP button is clicked. 366 367 367 Calls DocumentationWindow with the path of the location within the 368 documentation tree (after /doc/ ....". Note that when using old 369 versions of Wx (before 2.9) and thus not the release version of 370 installers, the help comes up at the top level of the file as 368 documentation tree (after /doc/ ....". Note that when using old 369 versions of Wx (before 2.9) and thus not the release version of 370 installers, the help comes up at the top level of the file as 371 371 webbrowser does not pass anything past the # to the browser when it is 372 372 running "file:///...." 373 373 374 374 :param evt: Triggers on clicking the help button 375 375 """ 376 376 377 377 _TreeLocation = "user/perspectives/calculator/density_calculator_help.html" 378 378 _doc_viewer = DocumentationWindow(self, -1, \ 379 _TreeLocation, "Density/Volume Calculator Help")379 _TreeLocation, "Density/Volume Calculator Help") 380 380 381 381 def clear_outputs(self): … … 385 385 self.molar_mass_ctl.SetValue("") 386 386 self.output_ctl.SetValue("") 387 387 388 388 def _format_number(self, value=None): 389 389 """ 390 Return a float in a standardized, human-readable formatted string 391 """ 392 try: 390 Return a float in a standardized, human-readable formatted string 391 """ 392 try: 393 393 value = float(value) 394 394 except: … … 397 397 398 398 output = "%-12.5f" % value 399 return output.lstrip().rstrip() 400 399 return output.lstrip().rstrip() 400 401 401 class DensityWindow(widget.CHILD_FRAME): 402 402 """ 403 403 """ 404 404 def __init__(self, parent=None, title="Density/Volume Calculator", 405 base=None, manager=None, 406 size=(PANEL_SIZE *1.05, PANEL_SIZE/1.55), *args, **kwds):405 base=None, manager=None, 406 size=(PANEL_SIZE * 1.05, PANEL_SIZE / 1.55), *args, **kwds): 407 407 """ 408 408 """ … … 417 417 self.SetPosition((25, 10)) 418 418 self.Show(True) 419 419 420 420 def on_close(self, event): 421 421 """ … … 425 425 self.manager.cal_md_frame = None 426 426 self.Destroy() 427 428 427 428 429 429 class ViewApp(wx.App): 430 430 """ … … 434 434 """ 435 435 widget.CHILD_FRAME = wx.Frame 436 frame = DensityWindow(None, title="Density/Volume Calculator") 436 frame = DensityWindow(None, title="Density/Volume Calculator") 437 437 frame.Show(True) 438 438 self.SetTopWindow(frame) 439 439 return True 440 441 442 if __name__ == "__main__": 440 441 442 if __name__ == "__main__": 443 443 app = ViewApp(0) 444 app.MainLoop() 444 app.MainLoop()
Note: See TracChangeset
for help on using the changeset viewer.