source: sasview/calculatorview/perspectives/calculator/slit_length_calculator_panel.py @ 378d2eb

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

working on the slit length calculator

  • Property mode set to 100644
File size: 9.1 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 = 190
25    FONT_VARIANT = 0
26else:
27    PANEL_WIDTH = 530
28    PANEL_HEIGHT = 220
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 Length Calculator"
38    ## Name to appear on the window title bar
39    window_caption = "Slit Length 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.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
66       
67    def _layout_data_name(self):
68        """
69            Fill the sizer containing data's name
70        """
71        data_name_txt = wx.StaticText(self, -1, 'Data: ')
72        self.data_name_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH*4,-1))
73        #control that triggers importing data
74        id = wx.NewId()
75        self.browse_button = wx.Button(self, id, "Browse")
76        hint_on_browse = "Click on this button to import data in this panel."
77        self.browse_button.SetToolTipString(hint_on_browse)
78        self.Bind(wx.EVT_BUTTON, self.on_load_data, id=id)
79        self.data_name_sizer.AddMany([(data_name_txt, 0, wx.LEFT, 15),
80                                      (self.data_name_tcl, 0, wx.LEFT, 10),
81                                      (self.browse_button, 0, wx.LEFT, 10)])
82    def _layout_slit_size(self):
83        """
84            Fill the sizer containing slit size information
85        """
86        slit_size_txt = wx.StaticText(self, -1, 'Slit Size: ')
87        self.slit_size_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
88        slit_size_hint = " Estimated slit size"
89        self.slit_size_tcl.SetToolTipString(slit_size_hint)
90        slit_size_unit_txt = wx.StaticText(self, -1, 'Unit: ')
91        self.slit_size_unit_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) 
92        slit_size_unit_hint = "Slit size's unit"
93        self.slit_size_unit_tcl.SetToolTipString(slit_size_unit_hint)
94        self.slit_size_sizer.AddMany([(slit_size_txt, 0, wx.LEFT, 15),
95                                      (self.slit_size_tcl, 0, wx.LEFT, 10),
96                                      (slit_size_unit_txt, 0, wx.LEFT, 10),
97                                      (self.slit_size_unit_tcl, 0, wx.LEFT, 10)])
98   
99    def _layout_button(self): 
100        """
101            Do the layout for the button widgets
102        """ 
103        self.bt_close = wx.Button(self, wx.ID_CANCEL,'Close')
104        self.bt_close.Bind(wx.EVT_BUTTON, self.on_close)
105        self.bt_close.SetToolTipString("Apply current changes to the source.")
106        self.button_sizer.AddMany([(self.bt_close, 0, wx.LEFT, 390)])
107       
108    def _do_layout(self):
109        """
110            Draw window content
111        """
112        self._define_structure()
113        self._layout_data_name()
114        self._layout_slit_size()
115        self._layout_button()
116        self.boxsizer_source.AddMany([(self.data_name_sizer, 0,
117                                          wx.EXPAND|wx.TOP|wx.BOTTOM, 5),
118                                   (self.slit_size_sizer, 0,
119                                     wx.EXPAND|wx.TOP|wx.BOTTOM, 5)])
120        self.main_sizer.AddMany([(self.boxsizer_source, 0, wx.ALL, 10),
121                                  (self.button_sizer, 0,
122                                    wx.EXPAND|wx.TOP|wx.BOTTOM, 5)])
123       
124        self.SetSizer(self.main_sizer)
125        self.SetAutoLayout(True)
126       
127    def choose_data_file(self, location=None):
128        path = None
129        filename = ''
130        if location == None:
131            location = os.getcwd()
132       
133        wildcard = "SAXSess Data 1D (*.DAT, *.dat)|*.DAT" 
134       
135        dlg = wx.FileDialog(self, "Choose a file", location,"", wildcard, wx.OPEN)
136        if dlg.ShowModal() == wx.ID_OK:
137            path = dlg.GetPath()
138            filename = os.path.basename(path)
139        dlg.Destroy()
140       
141        return path, filename
142
143    def on_close(self, event):
144        """
145            close the window containing this panel
146        """
147        self.parent.Close()
148       
149    def on_load_data(self, event):
150        """
151            Open a file dialog to allow the user to select a given file.
152            The user can allow load file with extension .DAT or .dat.
153            Display the slit size corresponding to the loaded data.
154        """
155        path, filename = self.choose_data_file(location=self._default_save_location)
156       
157        if path is None or filename == '':
158            return 
159        self._default_save_location = path
160        try:
161            #Load data
162            from load_thread import DataReader
163            ## If a thread is already started, stop it
164            if self.reader is not None and self.reader.isrunning():
165                self.reader.stop()
166            if self.parent.parent is not None:
167                wx.PostEvent(self.parent.parent, StatusEvent(status="Loading...",
168                                type="progress"))
169            self.reader = DataReader(path=path,
170                                     filename=filename,
171                                    completefn=self.complete_loading,
172                                    updatefn=None)
173            self.reader.queue()
174        except:
175            if self.parent.parent is None:
176                return 
177            msg = "Slit Length Calculator: %s"%(sys.exc_value)
178            wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
179            return 
180           
181    def complete_loading(self, data=None, filename=''):
182        """
183            Complete the loading and compute the slit size
184        """
185        if data is not None and data.__class__.__name__ == 'Data2D':
186            if self.parent.parent is None:
187                return 
188            msg = "Slit Length cannot be computed for 2D Data"
189            wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
190            return 
191        self.data_name_tcl.SetValue(str(filename))
192        #compute the slit size
193        try:
194            x = data.x
195            y = data.y
196            if x == [] or  x is None or y == [] or y is None:
197                 msg = "The current data is empty please check x and y"
198                 raise ValueError, msg
199            slit_length_calculator = SlitlengthCalculator()
200            slit_length_calculator.set_data(x=x, y=y)
201            slit_length = slit_length_calculator.get_slit_length()
202        except:
203            if self.parent.parent is None:
204                return 
205            msg = "Slit Length Calculator: %s"%(sys.exc_value)
206            wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
207            return 
208        self.slit_size_tcl.SetValue(str(slit_length))
209        #Display unit
210        self.slit_size_unit_tcl.SetValue('[Unknown]')
211        if self.parent.parent is None:
212            return 
213        msg = "Load Complete"
214        wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop'))
215   
216class SlitLengthCalculatorWindow(wx.Frame):
217    def __init__(self, parent=None, id=1, title="Slit Length Calculator"):
218        wx.Frame.__init__(self, parent, id, title, size=(PANEL_WIDTH,PANEL_HEIGHT))
219        self.parent = parent
220        self.panel = SlitLengthCalculatorPanel(parent=self)
221        self.Centre()
222        self.Show(True)
223       
224if __name__ == "__main__": 
225    app = wx.PySimpleApp()
226    frame = SlitLengthCalculatorWindow()   
227    frame.Show(True)
228    app.MainLoop()     
Note: See TracBrowser for help on using the repository browser.