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