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