source: sasview/calculatorview/perspectives/calculator/sld_panel.py @ df7ed14

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since df7ed14 was edb3a5d0, checked in by Gervaise Alina <gervyh@…>, 14 years ago

hiding button

  • Property mode set to 100644
File size: 15.4 KB
RevLine 
[427fa87]1"""
2    This module provide GUI for the neutron scattering length density calculator
3    @author: Gervaise B. Alina
4"""
5
6import wx
7import sys
8
9from sans.guiframe.utils import format_number, check_float
10from sans.guicomm.events import StatusEvent 
11from sans.calculator.sld_calculator import SldCalculator
12
13_BOX_WIDTH = 76
14_STATICBOX_WIDTH = 350
15_SCALE = 1e-6
16
17#SLD panel size
[39e49a1]18if sys.platform.count("win32")>0:
[427fa87]19    _STATICBOX_WIDTH = 350
20    PANEL_SIZE = 400
21    FONT_VARIANT = 0
22else:
23    _STATICBOX_WIDTH = 380
24    PANEL_SIZE = 430
25    FONT_VARIANT = 1
26   
27class SldPanel(wx.Panel):
28    """
29        Provides the SLD calculator GUI.
30    """
31    ## Internal nickname for the window, used by the AUI manager
32    window_name = "SLD Calculator"
33    ## Name to appear on the window title bar
34    window_caption = "SLD Calculator"
35    ## Flag to tell the AUI manager to put this panel in the center pane
36    CENTER_PANE = True
37    def __init__(self, parent,base=None, id = -1):
38        wx.Panel.__init__(self, parent, id = id)
39        #Font size
40        self.SetWindowVariant(variant=FONT_VARIANT)
41       
42        # Object that receive status event
[39e49a1]43        self.base = base
[427fa87]44        self.calculator = SldCalculator()
45        self.wavelength = self.calculator.wavelength
46       
47        self._do_layout()
48        self.SetAutoLayout(True)
49        self.Layout()
50       
51
52    def _do_layout(self):
53        """
54            Draw window content
55        """
56        unit_a = '[A]'
57        unit_density = '[g/cm^(3)]'
[39e49a1]58        unit_sld = '[1/A^(2)]'
59        unit_cm1 ='[1/cm]'
60        unit_cm ='[cm]'
[427fa87]61        sizer_input = wx.GridBagSizer(5,5)
62        sizer_output = wx.GridBagSizer(5,5)
63        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
64        sizer1 = wx.BoxSizer(wx.HORIZONTAL)
65        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
66        sizer3 = wx.BoxSizer(wx.HORIZONTAL)
67        #---------inputs----------------
68        inputbox = wx.StaticBox(self, -1, "Input")
69        boxsizer1 = wx.StaticBoxSizer(inputbox, wx.VERTICAL)
70        boxsizer1.SetMinSize((_STATICBOX_WIDTH,-1))
71       
72        compound_txt = wx.StaticText(self, -1, 'Compound ')
73        self.compound_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
74        density_txt = wx.StaticText(self, -1, 'Density ')
75        self.density_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
76        unit_density_txt = wx.StaticText(self, -1, unit_density)
77        wavelength_txt = wx.StaticText(self, -1, 'Wavelength ')
78        self.wavelength_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
79        self.wavelength_ctl.SetValue(str(self.wavelength))
80        unit_a_txt = wx.StaticText(self, -1, unit_a)
81        iy = 0
82        ix = 0
83        sizer_input.Add(compound_txt,(iy, ix),(1,1),\
84                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
85        ix +=1
86        sizer_input.Add(self.compound_ctl,(iy, ix),(1,1),\
87                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
88        iy += 1
89        ix = 0
90        sizer_input.Add(density_txt,(iy, ix),(1,1),\
91                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[39e49a1]92        ix += 1
93        sizer_input.Add(self.density_ctl, (iy, ix), (1,1),\
[427fa87]94                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
95        ix +=1
96        sizer_input.Add(unit_density_txt,(iy, ix),(1,1),\
97                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
98        iy += 1
99        ix = 0
[39e49a1]100        sizer_input.Add(wavelength_txt, (iy, ix),(1,1),\
[427fa87]101                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[39e49a1]102        ix += 1
[427fa87]103        sizer_input.Add(self.wavelength_ctl,(iy, ix),(1,1),\
104                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
[39e49a1]105        ix += 1
[427fa87]106        sizer_input.Add(unit_a_txt,(iy, ix),(1,1),\
107                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
108        boxsizer1.Add( sizer_input )
109        sizer1.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10)
110        #---------Outputs sizer--------
111        outputbox = wx.StaticBox(self, -1, "Output")
112        boxsizer2 = wx.StaticBoxSizer(outputbox, wx.VERTICAL)
113        boxsizer2.SetMinSize((_STATICBOX_WIDTH,-1))
114       
[39e49a1]115        i_complex = '+ i'
[427fa87]116        neutron_sld_txt = wx.StaticText(self, -1, 'Neutron SLD')
117        self.neutron_sld_reel_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
118        self.neutron_sld_reel_ctl.SetEditable(False)
[017ec0b7]119        self.neutron_sld_reel_ctl.SetToolTipString("Neutron SLD real.")
[427fa87]120        self.neutron_sld_im_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
121        self.neutron_sld_im_ctl.SetEditable(False)
122        self.neutron_sld_im_ctl.SetToolTipString("Neutron SLD imaginary.")
123        neutron_sld_units_txt = wx.StaticText(self, -1, unit_sld)
124       
125        cu_ka_sld_txt = wx.StaticText(self, -1, 'Cu Ka SLD')
126        self.cu_ka_sld_reel_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
127        self.cu_ka_sld_reel_ctl.SetEditable(False)
[017ec0b7]128        self.cu_ka_sld_reel_ctl.SetToolTipString("Cu Ka SLD real.")
[427fa87]129        self.cu_ka_sld_im_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
130        self.cu_ka_sld_im_ctl.SetEditable(False)
131        self.cu_ka_sld_im_ctl.SetToolTipString("Cu Ka SLD imaginary.")
132        cu_ka_sld_units_txt = wx.StaticText(self, -1, unit_sld)
133       
134        mo_ka_sld_txt = wx.StaticText(self, -1, 'Mo Ka SLD')
135        self.mo_ka_sld_reel_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
136        self.mo_ka_sld_reel_ctl.SetEditable(False)
[017ec0b7]137        self.mo_ka_sld_reel_ctl.SetToolTipString("Mo Ka SLD real.")
[427fa87]138        self.mo_ka_sld_im_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
139        self.mo_ka_sld_im_ctl.SetEditable(False)
[017ec0b7]140        self.mo_ka_sld_im_ctl.SetToolTipString("Mo Ka SLD imaginary.")
[427fa87]141        mo_ka_sld_units_txt = wx.StaticText(self, -1, unit_sld)
142       
143        neutron_inc_txt = wx.StaticText(self, -1, 'Neutron Inc. Xs')
144        self.neutron_inc_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
145        self.neutron_inc_ctl.SetEditable(False)
146        self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs")
147        neutron_inc_units_txt = wx.StaticText(self, -1,  unit_cm1)
[edb3a5d0]148        neutron_inc_txt.Hide()
149        self.neutron_inc_ctl.Hide()
150        neutron_inc_units_txt.Hide()
[90b23e1]151        neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs')     
[427fa87]152        self.neutron_abs_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
153        self.neutron_abs_ctl.SetEditable(False)
154        self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs")
155        neutron_abs_units_txt = wx.StaticText(self, -1,  unit_cm1)
[edb3a5d0]156        neutron_abs_txt.Hide()
157        self.neutron_abs_ctl.Hide()
158        neutron_abs_units_txt.Hide()
[427fa87]159        neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length')
160        self.neutron_length_ctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,-1))
161        self.neutron_length_ctl.SetEditable(False)
162        self.neutron_length_ctl.SetToolTipString("Neutron 1/e length")
163        neutron_length_units_txt = wx.StaticText(self, -1,  unit_cm)
[edb3a5d0]164        neutron_length_txt.Hide()
165        self.neutron_length_ctl.Hide()
166        neutron_length_units_txt.Hide()
[427fa87]167        iy = 0
168        ix = 0
169        sizer_output.Add(neutron_sld_txt,(iy, ix),(1,1),
170                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[39e49a1]171        ix += 1
[427fa87]172        sizer_output.Add(self.neutron_sld_reel_ctl,(iy, ix),(1,1),
173                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
[39e49a1]174        ix += 1
[427fa87]175        sizer_output.Add(wx.StaticText(self, -1, i_complex)
176                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
177        ix +=1
178        sizer_output.Add(self.neutron_sld_im_ctl
179                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
180        ix +=1
181        sizer_output.Add(neutron_sld_units_txt
182                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
183        iy += 1
184        ix = 0
185        sizer_output.Add(cu_ka_sld_txt,(iy, ix),(1,1),
186                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
187        ix +=1
188        sizer_output.Add(self.cu_ka_sld_reel_ctl,(iy, ix),(1,1),
189                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
190        ix +=1
191        sizer_output.Add(wx.StaticText(self, -1, i_complex)
192                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0)
[39e49a1]193        ix += 1
[427fa87]194        sizer_output.Add(self.cu_ka_sld_im_ctl
195                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
[39e49a1]196        ix += 1
[427fa87]197        sizer_output.Add(cu_ka_sld_units_txt
198                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
199        iy += 1
200        ix = 0
201        sizer_output.Add(mo_ka_sld_txt,(iy, ix),(1,1),
202                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
203        ix +=1
204        sizer_output.Add(self.mo_ka_sld_reel_ctl,(iy, ix),(1,1),
205                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
206        ix +=1
207        sizer_output.Add(wx.StaticText(self, -1, i_complex)
208                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0)
[39e49a1]209        ix += 1
[427fa87]210        sizer_output.Add(self.mo_ka_sld_im_ctl
211                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
212        ix +=1
213        sizer_output.Add(mo_ka_sld_units_txt
214                         ,(iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0)
215        iy += 1
216        ix = 0
217        sizer_output.Add(neutron_inc_txt,(iy, ix),(1,1),
218                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[39e49a1]219        ix += 1
[427fa87]220        sizer_output.Add(self.neutron_inc_ctl,(iy, ix),(1,1),
221                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
[39e49a1]222        ix += 2
[427fa87]223        sizer_output.Add(neutron_inc_units_txt,(iy, ix),(1,1),
224                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
225        iy += 1
226        ix = 0
[39e49a1]227        sizer_output.Add(neutron_abs_txt, (iy, ix), (1,1),
[427fa87]228                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
[39e49a1]229        ix += 1
230        sizer_output.Add(self.neutron_abs_ctl, (iy, ix), (1,1),
[427fa87]231                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
232        ix +=2
[39e49a1]233        sizer_output.Add(neutron_abs_units_txt, (iy, ix), (1,1),
[427fa87]234                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
235        iy += 1
236        ix = 0
[39e49a1]237        sizer_output.Add(neutron_length_txt, (iy, ix), (1,1),
[427fa87]238                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
239        ix +=1
[39e49a1]240        sizer_output.Add(self.neutron_length_ctl, (iy, ix), (1,1),
[427fa87]241                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
[39e49a1]242        ix += 2
243        sizer_output.Add(neutron_length_units_txt, (iy, ix), (1,1),
[427fa87]244                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
245        boxsizer2.Add( sizer_output )
[39e49a1]246        sizer2.Add(boxsizer2,0, wx.EXPAND|wx.ALL, 10)
[427fa87]247        #-----Button  sizer------------
248   
249        id = wx.NewId()
250        button_calculate = wx.Button(self, id, "Calculate")
[017ec0b7]251        button_calculate.SetToolTipString("Calculate SLD.")
[427fa87]252        self.Bind(wx.EVT_BUTTON, self.calculateSld, id = id)   
253       
254        sizer_button.Add((250, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
255        sizer_button.Add(button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE,20)
256        sizer3.Add(sizer_button)
257        #---------layout----------------
258        vbox  = wx.BoxSizer(wx.VERTICAL)
259        vbox.Add(sizer1)
260        vbox.Add(sizer2)
261        vbox.Add(sizer3)
262        vbox.Fit(self) 
263        self.SetSizer(vbox)
264       
265       
266    def check_inputs(self):
267        """Check validity user inputs"""
[39e49a1]268        flag = True
[427fa87]269       
270        if check_float(self.density_ctl):
271            self.density = float(self.density_ctl.GetValue())
272        else:
[39e49a1]273            flag = False
[427fa87]274            raise ValueError,"Error for Density value :expect float"
275   
[39e49a1]276        self.wavelength = self.wavelength_ctl.GetValue()
277        if self.wavelength.lstrip().rstrip() == "":
[427fa87]278            self.wavelength = self.calculator.wavelength
279        else:
280            if check_float(self.wavelength_ctl):
[39e49a1]281                self.wavelength = float(self.wavelength)
[427fa87]282            else:
283                flag = False
284                raise ValueError,"Error for wavelenth value :expect float"
285               
[39e49a1]286        self.formulata_text = self.compound_ctl.GetValue().lstrip().rstrip()
287        if self.formulata_text != "":
[427fa87]288            self.compound_ctl.SetBackgroundColour(wx.WHITE)
289            self.compound_ctl.Refresh()
290        else:
291            self.compound_ctl.SetBackgroundColour("pink")
292            self.compound_ctl.Refresh()
[39e49a1]293            flag = False
[427fa87]294            raise ValueError, "Enter a formula"
295        return flag
296       
297    def calculateSld(self, event):
298        """
299            Calculate the neutron scattering density length of a molecule
300        """
301        try:
302            #Check validity user inputs
303            if self.check_inputs():
304                #get ready to compute
305                try:
[39e49a1]306                    self.calculator.set_value(self.formulata_text,
307                                              self.density, self.wavelength)
[427fa87]308                except:
[39e49a1]309                    if self.base is not None:
310                        msg = "SLD Calculator: %s" %(sys.exc_value)
311                        wx.PostEvent(self.base, StatusEvent(status=msg))
[427fa87]312                    else:
313                        raise
314                    return
315               
316                # Compute the Cu SLD
[39e49a1]317                Cu_reel, Cu_im = self.calculator.calculate_xray_sld( "Cu")
318                self.cu_ka_sld_reel_ctl.SetValue(format_number(Cu_reel *_SCALE))
319                self.cu_ka_sld_im_ctl.SetValue(format_number(Cu_im * _SCALE))
[427fa87]320               
321                # Compute the Mo SLD
[39e49a1]322                Mo_reel, Mo_im = self.calculator.calculate_xray_sld( "Mo")
323                self.mo_ka_sld_reel_ctl.SetValue(format_number(Mo_reel *_SCALE))
324                self.mo_ka_sld_im_ctl.SetValue(format_number(Mo_im * _SCALE))
[427fa87]325               
[25a2ee3]326                sld_real, sld_im, inc = self.calculator.calculate_neutron_sld()
[39e49a1]327                length = self.calculator.calculate_length()
[427fa87]328                # Neutron SLD
[25a2ee3]329                #need to be compute by element package
330                absorp = 1.0 
331                length = 1.0
332                self.neutron_sld_reel_ctl.SetValue(format_number(sld_real * _SCALE))
333                self.neutron_sld_im_ctl.SetValue(format_number(sld_im * _SCALE))
[39e49a1]334                self.neutron_inc_ctl.SetValue(format_number(inc))
[427fa87]335                self.neutron_abs_ctl.SetValue(format_number(absorp))
336                # Neutron length
337                self.neutron_length_ctl.SetValue(format_number(length))
338                # display wavelength
339                self.wavelength_ctl.SetValue(str(self.wavelength))
340        except:
[39e49a1]341            if self.base is not None:
342                  msg = "SLD Calculator: %s"%(sys.exc_value)
343                  wx.PostEvent(self.base, StatusEvent(status=msg))
[427fa87]344            else:
345                raise
346            return   
347
348   
349       
350 
351   
352   
353 
354       
355               
356class SldWindow(wx.Frame):
357    def __init__(self, parent=None, id=1, title="SLD Calculator",base=None):
358        wx.Frame.__init__(self, parent, id, title, size=( PANEL_SIZE,  PANEL_SIZE))
359       
360        self.panel = SldPanel(self, base=base)
361        self.Centre()
362        self.Show(True)
363       
364class ViewApp(wx.App):
365    def OnInit(self):
366        frame = SldWindow(None, -1, 'SLD Calculator')   
367        frame.Show(True)
368        self.SetTopWindow(frame)
369       
370        return True
371       
372
373if __name__ == "__main__": 
374    app = ViewApp(0)
375    app.MainLoop()     
Note: See TracBrowser for help on using the repository browser.