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 | |
---|
290 | |
---|
291 | def onLeftDown(self,event): |
---|
292 | """ |
---|
293 | left button down and ready to drag |
---|
294 | Display the position of the mouse on the statusbar |
---|
295 | """ |
---|
296 | PlotPanel.onLeftDown(self, event) |
---|
297 | ax = event.inaxes |
---|
298 | if ax != None: |
---|
299 | try: |
---|
300 | pos_x = float(event.xdata)# / size_x |
---|
301 | pos_y = float(event.ydata)# / size_y |
---|
302 | pos_x = "%8.3g"% pos_x |
---|
303 | pos_y = "%8.3g"% pos_y |
---|
304 | self.position = str(pos_x), str(pos_y) |
---|
305 | wx.PostEvent(self.parent, StatusEvent(status=self.position)) |
---|
306 | except: |
---|
307 | self.position = None |
---|
308 | # unfocus all |
---|
309 | self.parent.set_plot_unfocus() |
---|
310 | #post nd event to notify guiframe that this panel is on focus |
---|
311 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
312 | |
---|
313 | |
---|
314 | def _ontoggle_hide_error(self, event): |
---|
315 | """ |
---|
316 | Toggle error display to hide or show |
---|
317 | """ |
---|
318 | # Check zoom |
---|
319 | xlo, xhi = self.subplot.get_xlim() |
---|
320 | ylo, yhi = self.subplot.get_ylim() |
---|
321 | |
---|
322 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
323 | if self.hide_menu.GetText() == "Hide Error Bar": |
---|
324 | selected_plot.hide_error = True |
---|
325 | else: |
---|
326 | selected_plot.hide_error = False |
---|
327 | ## increment graph color |
---|
328 | self.graph.render(self) |
---|
329 | self.subplot.figure.canvas.draw_idle() |
---|
330 | # Check if zoomed |
---|
331 | toolbar_zoomed = self.toolbar.GetToolEnabled(self.toolbar._NTB2_BACK) |
---|
332 | if self.is_zoomed or toolbar_zoomed: |
---|
333 | # Recover the x,y limits |
---|
334 | self.subplot.set_xlim((xlo, xhi)) |
---|
335 | self.subplot.set_ylim((ylo, yhi)) |
---|
336 | |
---|
337 | |
---|
338 | def _onRemove(self, event): |
---|
339 | """ |
---|
340 | Remove a plottable from the graph and render the graph |
---|
341 | |
---|
342 | :param event: Menu event |
---|
343 | |
---|
344 | """ |
---|
345 | ## Check if there is a selected graph to remove |
---|
346 | if self.graph.selected_plottable in self.plots.keys(): |
---|
347 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
348 | id = self.graph.selected_plottable |
---|
349 | self.remove_data_by_id(id) |
---|
350 | |
---|
351 | def onContextMenu(self, event): |
---|
352 | """ |
---|
353 | 1D plot context menu |
---|
354 | |
---|
355 | :param event: wx context event |
---|
356 | |
---|
357 | """ |
---|
358 | self._slicerpop = PanelMenu() |
---|
359 | self._slicerpop.set_plots(self.plots) |
---|
360 | self._slicerpop.set_graph(self.graph) |
---|
361 | # Various plot options |
---|
362 | id = wx.NewId() |
---|
363 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
364 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
365 | id = wx.NewId() |
---|
366 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
367 | wx.EVT_MENU(self, id, self.onPrint) |
---|
368 | id = wx.NewId() |
---|
369 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
370 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
371 | |
---|
372 | id = wx.NewId() |
---|
373 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
374 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
375 | |
---|
376 | self._slicerpop.AppendSeparator() |
---|
377 | |
---|
378 | #add menu of other plugins |
---|
379 | item_list = self.parent.get_context_menu(self) |
---|
380 | |
---|
381 | if (not item_list == None) and (not len(item_list) == 0): |
---|
382 | for item in item_list: |
---|
383 | try: |
---|
384 | id = wx.NewId() |
---|
385 | self._slicerpop.Append(id, item[0], item[1]) |
---|
386 | wx.EVT_MENU(self, id, item[2]) |
---|
387 | except: |
---|
388 | msg = "ModelPanel1D.onContextMenu: " |
---|
389 | msg += "bad menu item %s" % sys.exc_value |
---|
390 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
391 | pass |
---|
392 | self._slicerpop.AppendSeparator() |
---|
393 | #id = wx.NewId() |
---|
394 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
395 | if self.graph.selected_plottable in self.plots: |
---|
396 | plot = self.plots[self.graph.selected_plottable] |
---|
397 | |
---|
398 | id = wx.NewId() |
---|
399 | name = plot.name |
---|
400 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
401 | wx.EVT_MENU(self, id, self._onSave) |
---|
402 | self._slicerpop.AppendSeparator() |
---|
403 | if self.parent.ClassName.count('wxDialog') == 0: |
---|
404 | id = wx.NewId() |
---|
405 | self._slicerpop.Append(id, '&Linear Fit') |
---|
406 | wx.EVT_MENU(self, id, self.onFitting) |
---|
407 | self._slicerpop.AppendSeparator() |
---|
408 | |
---|
409 | id = wx.NewId() |
---|
410 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
411 | wx.EVT_MENU(self, id, self._onRemove) |
---|
412 | if not plot.is_data: |
---|
413 | id = wx.NewId() |
---|
414 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
415 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
416 | self._slicerpop.AppendSeparator() |
---|
417 | symbol_menu = wx.Menu() |
---|
418 | for label in self._symbol_labels: |
---|
419 | id = wx.NewId() |
---|
420 | symbol_menu.Append(id, str(label), str(label)) |
---|
421 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
422 | id = wx.NewId() |
---|
423 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
424 | |
---|
425 | color_menu = wx.Menu() |
---|
426 | for label in self._color_labels: |
---|
427 | id = wx.NewId() |
---|
428 | color_menu.Append(id, str(label), str(label)) |
---|
429 | wx.EVT_MENU(self, id, self.onChangeColor) |
---|
430 | id = wx.NewId() |
---|
431 | self._slicerpop.AppendMenu(id, '&Modify Symbol Color', color_menu) |
---|
432 | |
---|
433 | size_menu = wx.Menu() |
---|
434 | for i in range(10): |
---|
435 | id = wx.NewId() |
---|
436 | size_menu.Append(id, str(i), str(i)) |
---|
437 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
438 | id = wx.NewId() |
---|
439 | size_menu.Append(id, '&Custom', 'Custom') |
---|
440 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
441 | id = wx.NewId() |
---|
442 | self._slicerpop.AppendMenu(id, '&Modify Symbol Size', size_menu) |
---|
443 | |
---|
444 | self.hide_menu = self._slicerpop.Append(id, "Hide Error Bar") |
---|
445 | |
---|
446 | if plot.dy is not None and plot.dy != []: |
---|
447 | if plot.hide_error : |
---|
448 | self.hide_menu.SetText('Show Error Bar') |
---|
449 | else: |
---|
450 | self.hide_menu.SetText('Hide Error Bar') |
---|
451 | else: |
---|
452 | self.hide_menu.Enable(False) |
---|
453 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
454 | |
---|
455 | self._slicerpop.AppendSeparator() |
---|
456 | id = wx.NewId() |
---|
457 | self._slicerpop.Append(id, '&Edit Legend Label', 'Edit Legend Label') |
---|
458 | wx.EVT_MENU(self, id, self.onEditLabels) |
---|
459 | # Option to hide |
---|
460 | #TODO: implement functionality to hide a plottable (legend click) |
---|
461 | |
---|
462 | loc_menu = wx.Menu() |
---|
463 | for label in self._loc_labels: |
---|
464 | id = wx.NewId() |
---|
465 | loc_menu.Append(id, str(label), str(label)) |
---|
466 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
467 | id = wx.NewId() |
---|
468 | self._slicerpop.AppendMenu(id, '&Modify Legend Location', loc_menu) |
---|
469 | |
---|
470 | id = wx.NewId() |
---|
471 | self._slicerpop.Append(id, '&Toggle Legend On/Off', 'Toggle Legend On/Off') |
---|
472 | wx.EVT_MENU(self, id, self.onLegend) |
---|
473 | self._slicerpop.AppendSeparator() |
---|
474 | |
---|
475 | id = wx.NewId() |
---|
476 | self._slicerpop.Append(id, '&Edit Y Axis Label') |
---|
477 | wx.EVT_MENU(self, id, self._on_yaxis_label) |
---|
478 | id = wx.NewId() |
---|
479 | self._slicerpop.Append(id, '&Edit X Axis Label') |
---|
480 | wx.EVT_MENU(self, id, self._on_xaxis_label) |
---|
481 | |
---|
482 | id = wx.NewId() |
---|
483 | self._slicerpop.Append(id, '&Toggle Grid On/Off', 'Toggle Grid On/Off') |
---|
484 | wx.EVT_MENU(self, id, self.onGridOnOff) |
---|
485 | self._slicerpop.AppendSeparator() |
---|
486 | |
---|
487 | if self.position != None: |
---|
488 | id = wx.NewId() |
---|
489 | self._slicerpop.Append(id, '&Add Text') |
---|
490 | wx.EVT_MENU(self, id, self._on_addtext) |
---|
491 | id = wx.NewId() |
---|
492 | self._slicerpop.Append(id, '&Remove Text') |
---|
493 | wx.EVT_MENU(self, id, self._on_removetext) |
---|
494 | self._slicerpop.AppendSeparator() |
---|
495 | id = wx.NewId() |
---|
496 | self._slicerpop.Append(id, '&Change Scale') |
---|
497 | wx.EVT_MENU(self, id, self._onProperties) |
---|
498 | self._slicerpop.AppendSeparator() |
---|
499 | id = wx.NewId() |
---|
500 | self._slicerpop.Append(id, '&Reset Graph Range') |
---|
501 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
502 | try: |
---|
503 | pos_evt = event.GetPosition() |
---|
504 | pos = self.ScreenToClient(pos_evt) |
---|
505 | except: |
---|
506 | pos_x, pos_y = self.toolbar.GetPositionTuple() |
---|
507 | pos = (pos_x, pos_y + 5) |
---|
508 | |
---|
509 | if self.parent.ClassName.count('wxDialog') == 0: |
---|
510 | self._slicerpop.AppendSeparator() |
---|
511 | id = wx.NewId() |
---|
512 | self._slicerpop.Append(id, '&Window Title') |
---|
513 | wx.EVT_MENU(self, id, self.onChangeCaption) |
---|
514 | |
---|
515 | self.PopupMenu(self._slicerpop, pos) |
---|
516 | |
---|
517 | def onFreeze(self, event): |
---|
518 | """ |
---|
519 | """ |
---|
520 | plot = self.plots[self.graph.selected_plottable] |
---|
521 | self.parent.onfreeze([plot.id]) |
---|
522 | |
---|
523 | def onEditLabels(self, event): |
---|
524 | """ |
---|
525 | Edit legend label |
---|
526 | """ |
---|
527 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
528 | label = selected_plot.label |
---|
529 | dial = LabelDialog(None, -1, 'Change Legend Label', label) |
---|
530 | if dial.ShowModal() == wx.ID_OK: |
---|
531 | newLabel = dial.getText() |
---|
532 | selected_plot.label = newLabel |
---|
533 | # Updata Graph menu help string |
---|
534 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
535 | helpString = 'Show/Hide Graph: ' |
---|
536 | for plot in self.plots.itervalues(): |
---|
537 | helpString += (' ' + str(plot.label) +';') |
---|
538 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
539 | self._is_changed_legend_label = True |
---|
540 | #break |
---|
541 | dial.Destroy() |
---|
542 | |
---|
543 | ## render the graph |
---|
544 | try: |
---|
545 | self._onEVT_FUNC_PROPERTY() |
---|
546 | except: |
---|
547 | msg=" Encountered singular points..." |
---|
548 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
549 | "Plotting Erorr: %s"% msg, info="error")) |
---|
550 | |
---|
551 | def onChangeColor(self, event): |
---|
552 | """ |
---|
553 | Changes the color of the graph when selected |
---|
554 | """ |
---|
555 | menu = event.GetEventObject() |
---|
556 | id = event.GetId() |
---|
557 | label = menu.GetLabel(id) |
---|
558 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
559 | selected_plot.custom_color = self._color_labels[label] |
---|
560 | ## Set the view scale for all plots |
---|
561 | self._check_zoom_plot() |
---|
562 | #self._onEVT_FUNC_PROPERTY() |
---|
563 | #wx.PostEvent(self.parent, |
---|
564 | # NewColorEvent(color=selected_plot.custom_color, |
---|
565 | # id=selected_plot.id)) |
---|
566 | |
---|
567 | def onChangeSize(self, event): |
---|
568 | |
---|
569 | menu = event.GetEventObject() |
---|
570 | id = event.GetId() |
---|
571 | label = menu.GetLabel(id) |
---|
572 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
573 | |
---|
574 | if label == "&Custom": |
---|
575 | sizedial = SizeDialog(None, -1, 'Change Marker Size') |
---|
576 | if sizedial.ShowModal() == wx.ID_OK: |
---|
577 | try: |
---|
578 | label = sizedial.getText() |
---|
579 | selected_plot.markersize = int(label) |
---|
580 | self._check_zoom_plot() |
---|
581 | except: |
---|
582 | msg = 'Symbol Size: Got an invalid Value.' |
---|
583 | wx.PostEvent( self.parent, |
---|
584 | StatusEvent(status= msg, info='error')) |
---|
585 | sizedial.Destroy() |
---|
586 | |
---|
587 | |
---|
588 | |
---|
589 | def onChangeSymbol(self, event): |
---|
590 | """ |
---|
591 | """ |
---|
592 | menu = event.GetEventObject() |
---|
593 | id = event.GetId() |
---|
594 | label = menu.GetLabel(id) |
---|
595 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
596 | selected_plot.symbol = self._symbol_labels[label] |
---|
597 | ## Set the view scale for all plots |
---|
598 | self._check_zoom_plot() |
---|
599 | #self._onEVT_FUNC_PROPERTY() |
---|
600 | ## render the graph |
---|
601 | #self.graph.render(self) |
---|
602 | #self.subplot.figure.canvas.draw_idle() |
---|
603 | |
---|
604 | |
---|
605 | |
---|
606 | def _onsaveTXT(self, path): |
---|
607 | """ |
---|
608 | Save file as txt |
---|
609 | |
---|
610 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
611 | |
---|
612 | """ |
---|
613 | data = self.plots[self.graph.selected_plottable] |
---|
614 | |
---|
615 | if not path == None: |
---|
616 | out = open(path, 'w') |
---|
617 | has_errors = True |
---|
618 | if data.dy == None or data.dy == []: |
---|
619 | has_errors = False |
---|
620 | # Sanity check |
---|
621 | if has_errors: |
---|
622 | try: |
---|
623 | if len(data.y) != len(data.dy): |
---|
624 | has_errors = False |
---|
625 | except: |
---|
626 | has_errors = False |
---|
627 | if has_errors: |
---|
628 | if data.dx != None or data.dx != []: |
---|
629 | out.write("<X> <Y> <dY> <dX>\n") |
---|
630 | else: |
---|
631 | out.write("<X> <Y> <dY>\n") |
---|
632 | else: |
---|
633 | out.write("<X> <Y>\n") |
---|
634 | |
---|
635 | for i in range(len(data.x)): |
---|
636 | if has_errors: |
---|
637 | if data.dx != None or data.dx != []: |
---|
638 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
639 | data.y[i], |
---|
640 | data.dy[i], |
---|
641 | data.dx[i])) |
---|
642 | else: |
---|
643 | out.write("%g %g %g\n" % (data.x[i], |
---|
644 | data.y[i], |
---|
645 | data.dy[i])) |
---|
646 | else: |
---|
647 | out.write("%g %g\n" % (data.x[i], |
---|
648 | data.y[i])) |
---|
649 | out.close() |
---|
650 | try: |
---|
651 | self._default_save_location = os.path.dirname(path) |
---|
652 | self.parent._default_save_location = self._default_save_location |
---|
653 | except: |
---|
654 | pass |
---|
655 | |
---|
656 | def _onSave(self, evt): |
---|
657 | """ |
---|
658 | Save a data set to a text file |
---|
659 | |
---|
660 | :param evt: Menu event |
---|
661 | |
---|
662 | """ |
---|
663 | |
---|
664 | path = None |
---|
665 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
666 | "CanSAS 1D files(*.xml)|*.xml" |
---|
667 | default_name = self.plots[self.graph.selected_plottable].label |
---|
668 | if default_name.count('.') > 0: |
---|
669 | default_name = default_name.split('.')[0] |
---|
670 | default_name += "_out" |
---|
671 | if self.parent != None: |
---|
672 | self._default_save_location = self.parent._default_save_location |
---|
673 | dlg = wx.FileDialog(self, "Choose a file", |
---|
674 | self._default_save_location, |
---|
675 | default_name, wildcard , wx.SAVE) |
---|
676 | |
---|
677 | if dlg.ShowModal() == wx.ID_OK: |
---|
678 | path = dlg.GetPath() |
---|
679 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
680 | # This is MAC Fix |
---|
681 | ext_num = dlg.GetFilterIndex() |
---|
682 | if ext_num == 0: |
---|
683 | format = '.txt' |
---|
684 | else: |
---|
685 | format = '.xml' |
---|
686 | path = os.path.splitext(path)[0] + format |
---|
687 | mypath = os.path.basename(path) |
---|
688 | |
---|
689 | #TODO: This is bad design. The DataLoader is designed |
---|
690 | #to recognize extensions. |
---|
691 | # It should be a simple matter of calling the . |
---|
692 | #save(file, data, '.xml') method |
---|
693 | # of the sans.dataloader.loader.Loader class. |
---|
694 | from sans.dataloader.loader import Loader |
---|
695 | #Instantiate a loader |
---|
696 | loader = Loader() |
---|
697 | data = self.plots[self.graph.selected_plottable] |
---|
698 | format = ".txt" |
---|
699 | if os.path.splitext(mypath)[1].lower() == format: |
---|
700 | # Make sure the ext included in the file name |
---|
701 | # especially on MAC |
---|
702 | fName = os.path.splitext(path)[0] + format |
---|
703 | self._onsaveTXT(fName) |
---|
704 | format = ".xml" |
---|
705 | if os.path.splitext(mypath)[1].lower() == format: |
---|
706 | # Make sure the ext included in the file name |
---|
707 | # especially on MAC |
---|
708 | fName = os.path.splitext(path)[0] + format |
---|
709 | loader.save(fName, data, format) |
---|
710 | try: |
---|
711 | self._default_save_location = os.path.dirname(path) |
---|
712 | self.parent._default_save_location = self._default_save_location |
---|
713 | except: |
---|
714 | pass |
---|
715 | dlg.Destroy() |
---|
716 | |
---|
717 | def _add_more_tool(self): |
---|
718 | """ |
---|
719 | Add refresh, add/delete button in the tool bar |
---|
720 | """ |
---|
721 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
722 | return |
---|
723 | self.toolbar.AddSeparator() |
---|
724 | id_delete = wx.NewId() |
---|
725 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
726 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
727 | 'Delete', 'permanently Delete') |
---|
728 | |
---|
729 | self.toolbar.Realize() |
---|
730 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
731 | |
---|
732 | """ |
---|
733 | self.toolbar.AddSeparator() |
---|
734 | id_text = wx.NewId() |
---|
735 | text = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) |
---|
736 | self.toolbar.AddSimpleTool(id_text, text, |
---|
737 | 'Remove Text from Plot', 'Removes text from plot') |
---|
738 | |
---|
739 | self.toolbar.Realize() |
---|
740 | wx.EVT_TOOL(self, id_text, self._on_removetext) |
---|
741 | """ |
---|
742 | def _on_delete(self, event): |
---|
743 | """ |
---|
744 | Refreshes the plotpanel on refresh tollbar button |
---|
745 | """ |
---|
746 | |
---|
747 | if self.parent is not None: |
---|
748 | wx.PostEvent(self.parent, |
---|
749 | NewPlotEvent(group_id=self.group_id, |
---|
750 | action="delete")) |
---|
751 | |
---|
752 | |
---|