[b9a5f0e] | 1 | """ |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[26d2d88] | 4 | project funded by the US National Science Foundation. |
---|
[b9a5f0e] | 5 | |
---|
| 6 | See the license text in license.txt |
---|
| 7 | |
---|
| 8 | copyright 2008, 2009, University of Tennessee |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | import wx |
---|
| 12 | import sys |
---|
| 13 | |
---|
[d85c194] | 14 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
[b699768] | 15 | from sas.sascalc.calculator.kiessig_calculator import KiessigThicknessCalculator |
---|
[b9a5f0e] | 16 | from calculator_widgets import OutputTextCtrl |
---|
| 17 | from calculator_widgets import InputTextCtrl |
---|
[d85c194] | 18 | from sas.sasgui.perspectives.calculator import calculator_widgets as widget |
---|
| 19 | from sas.sasgui.guiframe.documentation_window import DocumentationWindow |
---|
[b9a5f0e] | 20 | |
---|
| 21 | _BOX_WIDTH = 77 |
---|
[26d2d88] | 22 | #Slit length panel size |
---|
[b9a5f0e] | 23 | if sys.platform.count("win32") > 0: |
---|
[d5419f7f] | 24 | PANEL_TOP = 0 |
---|
[b9a5f0e] | 25 | PANEL_WIDTH = 500 |
---|
[9654baf] | 26 | PANEL_HEIGHT = 230 |
---|
[b9a5f0e] | 27 | FONT_VARIANT = 0 |
---|
| 28 | else: |
---|
[d5419f7f] | 29 | PANEL_TOP = 60 |
---|
[b9a5f0e] | 30 | PANEL_WIDTH = 560 |
---|
| 31 | PANEL_HEIGHT = 230 |
---|
| 32 | FONT_VARIANT = 1 |
---|
| 33 | |
---|
| 34 | class KiessigThicknessCalculatorPanel(wx.Panel, PanelBase): |
---|
| 35 | """ |
---|
| 36 | Provides the Kiessig thickness calculator GUI. |
---|
| 37 | """ |
---|
| 38 | ## Internal nickname for the window, used by the AUI manager |
---|
| 39 | window_name = "Kiessig Thickness Calculator" |
---|
| 40 | ## Name to appear on the window title bar |
---|
| 41 | window_caption = "Kiessig Thickness Calculator" |
---|
| 42 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
| 43 | CENTER_PANE = True |
---|
[26d2d88] | 44 | |
---|
[b9a5f0e] | 45 | def __init__(self, parent, *args, **kwds): |
---|
| 46 | wx.Panel.__init__(self, parent, *args, **kwds) |
---|
| 47 | PanelBase.__init__(self) |
---|
[26d2d88] | 48 | #Font size |
---|
| 49 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
[b9a5f0e] | 50 | # Object that receive status event |
---|
| 51 | self.parent = parent |
---|
| 52 | self.kiessig = KiessigThicknessCalculator() |
---|
| 53 | #layout attribute |
---|
| 54 | self.hint_sizer = None |
---|
| 55 | self._do_layout() |
---|
[26d2d88] | 56 | |
---|
[b9a5f0e] | 57 | def _define_structure(self): |
---|
| 58 | """ |
---|
| 59 | Define the main sizers building to build this application. |
---|
| 60 | """ |
---|
| 61 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 62 | self.box_source = wx.StaticBox(self, -1, |
---|
| 63 | str("Kiessig Thickness Calculator")) |
---|
| 64 | self.boxsizer_source = wx.StaticBoxSizer(self.box_source, |
---|
[26d2d88] | 65 | wx.VERTICAL) |
---|
[b9a5f0e] | 66 | self.dq_name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 67 | self.thickness_size_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 68 | self.hint_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 69 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[26d2d88] | 70 | |
---|
[b9a5f0e] | 71 | def _layout_dq_name(self): |
---|
| 72 | """ |
---|
| 73 | Fill the sizer containing dq name |
---|
| 74 | """ |
---|
| 75 | # get the default dq |
---|
| 76 | dq_value = str(self.kiessig.get_deltaq()) |
---|
| 77 | dq_unit_txt = wx.StaticText(self, -1, '[1/A]') |
---|
[26d2d88] | 78 | dq_name_txt = wx.StaticText(self, -1, |
---|
| 79 | 'Kiessig Fringe Width (Delta Q): ') |
---|
[b9a5f0e] | 80 | self.dq_name_tcl = InputTextCtrl(self, -1, |
---|
| 81 | size=(_BOX_WIDTH,-1)) |
---|
| 82 | dq_hint = "Type the Kiessig Fringe Width (Delta Q)" |
---|
| 83 | self.dq_name_tcl.SetValue(dq_value) |
---|
| 84 | self.dq_name_tcl.SetToolTipString(dq_hint) |
---|
| 85 | #control that triggers importing data |
---|
| 86 | id = wx.NewId() |
---|
| 87 | self.compute_button = wx.Button(self, id, "Compute") |
---|
| 88 | hint_on_compute = "Compute the diameter/thickness in the real space." |
---|
| 89 | self.compute_button.SetToolTipString(hint_on_compute) |
---|
| 90 | self.Bind(wx.EVT_BUTTON, self.on_compute, id=id) |
---|
| 91 | self.dq_name_sizer.AddMany([(dq_name_txt, 0, wx.LEFT, 15), |
---|
| 92 | (self.dq_name_tcl, 0, wx.LEFT, 15), |
---|
[26d2d88] | 93 | (dq_unit_txt, 0, wx.LEFT, 10), |
---|
| 94 | (self.compute_button, 0, wx.LEFT, 30)]) |
---|
| 95 | |
---|
[b9a5f0e] | 96 | def _layout_thickness_size(self): |
---|
| 97 | """ |
---|
| 98 | Fill the sizer containing thickness information |
---|
| 99 | """ |
---|
| 100 | thick_unit = '['+self.kiessig.get_thickness_unit() +']' |
---|
[26d2d88] | 101 | thickness_size_txt = wx.StaticText(self, -1, |
---|
[b9a5f0e] | 102 | 'Thickness (or Diameter): ') |
---|
[26d2d88] | 103 | self.thickness_size_tcl = OutputTextCtrl(self, -1, |
---|
[b9a5f0e] | 104 | size=(_BOX_WIDTH,-1)) |
---|
| 105 | thickness_size_hint = " Estimated Size in Real Space" |
---|
| 106 | self.thickness_size_tcl.SetToolTipString(thickness_size_hint) |
---|
| 107 | thickness_size_unit_txt = wx.StaticText(self, -1, thick_unit) |
---|
[26d2d88] | 108 | |
---|
[b9a5f0e] | 109 | self.thickness_size_sizer.AddMany([(thickness_size_txt, 0, wx.LEFT, 15), |
---|
[26d2d88] | 110 | (self.thickness_size_tcl, 0, wx.LEFT, 15), |
---|
| 111 | (thickness_size_unit_txt, 0, wx.LEFT, 10)]) |
---|
| 112 | |
---|
[b9a5f0e] | 113 | def _layout_hint(self): |
---|
| 114 | """ |
---|
| 115 | Fill the sizer containing hint |
---|
| 116 | """ |
---|
| 117 | hint_msg = "This tool is to approximately estimate " |
---|
| 118 | hint_msg += "the thickness of a layer" |
---|
| 119 | hint_msg += " or the diameter of particles\n " |
---|
| 120 | hint_msg += "from the Kiessig fringe width in SAS/NR data." |
---|
| 121 | hint_msg += "" |
---|
| 122 | self.hint_txt = wx.StaticText(self, -1, hint_msg) |
---|
| 123 | self.hint_sizer.AddMany([(self.hint_txt, 0, wx.LEFT, 15)]) |
---|
[26d2d88] | 124 | |
---|
[b9a5f0e] | 125 | def _layout_button(self): |
---|
| 126 | """ |
---|
| 127 | Do the layout for the button widgets |
---|
| 128 | """ |
---|
[9654baf] | 129 | id = wx.NewId() |
---|
[26d2d88] | 130 | self.bt_help = wx.Button(self, id, 'HELP') |
---|
[9654baf] | 131 | self.bt_help.Bind(wx.EVT_BUTTON, self.on_help) |
---|
| 132 | self.bt_help.SetToolTipString("Help using the Kiessig fringe calculator.") |
---|
| 133 | |
---|
[26d2d88] | 134 | self.bt_close = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
[b9a5f0e] | 135 | self.bt_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
| 136 | self.bt_close.SetToolTipString("Close this window.") |
---|
[9654baf] | 137 | self.button_sizer.AddMany([(self.bt_help, 0, wx.LEFT, 260), |
---|
| 138 | (self.bt_close, 0, wx.LEFT, 20)]) |
---|
[26d2d88] | 139 | |
---|
[b9a5f0e] | 140 | def _do_layout(self): |
---|
| 141 | """ |
---|
| 142 | Draw window content |
---|
| 143 | """ |
---|
| 144 | self._define_structure() |
---|
| 145 | self._layout_dq_name() |
---|
| 146 | self._layout_thickness_size() |
---|
| 147 | self._layout_hint() |
---|
| 148 | self._layout_button() |
---|
| 149 | self.boxsizer_source.AddMany([(self.dq_name_sizer, 0, |
---|
[26d2d88] | 150 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
| 151 | (self.thickness_size_sizer, 0, |
---|
| 152 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
| 153 | (self.hint_sizer, 0, |
---|
| 154 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
[b9a5f0e] | 155 | self.main_sizer.AddMany([(self.boxsizer_source, 0, wx.ALL, 10), |
---|
[26d2d88] | 156 | (self.button_sizer, 0, |
---|
| 157 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
[b9a5f0e] | 158 | self.SetSizer(self.main_sizer) |
---|
| 159 | self.SetAutoLayout(True) |
---|
| 160 | |
---|
[26d2d88] | 161 | def on_help(self, event): |
---|
[9654baf] | 162 | """ |
---|
| 163 | Bring up the Kiessig fringe calculator Documentation whenever |
---|
[26d2d88] | 164 | the HELP button is clicked. |
---|
[9654baf] | 165 | Calls DocumentationWindow with the path of the location within the |
---|
[26d2d88] | 166 | documentation tree (after /doc/ ....". Note that when using old |
---|
| 167 | versions of Wx (before 2.9) and thus not the release version of |
---|
| 168 | installers, the help comes up at the top level of the file as |
---|
[9654baf] | 169 | webbrowser does not pass anything past the # to the browser when it is |
---|
| 170 | running "file:///...." |
---|
[26d2d88] | 171 | |
---|
[9654baf] | 172 | :param evt: Triggers on clicking the help button |
---|
| 173 | """ |
---|
[d0248bd] | 174 | _TreeLocation = "user/sasgui/perspectives/calculator/" |
---|
| 175 | _TreeLocation += "kiessig_calculator_help.html" |
---|
[3db44fb] | 176 | _doc_viewer = DocumentationWindow(self, -1, _TreeLocation, "", |
---|
[26d2d88] | 177 | "Density/Volume Calculator Help") |
---|
[9654baf] | 178 | |
---|
[b9a5f0e] | 179 | def on_close(self, event): |
---|
| 180 | """ |
---|
| 181 | close the window containing this panel |
---|
| 182 | """ |
---|
| 183 | self.parent.Close() |
---|
| 184 | if event is not None: |
---|
| 185 | event.Skip() |
---|
[26d2d88] | 186 | |
---|
[b9a5f0e] | 187 | def on_compute(self, event): |
---|
| 188 | """ |
---|
| 189 | Execute the computation of thickness |
---|
| 190 | """ |
---|
| 191 | # skip for another event |
---|
| 192 | if event != None: |
---|
| 193 | event.Skip() |
---|
| 194 | dq = self.dq_name_tcl.GetValue() |
---|
| 195 | self.kiessig.set_deltaq(dq) |
---|
| 196 | # calculate the thickness |
---|
| 197 | output = self.kiessig.compute_thickness() |
---|
| 198 | thickness = self.format_number(output) |
---|
| 199 | # set tcl |
---|
| 200 | self.thickness_size_tcl.SetValue(str(thickness)) |
---|
[26d2d88] | 201 | |
---|
[b9a5f0e] | 202 | def format_number(self, value=None): |
---|
| 203 | """ |
---|
[26d2d88] | 204 | Return a float in a standardized, human-readable formatted string |
---|
[b9a5f0e] | 205 | """ |
---|
[26d2d88] | 206 | try: |
---|
[b9a5f0e] | 207 | value = float(value) |
---|
| 208 | except: |
---|
| 209 | output = None |
---|
| 210 | return output |
---|
| 211 | |
---|
| 212 | output = "%-7.4g" % value |
---|
[26d2d88] | 213 | return output.lstrip().rstrip() |
---|
[b9a5f0e] | 214 | |
---|
| 215 | def _onparamEnter(self, event = None): |
---|
| 216 | """ |
---|
| 217 | On Text_enter_callback, perform compute |
---|
| 218 | """ |
---|
| 219 | self.on_compute(event) |
---|
[26d2d88] | 220 | |
---|
[b9a5f0e] | 221 | class KiessigWindow(widget.CHILD_FRAME): |
---|
| 222 | def __init__(self, parent=None, manager=None, |
---|
| 223 | title="Kiessig Thickness Calculator", |
---|
| 224 | size=(PANEL_WIDTH,PANEL_HEIGHT), *args, **kwds): |
---|
| 225 | kwds['title'] = title |
---|
| 226 | kwds['size'] = size |
---|
| 227 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwds) |
---|
| 228 | self.parent = parent |
---|
| 229 | self.manager = manager |
---|
| 230 | self.panel = KiessigThicknessCalculatorPanel(parent=self) |
---|
| 231 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
[d5419f7f] | 232 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
[b9a5f0e] | 233 | self.Show(True) |
---|
| 234 | |
---|
| 235 | def on_close(self, event): |
---|
| 236 | """ |
---|
| 237 | Close event |
---|
| 238 | """ |
---|
| 239 | if self.manager != None: |
---|
| 240 | self.manager.kiessig_frame = None |
---|
| 241 | self.Destroy() |
---|
| 242 | |
---|
[26d2d88] | 243 | if __name__ == "__main__": |
---|
[b9a5f0e] | 244 | app = wx.PySimpleApp() |
---|
| 245 | widget.CHILD_FRAME = wx.Frame |
---|
[26d2d88] | 246 | frame = KiessigWindow() |
---|
[b9a5f0e] | 247 | frame.Show(True) |
---|
[26d2d88] | 248 | app.MainLoop() |
---|