1 | |
---|
2 | ################################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | # |
---|
7 | #See the license text in license.txt |
---|
8 | # |
---|
9 | #copyright 2008, University of Tennessee |
---|
10 | ################################################################################ |
---|
11 | |
---|
12 | |
---|
13 | import wx |
---|
14 | import sys |
---|
15 | import os |
---|
16 | import pylab |
---|
17 | import math |
---|
18 | import numpy |
---|
19 | import time |
---|
20 | |
---|
21 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
22 | from danse.common.plottools.SizeDialog import SizeDialog |
---|
23 | from danse.common.plottools.LabelDialog import LabelDialog |
---|
24 | #from danse.common.plottools.plottables import Graph |
---|
25 | from sans.guiframe import dataFitting |
---|
26 | from sans.guiframe.events import EVT_NEW_PLOT |
---|
27 | from sans.guiframe.events import StatusEvent |
---|
28 | from sans.guiframe.events import NewPlotEvent |
---|
29 | from sans.guiframe.events import NewColorEvent |
---|
30 | from sans.guiframe.events import SlicerEvent |
---|
31 | from sans.guiframe.events import PanelOnFocusEvent |
---|
32 | from sans.guiframe.events import EVT_NEW_LOADED_DATA |
---|
33 | from sans.guiframe.utils import PanelMenu |
---|
34 | from sans.guiframe.dataFitting import Data1D |
---|
35 | from sans.guiframe.panel_base import PanelBase |
---|
36 | from binder import BindArtist |
---|
37 | |
---|
38 | DEFAULT_QMAX = 0.05 |
---|
39 | DEFAULT_QSTEP = 0.001 |
---|
40 | DEFAULT_BEAM = 0.005 |
---|
41 | BIN_WIDTH = 1 |
---|
42 | |
---|
43 | |
---|
44 | class ModelPanel1D(PlotPanel, PanelBase): |
---|
45 | """ |
---|
46 | Plot panel for use with the GUI manager |
---|
47 | """ |
---|
48 | |
---|
49 | ## Internal name for the AUI manager |
---|
50 | window_name = "plotpanel" |
---|
51 | ## Title to appear on top of the window |
---|
52 | window_caption = "Plot Panel" |
---|
53 | ## Flag to tell the GUI manager that this panel is not |
---|
54 | # tied to any perspective |
---|
55 | ALWAYS_ON = True |
---|
56 | ## Group ID |
---|
57 | group_id = None |
---|
58 | |
---|
59 | def __init__(self, parent, id=-1, color = None, |
---|
60 | dpi=None, style=wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
61 | PlotPanel.__init__(self, parent, id=id, style=style, **kwargs) |
---|
62 | PanelBase.__init__(self, parent) |
---|
63 | ## Reference to the parent window |
---|
64 | self.parent = parent |
---|
65 | ## Plottables |
---|
66 | self.plots = {} |
---|
67 | #context menu |
---|
68 | self._slicerpop = None |
---|
69 | |
---|
70 | self._available_data = [] |
---|
71 | self._menu_add_ids = [] |
---|
72 | self._symbol_labels = self.get_symbol_label() |
---|
73 | self._color_labels = self.get_color_label() |
---|
74 | self.currColorIndex = "" |
---|
75 | |
---|
76 | self.hide_menu = None |
---|
77 | ## Unique ID (from gui_manager) |
---|
78 | self.uid = None |
---|
79 | self.x_size = None |
---|
80 | ## Default locations |
---|
81 | self._default_save_location = os.getcwd() |
---|
82 | self.size = None |
---|
83 | ## Graph |
---|
84 | #self.graph = Graph() |
---|
85 | self.graph.xaxis("\\rm{Q}", 'A^{-1}') |
---|
86 | self.graph.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
87 | self.graph.render(self) |
---|
88 | |
---|
89 | # In resizing event |
---|
90 | self.resizing = False |
---|
91 | self.canvas.set_resizing(self.resizing) |
---|
92 | self.Bind(wx.EVT_SIZE, self._OnReSize) |
---|
93 | self._add_more_tool() |
---|
94 | |
---|
95 | def get_symbol_label(self): |
---|
96 | """ |
---|
97 | Associates label to symbol |
---|
98 | """ |
---|
99 | _labels = {} |
---|
100 | i = 0 |
---|
101 | _labels['Circle'] = i |
---|
102 | i += 1 |
---|
103 | _labels['Cross X '] = i |
---|
104 | i += 1 |
---|
105 | _labels['Triangle Down'] = i |
---|
106 | i += 1 |
---|
107 | _labels['Triangle Up'] = i |
---|
108 | i += 1 |
---|
109 | _labels['Triangle Left'] = i |
---|
110 | i += 1 |
---|
111 | _labels['Triangle Right'] = i |
---|
112 | i += 1 |
---|
113 | _labels['Cross +'] = i |
---|
114 | i += 1 |
---|
115 | _labels['Square'] = i |
---|
116 | i += 1 |
---|
117 | _labels['Diamond'] = i |
---|
118 | i += 1 |
---|
119 | _labels['Diamond'] = i |
---|
120 | i += 1 |
---|
121 | _labels['Hexagon1'] = i |
---|
122 | i += 1 |
---|
123 | _labels['Hexagon2'] = i |
---|
124 | i += 1 |
---|
125 | _labels['Pentagon'] = i |
---|
126 | i += 1 |
---|
127 | _labels['Line'] = i |
---|
128 | return _labels |
---|
129 | |
---|
130 | def get_color_label(self): |
---|
131 | """ |
---|
132 | Associates label to a specific color |
---|
133 | """ |
---|
134 | _labels = {} |
---|
135 | i = 0 |
---|
136 | _labels['Blue'] = i |
---|
137 | i += 1 |
---|
138 | _labels['Green'] = i |
---|
139 | i += 1 |
---|
140 | _labels['Red'] = i |
---|
141 | i += 1 |
---|
142 | _labels['Cyan'] = i |
---|
143 | i += 1 |
---|
144 | _labels['Magenta'] = i |
---|
145 | i += 1 |
---|
146 | _labels['Yellow'] = i |
---|
147 | return _labels |
---|
148 | |
---|
149 | |
---|
150 | def set_data(self, list=None): |
---|
151 | """ |
---|
152 | """ |
---|
153 | pass |
---|
154 | |
---|
155 | def _reset(self): |
---|
156 | """ |
---|
157 | Resets internal data and graph |
---|
158 | """ |
---|
159 | self.graph.reset() |
---|
160 | self.plots = {} |
---|
161 | |
---|
162 | def _OnReSize(self, event): |
---|
163 | """ |
---|
164 | On response of the resize of a panel, set axes_visiable False |
---|
165 | """ |
---|
166 | # It was found that wx >= 2.9.3 sends an event even if no size changed. |
---|
167 | # So manually recode the size (=x_size) and compare here. |
---|
168 | if self.x_size != None: |
---|
169 | if self.x_size == self.GetSize(): |
---|
170 | self.resizing = False |
---|
171 | self.canvas.set_resizing(self.resizing) |
---|
172 | return |
---|
173 | self.x_size = self.GetSize() |
---|
174 | |
---|
175 | # Ready for another event |
---|
176 | # Do not remove this Skip. Otherwise it will get runtime error on wx>=2.9. |
---|
177 | event.Skip() |
---|
178 | # set the resizing flag |
---|
179 | self.resizing = True |
---|
180 | self.canvas.set_resizing(self.resizing) |
---|
181 | self.parent.set_schedule(True) |
---|
182 | pos_x, pos_y = self.GetPositionTuple() |
---|
183 | if pos_x != 0 and pos_y != 0: |
---|
184 | self.size, _ = self.GetClientSizeTuple() |
---|
185 | |
---|
186 | def set_resizing(self, resizing=False): |
---|
187 | """ |
---|
188 | Set the resizing (True/False) |
---|
189 | """ |
---|
190 | self.resizing = resizing |
---|
191 | #self.canvas.set_resizing(resizing) |
---|
192 | |
---|
193 | def schedule_full_draw(self, func='append'): |
---|
194 | """ |
---|
195 | Put self in schedule to full redraw list |
---|
196 | """ |
---|
197 | # append/del this panel in the schedule list |
---|
198 | self.parent.set_schedule_full_draw(self, func) |
---|
199 | |
---|
200 | |
---|
201 | def remove_data_by_id(self, id): |
---|
202 | """' |
---|
203 | remove data from plot |
---|
204 | """ |
---|
205 | if id in self.plots.keys(): |
---|
206 | data = self.plots[id] |
---|
207 | self.graph.delete(data) |
---|
208 | data_manager = self._manager.parent.get_data_manager() |
---|
209 | data_list, theory_list = data_manager.get_by_id(id_list=[id]) |
---|
210 | |
---|
211 | if id in data_list.keys(): |
---|
212 | data = data_list[id] |
---|
213 | if id in theory_list.keys(): |
---|
214 | data = theory_list[id] |
---|
215 | |
---|
216 | del self.plots[id] |
---|
217 | self.graph.render(self) |
---|
218 | self.subplot.figure.canvas.draw_idle() |
---|
219 | if len(self.graph.plottables) == 0: |
---|
220 | #onRemove: graph is empty must be the panel must be destroyed |
---|
221 | self.parent.delete_panel(self.uid) |
---|
222 | |
---|
223 | def plot_data(self, data): |
---|
224 | """ |
---|
225 | Data is ready to be displayed |
---|
226 | |
---|
227 | :param event: data event |
---|
228 | """ |
---|
229 | if data.id in self.plots.keys(): |
---|
230 | #replace |
---|
231 | self.graph.replace(data) |
---|
232 | self.plots[data.id] = data |
---|
233 | else: |
---|
234 | self.plots[data.id] = data |
---|
235 | self.graph.add(self.plots[data.id]) |
---|
236 | |
---|
237 | ## Set the view scale for all plots |
---|
238 | self._onEVT_FUNC_PROPERTY() |
---|
239 | ## render the graph<=No need this done in canvas |
---|
240 | #self.graph.render(self) |
---|
241 | #self.subplot.figure.canvas.draw_idle() |
---|
242 | |
---|
243 | def draw_plot(self): |
---|
244 | """ |
---|
245 | Draw plot |
---|
246 | """ |
---|
247 | self.draw() |
---|
248 | |
---|
249 | |
---|
250 | |
---|
251 | def onLeftDown(self,event): |
---|
252 | """ |
---|
253 | left button down and ready to drag |
---|
254 | Display the position of the mouse on the statusbar |
---|
255 | """ |
---|
256 | PlotPanel.onLeftDown(self, event) |
---|
257 | ax = event.inaxes |
---|
258 | if ax != None: |
---|
259 | position = "x: %8.3g y: %8.3g" % (event.xdata, event.ydata) |
---|
260 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
261 | # unfocus all |
---|
262 | self.parent.set_plot_unfocus() |
---|
263 | #post nd event to notify guiframe that this panel is on focus |
---|
264 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
265 | |
---|
266 | |
---|
267 | def _ontoggle_hide_error(self, event): |
---|
268 | """ |
---|
269 | Toggle error display to hide or show |
---|
270 | """ |
---|
271 | |
---|
272 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
273 | if self.hide_menu.GetText() == "Hide Error": |
---|
274 | selected_plot.hide_error = True |
---|
275 | else: |
---|
276 | selected_plot.hide_error = False |
---|
277 | ## increment graph color |
---|
278 | self.graph.render(self) |
---|
279 | self.subplot.figure.canvas.draw_idle() |
---|
280 | |
---|
281 | def _onRemove(self, event): |
---|
282 | """ |
---|
283 | Remove a plottable from the graph and render the graph |
---|
284 | |
---|
285 | :param event: Menu event |
---|
286 | |
---|
287 | """ |
---|
288 | ## Check if there is a selected graph to remove |
---|
289 | if self.graph.selected_plottable in self.plots.keys(): |
---|
290 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
291 | id = self.graph.selected_plottable |
---|
292 | self.remove_data_by_id(id) |
---|
293 | |
---|
294 | def onContextMenu(self, event): |
---|
295 | """ |
---|
296 | 1D plot context menu |
---|
297 | |
---|
298 | :param event: wx context event |
---|
299 | |
---|
300 | """ |
---|
301 | self._slicerpop = PanelMenu() |
---|
302 | self._slicerpop.set_plots(self.plots) |
---|
303 | self._slicerpop.set_graph(self.graph) |
---|
304 | # Various plot options |
---|
305 | id = wx.NewId() |
---|
306 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
307 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
308 | id = wx.NewId() |
---|
309 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
310 | wx.EVT_MENU(self, id, self.onPrint) |
---|
311 | id = wx.NewId() |
---|
312 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
313 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
314 | |
---|
315 | id = wx.NewId() |
---|
316 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
317 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
318 | |
---|
319 | self._slicerpop.AppendSeparator() |
---|
320 | |
---|
321 | #add menu of other plugins |
---|
322 | item_list = self.parent.get_context_menu(self) |
---|
323 | |
---|
324 | if (not item_list == None) and (not len(item_list) == 0): |
---|
325 | for item in item_list: |
---|
326 | try: |
---|
327 | id = wx.NewId() |
---|
328 | self._slicerpop.Append(id, item[0], item[1]) |
---|
329 | wx.EVT_MENU(self, id, item[2]) |
---|
330 | except: |
---|
331 | msg = "ModelPanel1D.onContextMenu: " |
---|
332 | msg += "bad menu item %s" % sys.exc_value |
---|
333 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
334 | pass |
---|
335 | self._slicerpop.AppendSeparator() |
---|
336 | #id = wx.NewId() |
---|
337 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
338 | if self.graph.selected_plottable in self.plots: |
---|
339 | plot = self.plots[self.graph.selected_plottable] |
---|
340 | |
---|
341 | id = wx.NewId() |
---|
342 | name = plot.name |
---|
343 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
344 | self._slicerpop.AppendSeparator() |
---|
345 | if plot.name != 'SLD': |
---|
346 | wx.EVT_MENU(self, id, self._onSave) |
---|
347 | id = wx.NewId() |
---|
348 | self._slicerpop.Append(id, '&Linear Fit') |
---|
349 | wx.EVT_MENU(self, id, self.onFitting) |
---|
350 | self._slicerpop.AppendSeparator() |
---|
351 | |
---|
352 | id = wx.NewId() |
---|
353 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
354 | wx.EVT_MENU(self, id, self._onRemove) |
---|
355 | if not plot.is_data: |
---|
356 | id = wx.NewId() |
---|
357 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
358 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
359 | self._slicerpop.AppendSeparator() |
---|
360 | symbol_menu = wx.Menu() |
---|
361 | for label in self._symbol_labels: |
---|
362 | id = wx.NewId() |
---|
363 | symbol_menu.Append(id, str(label), str(label)) |
---|
364 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
365 | id = wx.NewId() |
---|
366 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
367 | |
---|
368 | color_menu = wx.Menu() |
---|
369 | for label in self._color_labels: |
---|
370 | id = wx.NewId() |
---|
371 | color_menu.Append(id, str(label), str(label)) |
---|
372 | wx.EVT_MENU(self, id, self.onChangeColor) |
---|
373 | id = wx.NewId() |
---|
374 | self._slicerpop.AppendMenu(id, '&Modify Symbol Color', color_menu) |
---|
375 | |
---|
376 | size_menu = wx.Menu() |
---|
377 | for i in range(10): |
---|
378 | id = wx.NewId() |
---|
379 | size_menu.Append(id, str(i), str(i)) |
---|
380 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
381 | id = wx.NewId() |
---|
382 | size_menu.Append(id, '&Custom', 'Custom') |
---|
383 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
384 | id = wx.NewId() |
---|
385 | self._slicerpop.AppendMenu(id, '&Modify Symbol Size', size_menu) |
---|
386 | |
---|
387 | self._slicerpop.AppendSeparator() |
---|
388 | |
---|
389 | self.hide_menu = self._slicerpop.Append(id, "Hide Error") |
---|
390 | |
---|
391 | if plot.dy is not None and plot.dy != []: |
---|
392 | if plot.hide_error : |
---|
393 | self.hide_menu.SetText('Show Error') |
---|
394 | else: |
---|
395 | self.hide_menu.SetText('Hide Error') |
---|
396 | else: |
---|
397 | self.hide_menu.Enable(False) |
---|
398 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
399 | |
---|
400 | self._slicerpop.AppendSeparator() |
---|
401 | id = wx.NewId() |
---|
402 | self._slicerpop.Append(id, '&Edit Legend Label', 'Edit Legend Label') |
---|
403 | wx.EVT_MENU(self, id, self.onEditLabels) |
---|
404 | # Option to hide |
---|
405 | #TODO: implement functionality to hide a plottable (legend click) |
---|
406 | |
---|
407 | loc_menu = wx.Menu() |
---|
408 | for label in self._loc_labels: |
---|
409 | id = wx.NewId() |
---|
410 | loc_menu.Append(id, str(label), str(label)) |
---|
411 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
412 | id = wx.NewId() |
---|
413 | self._slicerpop.AppendMenu(id, '&Modify Legend Location', loc_menu) |
---|
414 | |
---|
415 | id = wx.NewId() |
---|
416 | self._slicerpop.Append(id, '&Toggle Legend On/Off', 'Toggle Legend On/Off') |
---|
417 | wx.EVT_MENU(self, id, self.onLegend) |
---|
418 | self._slicerpop.AppendSeparator() |
---|
419 | |
---|
420 | id = wx.NewId() |
---|
421 | self._slicerpop.Append(id, '&Change scale') |
---|
422 | wx.EVT_MENU(self, id, self._onProperties) |
---|
423 | id = wx.NewId() |
---|
424 | self._slicerpop.Append(id, '&Reset Graph') |
---|
425 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
426 | pos = event.GetPosition() |
---|
427 | pos = self.ScreenToClient(pos) |
---|
428 | self.PopupMenu(self._slicerpop, pos) |
---|
429 | |
---|
430 | def onFreeze(self, event): |
---|
431 | """ |
---|
432 | """ |
---|
433 | plot = self.plots[self.graph.selected_plottable] |
---|
434 | self.parent.onfreeze([plot.id]) |
---|
435 | |
---|
436 | def onEditLabels(self, event): |
---|
437 | """ |
---|
438 | """ |
---|
439 | menu = event.GetEventObject() |
---|
440 | id = event.GetId() |
---|
441 | label = menu.GetLabel(id) |
---|
442 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
443 | |
---|
444 | dial = LabelDialog(None, -1, 'Change Plot Label') |
---|
445 | if dial.ShowModal() == wx.ID_OK: |
---|
446 | newLabel = dial.getText() |
---|
447 | selected_plot.name = newLabel |
---|
448 | dial.Destroy() |
---|
449 | |
---|
450 | self.graph.render(self) |
---|
451 | self._onEVT_FUNC_PROPERTY() |
---|
452 | |
---|
453 | |
---|
454 | def onChangeColor(self, event): |
---|
455 | """ |
---|
456 | Changes the color of the graph when selected |
---|
457 | """ |
---|
458 | menu = event.GetEventObject() |
---|
459 | id = event.GetId() |
---|
460 | label = menu.GetLabel(id) |
---|
461 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
462 | selected_plot.custom_color = self._color_labels[label] |
---|
463 | ## Set the view scale for all plots |
---|
464 | self._onEVT_FUNC_PROPERTY() |
---|
465 | ## render the graph |
---|
466 | #self.graph.render(self) |
---|
467 | #self.subplot.figure.canvas.draw_idle() |
---|
468 | print "PARENT: ", self.parent |
---|
469 | wx.PostEvent(self.parent, |
---|
470 | NewColorEvent(color=selected_plot.custom_color, |
---|
471 | id=selected_plot.id)) |
---|
472 | |
---|
473 | def onChangeSize(self, event): |
---|
474 | |
---|
475 | menu = event.GetEventObject() |
---|
476 | id = event.GetId() |
---|
477 | label = menu.GetLabel(id) |
---|
478 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
479 | |
---|
480 | if label == "&Custom": |
---|
481 | sizedial = SizeDialog(None, -1, 'Change Marker Size') |
---|
482 | if sizedial.ShowModal() == wx.ID_OK: |
---|
483 | label = sizedial.getText() |
---|
484 | sizedial.Destroy() |
---|
485 | |
---|
486 | selected_plot.marker_size = int(label) |
---|
487 | self._onEVT_FUNC_PROPERTY() |
---|
488 | ## Set the view scale for all plots |
---|
489 | |
---|
490 | ## render the graph |
---|
491 | #self.graph.render(self) |
---|
492 | #self.subplot.figure.canvas.draw_idle() |
---|
493 | |
---|
494 | |
---|
495 | def onChangeSymbol(self, event): |
---|
496 | """ |
---|
497 | """ |
---|
498 | menu = event.GetEventObject() |
---|
499 | id = event.GetId() |
---|
500 | label = menu.GetLabel(id) |
---|
501 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
502 | selected_plot.symbol = self._symbol_labels[label] |
---|
503 | ## Set the view scale for all plots |
---|
504 | self._onEVT_FUNC_PROPERTY() |
---|
505 | ## render the graph |
---|
506 | #self.graph.render(self) |
---|
507 | #self.subplot.figure.canvas.draw_idle() |
---|
508 | |
---|
509 | |
---|
510 | |
---|
511 | def _onsaveTXT(self, path): |
---|
512 | """ |
---|
513 | Save file as txt |
---|
514 | |
---|
515 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
516 | |
---|
517 | """ |
---|
518 | data = self.plots[self.graph.selected_plottable] |
---|
519 | |
---|
520 | if not path == None: |
---|
521 | out = open(path, 'w') |
---|
522 | has_errors = True |
---|
523 | if data.dy == None or data.dy == []: |
---|
524 | has_errors = False |
---|
525 | # Sanity check |
---|
526 | if has_errors: |
---|
527 | try: |
---|
528 | if len(data.y) != len(data.dy): |
---|
529 | has_errors = False |
---|
530 | except: |
---|
531 | has_errors = False |
---|
532 | if has_errors: |
---|
533 | if data.dx != None: |
---|
534 | out.write("<X> <Y> <dY> <dX>\n") |
---|
535 | else: |
---|
536 | out.write("<X> <Y> <dY>\n") |
---|
537 | else: |
---|
538 | out.write("<X> <Y>\n") |
---|
539 | |
---|
540 | for i in range(len(data.x)): |
---|
541 | if has_errors: |
---|
542 | if data.dx != None: |
---|
543 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
544 | data.y[i], |
---|
545 | data.dy[i], |
---|
546 | data.dx[i])) |
---|
547 | else: |
---|
548 | out.write("%g %g %g\n" % (data.x[i], |
---|
549 | data.y[i], |
---|
550 | data.dy[i])) |
---|
551 | else: |
---|
552 | out.write("%g %g\n" % (data.x[i], |
---|
553 | data.y[i])) |
---|
554 | out.close() |
---|
555 | try: |
---|
556 | self._default_save_location = os.path.dirname(path) |
---|
557 | except: |
---|
558 | pass |
---|
559 | |
---|
560 | def _onSave(self, evt): |
---|
561 | """ |
---|
562 | Save a data set to a text file |
---|
563 | |
---|
564 | :param evt: Menu event |
---|
565 | |
---|
566 | """ |
---|
567 | |
---|
568 | path = None |
---|
569 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
570 | "CanSAS 1D files(*.xml)|*.xml" |
---|
571 | dlg = wx.FileDialog(self, "Choose a file", |
---|
572 | self._default_save_location, |
---|
573 | "", wildcard , wx.SAVE) |
---|
574 | |
---|
575 | if dlg.ShowModal() == wx.ID_OK: |
---|
576 | path = dlg.GetPath() |
---|
577 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
578 | # This is MAC Fix |
---|
579 | ext_num = dlg.GetFilterIndex() |
---|
580 | if ext_num == 0: |
---|
581 | format = '.txt' |
---|
582 | else: |
---|
583 | format = '.xml' |
---|
584 | path = os.path.splitext(path)[0] + format |
---|
585 | mypath = os.path.basename(path) |
---|
586 | |
---|
587 | #TODO: This is bad design. The DataLoader is designed |
---|
588 | #to recognize extensions. |
---|
589 | # It should be a simple matter of calling the . |
---|
590 | #save(file, data, '.xml') method |
---|
591 | # of the sans.dataloader.loader.Loader class. |
---|
592 | from sans.dataloader.loader import Loader |
---|
593 | #Instantiate a loader |
---|
594 | loader = Loader() |
---|
595 | data = self.plots[self.graph.selected_plottable] |
---|
596 | format = ".txt" |
---|
597 | if os.path.splitext(mypath)[1].lower() == format: |
---|
598 | # Make sure the ext included in the file name |
---|
599 | # especially on MAC |
---|
600 | fName = os.path.splitext(path)[0] + format |
---|
601 | self._onsaveTXT(fName) |
---|
602 | format = ".xml" |
---|
603 | if os.path.splitext(mypath)[1].lower() == format: |
---|
604 | # Make sure the ext included in the file name |
---|
605 | # especially on MAC |
---|
606 | fName = os.path.splitext(path)[0] + format |
---|
607 | loader.save(fName, data, format) |
---|
608 | try: |
---|
609 | self._default_save_location = os.path.dirname(path) |
---|
610 | except: |
---|
611 | pass |
---|
612 | dlg.Destroy() |
---|
613 | |
---|
614 | def _add_more_tool(self): |
---|
615 | """ |
---|
616 | Add refresh, add/delete button in the tool bar |
---|
617 | """ |
---|
618 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
619 | return |
---|
620 | self.toolbar.AddSeparator() |
---|
621 | id_delete = wx.NewId() |
---|
622 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
623 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
624 | 'Delete', 'permanently Delete') |
---|
625 | |
---|
626 | self.toolbar.Realize() |
---|
627 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
628 | |
---|
629 | #New toolbar option - adding text to the plot |
---|
630 | self.toolbar.AddSeparator() |
---|
631 | id_text = wx.NewId() |
---|
632 | text = wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR) |
---|
633 | self.toolbar.AddSimpleTool(id_text, text, |
---|
634 | 'Add Text to Plot', 'Adds text to plot') |
---|
635 | |
---|
636 | self.toolbar.Realize() |
---|
637 | wx.EVT_TOOL(self, id_text, self._on_addtext) |
---|
638 | |
---|
639 | self.toolbar.AddSeparator() |
---|
640 | id_text = wx.NewId() |
---|
641 | text = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) |
---|
642 | self.toolbar.AddSimpleTool(id_text, text, |
---|
643 | 'Remove Text from Plot', 'Removes text from plot') |
---|
644 | |
---|
645 | self.toolbar.Realize() |
---|
646 | wx.EVT_TOOL(self, id_text, self._on_removetext) |
---|
647 | |
---|
648 | def _on_delete(self, event): |
---|
649 | """ |
---|
650 | Refreshes the plotpanel on refresh tollbar button |
---|
651 | """ |
---|
652 | |
---|
653 | if self.parent is not None: |
---|
654 | wx.PostEvent(self.parent, |
---|
655 | NewPlotEvent(group_id=self.group_id, |
---|
656 | action="delete")) |
---|
657 | |
---|
658 | |
---|