source: sasview/calculatorview/perspectives/calculator/slit_length_calculator_panel.py @ b500652

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 b500652 was b500652, checked in by Gervaise Alina <gervyh@…>, 14 years ago

add hint on slit length editor

  • Property mode set to 100644
File size: 9.6 KB
Line 
1"""
2   This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2008, 2009, University of Tennessee
9"""
10
11import wx
12import sys
13import os
14
15from DataLoader.readers.ascii_reader import Reader
16from sans.guicomm.events import StatusEvent 
17from sans.calculator.slit_length_calculator import SlitlengthCalculator 
18from calculator_widgets import OutputTextCtrl
19
20_BOX_WIDTH = 76
21#Slit length panel size
22if sys.platform.count("win32") > 0:
23    PANEL_WIDTH = 500
24    PANEL_HEIGHT = 200
25    FONT_VARIANT = 0
26else:
27    PANEL_WIDTH = 530
28    PANEL_HEIGHT = 230
29    FONT_VARIANT = 1
30   
31
32class SlitLengthCalculatorPanel(wx.Panel):
33    """
34        Provides the slit length calculator GUI.
35    """
36    ## Internal nickname for the window, used by the AUI manager
37    window_name = "Slit Size Calculator"
38    ## Name to appear on the window title bar
39    window_caption = "Slit Size Calculator"
40    ## Flag to tell the AUI manager to put this panel in the center pane
41    CENTER_PANE = True
42   
43    def __init__(self, parent, id=-1, *args, **kwds):
44        wx.Panel.__init__(self, parent, id=id, *args, **kwds)
45        #Font size
46        self.SetWindowVariant(variant=FONT_VARIANT)
47        #thread to read data
48        self.reader = None
49        # Default location
50        self._default_save_location = os.getcwd() 
51        # Object that receive status event
52        self.parent = parent
53        self._do_layout()
54       
55    def _define_structure(self):
56        """
57            Define the main sizers building to build this application.
58        """
59        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
60        self.box_source = wx.StaticBox(self, -1,str("Slit Size Calculator"))
61        self.boxsizer_source = wx.StaticBoxSizer(self.box_source,
62                                                    wx.VERTICAL)
63        self.data_name_sizer = wx.BoxSizer(wx.HORIZONTAL)
64        self.slit_size_sizer = wx.BoxSizer(wx.HORIZONTAL)
65        self.hint_sizer = wx.BoxSizer(wx.HORIZONTAL)
66        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
67       
68    def _layout_data_name(self):
69        """
70            Fill the sizer containing data's name
71        """
72        data_name_txt = wx.StaticText(self, -1, 'Data: ')
73        self.data_name_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH*4,-1))
74        #control that triggers importing data
75        id = wx.NewId()
76        self.browse_button = wx.Button(self, id, "Browse")
77        hint_on_browse = "Click on this button to import data in this panel."
78        self.browse_button.SetToolTipString(hint_on_browse)
79        self.Bind(wx.EVT_BUTTON, self.on_load_data, id=id)
80        self.data_name_sizer.AddMany([(data_name_txt, 0, wx.LEFT, 15),
81                                      (self.data_name_tcl, 0, wx.LEFT, 10),
82                                      (self.browse_button, 0, wx.LEFT, 10)])
83    def _layout_slit_size(self):
84        """
85            Fill the sizer containing slit size information
86        """
87        slit_size_txt = wx.StaticText(self, -1, 'Full Slit Size: ')
88        self.slit_size_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
89        slit_size_hint = " Estimated slit size"
90        self.slit_size_tcl.SetToolTipString(slit_size_hint)
91        slit_size_unit_txt = wx.StaticText(self, -1, 'Unit: ')
92        self.slit_size_unit_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) 
93        slit_size_unit_hint = "Slit size's unit"
94        self.slit_size_unit_tcl.SetToolTipString(slit_size_unit_hint)
95        self.slit_size_sizer.AddMany([(slit_size_txt, 0, wx.LEFT, 15),
96                                      (self.slit_size_tcl, 0, wx.LEFT, 10),
97                                      (slit_size_unit_txt, 0, wx.LEFT, 10),
98                                      (self.slit_size_unit_tcl, 0, wx.LEFT, 10)])
99   
100    def _layout_hint(self):
101        """
102            Fill the sizer containing hint
103        """
104        hint_msg = "This calculation works only for  SAXSess beam profile data."
105        self.hint_txt = wx.StaticText(self, -1, hint_msg)
106        self.hint_sizer.AddMany([(self.hint_txt, 0, wx.LEFT, 15)])
107   
108    def _layout_button(self): 
109        """
110            Do the layout for the button widgets
111        """ 
112        self.bt_close = wx.Button(self, wx.ID_CANCEL,'Close')
113        self.bt_close.Bind(wx.EVT_BUTTON, self.on_close)
114        self.bt_close.SetToolTipString("Close this window.")
115        self.button_sizer.AddMany([(self.bt_close, 0, wx.LEFT, 390)])
116       
117    def _do_layout(self):
118        """
119            Draw window content
120        """
121        self._define_structure()
122        self._layout_data_name()
123        self._layout_slit_size()
124        self._layout_hint()
125        self._layout_button()
126        self.boxsizer_source.AddMany([(self.data_name_sizer, 0,
127                                          wx.EXPAND|wx.TOP|wx.BOTTOM, 5),
128                                   (self.slit_size_sizer, 0,
129                                     wx.EXPAND|wx.TOP|wx.BOTTOM, 5),
130                                     (self.hint_sizer, 0,
131                                     wx.EXPAND|wx.TOP|wx.BOTTOM, 5)])
132        self.main_sizer.AddMany([(self.boxsizer_source, 0, wx.ALL, 10),
133                                  (self.button_sizer, 0,
134                                    wx.EXPAND|wx.TOP|wx.BOTTOM, 5)])
135        self.SetSizer(self.main_sizer)
136        self.SetAutoLayout(True)
137       
138    def choose_data_file(self, location=None):
139        path = None
140        filename = ''
141        if location == None:
142            location = os.getcwd()
143       
144        wildcard = "SAXSess Data 1D (*.DAT, *.dat)|*.DAT" 
145       
146        dlg = wx.FileDialog(self, "Choose a file", location,"", wildcard, wx.OPEN)
147        if dlg.ShowModal() == wx.ID_OK:
148            path = dlg.GetPath()
149            filename = os.path.basename(path)
150        dlg.Destroy()
151       
152        return path, filename
153
154    def on_close(self, event):
155        """
156            close the window containing this panel
157        """
158        self.parent.Close()
159       
160    def on_load_data(self, event):
161        """
162            Open a file dialog to allow the user to select a given file.
163            The user can allow load file with extension .DAT or .dat.
164            Display the slit size corresponding to the loaded data.
165        """
166        path, filename = self.choose_data_file(location=self._default_save_location)
167       
168        if path is None or filename == '':
169            return 
170        self._default_save_location = path
171        try:
172            #Load data
173            from load_thread import DataReader
174            ## If a thread is already started, stop it
175            if self.reader is not None and self.reader.isrunning():
176                self.reader.stop()
177            if self.parent.parent is not None:
178                wx.PostEvent(self.parent.parent, StatusEvent(status="Loading...",
179                                type="progress"))
180            self.reader = DataReader(path=path,
181                                     filename=filename,
182                                    completefn=self.complete_loading,
183                                    updatefn=None)
184            self.reader.queue()
185        except:
186            if self.parent.parent is None:
187                return 
188            msg = "Slit Length Calculator: %s"%(sys.exc_value)
189            wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
190            return 
191           
192    def complete_loading(self, data=None, filename=''):
193        """
194            Complete the loading and compute the slit size
195        """
196        if data is not None and data.__class__.__name__ == 'Data2D':
197            if self.parent.parent is None:
198                return 
199            msg = "Slit Length cannot be computed for 2D Data"
200            wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
201            return 
202        self.data_name_tcl.SetValue(str(filename))
203        #compute the slit size
204        try:
205            x = data.x
206            y = data.y
207            if x == [] or  x is None or y == [] or y is None:
208                 msg = "The current data is empty please check x and y"
209                 raise ValueError, msg
210            slit_length_calculator = SlitlengthCalculator()
211            slit_length_calculator.set_data(x=x, y=y)
212            slit_length = slit_length_calculator.get_slit_length()
213        except:
214            if self.parent.parent is None:
215                return 
216            msg = "Slit Size Calculator: %s"%(sys.exc_value)
217            wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
218            return 
219        self.slit_size_tcl.SetValue(str(slit_length))
220        #Display unit
221        self.slit_size_unit_tcl.SetValue('[Unknown]')
222        if self.parent.parent is None:
223            return 
224        msg = "Load Complete"
225        wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
226   
227class SlitLengthCalculatorWindow(wx.Frame):
228    def __init__(self, parent=None, id=1, title="Slit Size Calculator"):
229        wx.Frame.__init__(self, parent, id, title, size=(PANEL_WIDTH,PANEL_HEIGHT))
230        self.parent = parent
231        self.panel = SlitLengthCalculatorPanel(parent=self)
232        self.Centre()
233        self.Show(True)
234       
235if __name__ == "__main__": 
236    app = wx.PySimpleApp()
237    frame = SlitLengthCalculatorWindow()   
238    frame.Show(True)
239    app.MainLoop()     
Note: See TracBrowser for help on using the repository browser.