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 = "Graph" |
---|
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 | if self.is_zoomed: |
---|
162 | self.is_zoomed = False |
---|
163 | |
---|
164 | def _OnReSize(self, event): |
---|
165 | """ |
---|
166 | On response of the resize of a panel, set axes_visiable False |
---|
167 | """ |
---|
168 | # It was found that wx >= 2.9.3 sends an event even if no size changed. |
---|
169 | # So manually recode the size (=x_size) and compare here. |
---|
170 | if self.x_size != None: |
---|
171 | if self.x_size == self.GetSize(): |
---|
172 | self.resizing = False |
---|
173 | self.canvas.set_resizing(self.resizing) |
---|
174 | return |
---|
175 | self.x_size = self.GetSize() |
---|
176 | |
---|
177 | # Ready for another event |
---|
178 | # Do not remove this Skip. Otherwise it will get runtime error on wx>=2.9. |
---|
179 | event.Skip() |
---|
180 | # set the resizing flag |
---|
181 | self.resizing = True |
---|
182 | self.canvas.set_resizing(self.resizing) |
---|
183 | self.parent.set_schedule(True) |
---|
184 | pos_x, pos_y = self.GetPositionTuple() |
---|
185 | if pos_x != 0 and pos_y != 0: |
---|
186 | self.size, _ = self.GetClientSizeTuple() |
---|
187 | |
---|
188 | def set_resizing(self, resizing=False): |
---|
189 | """ |
---|
190 | Set the resizing (True/False) |
---|
191 | """ |
---|
192 | self.resizing = resizing |
---|
193 | #self.canvas.set_resizing(resizing) |
---|
194 | |
---|
195 | def schedule_full_draw(self, func='append'): |
---|
196 | """ |
---|
197 | Put self in schedule to full redraw list |
---|
198 | """ |
---|
199 | # append/del this panel in the schedule list |
---|
200 | self.parent.set_schedule_full_draw(self, func) |
---|
201 | |
---|
202 | |
---|
203 | def remove_data_by_id(self, id): |
---|
204 | """' |
---|
205 | remove data from plot |
---|
206 | """ |
---|
207 | if id in self.plots.keys(): |
---|
208 | data = self.plots[id] |
---|
209 | self.graph.delete(data) |
---|
210 | data_manager = self._manager.parent.get_data_manager() |
---|
211 | data_list, theory_list = data_manager.get_by_id(id_list=[id]) |
---|
212 | |
---|
213 | if id in data_list.keys(): |
---|
214 | data = data_list[id] |
---|
215 | if id in theory_list.keys(): |
---|
216 | data = theory_list[id] |
---|
217 | |
---|
218 | del self.plots[id] |
---|
219 | self.graph.render(self) |
---|
220 | self.subplot.figure.canvas.draw_idle() |
---|
221 | if len(self.graph.plottables) == 0: |
---|
222 | #onRemove: graph is empty must be the panel must be destroyed |
---|
223 | self.parent.delete_panel(self.uid) |
---|
224 | |
---|
225 | def plot_data(self, data): |
---|
226 | """ |
---|
227 | Data is ready to be displayed |
---|
228 | |
---|
229 | :param event: data event |
---|
230 | """ |
---|
231 | if data.id in self.plots.keys(): |
---|
232 | #Recover panel prop.s |
---|
233 | xlo, xhi = self.subplot.get_xlim() |
---|
234 | ylo, yhi = self.subplot.get_ylim() |
---|
235 | old_data = self.plots[data.id] |
---|
236 | data.label = old_data.label |
---|
237 | data.custom_color = old_data.custom_color |
---|
238 | # Replace data |
---|
239 | self.graph.replace(data) |
---|
240 | self.plots[data.id] = data |
---|
241 | ## Set the view scale for all plots |
---|
242 | self._onEVT_FUNC_PROPERTY() |
---|
243 | # Check if zoomed |
---|
244 | toolbar_zoomed = self.toolbar.GetToolEnabled(self.toolbar._NTB2_BACK) |
---|
245 | if self.is_zoomed or toolbar_zoomed: |
---|
246 | # Recover the x,y limits |
---|
247 | self.subplot.set_xlim((xlo, xhi)) |
---|
248 | self.subplot.set_ylim((ylo, yhi)) |
---|
249 | # Update Graph menu and help string |
---|
250 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
251 | helpString = 'Show/Hide Graph: ' |
---|
252 | for plot in self.plots.itervalues(): |
---|
253 | helpString += (' ' + plot.label +';') |
---|
254 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
255 | else: |
---|
256 | self.plots[data.id] = data |
---|
257 | self.graph.add(self.plots[data.id]) |
---|
258 | ## Set the view scale for all plots |
---|
259 | self._onEVT_FUNC_PROPERTY() |
---|
260 | self.toolbar.update() |
---|
261 | if self.is_zoomed: |
---|
262 | self.is_zoomed = False |
---|
263 | |
---|
264 | |
---|
265 | def draw_plot(self): |
---|
266 | """ |
---|
267 | Draw plot |
---|
268 | """ |
---|
269 | self.draw() |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | def onLeftDown(self,event): |
---|
274 | """ |
---|
275 | left button down and ready to drag |
---|
276 | Display the position of the mouse on the statusbar |
---|
277 | """ |
---|
278 | PlotPanel.onLeftDown(self, event) |
---|
279 | # unfocus all |
---|
280 | self.parent.set_plot_unfocus() |
---|
281 | #post nd event to notify guiframe that this panel is on focus |
---|
282 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
283 | |
---|
284 | |
---|
285 | def _ontoggle_hide_error(self, event): |
---|
286 | """ |
---|
287 | Toggle error display to hide or show |
---|
288 | """ |
---|
289 | |
---|
290 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
291 | if self.hide_menu.GetText() == "Hide Error": |
---|
292 | selected_plot.hide_error = True |
---|
293 | else: |
---|
294 | selected_plot.hide_error = False |
---|
295 | ## increment graph color |
---|
296 | self.graph.render(self) |
---|
297 | self.subplot.figure.canvas.draw_idle() |
---|
298 | |
---|
299 | def _onRemove(self, event): |
---|
300 | """ |
---|
301 | Remove a plottable from the graph and render the graph |
---|
302 | |
---|
303 | :param event: Menu event |
---|
304 | |
---|
305 | """ |
---|
306 | ## Check if there is a selected graph to remove |
---|
307 | if self.graph.selected_plottable in self.plots.keys(): |
---|
308 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
309 | id = self.graph.selected_plottable |
---|
310 | self.remove_data_by_id(id) |
---|
311 | |
---|
312 | def onContextMenu(self, event): |
---|
313 | """ |
---|
314 | 1D plot context menu |
---|
315 | |
---|
316 | :param event: wx context event |
---|
317 | |
---|
318 | """ |
---|
319 | self._slicerpop = PanelMenu() |
---|
320 | self._slicerpop.set_plots(self.plots) |
---|
321 | self._slicerpop.set_graph(self.graph) |
---|
322 | # Various plot options |
---|
323 | id = wx.NewId() |
---|
324 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
325 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
326 | id = wx.NewId() |
---|
327 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
328 | wx.EVT_MENU(self, id, self.onPrint) |
---|
329 | id = wx.NewId() |
---|
330 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
331 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
332 | |
---|
333 | id = wx.NewId() |
---|
334 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
335 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
336 | |
---|
337 | self._slicerpop.AppendSeparator() |
---|
338 | |
---|
339 | #add menu of other plugins |
---|
340 | item_list = self.parent.get_context_menu(self) |
---|
341 | |
---|
342 | if (not item_list == None) and (not len(item_list) == 0): |
---|
343 | for item in item_list: |
---|
344 | try: |
---|
345 | id = wx.NewId() |
---|
346 | self._slicerpop.Append(id, item[0], item[1]) |
---|
347 | wx.EVT_MENU(self, id, item[2]) |
---|
348 | except: |
---|
349 | msg = "ModelPanel1D.onContextMenu: " |
---|
350 | msg += "bad menu item %s" % sys.exc_value |
---|
351 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
352 | pass |
---|
353 | self._slicerpop.AppendSeparator() |
---|
354 | #id = wx.NewId() |
---|
355 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
356 | if self.graph.selected_plottable in self.plots: |
---|
357 | plot = self.plots[self.graph.selected_plottable] |
---|
358 | |
---|
359 | id = wx.NewId() |
---|
360 | name = plot.name |
---|
361 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
362 | self._slicerpop.AppendSeparator() |
---|
363 | if plot.name != 'SLD': |
---|
364 | wx.EVT_MENU(self, id, self._onSave) |
---|
365 | id = wx.NewId() |
---|
366 | self._slicerpop.Append(id, '&Linear Fit') |
---|
367 | wx.EVT_MENU(self, id, self.onFitting) |
---|
368 | self._slicerpop.AppendSeparator() |
---|
369 | |
---|
370 | id = wx.NewId() |
---|
371 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
372 | wx.EVT_MENU(self, id, self._onRemove) |
---|
373 | if not plot.is_data: |
---|
374 | id = wx.NewId() |
---|
375 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
376 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
377 | self._slicerpop.AppendSeparator() |
---|
378 | symbol_menu = wx.Menu() |
---|
379 | for label in self._symbol_labels: |
---|
380 | id = wx.NewId() |
---|
381 | symbol_menu.Append(id, str(label), str(label)) |
---|
382 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
383 | id = wx.NewId() |
---|
384 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
385 | |
---|
386 | color_menu = wx.Menu() |
---|
387 | for label in self._color_labels: |
---|
388 | id = wx.NewId() |
---|
389 | color_menu.Append(id, str(label), str(label)) |
---|
390 | wx.EVT_MENU(self, id, self.onChangeColor) |
---|
391 | id = wx.NewId() |
---|
392 | self._slicerpop.AppendMenu(id, '&Modify Symbol Color', color_menu) |
---|
393 | |
---|
394 | size_menu = wx.Menu() |
---|
395 | for i in range(10): |
---|
396 | id = wx.NewId() |
---|
397 | size_menu.Append(id, str(i), str(i)) |
---|
398 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
399 | id = wx.NewId() |
---|
400 | size_menu.Append(id, '&Custom', 'Custom') |
---|
401 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
402 | id = wx.NewId() |
---|
403 | self._slicerpop.AppendMenu(id, '&Modify Symbol Size', size_menu) |
---|
404 | |
---|
405 | self._slicerpop.AppendSeparator() |
---|
406 | |
---|
407 | self.hide_menu = self._slicerpop.Append(id, "Hide Error") |
---|
408 | |
---|
409 | if plot.dy is not None and plot.dy != []: |
---|
410 | if plot.hide_error : |
---|
411 | self.hide_menu.SetText('Show Error') |
---|
412 | else: |
---|
413 | self.hide_menu.SetText('Hide Error') |
---|
414 | else: |
---|
415 | self.hide_menu.Enable(False) |
---|
416 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
417 | |
---|
418 | self._slicerpop.AppendSeparator() |
---|
419 | id = wx.NewId() |
---|
420 | self._slicerpop.Append(id, '&Edit Legend Label', 'Edit Legend Label') |
---|
421 | wx.EVT_MENU(self, id, self.onEditLabels) |
---|
422 | # Option to hide |
---|
423 | #TODO: implement functionality to hide a plottable (legend click) |
---|
424 | |
---|
425 | loc_menu = wx.Menu() |
---|
426 | for label in self._loc_labels: |
---|
427 | id = wx.NewId() |
---|
428 | loc_menu.Append(id, str(label), str(label)) |
---|
429 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
430 | id = wx.NewId() |
---|
431 | self._slicerpop.AppendMenu(id, '&Modify Legend Location', loc_menu) |
---|
432 | |
---|
433 | id = wx.NewId() |
---|
434 | self._slicerpop.Append(id, '&Toggle Legend On/Off', 'Toggle Legend On/Off') |
---|
435 | wx.EVT_MENU(self, id, self.onLegend) |
---|
436 | self._slicerpop.AppendSeparator() |
---|
437 | |
---|
438 | id = wx.NewId() |
---|
439 | self._slicerpop.Append(id, '&Modify Y Axis Label') |
---|
440 | wx.EVT_MENU(self, id, self._on_yaxis_label) |
---|
441 | id = wx.NewId() |
---|
442 | self._slicerpop.Append(id, '&Modify X Axis Label') |
---|
443 | wx.EVT_MENU(self, id, self._on_xaxis_label) |
---|
444 | self._slicerpop.AppendSeparator() |
---|
445 | |
---|
446 | if self.position != None: |
---|
447 | id = wx.NewId() |
---|
448 | self._slicerpop.Append(id, '&Add Text') |
---|
449 | wx.EVT_MENU(self, id, self._on_addtext) |
---|
450 | id = wx.NewId() |
---|
451 | self._slicerpop.Append(id, '&Remove Text') |
---|
452 | wx.EVT_MENU(self, id, self._on_removetext) |
---|
453 | self._slicerpop.AppendSeparator() |
---|
454 | id = wx.NewId() |
---|
455 | self._slicerpop.Append(id, '&Change Scale') |
---|
456 | wx.EVT_MENU(self, id, self._onProperties) |
---|
457 | self._slicerpop.AppendSeparator() |
---|
458 | id = wx.NewId() |
---|
459 | self._slicerpop.Append(id, '&Reset Axis Ranges') |
---|
460 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
461 | try: |
---|
462 | pos_evt = event.GetPosition() |
---|
463 | pos = self.ScreenToClient(pos_evt) |
---|
464 | except: |
---|
465 | pos_x, pos_y = self.toolbar.GetPositionTuple() |
---|
466 | pos = (pos_x, pos_y + 5) |
---|
467 | |
---|
468 | self._slicerpop.AppendSeparator() |
---|
469 | id = wx.NewId() |
---|
470 | self._slicerpop.Append(id, '&Window Title') |
---|
471 | wx.EVT_MENU(self, id, self.onChangeCaption) |
---|
472 | |
---|
473 | self.PopupMenu(self._slicerpop, pos) |
---|
474 | |
---|
475 | def onFreeze(self, event): |
---|
476 | """ |
---|
477 | """ |
---|
478 | plot = self.plots[self.graph.selected_plottable] |
---|
479 | self.parent.onfreeze([plot.id]) |
---|
480 | |
---|
481 | def onEditLabels(self, event): |
---|
482 | """ |
---|
483 | Edit legend label |
---|
484 | """ |
---|
485 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
486 | label = selected_plot.label |
---|
487 | dial = LabelDialog(None, -1, 'Change Legend Label', label) |
---|
488 | if dial.ShowModal() == wx.ID_OK: |
---|
489 | newLabel = dial.getText() |
---|
490 | selected_plot.label = newLabel |
---|
491 | # Updata Graph menu help string |
---|
492 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
493 | helpString = 'Show/Hide Graph: ' |
---|
494 | for plot in self.plots.itervalues(): |
---|
495 | helpString += (' ' + plot.label +';') |
---|
496 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
497 | #break |
---|
498 | dial.Destroy() |
---|
499 | |
---|
500 | ## render the graph |
---|
501 | self._onEVT_FUNC_PROPERTY() |
---|
502 | |
---|
503 | def onChangeColor(self, event): |
---|
504 | """ |
---|
505 | Changes the color of the graph when selected |
---|
506 | """ |
---|
507 | menu = event.GetEventObject() |
---|
508 | id = event.GetId() |
---|
509 | label = menu.GetLabel(id) |
---|
510 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
511 | selected_plot.custom_color = self._color_labels[label] |
---|
512 | ## Set the view scale for all plots |
---|
513 | self._onEVT_FUNC_PROPERTY() |
---|
514 | ## render the graph |
---|
515 | #self.graph.render(self) |
---|
516 | #self.subplot.figure.canvas.draw_idle() |
---|
517 | print "PARENT: ", self.parent |
---|
518 | wx.PostEvent(self.parent, |
---|
519 | NewColorEvent(color=selected_plot.custom_color, |
---|
520 | id=selected_plot.id)) |
---|
521 | |
---|
522 | def onChangeSize(self, event): |
---|
523 | |
---|
524 | menu = event.GetEventObject() |
---|
525 | id = event.GetId() |
---|
526 | label = menu.GetLabel(id) |
---|
527 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
528 | |
---|
529 | if label == "&Custom": |
---|
530 | sizedial = SizeDialog(None, -1, 'Change Marker Size') |
---|
531 | if sizedial.ShowModal() == wx.ID_OK: |
---|
532 | label = sizedial.getText() |
---|
533 | sizedial.Destroy() |
---|
534 | |
---|
535 | selected_plot.marker_size = int(label) |
---|
536 | self._onEVT_FUNC_PROPERTY() |
---|
537 | ## Set the view scale for all plots |
---|
538 | |
---|
539 | ## render the graph |
---|
540 | #self.graph.render(self) |
---|
541 | #self.subplot.figure.canvas.draw_idle() |
---|
542 | |
---|
543 | |
---|
544 | def onChangeSymbol(self, event): |
---|
545 | """ |
---|
546 | """ |
---|
547 | menu = event.GetEventObject() |
---|
548 | id = event.GetId() |
---|
549 | label = menu.GetLabel(id) |
---|
550 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
551 | selected_plot.symbol = self._symbol_labels[label] |
---|
552 | ## Set the view scale for all plots |
---|
553 | self._onEVT_FUNC_PROPERTY() |
---|
554 | ## render the graph |
---|
555 | #self.graph.render(self) |
---|
556 | #self.subplot.figure.canvas.draw_idle() |
---|
557 | |
---|
558 | |
---|
559 | |
---|
560 | def _onsaveTXT(self, path): |
---|
561 | """ |
---|
562 | Save file as txt |
---|
563 | |
---|
564 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
565 | |
---|
566 | """ |
---|
567 | data = self.plots[self.graph.selected_plottable] |
---|
568 | |
---|
569 | if not path == None: |
---|
570 | out = open(path, 'w') |
---|
571 | has_errors = True |
---|
572 | if data.dy == None or data.dy == []: |
---|
573 | has_errors = False |
---|
574 | # Sanity check |
---|
575 | if has_errors: |
---|
576 | try: |
---|
577 | if len(data.y) != len(data.dy): |
---|
578 | has_errors = False |
---|
579 | except: |
---|
580 | has_errors = False |
---|
581 | if has_errors: |
---|
582 | if data.dx != None: |
---|
583 | out.write("<X> <Y> <dY> <dX>\n") |
---|
584 | else: |
---|
585 | out.write("<X> <Y> <dY>\n") |
---|
586 | else: |
---|
587 | out.write("<X> <Y>\n") |
---|
588 | |
---|
589 | for i in range(len(data.x)): |
---|
590 | if has_errors: |
---|
591 | if data.dx != None: |
---|
592 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
593 | data.y[i], |
---|
594 | data.dy[i], |
---|
595 | data.dx[i])) |
---|
596 | else: |
---|
597 | out.write("%g %g %g\n" % (data.x[i], |
---|
598 | data.y[i], |
---|
599 | data.dy[i])) |
---|
600 | else: |
---|
601 | out.write("%g %g\n" % (data.x[i], |
---|
602 | data.y[i])) |
---|
603 | out.close() |
---|
604 | try: |
---|
605 | self._default_save_location = os.path.dirname(path) |
---|
606 | except: |
---|
607 | pass |
---|
608 | |
---|
609 | def _onSave(self, evt): |
---|
610 | """ |
---|
611 | Save a data set to a text file |
---|
612 | |
---|
613 | :param evt: Menu event |
---|
614 | |
---|
615 | """ |
---|
616 | |
---|
617 | path = None |
---|
618 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
619 | "CanSAS 1D files(*.xml)|*.xml" |
---|
620 | dlg = wx.FileDialog(self, "Choose a file", |
---|
621 | self._default_save_location, |
---|
622 | "", wildcard , wx.SAVE) |
---|
623 | |
---|
624 | if dlg.ShowModal() == wx.ID_OK: |
---|
625 | path = dlg.GetPath() |
---|
626 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
627 | # This is MAC Fix |
---|
628 | ext_num = dlg.GetFilterIndex() |
---|
629 | if ext_num == 0: |
---|
630 | format = '.txt' |
---|
631 | else: |
---|
632 | format = '.xml' |
---|
633 | path = os.path.splitext(path)[0] + format |
---|
634 | mypath = os.path.basename(path) |
---|
635 | |
---|
636 | #TODO: This is bad design. The DataLoader is designed |
---|
637 | #to recognize extensions. |
---|
638 | # It should be a simple matter of calling the . |
---|
639 | #save(file, data, '.xml') method |
---|
640 | # of the sans.dataloader.loader.Loader class. |
---|
641 | from sans.dataloader.loader import Loader |
---|
642 | #Instantiate a loader |
---|
643 | loader = Loader() |
---|
644 | data = self.plots[self.graph.selected_plottable] |
---|
645 | format = ".txt" |
---|
646 | if os.path.splitext(mypath)[1].lower() == format: |
---|
647 | # Make sure the ext included in the file name |
---|
648 | # especially on MAC |
---|
649 | fName = os.path.splitext(path)[0] + format |
---|
650 | self._onsaveTXT(fName) |
---|
651 | format = ".xml" |
---|
652 | if os.path.splitext(mypath)[1].lower() == format: |
---|
653 | # Make sure the ext included in the file name |
---|
654 | # especially on MAC |
---|
655 | fName = os.path.splitext(path)[0] + format |
---|
656 | loader.save(fName, data, format) |
---|
657 | try: |
---|
658 | self._default_save_location = os.path.dirname(path) |
---|
659 | except: |
---|
660 | pass |
---|
661 | dlg.Destroy() |
---|
662 | |
---|
663 | def _add_more_tool(self): |
---|
664 | """ |
---|
665 | Add refresh, add/delete button in the tool bar |
---|
666 | """ |
---|
667 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
668 | return |
---|
669 | self.toolbar.AddSeparator() |
---|
670 | id_delete = wx.NewId() |
---|
671 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
672 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
673 | 'Delete', 'permanently Delete') |
---|
674 | |
---|
675 | self.toolbar.Realize() |
---|
676 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
677 | |
---|
678 | """ |
---|
679 | self.toolbar.AddSeparator() |
---|
680 | id_text = wx.NewId() |
---|
681 | text = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) |
---|
682 | self.toolbar.AddSimpleTool(id_text, text, |
---|
683 | 'Remove Text from Plot', 'Removes text from plot') |
---|
684 | |
---|
685 | self.toolbar.Realize() |
---|
686 | wx.EVT_TOOL(self, id_text, self._on_removetext) |
---|
687 | """ |
---|
688 | def _on_delete(self, event): |
---|
689 | """ |
---|
690 | Refreshes the plotpanel on refresh tollbar button |
---|
691 | """ |
---|
692 | |
---|
693 | if self.parent is not None: |
---|
694 | wx.PostEvent(self.parent, |
---|
695 | NewPlotEvent(group_id=self.group_id, |
---|
696 | action="delete")) |
---|
697 | |
---|
698 | |
---|