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 |
---|
19 | |
---|
20 | _BOX_WIDTH = 76 |
---|
21 | #Slit length panel size |
---|
22 | if sys.platform.count("win32") > 0: |
---|
23 | PANEL_WIDTH = 500 |
---|
24 | PANEL_HEIGHT = 190 |
---|
25 | FONT_VARIANT = 0 |
---|
26 | else: |
---|
27 | PANEL_WIDTH = 530 |
---|
28 | PANEL_HEIGHT = 220 |
---|
29 | FONT_VARIANT = 1 |
---|
30 | |
---|
31 | |
---|
32 | class 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 | |
---|
216 | class 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 | |
---|
224 | if __name__ == "__main__": |
---|
225 | app = wx.PySimpleApp() |
---|
226 | frame = SlitLengthCalculatorWindow() |
---|
227 | frame.Show(True) |
---|
228 | app.MainLoop() |
---|