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 except label |
---|
231 | label = self.plots[data.id].label |
---|
232 | data.label = label |
---|
233 | self.graph.replace(data) |
---|
234 | self.plots[data.id] = data |
---|
235 | else: |
---|
236 | self.plots[data.id] = data |
---|
237 | self.graph.add(self.plots[data.id]) |
---|
238 | |
---|
239 | ## Set the view scale for all plots |
---|
240 | self._onEVT_FUNC_PROPERTY() |
---|
241 | ## render the graph<=No need this done in canvas |
---|
242 | #self.graph.render(self) |
---|
243 | #self.subplot.figure.canvas.draw_idle() |
---|
244 | |
---|
245 | def draw_plot(self): |
---|
246 | """ |
---|
247 | Draw plot |
---|
248 | """ |
---|
249 | self.draw() |
---|
250 | |
---|
251 | |
---|
252 | |
---|
253 | def onLeftDown(self,event): |
---|
254 | """ |
---|
255 | left button down and ready to drag |
---|
256 | Display the position of the mouse on the statusbar |
---|
257 | """ |
---|
258 | PlotPanel.onLeftDown(self, event) |
---|
259 | ax = event.inaxes |
---|
260 | if ax != None: |
---|
261 | # data coordinate position |
---|
262 | pos_x = "%8.3g"% event.xdata |
---|
263 | pos_y = "%8.3g"% event.ydata |
---|
264 | position = "x: %s y: %s" % (pos_x, pos_y) |
---|
265 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
266 | # unfocus all |
---|
267 | self.parent.set_plot_unfocus() |
---|
268 | #post nd event to notify guiframe that this panel is on focus |
---|
269 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
270 | |
---|
271 | |
---|
272 | def _ontoggle_hide_error(self, event): |
---|
273 | """ |
---|
274 | Toggle error display to hide or show |
---|
275 | """ |
---|
276 | |
---|
277 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
278 | if self.hide_menu.GetText() == "Hide Error": |
---|
279 | selected_plot.hide_error = True |
---|
280 | else: |
---|
281 | selected_plot.hide_error = False |
---|
282 | ## increment graph color |
---|
283 | self.graph.render(self) |
---|
284 | self.subplot.figure.canvas.draw_idle() |
---|
285 | |
---|
286 | def _onRemove(self, event): |
---|
287 | """ |
---|
288 | Remove a plottable from the graph and render the graph |
---|
289 | |
---|
290 | :param event: Menu event |
---|
291 | |
---|
292 | """ |
---|
293 | ## Check if there is a selected graph to remove |
---|
294 | if self.graph.selected_plottable in self.plots.keys(): |
---|
295 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
296 | id = self.graph.selected_plottable |
---|
297 | self.remove_data_by_id(id) |
---|
298 | |
---|
299 | def onContextMenu(self, event): |
---|
300 | """ |
---|
301 | 1D plot context menu |
---|
302 | |
---|
303 | :param event: wx context event |
---|
304 | |
---|
305 | """ |
---|
306 | self._slicerpop = PanelMenu() |
---|
307 | self._slicerpop.set_plots(self.plots) |
---|
308 | self._slicerpop.set_graph(self.graph) |
---|
309 | # Various plot options |
---|
310 | id = wx.NewId() |
---|
311 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
312 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
313 | id = wx.NewId() |
---|
314 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
315 | wx.EVT_MENU(self, id, self.onPrint) |
---|
316 | id = wx.NewId() |
---|
317 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
318 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
319 | |
---|
320 | id = wx.NewId() |
---|
321 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
322 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
323 | |
---|
324 | self._slicerpop.AppendSeparator() |
---|
325 | |
---|
326 | #add menu of other plugins |
---|
327 | item_list = self.parent.get_context_menu(self) |
---|
328 | |
---|
329 | if (not item_list == None) and (not len(item_list) == 0): |
---|
330 | for item in item_list: |
---|
331 | try: |
---|
332 | id = wx.NewId() |
---|
333 | self._slicerpop.Append(id, item[0], item[1]) |
---|
334 | wx.EVT_MENU(self, id, item[2]) |
---|
335 | except: |
---|
336 | msg = "ModelPanel1D.onContextMenu: " |
---|
337 | msg += "bad menu item %s" % sys.exc_value |
---|
338 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
339 | pass |
---|
340 | self._slicerpop.AppendSeparator() |
---|
341 | #id = wx.NewId() |
---|
342 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
343 | if self.graph.selected_plottable in self.plots: |
---|
344 | plot = self.plots[self.graph.selected_plottable] |
---|
345 | |
---|
346 | id = wx.NewId() |
---|
347 | name = plot.name |
---|
348 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
349 | self._slicerpop.AppendSeparator() |
---|
350 | if plot.name != 'SLD': |
---|
351 | wx.EVT_MENU(self, id, self._onSave) |
---|
352 | id = wx.NewId() |
---|
353 | self._slicerpop.Append(id, '&Linear Fit') |
---|
354 | wx.EVT_MENU(self, id, self.onFitting) |
---|
355 | self._slicerpop.AppendSeparator() |
---|
356 | |
---|
357 | id = wx.NewId() |
---|
358 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
359 | wx.EVT_MENU(self, id, self._onRemove) |
---|
360 | if not plot.is_data: |
---|
361 | id = wx.NewId() |
---|
362 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
363 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
364 | self._slicerpop.AppendSeparator() |
---|
365 | symbol_menu = wx.Menu() |
---|
366 | for label in self._symbol_labels: |
---|
367 | id = wx.NewId() |
---|
368 | symbol_menu.Append(id, str(label), str(label)) |
---|
369 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
370 | id = wx.NewId() |
---|
371 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
372 | |
---|
373 | color_menu = wx.Menu() |
---|
374 | for label in self._color_labels: |
---|
375 | id = wx.NewId() |
---|
376 | color_menu.Append(id, str(label), str(label)) |
---|
377 | wx.EVT_MENU(self, id, self.onChangeColor) |
---|
378 | id = wx.NewId() |
---|
379 | self._slicerpop.AppendMenu(id, '&Modify Symbol Color', color_menu) |
---|
380 | |
---|
381 | size_menu = wx.Menu() |
---|
382 | for i in range(10): |
---|
383 | id = wx.NewId() |
---|
384 | size_menu.Append(id, str(i), str(i)) |
---|
385 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
386 | id = wx.NewId() |
---|
387 | size_menu.Append(id, '&Custom', 'Custom') |
---|
388 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
389 | id = wx.NewId() |
---|
390 | self._slicerpop.AppendMenu(id, '&Modify Symbol Size', size_menu) |
---|
391 | |
---|
392 | self._slicerpop.AppendSeparator() |
---|
393 | |
---|
394 | self.hide_menu = self._slicerpop.Append(id, "Hide Error") |
---|
395 | |
---|
396 | if plot.dy is not None and plot.dy != []: |
---|
397 | if plot.hide_error : |
---|
398 | self.hide_menu.SetText('Show Error') |
---|
399 | else: |
---|
400 | self.hide_menu.SetText('Hide Error') |
---|
401 | else: |
---|
402 | self.hide_menu.Enable(False) |
---|
403 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
404 | |
---|
405 | self._slicerpop.AppendSeparator() |
---|
406 | id = wx.NewId() |
---|
407 | self._slicerpop.Append(id, '&Edit Legend Label', 'Edit Legend Label') |
---|
408 | wx.EVT_MENU(self, id, self.onEditLabels) |
---|
409 | # Option to hide |
---|
410 | #TODO: implement functionality to hide a plottable (legend click) |
---|
411 | |
---|
412 | loc_menu = wx.Menu() |
---|
413 | for label in self._loc_labels: |
---|
414 | id = wx.NewId() |
---|
415 | loc_menu.Append(id, str(label), str(label)) |
---|
416 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
417 | id = wx.NewId() |
---|
418 | self._slicerpop.AppendMenu(id, '&Modify Legend Location', loc_menu) |
---|
419 | |
---|
420 | id = wx.NewId() |
---|
421 | self._slicerpop.Append(id, '&Toggle Legend On/Off', 'Toggle Legend On/Off') |
---|
422 | wx.EVT_MENU(self, id, self.onLegend) |
---|
423 | self._slicerpop.AppendSeparator() |
---|
424 | |
---|
425 | if self.position != None: |
---|
426 | id = wx.NewId() |
---|
427 | self._slicerpop.Append(id, '&Add text') |
---|
428 | wx.EVT_MENU(self, id, self._on_addtext) |
---|
429 | id = wx.NewId() |
---|
430 | self._slicerpop.Append(id, '&Remove text') |
---|
431 | wx.EVT_MENU(self, id, self._on_removetext) |
---|
432 | id = wx.NewId() |
---|
433 | self._slicerpop.Append(id, '&Change scale') |
---|
434 | wx.EVT_MENU(self, id, self._onProperties) |
---|
435 | id = wx.NewId() |
---|
436 | self._slicerpop.Append(id, '&Reset Graph') |
---|
437 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
438 | pos = event.GetPosition() |
---|
439 | pos = self.ScreenToClient(pos) |
---|
440 | self.PopupMenu(self._slicerpop, pos) |
---|
441 | |
---|
442 | def onFreeze(self, event): |
---|
443 | """ |
---|
444 | """ |
---|
445 | plot = self.plots[self.graph.selected_plottable] |
---|
446 | self.parent.onfreeze([plot.id]) |
---|
447 | |
---|
448 | def onEditLabels(self, event): |
---|
449 | """ |
---|
450 | Edit legend label |
---|
451 | """ |
---|
452 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
453 | label = selected_plot.label |
---|
454 | dial = LabelDialog(None, -1, 'Change Legend Label', label) |
---|
455 | if dial.ShowModal() == wx.ID_OK: |
---|
456 | newLabel = dial.getText() |
---|
457 | selected_plot.label = newLabel |
---|
458 | dial.Destroy() |
---|
459 | ## render the graph |
---|
460 | self._onEVT_FUNC_PROPERTY() |
---|
461 | |
---|
462 | def onChangeColor(self, event): |
---|
463 | """ |
---|
464 | Changes the color of the graph when selected |
---|
465 | """ |
---|
466 | menu = event.GetEventObject() |
---|
467 | id = event.GetId() |
---|
468 | label = menu.GetLabel(id) |
---|
469 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
470 | selected_plot.custom_color = self._color_labels[label] |
---|
471 | ## Set the view scale for all plots |
---|
472 | self._onEVT_FUNC_PROPERTY() |
---|
473 | ## render the graph |
---|
474 | #self.graph.render(self) |
---|
475 | #self.subplot.figure.canvas.draw_idle() |
---|
476 | print "PARENT: ", self.parent |
---|
477 | wx.PostEvent(self.parent, |
---|
478 | NewColorEvent(color=selected_plot.custom_color, |
---|
479 | id=selected_plot.id)) |
---|
480 | |
---|
481 | def onChangeSize(self, event): |
---|
482 | |
---|
483 | menu = event.GetEventObject() |
---|
484 | id = event.GetId() |
---|
485 | label = menu.GetLabel(id) |
---|
486 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
487 | |
---|
488 | if label == "&Custom": |
---|
489 | sizedial = SizeDialog(None, -1, 'Change Marker Size') |
---|
490 | if sizedial.ShowModal() == wx.ID_OK: |
---|
491 | label = sizedial.getText() |
---|
492 | sizedial.Destroy() |
---|
493 | |
---|
494 | selected_plot.marker_size = int(label) |
---|
495 | self._onEVT_FUNC_PROPERTY() |
---|
496 | ## Set the view scale for all plots |
---|
497 | |
---|
498 | ## render the graph |
---|
499 | #self.graph.render(self) |
---|
500 | #self.subplot.figure.canvas.draw_idle() |
---|
501 | |
---|
502 | |
---|
503 | def onChangeSymbol(self, event): |
---|
504 | """ |
---|
505 | """ |
---|
506 | menu = event.GetEventObject() |
---|
507 | id = event.GetId() |
---|
508 | label = menu.GetLabel(id) |
---|
509 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
510 | selected_plot.symbol = self._symbol_labels[label] |
---|
511 | ## Set the view scale for all plots |
---|
512 | self._onEVT_FUNC_PROPERTY() |
---|
513 | ## render the graph |
---|
514 | #self.graph.render(self) |
---|
515 | #self.subplot.figure.canvas.draw_idle() |
---|
516 | |
---|
517 | |
---|
518 | |
---|
519 | def _onsaveTXT(self, path): |
---|
520 | """ |
---|
521 | Save file as txt |
---|
522 | |
---|
523 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
524 | |
---|
525 | """ |
---|
526 | data = self.plots[self.graph.selected_plottable] |
---|
527 | |
---|
528 | if not path == None: |
---|
529 | out = open(path, 'w') |
---|
530 | has_errors = True |
---|
531 | if data.dy == None or data.dy == []: |
---|
532 | has_errors = False |
---|
533 | # Sanity check |
---|
534 | if has_errors: |
---|
535 | try: |
---|
536 | if len(data.y) != len(data.dy): |
---|
537 | has_errors = False |
---|
538 | except: |
---|
539 | has_errors = False |
---|
540 | if has_errors: |
---|
541 | if data.dx != None: |
---|
542 | out.write("<X> <Y> <dY> <dX>\n") |
---|
543 | else: |
---|
544 | out.write("<X> <Y> <dY>\n") |
---|
545 | else: |
---|
546 | out.write("<X> <Y>\n") |
---|
547 | |
---|
548 | for i in range(len(data.x)): |
---|
549 | if has_errors: |
---|
550 | if data.dx != None: |
---|
551 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
552 | data.y[i], |
---|
553 | data.dy[i], |
---|
554 | data.dx[i])) |
---|
555 | else: |
---|
556 | out.write("%g %g %g\n" % (data.x[i], |
---|
557 | data.y[i], |
---|
558 | data.dy[i])) |
---|
559 | else: |
---|
560 | out.write("%g %g\n" % (data.x[i], |
---|
561 | data.y[i])) |
---|
562 | out.close() |
---|
563 | try: |
---|
564 | self._default_save_location = os.path.dirname(path) |
---|
565 | except: |
---|
566 | pass |
---|
567 | |
---|
568 | def _onSave(self, evt): |
---|
569 | """ |
---|
570 | Save a data set to a text file |
---|
571 | |
---|
572 | :param evt: Menu event |
---|
573 | |
---|
574 | """ |
---|
575 | |
---|
576 | path = None |
---|
577 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
578 | "CanSAS 1D files(*.xml)|*.xml" |
---|
579 | dlg = wx.FileDialog(self, "Choose a file", |
---|
580 | self._default_save_location, |
---|
581 | "", wildcard , wx.SAVE) |
---|
582 | |
---|
583 | if dlg.ShowModal() == wx.ID_OK: |
---|
584 | path = dlg.GetPath() |
---|
585 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
586 | # This is MAC Fix |
---|
587 | ext_num = dlg.GetFilterIndex() |
---|
588 | if ext_num == 0: |
---|
589 | format = '.txt' |
---|
590 | else: |
---|
591 | format = '.xml' |
---|
592 | path = os.path.splitext(path)[0] + format |
---|
593 | mypath = os.path.basename(path) |
---|
594 | |
---|
595 | #TODO: This is bad design. The DataLoader is designed |
---|
596 | #to recognize extensions. |
---|
597 | # It should be a simple matter of calling the . |
---|
598 | #save(file, data, '.xml') method |
---|
599 | # of the sans.dataloader.loader.Loader class. |
---|
600 | from sans.dataloader.loader import Loader |
---|
601 | #Instantiate a loader |
---|
602 | loader = Loader() |
---|
603 | data = self.plots[self.graph.selected_plottable] |
---|
604 | format = ".txt" |
---|
605 | if os.path.splitext(mypath)[1].lower() == format: |
---|
606 | # Make sure the ext included in the file name |
---|
607 | # especially on MAC |
---|
608 | fName = os.path.splitext(path)[0] + format |
---|
609 | self._onsaveTXT(fName) |
---|
610 | format = ".xml" |
---|
611 | if os.path.splitext(mypath)[1].lower() == format: |
---|
612 | # Make sure the ext included in the file name |
---|
613 | # especially on MAC |
---|
614 | fName = os.path.splitext(path)[0] + format |
---|
615 | loader.save(fName, data, format) |
---|
616 | try: |
---|
617 | self._default_save_location = os.path.dirname(path) |
---|
618 | except: |
---|
619 | pass |
---|
620 | dlg.Destroy() |
---|
621 | |
---|
622 | def _add_more_tool(self): |
---|
623 | """ |
---|
624 | Add refresh, add/delete button in the tool bar |
---|
625 | """ |
---|
626 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
627 | return |
---|
628 | self.toolbar.AddSeparator() |
---|
629 | id_delete = wx.NewId() |
---|
630 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
631 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
632 | 'Delete', 'permanently Delete') |
---|
633 | |
---|
634 | self.toolbar.Realize() |
---|
635 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
636 | """ |
---|
637 | #New toolbar option - adding text to the plot |
---|
638 | self.toolbar.AddSeparator() |
---|
639 | id_text = wx.NewId() |
---|
640 | text = wx.ArtProvider.GetBitmap(wx.ART_PASTE, wx.ART_TOOLBAR) |
---|
641 | self.toolbar.AddSimpleTool(id_text, text, |
---|
642 | 'Add Text to Plot', 'Adds text to plot') |
---|
643 | |
---|
644 | self.toolbar.Realize() |
---|
645 | wx.EVT_TOOL(self, id_text, self._on_addtext) |
---|
646 | |
---|
647 | self.toolbar.AddSeparator() |
---|
648 | id_text = wx.NewId() |
---|
649 | text = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) |
---|
650 | self.toolbar.AddSimpleTool(id_text, text, |
---|
651 | 'Remove Text from Plot', 'Removes text from plot') |
---|
652 | |
---|
653 | self.toolbar.Realize() |
---|
654 | wx.EVT_TOOL(self, id_text, self._on_removetext) |
---|
655 | """ |
---|
656 | def _on_delete(self, event): |
---|
657 | """ |
---|
658 | Refreshes the plotpanel on refresh tollbar button |
---|
659 | """ |
---|
660 | |
---|
661 | if self.parent is not None: |
---|
662 | wx.PostEvent(self.parent, |
---|
663 | NewPlotEvent(group_id=self.group_id, |
---|
664 | action="delete")) |
---|
665 | |
---|
666 | |
---|