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