source: sasview/calculatorview/src/sans/perspectives/calculator/slit_length_calculator_panel.py @ e35534f

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 e35534f was e35534f, checked in by Jae Cho <jhjcho@…>, 11 years ago

default tool windows position changes: mac fixes

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