[1bf33c1] | 1 | """ |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | See the license text in license.txt |
---|
| 7 | |
---|
| 8 | copyright 2008, University of Tennessee |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | import wx |
---|
[4ac8556] | 13 | import sys |
---|
| 14 | import os |
---|
| 15 | import pylab |
---|
| 16 | import math |
---|
| 17 | import numpy |
---|
| 18 | import time |
---|
[0d9dae8] | 19 | |
---|
[1bf33c1] | 20 | import danse.common.plottools |
---|
| 21 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
[3b69ca6] | 22 | from danse.common.plottools.plottables import Graph,Theory1D |
---|
| 23 | from sans.guiframe import dataFitting |
---|
[1bf33c1] | 24 | from sans.guicomm.events import EVT_NEW_PLOT |
---|
[18eba35] | 25 | from sans.guicomm.events import StatusEvent ,NewPlotEvent,SlicerEvent,ErrorDataEvent |
---|
[1debb29] | 26 | from sans.guicomm.events import RemoveDataEvent |
---|
[0d9dae8] | 27 | from sans.guiframe.utils import PanelMenu |
---|
[4ac8556] | 28 | from sans.guiframe.dataFitting import Data1D |
---|
[1bf33c1] | 29 | |
---|
| 30 | from binder import BindArtist |
---|
| 31 | |
---|
[0d9dae8] | 32 | |
---|
| 33 | DEFAULT_QMAX = 0.05 |
---|
[1bf33c1] | 34 | DEFAULT_QSTEP = 0.001 |
---|
| 35 | DEFAULT_BEAM = 0.005 |
---|
| 36 | BIN_WIDTH =1 |
---|
| 37 | |
---|
[0d9dae8] | 38 | |
---|
[1bf33c1] | 39 | class ModelPanel1D(PlotPanel): |
---|
| 40 | """ |
---|
| 41 | Plot panel for use with the GUI manager |
---|
| 42 | """ |
---|
| 43 | |
---|
| 44 | ## Internal name for the AUI manager |
---|
| 45 | window_name = "plotpanel" |
---|
| 46 | ## Title to appear on top of the window |
---|
| 47 | window_caption = "Plot Panel" |
---|
| 48 | ## Flag to tell the GUI manager that this panel is not |
---|
| 49 | # tied to any perspective |
---|
| 50 | ALWAYS_ON = True |
---|
| 51 | ## Group ID |
---|
| 52 | group_id = None |
---|
| 53 | |
---|
| 54 | def __init__(self, parent, id = -1, color = None,\ |
---|
| 55 | dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
| 56 | """ |
---|
| 57 | Initialize the panel |
---|
| 58 | """ |
---|
| 59 | PlotPanel.__init__(self, parent, id = id, style = style, **kwargs) |
---|
| 60 | |
---|
| 61 | ## Reference to the parent window |
---|
| 62 | self.parent = parent |
---|
| 63 | ## Plottables |
---|
| 64 | self.plots = {} |
---|
[869c368] | 65 | ## save errors dy for each data plotted |
---|
| 66 | self.err_dy={} |
---|
[6c0568b] | 67 | ## flag to determine if the hide or show context menu item should |
---|
| 68 | ## be displayed |
---|
[31482fc] | 69 | self.errors_hide=False |
---|
[1bf33c1] | 70 | ## Unique ID (from gui_manager) |
---|
| 71 | self.uid = None |
---|
| 72 | ## Action IDs for internal call-backs |
---|
| 73 | self.action_ids = {} |
---|
[6063b16] | 74 | ## Default locations |
---|
| 75 | self._default_save_location = os.getcwd() |
---|
[1bf33c1] | 76 | ## Graph |
---|
| 77 | self.graph = Graph() |
---|
| 78 | self.graph.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 79 | self.graph.yaxis("\\rm{Intensity} ","cm^{-1}") |
---|
| 80 | self.graph.render(self) |
---|
| 81 | |
---|
[6c0568b] | 82 | |
---|
[1bf33c1] | 83 | def _reset(self): |
---|
| 84 | """ |
---|
| 85 | Resets internal data and graph |
---|
| 86 | """ |
---|
| 87 | self.graph.reset() |
---|
| 88 | self.plots = {} |
---|
| 89 | self.action_ids = {} |
---|
| 90 | |
---|
[6c0568b] | 91 | |
---|
[1bf33c1] | 92 | def _onEVT_1DREPLOT(self, event): |
---|
| 93 | """ |
---|
| 94 | Data is ready to be displayed |
---|
| 95 | @param event: data event |
---|
| 96 | """ |
---|
[ffd23b5] | 97 | |
---|
[1bf33c1] | 98 | #TODO: Check for existence of plot attribute |
---|
| 99 | # Check whether this is a replot. If we ask for a replot |
---|
| 100 | # and the plottable no longer exists, ignore the event. |
---|
| 101 | if hasattr(event, "update") and event.update==True \ |
---|
| 102 | and event.plot.name not in self.plots.keys(): |
---|
| 103 | return |
---|
| 104 | |
---|
| 105 | if hasattr(event, "reset"): |
---|
| 106 | self._reset() |
---|
| 107 | |
---|
| 108 | is_new = True |
---|
| 109 | if event.plot.name in self.plots.keys(): |
---|
| 110 | # Check whether the class of plottable changed |
---|
| 111 | if not event.plot.__class__==self.plots[event.plot.name].__class__: |
---|
[ab8f936] | 112 | #overwrite a plottable using the same name |
---|
[1bf33c1] | 113 | self.graph.delete(self.plots[event.plot.name]) |
---|
| 114 | else: |
---|
[ab8f936] | 115 | # plottable is already draw on the panel |
---|
[1bf33c1] | 116 | is_new = False |
---|
[ffd23b5] | 117 | |
---|
[1bf33c1] | 118 | if is_new: |
---|
[ab8f936] | 119 | # a new plottable overwrites a plotted one using the same id |
---|
| 120 | for plottable in self.plots.itervalues(): |
---|
[a7bd562] | 121 | if hasattr(event.plot,"id") and hasattr(plottable, "id"): |
---|
[e48a62e] | 122 | if event.plot.id==plottable.id : |
---|
| 123 | self.graph.delete(plottable) |
---|
[ab8f936] | 124 | |
---|
[1bf33c1] | 125 | self.plots[event.plot.name] = event.plot |
---|
| 126 | self.graph.add(self.plots[event.plot.name]) |
---|
| 127 | else: |
---|
[ab8f936] | 128 | #replot the graph |
---|
[1bf33c1] | 129 | self.plots[event.plot.name].x = event.plot.x |
---|
| 130 | self.plots[event.plot.name].y = event.plot.y |
---|
| 131 | self.plots[event.plot.name].dy = event.plot.dy |
---|
| 132 | if hasattr(event.plot, 'dx') and hasattr(self.plots[event.plot.name], 'dx'): |
---|
| 133 | self.plots[event.plot.name].dx = event.plot.dx |
---|
[31482fc] | 134 | |
---|
[1bf33c1] | 135 | #TODO: Should re-factor this |
---|
[6c0568b] | 136 | ## for all added plot the option to hide error show be displayed first |
---|
[31482fc] | 137 | #self.errors_hide = 0 |
---|
[6c0568b] | 138 | ## Set axis labels |
---|
[1bf33c1] | 139 | self.graph.xaxis(event.plot._xaxis, event.plot._xunit) |
---|
| 140 | self.graph.yaxis(event.plot._yaxis, event.plot._yunit) |
---|
[6c0568b] | 141 | ## Set the view scale for all plots |
---|
[1bf33c1] | 142 | self._onEVT_FUNC_PROPERTY() |
---|
[6c0568b] | 143 | ## render the graph |
---|
[1bf33c1] | 144 | self.graph.render(self) |
---|
| 145 | self.subplot.figure.canvas.draw_idle() |
---|
[31482fc] | 146 | if self.errors_hide: |
---|
| 147 | self._on_remove_errors(evt=None) |
---|
| 148 | else: |
---|
| 149 | self._on_add_errors( evt=None) |
---|
| 150 | return |
---|
| 151 | |
---|
[1bf33c1] | 152 | def onLeftDown(self,event): |
---|
[6c0568b] | 153 | """ |
---|
| 154 | left button down and ready to drag |
---|
| 155 | Display the position of the mouse on the statusbar |
---|
| 156 | """ |
---|
[1bf33c1] | 157 | PlotPanel.onLeftDown(self, event) |
---|
| 158 | ax = event.inaxes |
---|
| 159 | if ax != None: |
---|
| 160 | position = "x: %8.3g y: %8.3g" % (event.xdata, event.ydata) |
---|
| 161 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
| 162 | |
---|
[6c0568b] | 163 | |
---|
[1bf33c1] | 164 | def _onRemove(self, event): |
---|
| 165 | """ |
---|
[6c0568b] | 166 | Remove a plottable from the graph and render the graph |
---|
| 167 | @param event: Menu event |
---|
[1bf33c1] | 168 | """ |
---|
[6c0568b] | 169 | ## Check if there is a selected graph to remove |
---|
[1debb29] | 170 | if not self.graph.selected_plottable == None and\ |
---|
| 171 | self.graph.selected_plottable in self.plots.keys(): |
---|
| 172 | color=self.graph.plottables[self.plots[self.graph.selected_plottable]] |
---|
| 173 | |
---|
| 174 | event = RemoveDataEvent(data =self.plots[self.graph.selected_plottable]) |
---|
| 175 | wx.PostEvent(self.parent, event) |
---|
[1bf33c1] | 176 | self.graph.delete(self.plots[self.graph.selected_plottable]) |
---|
| 177 | del self.plots[self.graph.selected_plottable] |
---|
[1debb29] | 178 | ## increment graph color |
---|
| 179 | self.graph.color += color |
---|
[1bf33c1] | 180 | self.graph.render(self) |
---|
| 181 | self.subplot.figure.canvas.draw_idle() |
---|
[1debb29] | 182 | |
---|
[1bf33c1] | 183 | |
---|
| 184 | |
---|
| 185 | def onContextMenu(self, event): |
---|
| 186 | """ |
---|
| 187 | 1D plot context menu |
---|
| 188 | @param event: wx context event |
---|
| 189 | """ |
---|
| 190 | slicerpop = PanelMenu() |
---|
| 191 | slicerpop.set_plots(self.plots) |
---|
| 192 | slicerpop.set_graph(self.graph) |
---|
| 193 | |
---|
[9a585d0] | 194 | # Various plot options |
---|
| 195 | id = wx.NewId() |
---|
| 196 | slicerpop.Append(id,'&Save image', 'Save image as PNG') |
---|
| 197 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
| 198 | |
---|
| 199 | id = wx.NewId() |
---|
| 200 | slicerpop.Append(id,'&Print image', 'Print image ') |
---|
[18eba35] | 201 | wx.EVT_MENU(self, id, self.onPrint) |
---|
| 202 | |
---|
| 203 | id = wx.NewId() |
---|
| 204 | slicerpop.Append(id,'&Print Preview', 'image preview for print') |
---|
| 205 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
[9a585d0] | 206 | |
---|
| 207 | slicerpop.AppendSeparator() |
---|
| 208 | item_list = self.parent.get_context_menu(self.graph) |
---|
[6c0568b] | 209 | |
---|
[9a585d0] | 210 | if (not item_list==None) and (not len(item_list)==0): |
---|
| 211 | for item in item_list: |
---|
| 212 | try: |
---|
| 213 | id = wx.NewId() |
---|
| 214 | slicerpop.Append(id, item[0], item[1]) |
---|
| 215 | wx.EVT_MENU(self, id, item[2]) |
---|
| 216 | except: |
---|
[6c0568b] | 217 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 218 | "ModelPanel1D.onContextMenu: bad menu item %s"%sys.exc_value)) |
---|
[9a585d0] | 219 | pass |
---|
| 220 | slicerpop.AppendSeparator() |
---|
[6c0568b] | 221 | |
---|
[1bf33c1] | 222 | if self.graph.selected_plottable in self.plots: |
---|
| 223 | plot = self.plots[self.graph.selected_plottable] |
---|
| 224 | id = wx.NewId() |
---|
| 225 | name = plot.name |
---|
[6c0568b] | 226 | |
---|
[42d27f2] | 227 | slicerpop.Append(id, "&Save points" ) |
---|
[1bf33c1] | 228 | self.action_ids[str(id)] = plot |
---|
| 229 | wx.EVT_MENU(self, id, self._onSave) |
---|
[31482fc] | 230 | |
---|
[1bf33c1] | 231 | id = wx.NewId() |
---|
| 232 | slicerpop.Append(id, "Remove %s curve" % name) |
---|
| 233 | self.action_ids[str(id)] = plot |
---|
| 234 | wx.EVT_MENU(self, id, self._onRemove) |
---|
[9a585d0] | 235 | slicerpop.AppendSeparator() |
---|
[1bf33c1] | 236 | # Option to hide |
---|
| 237 | #TODO: implement functionality to hide a plottable (legend click) |
---|
[d468daa] | 238 | |
---|
[1bf33c1] | 239 | if self.graph.selected_plottable in self.plots: |
---|
[0b16ee3] | 240 | selected_plot= self.plots[self.graph.selected_plottable] |
---|
| 241 | #if self.plots[self.graph.selected_plottable].name in self.err_dy.iterkeys()\ |
---|
| 242 | # and self.errors_hide: |
---|
| 243 | if selected_plot.__class__.__name__=="Data1D": |
---|
| 244 | if selected_plot.dy ==None or selected_plot.dy== []: |
---|
| 245 | id = wx.NewId() |
---|
| 246 | slicerpop.Append(id, '&Show errors to data') |
---|
| 247 | wx.EVT_MENU(self, id, self._on_add_errors) |
---|
| 248 | elif numpy.all(selected_plot.dy==0): |
---|
| 249 | id = wx.NewId() |
---|
| 250 | slicerpop.Append(id, '&Show errors to data') |
---|
| 251 | wx.EVT_MENU(self, id, self._on_add_errors) |
---|
| 252 | else: |
---|
[dd66fbd] | 253 | id = wx.NewId() |
---|
[18eba35] | 254 | slicerpop.Append(id, '&Hide Error bars') |
---|
[dd66fbd] | 255 | wx.EVT_MENU(self, id, self._on_remove_errors) |
---|
[3cc533e] | 256 | |
---|
| 257 | id = wx.NewId() |
---|
| 258 | slicerpop.Append(id, '&Linear fit') |
---|
| 259 | wx.EVT_MENU(self, id, self.onFitting) |
---|
[1bf33c1] | 260 | |
---|
[9a585d0] | 261 | slicerpop.AppendSeparator() |
---|
[6c0568b] | 262 | |
---|
[1bf33c1] | 263 | id = wx.NewId() |
---|
| 264 | slicerpop.Append(id, '&Change scale') |
---|
| 265 | wx.EVT_MENU(self, id, self._onProperties) |
---|
[6c0568b] | 266 | |
---|
[1bf33c1] | 267 | id = wx.NewId() |
---|
| 268 | slicerpop.Append(id, '&Reset Graph') |
---|
[d468daa] | 269 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
[6d920cd] | 270 | |
---|
[1bf33c1] | 271 | pos = event.GetPosition() |
---|
| 272 | pos = self.ScreenToClient(pos) |
---|
| 273 | self.PopupMenu(slicerpop, pos) |
---|
[18eba35] | 274 | |
---|
| 275 | |
---|
[869c368] | 276 | def _on_remove_errors(self, evt): |
---|
[6c0568b] | 277 | """ |
---|
| 278 | Save name and dy of data in dictionary self.err_dy |
---|
| 279 | Create a new data1D with the same x, y |
---|
| 280 | vector and dy with zeros. |
---|
| 281 | post self.err_dy as event (ErrorDataEvent) for any object |
---|
| 282 | which wants to reconstruct the initial data. |
---|
| 283 | @param evt: Menu event |
---|
| 284 | """ |
---|
[869c368] | 285 | if not self.graph.selected_plottable == None: |
---|
[6c0568b] | 286 | ## store existing dy |
---|
[869c368] | 287 | name =self.plots[self.graph.selected_plottable].name |
---|
| 288 | dy = self.plots[self.graph.selected_plottable].dy |
---|
| 289 | self.err_dy[name]= dy |
---|
[6c0568b] | 290 | ## Create a new dy for a new plottable |
---|
[18eba35] | 291 | import numpy |
---|
| 292 | dy= numpy.zeros(len(self.plots[self.graph.selected_plottable].y)) |
---|
[0b16ee3] | 293 | selected_plot= self.plots[self.graph.selected_plottable] |
---|
| 294 | |
---|
| 295 | if selected_plot.__class__.__name__=="Data1D": |
---|
[8068b52] | 296 | # Make sure that we can pass a basic Data1D |
---|
| 297 | dxl = None |
---|
| 298 | dxw = None |
---|
| 299 | if hasattr(selected_plot, "dxl"): |
---|
| 300 | dxl = selected_plot.dxl |
---|
| 301 | if hasattr(selected_plot, "dxw"): |
---|
| 302 | dxw = selected_plot.dxw |
---|
[4ac8556] | 303 | new_plot = Data1D( x=selected_plot.x, |
---|
[3b69ca6] | 304 | y= selected_plot.y, |
---|
| 305 | dx=selected_plot.dx, |
---|
[4ac8556] | 306 | dy=dy) |
---|
| 307 | new_plot.dxl = dxl |
---|
| 308 | new_plot.dxw = dxw |
---|
| 309 | |
---|
[0b16ee3] | 310 | else: |
---|
[3b69ca6] | 311 | new_plot = Theory1D(x=selected_plot.x,y=selected_plot.y,dy=dy) |
---|
[869c368] | 312 | new_plot.interactive = True |
---|
[31482fc] | 313 | self.errors_hide = True |
---|
[869c368] | 314 | new_plot.name = self.plots[self.graph.selected_plottable].name |
---|
| 315 | if hasattr(self.plots[self.graph.selected_plottable], "group_id"): |
---|
| 316 | new_plot.group_id = self.plots[self.graph.selected_plottable].group_id |
---|
| 317 | new_plot.id = self.plots[self.graph.selected_plottable].id |
---|
| 318 | else: |
---|
| 319 | new_plot.group_id = str(time.time()) |
---|
| 320 | new_plot.id = str(time.time()) |
---|
| 321 | label, unit = self.plots[self.graph.selected_plottable].get_xaxis() |
---|
| 322 | new_plot.xaxis(label, unit) |
---|
| 323 | label, unit = self.plots[self.graph.selected_plottable].get_yaxis() |
---|
| 324 | new_plot.yaxis(label, unit) |
---|
[6c0568b] | 325 | ## save the color of the selected plottable before it is deleted |
---|
[869c368] | 326 | color=self.graph.plottables[self.plots[self.graph.selected_plottable]] |
---|
[18eba35] | 327 | self.graph.delete(self.plots[self.graph.selected_plottable]) |
---|
[6c0568b] | 328 | ## add newly created plottable to the graph with the save color |
---|
[0b16ee3] | 329 | self.graph.color += color |
---|
[dd66fbd] | 330 | self.graph.add(new_plot,color) |
---|
[6c0568b] | 331 | ## transforming the view of the new data into the same of the previous data |
---|
[869c368] | 332 | self._onEVT_FUNC_PROPERTY() |
---|
[6c0568b] | 333 | ## save the plot |
---|
[869c368] | 334 | self.plots[self.graph.selected_plottable]=new_plot |
---|
[6c0568b] | 335 | ## Render the graph |
---|
[869c368] | 336 | self.graph.render(self) |
---|
| 337 | self.subplot.figure.canvas.draw_idle() |
---|
[18eba35] | 338 | |
---|
| 339 | event = ErrorDataEvent(err_dy=self.err_dy) |
---|
| 340 | wx.PostEvent(self.parent, event) |
---|
[1bf33c1] | 341 | |
---|
[6c0568b] | 342 | |
---|
[1bf33c1] | 343 | def _on_add_errors(self, evt): |
---|
| 344 | """ |
---|
[6c0568b] | 345 | create a new data1D witht the errors saved in self.err_dy |
---|
| 346 | to show errors of the plot. |
---|
[1bf33c1] | 347 | Compute reasonable errors for a data set without |
---|
| 348 | errors and transorm the plottable to a Data1D |
---|
[6c0568b] | 349 | @param evt: Menu event |
---|
[1bf33c1] | 350 | """ |
---|
[4ac8556] | 351 | |
---|
[1bf33c1] | 352 | |
---|
[1debb29] | 353 | if not self.graph.selected_plottable == None \ |
---|
| 354 | and self.graph.selected_plottable in self.plots.keys(): |
---|
[6c0568b] | 355 | ##Reset the flag to display the hide option on the context menu |
---|
[31482fc] | 356 | self.errors_hide = False |
---|
[6c0568b] | 357 | ## restore dy |
---|
[1bf33c1] | 358 | length = len(self.plots[self.graph.selected_plottable].x) |
---|
| 359 | dy = numpy.zeros(length) |
---|
[3b69ca6] | 360 | |
---|
[869c368] | 361 | selected_plot= self.plots[self.graph.selected_plottable] |
---|
[0b16ee3] | 362 | |
---|
[869c368] | 363 | try: |
---|
| 364 | dy = self.err_dy[selected_plot.name] |
---|
[0b16ee3] | 365 | |
---|
[869c368] | 366 | except: |
---|
[bb5c1c7] | 367 | #for i in range(length): |
---|
| 368 | #dy[i] = math.sqrt(self.plots[self.graph.selected_plottable].y[i]) |
---|
[0b16ee3] | 369 | if hasattr(selected_plot,"dy"): |
---|
| 370 | dy= selected_plot.dy |
---|
| 371 | else: |
---|
| 372 | dy = numpy.zeros(selected_plot.dy) |
---|
| 373 | |
---|
[6c0568b] | 374 | ## Create a new plottable data1D |
---|
[0b16ee3] | 375 | if selected_plot.__class__.__name__=="Data1D": |
---|
[8068b52] | 376 | # Make sure that we can pass a basic Data1D |
---|
| 377 | dxl = None |
---|
| 378 | dxw = None |
---|
| 379 | if hasattr(selected_plot, "dxl"): |
---|
| 380 | dxl = selected_plot.dxl |
---|
| 381 | if hasattr(selected_plot, "dxw"): |
---|
| 382 | dxw = selected_plot.dxw |
---|
[4ac8556] | 383 | new_plot = Data1D( x=selected_plot.x, |
---|
[3b69ca6] | 384 | y= selected_plot.y, |
---|
| 385 | dx=selected_plot.dx, |
---|
[4ac8556] | 386 | dy=dy) |
---|
| 387 | new_plot.dxl = dxl |
---|
| 388 | new_plot.dxw = dxw |
---|
| 389 | |
---|
[0b16ee3] | 390 | else: |
---|
| 391 | ## Create a new plottable Theory1D |
---|
[3b69ca6] | 392 | new_plot = Theory1D(x=selected_plot.x,y=selected_plot.y,dy=dy) |
---|
| 393 | |
---|
[1bf33c1] | 394 | new_plot.interactive = True |
---|
| 395 | new_plot.name = self.plots[self.graph.selected_plottable].name |
---|
| 396 | if hasattr(self.plots[self.graph.selected_plottable], "group_id"): |
---|
| 397 | new_plot.group_id = self.plots[self.graph.selected_plottable].group_id |
---|
[d1dd9d4] | 398 | new_plot.id = self.plots[self.graph.selected_plottable].id |
---|
[1bf33c1] | 399 | else: |
---|
| 400 | new_plot.group_id = str(time.time()) |
---|
[d1dd9d4] | 401 | new_plot.id = str(time.time()) |
---|
[1bf33c1] | 402 | |
---|
| 403 | label, unit = self.plots[self.graph.selected_plottable].get_xaxis() |
---|
| 404 | new_plot.xaxis(label, unit) |
---|
| 405 | label, unit = self.plots[self.graph.selected_plottable].get_yaxis() |
---|
| 406 | new_plot.yaxis(label, unit) |
---|
[6c0568b] | 407 | ## save the color of the selected plottable before it is deleted |
---|
[18eba35] | 408 | color=self.graph.plottables[self.plots[self.graph.selected_plottable]] |
---|
| 409 | self.graph.delete(self.plots[self.graph.selected_plottable]) |
---|
[0b16ee3] | 410 | self.graph.color += color |
---|
[6c0568b] | 411 | ## add newly created plottable to the graph with the save color |
---|
[18eba35] | 412 | self.graph.add(new_plot, color) |
---|
[6c0568b] | 413 | ## transforming the view of the new data into the same of the previous data |
---|
[ffd23b5] | 414 | self._onEVT_FUNC_PROPERTY() |
---|
[6c0568b] | 415 | ## save the plot |
---|
[1bf33c1] | 416 | self.plots[self.graph.selected_plottable]=new_plot |
---|
[6c0568b] | 417 | ## render the graph with its new content |
---|
[1bf33c1] | 418 | self.graph.render(self) |
---|
[8bd764d] | 419 | self.subplot.figure.canvas.draw_idle() |
---|
| 420 | |
---|
[6c0568b] | 421 | |
---|
[42d27f2] | 422 | def _onsaveTXT(self, path): |
---|
| 423 | """ |
---|
| 424 | Save file as txt |
---|
[1abcb04] | 425 | |
---|
| 426 | TODO: Refactor and remove this method. See TODO in _onSave. |
---|
[42d27f2] | 427 | """ |
---|
| 428 | data = self.plots[self.graph.selected_plottable] |
---|
| 429 | |
---|
| 430 | if not path == None: |
---|
| 431 | out = open(path, 'w') |
---|
| 432 | has_errors = True |
---|
| 433 | if data.dy==None or data.dy==[]: |
---|
| 434 | has_errors = False |
---|
| 435 | |
---|
| 436 | # Sanity check |
---|
| 437 | if has_errors: |
---|
| 438 | try: |
---|
| 439 | if len(data.y) != len(data.dy): |
---|
| 440 | |
---|
| 441 | has_errors = False |
---|
| 442 | except: |
---|
| 443 | has_errors = False |
---|
[8bd764d] | 444 | |
---|
[42d27f2] | 445 | if has_errors: |
---|
| 446 | out.write("<X> <Y> <dY>\n") |
---|
| 447 | else: |
---|
| 448 | out.write("<X> <Y>\n") |
---|
| 449 | |
---|
| 450 | for i in range(len(data.x)): |
---|
| 451 | if has_errors: |
---|
| 452 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 453 | data.y[i], |
---|
| 454 | data.dy[i])) |
---|
| 455 | else: |
---|
| 456 | out.write("%g %g\n" % (data.x[i], |
---|
| 457 | data.y[i])) |
---|
| 458 | |
---|
| 459 | out.close() |
---|
[6063b16] | 460 | try: |
---|
| 461 | self._default_save_location = os.path.dirname(path) |
---|
| 462 | except: |
---|
| 463 | pass |
---|
[8bd764d] | 464 | |
---|
[1bf33c1] | 465 | def _onSave(self, evt): |
---|
| 466 | """ |
---|
| 467 | Save a data set to a text file |
---|
| 468 | @param evt: Menu event |
---|
| 469 | """ |
---|
[f0a9a3cc] | 470 | |
---|
[1bf33c1] | 471 | id = str(evt.GetId()) |
---|
| 472 | if id in self.action_ids: |
---|
| 473 | |
---|
| 474 | path = None |
---|
[5fe5871c] | 475 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
[7959f297] | 476 | "CanSAS 1D files(*.xml)|*.xml" |
---|
[6063b16] | 477 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 478 | self._default_save_location, "",wildcard , wx.SAVE) |
---|
[42d27f2] | 479 | |
---|
[1bf33c1] | 480 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 481 | path = dlg.GetPath() |
---|
| 482 | mypath = os.path.basename(path) |
---|
[1abcb04] | 483 | |
---|
| 484 | #TODO: This is bad design. The DataLoader is designed to recognize extensions. |
---|
| 485 | # It should be a simple matter of calling the .save(file, data, '.xml') method |
---|
[f0a9a3cc] | 486 | # of the DataLoader.loader.Loader class. |
---|
| 487 | from DataLoader.loader import Loader |
---|
| 488 | #Instantiate a loader |
---|
| 489 | loader = Loader() |
---|
| 490 | data = self.plots[self.graph.selected_plottable] |
---|
| 491 | format=".txt" |
---|
| 492 | if os.path.splitext(mypath)[1].lower() == format: |
---|
| 493 | self._onsaveTXT( path) |
---|
| 494 | |
---|
| 495 | format= ".xml" |
---|
| 496 | if os.path.splitext(mypath)[1].lower() ==format: |
---|
| 497 | loader.save( path, data, format) |
---|
| 498 | try: |
---|
| 499 | self._default_save_location = os.path.dirname(path) |
---|
| 500 | except: |
---|
| 501 | pass |
---|
[1bf33c1] | 502 | dlg.Destroy() |
---|
[5fe5871c] | 503 | |
---|
[f0a9a3cc] | 504 | |
---|
| 505 | |
---|
| 506 | |
---|
[5fe5871c] | 507 | |
---|
[6c0568b] | 508 | |
---|
[1bf33c1] | 509 | |
---|
[6c0568b] | 510 | |
---|
[1bf33c1] | 511 | |
---|