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