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