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