1 | """ |
---|
2 | This software was developed by the University of Tennessee as part of the |
---|
3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | project funded by the US National Science Foundation. |
---|
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 | import os |
---|
14 | |
---|
15 | from sans.guiframe.panel_base import PanelBase |
---|
16 | |
---|
17 | from sans.guiframe.events import StatusEvent |
---|
18 | from sans.calculator.slit_length_calculator import SlitlengthCalculator |
---|
19 | from calculator_widgets import OutputTextCtrl |
---|
20 | from calculator_widgets import InterActiveOutputTextCtrl |
---|
21 | |
---|
22 | _BOX_WIDTH = 76 |
---|
23 | #Slit length panel size |
---|
24 | if sys.platform.count("win32") > 0: |
---|
25 | PANEL_WIDTH = 500 |
---|
26 | PANEL_HEIGHT = 200 |
---|
27 | FONT_VARIANT = 0 |
---|
28 | else: |
---|
29 | PANEL_WIDTH = 530 |
---|
30 | PANEL_HEIGHT = 210 |
---|
31 | FONT_VARIANT = 1 |
---|
32 | |
---|
33 | class 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=None) |
---|
192 | self.reader.queue() |
---|
193 | except: |
---|
194 | if self.parent.parent is None: |
---|
195 | return |
---|
196 | msg = "Slit Length Calculator: %s" % (sys.exc_value) |
---|
197 | wx.PostEvent(self.parent.parent, |
---|
198 | StatusEvent(status=msg, type='stop')) |
---|
199 | return |
---|
200 | |
---|
201 | def complete_loading(self, data=None, filename=''): |
---|
202 | """ |
---|
203 | Complete the loading and compute the slit size |
---|
204 | """ |
---|
205 | if data is None or data.__class__.__name__ == 'Data2D': |
---|
206 | if self.parent.parent is None: |
---|
207 | return |
---|
208 | msg = "Slit Length cannot be computed for 2D Data" |
---|
209 | wx.PostEvent(self.parent.parent, |
---|
210 | StatusEvent(status=msg, type='stop')) |
---|
211 | return |
---|
212 | self.data_name_tcl.SetValue(str(data.filename)) |
---|
213 | #compute the slit size |
---|
214 | try: |
---|
215 | x = data.x |
---|
216 | y = data.y |
---|
217 | if x == [] or x is None or y == [] or y is None: |
---|
218 | msg = "The current data is empty please check x and y" |
---|
219 | raise ValueError, msg |
---|
220 | slit_length_calculator = SlitlengthCalculator() |
---|
221 | slit_length_calculator.set_data(x=x, y=y) |
---|
222 | slit_length = slit_length_calculator.calculate_slit_length() |
---|
223 | except: |
---|
224 | if self.parent.parent is None: |
---|
225 | return |
---|
226 | msg = "Slit Size Calculator: %s" % (sys.exc_value) |
---|
227 | wx.PostEvent(self.parent.parent, |
---|
228 | StatusEvent(status=msg, type='stop')) |
---|
229 | return |
---|
230 | self.slit_size_tcl.SetValue(str(slit_length)) |
---|
231 | #Display unit |
---|
232 | self.slit_size_unit_tcl.SetValue('[Unknown]') |
---|
233 | if self.parent.parent is None: |
---|
234 | return |
---|
235 | msg = "Load Complete" |
---|
236 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, type='stop')) |
---|
237 | |
---|
238 | |
---|
239 | class SlitLengthCalculatorWindow(wx.Frame): |
---|
240 | """ |
---|
241 | """ |
---|
242 | def __init__(self, parent=None, title="Slit Size Calculator", |
---|
243 | size=(PANEL_WIDTH,PANEL_HEIGHT), *args, **kwds): |
---|
244 | """ |
---|
245 | """ |
---|
246 | kwds['size']= size |
---|
247 | kwds['title']= title |
---|
248 | wx.Frame.__init__(self, parent, *args, **kwds) |
---|
249 | self.parent = parent |
---|
250 | self.panel = SlitLengthCalculatorPanel(parent=self) |
---|
251 | self.Centre() |
---|
252 | self.Show(True) |
---|
253 | |
---|
254 | if __name__ == "__main__": |
---|
255 | app = wx.PySimpleApp() |
---|
256 | frame = SlitLengthCalculatorWindow() |
---|
257 | frame.Show(True) |
---|
258 | app.MainLoop() |
---|