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 DataLoader.readers.ascii_reader import Reader |
---|
16 | from sans.guicomm.events import StatusEvent |
---|
17 | from sans.calculator.slit_length_calculator import SlitlengthCalculator |
---|
18 | from calculator_widgets import OutputTextCtrl, InterActiveOutputTextCtrl |
---|
19 | |
---|
20 | _BOX_WIDTH = 76 |
---|
21 | #Slit length panel size |
---|
22 | if sys.platform.count("win32") > 0: |
---|
23 | PANEL_WIDTH = 500 |
---|
24 | PANEL_HEIGHT = 200 |
---|
25 | FONT_VARIANT = 0 |
---|
26 | else: |
---|
27 | PANEL_WIDTH = 530 |
---|
28 | PANEL_HEIGHT = 230 |
---|
29 | FONT_VARIANT = 1 |
---|
30 | |
---|
31 | class SlitLengthCalculatorPanel(wx.Panel): |
---|
32 | """ |
---|
33 | Provides the slit length calculator GUI. |
---|
34 | """ |
---|
35 | ## Internal nickname for the window, used by the AUI manager |
---|
36 | window_name = "Slit Size Calculator" |
---|
37 | ## Name to appear on the window title bar |
---|
38 | window_caption = "Slit Size Calculator" |
---|
39 | ## Flag to tell the AUI manager to put this panel in the center pane |
---|
40 | CENTER_PANE = True |
---|
41 | |
---|
42 | def __init__(self, parent, id=-1, *args, **kwds): |
---|
43 | wx.Panel.__init__(self, parent, id=id, *args, **kwds) |
---|
44 | #Font size |
---|
45 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
46 | #thread to read data |
---|
47 | self.reader = None |
---|
48 | # Default location |
---|
49 | self._default_save_location = os.getcwd() |
---|
50 | # Object that receive status event |
---|
51 | self.parent = parent |
---|
52 | self._do_layout() |
---|
53 | |
---|
54 | def _define_structure(self): |
---|
55 | """ |
---|
56 | Define the main sizers building to build this application. |
---|
57 | """ |
---|
58 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
59 | self.box_source = wx.StaticBox(self, -1,str("Slit Size Calculator")) |
---|
60 | self.boxsizer_source = wx.StaticBoxSizer(self.box_source, |
---|
61 | wx.VERTICAL) |
---|
62 | self.data_name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
63 | self.slit_size_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
64 | self.hint_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 | data_hint = "Loaded data" |
---|
74 | self.data_name_tcl.SetToolTipString(data_hint) |
---|
75 | #control that triggers importing data |
---|
76 | id = wx.NewId() |
---|
77 | self.browse_button = wx.Button(self, id, "Browse") |
---|
78 | hint_on_browse = "Click on this button to import data in this panel." |
---|
79 | self.browse_button.SetToolTipString(hint_on_browse) |
---|
80 | self.Bind(wx.EVT_BUTTON, self.on_load_data, id=id) |
---|
81 | self.data_name_sizer.AddMany([(data_name_txt, 0, wx.LEFT, 15), |
---|
82 | (self.data_name_tcl, 0, wx.LEFT, 10), |
---|
83 | (self.browse_button, 0, wx.LEFT, 10)]) |
---|
84 | def _layout_slit_size(self): |
---|
85 | """ |
---|
86 | Fill the sizer containing slit size information |
---|
87 | """ |
---|
88 | slit_size_txt = wx.StaticText(self, -1, 'Slit Size (FWHM): ') |
---|
89 | self.slit_size_tcl = InterActiveOutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
90 | slit_size_hint = " Estimated full slit size" |
---|
91 | self.slit_size_tcl.SetToolTipString(slit_size_hint) |
---|
92 | slit_size_unit_txt = wx.StaticText(self, -1, 'Unit: ') |
---|
93 | self.slit_size_unit_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1)) |
---|
94 | slit_size_unit_hint = "Full slit size's unit" |
---|
95 | self.slit_size_unit_tcl.SetToolTipString(slit_size_unit_hint) |
---|
96 | self.slit_size_sizer.AddMany([(slit_size_txt, 0, wx.LEFT, 15), |
---|
97 | (self.slit_size_tcl, 0, wx.LEFT, 10), |
---|
98 | (slit_size_unit_txt, 0, wx.LEFT, 10), |
---|
99 | (self.slit_size_unit_tcl, 0, wx.LEFT, 10)]) |
---|
100 | |
---|
101 | def _layout_hint(self): |
---|
102 | """ |
---|
103 | Fill the sizer containing hint |
---|
104 | """ |
---|
105 | hint_msg = "This calculation works only for SAXSess beam profile data." |
---|
106 | self.hint_txt = wx.StaticText(self, -1, hint_msg) |
---|
107 | self.hint_sizer.AddMany([(self.hint_txt, 0, wx.LEFT, 15)]) |
---|
108 | |
---|
109 | def _layout_button(self): |
---|
110 | """ |
---|
111 | Do the layout for the button widgets |
---|
112 | """ |
---|
113 | self.bt_close = wx.Button(self, wx.ID_CANCEL,'Close') |
---|
114 | self.bt_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
115 | self.bt_close.SetToolTipString("Close this window.") |
---|
116 | self.button_sizer.AddMany([(self.bt_close, 0, wx.LEFT, 390)]) |
---|
117 | |
---|
118 | def _do_layout(self): |
---|
119 | """ |
---|
120 | Draw window content |
---|
121 | """ |
---|
122 | self._define_structure() |
---|
123 | self._layout_data_name() |
---|
124 | self._layout_slit_size() |
---|
125 | self._layout_hint() |
---|
126 | self._layout_button() |
---|
127 | self.boxsizer_source.AddMany([(self.data_name_sizer, 0, |
---|
128 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
129 | (self.slit_size_sizer, 0, |
---|
130 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5), |
---|
131 | (self.hint_sizer, 0, |
---|
132 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
133 | self.main_sizer.AddMany([(self.boxsizer_source, 0, wx.ALL, 10), |
---|
134 | (self.button_sizer, 0, |
---|
135 | wx.EXPAND|wx.TOP|wx.BOTTOM, 5)]) |
---|
136 | self.SetSizer(self.main_sizer) |
---|
137 | self.SetAutoLayout(True) |
---|
138 | |
---|
139 | def choose_data_file(self, location=None): |
---|
140 | path = None |
---|
141 | filename = '' |
---|
142 | if location == None: |
---|
143 | location = os.getcwd() |
---|
144 | |
---|
145 | wildcard = "SAXSess Data 1D (*.DAT, *.dat)|*.DAT" |
---|
146 | |
---|
147 | dlg = wx.FileDialog(self, "Choose a file", location,"", wildcard, wx.OPEN) |
---|
148 | if dlg.ShowModal() == wx.ID_OK: |
---|
149 | path = dlg.GetPath() |
---|
150 | filename = os.path.basename(path) |
---|
151 | dlg.Destroy() |
---|
152 | |
---|
153 | return path |
---|
154 | |
---|
155 | def on_close(self, event): |
---|
156 | """ |
---|
157 | close the window containing this panel |
---|
158 | """ |
---|
159 | self.parent.Close() |
---|
160 | |
---|
161 | def on_load_data(self, event): |
---|
162 | """ |
---|
163 | Open a file dialog to allow the user to select a given file. |
---|
164 | The user is only allow to load file with extension .DAT or .dat. |
---|
165 | Display the slit size corresponding to the loaded data. |
---|
166 | """ |
---|
167 | path = self.choose_data_file(location=self._default_save_location) |
---|
168 | |
---|
169 | if path is None: |
---|
170 | return |
---|
171 | self._default_save_location = path |
---|
172 | try: |
---|
173 | #Load data |
---|
174 | from load_thread import DataReader |
---|
175 | ## If a thread is already started, stop it |
---|
176 | if self.reader is not None and self.reader.isrunning(): |
---|
177 | self.reader.stop() |
---|
178 | if self.parent.parent is not None: |
---|
179 | wx.PostEvent(self.parent.parent, StatusEvent(status="Loading...", |
---|
180 | type="progress")) |
---|
181 | self.reader = DataReader(path=path, |
---|
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 None or 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(data.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 | |
---|
227 | class 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 | |
---|
235 | if __name__ == "__main__": |
---|
236 | app = wx.PySimpleApp() |
---|
237 | frame = SlitLengthCalculatorWindow() |
---|
238 | frame.Show(True) |
---|
239 | app.MainLoop() |
---|