[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 |
---|
[a07e72f] | 20 | |
---|
[1bf33c1] | 21 | from danse.common.plottools.PlotPanel import PlotPanel |
---|
[9f5c8bb] | 22 | #from danse.common.plottools.plottables import Graph |
---|
[3b69ca6] | 23 | from sans.guiframe import dataFitting |
---|
[df7046f] | 24 | from sans.guiframe.events import EVT_NEW_PLOT |
---|
| 25 | from sans.guiframe.events import StatusEvent |
---|
| 26 | from sans.guiframe.events import NewPlotEvent |
---|
| 27 | from sans.guiframe.events import SlicerEvent |
---|
[a45037aa] | 28 | from sans.guiframe.events import PanelOnFocusEvent |
---|
[52b8b74] | 29 | from sans.guiframe.events import EVT_NEW_LOADED_DATA |
---|
[0d9dae8] | 30 | from sans.guiframe.utils import PanelMenu |
---|
[4ac8556] | 31 | from sans.guiframe.dataFitting import Data1D |
---|
[691643c] | 32 | from sans.guiframe.panel_base import PanelBase |
---|
[1bf33c1] | 33 | from binder import BindArtist |
---|
| 34 | |
---|
[0d9dae8] | 35 | DEFAULT_QMAX = 0.05 |
---|
[1bf33c1] | 36 | DEFAULT_QSTEP = 0.001 |
---|
| 37 | DEFAULT_BEAM = 0.005 |
---|
[32c0841] | 38 | BIN_WIDTH = 1 |
---|
[1bf33c1] | 39 | |
---|
[0d9dae8] | 40 | |
---|
[691643c] | 41 | class ModelPanel1D(PlotPanel, PanelBase): |
---|
[1bf33c1] | 42 | """ |
---|
[d955bf19] | 43 | Plot panel for use with the GUI manager |
---|
[1bf33c1] | 44 | """ |
---|
| 45 | |
---|
| 46 | ## Internal name for the AUI manager |
---|
| 47 | window_name = "plotpanel" |
---|
| 48 | ## Title to appear on top of the window |
---|
| 49 | window_caption = "Plot Panel" |
---|
| 50 | ## Flag to tell the GUI manager that this panel is not |
---|
| 51 | # tied to any perspective |
---|
| 52 | ALWAYS_ON = True |
---|
| 53 | ## Group ID |
---|
| 54 | group_id = None |
---|
| 55 | |
---|
[32c0841] | 56 | def __init__(self, parent, id=-1, color = None, |
---|
| 57 | dpi=None, style=wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
| 58 | PlotPanel.__init__(self, parent, id=id, style=style, **kwargs) |
---|
[a45037aa] | 59 | PanelBase.__init__(self, parent) |
---|
[1bf33c1] | 60 | ## Reference to the parent window |
---|
| 61 | self.parent = parent |
---|
| 62 | ## Plottables |
---|
| 63 | self.plots = {} |
---|
[52b8b74] | 64 | #context menu |
---|
| 65 | self._slicerpop = None |
---|
[a07e72f] | 66 | |
---|
[52b8b74] | 67 | self._available_data = [] |
---|
| 68 | self._menu_add_ids = [] |
---|
[a07e72f] | 69 | self._symbol_labels = self.get_symbol_label() |
---|
| 70 | |
---|
| 71 | self.hide_menu = None |
---|
[1bf33c1] | 72 | ## Unique ID (from gui_manager) |
---|
| 73 | self.uid = None |
---|
[66718a1] | 74 | self.x_size = None |
---|
[6063b16] | 75 | ## Default locations |
---|
[adf44c2] | 76 | self._default_save_location = os.getcwd() |
---|
| 77 | self.size = None |
---|
[1bf33c1] | 78 | ## Graph |
---|
[9f5c8bb] | 79 | #self.graph = Graph() |
---|
[1bf33c1] | 80 | self.graph.xaxis("\\rm{Q}", 'A^{-1}') |
---|
[32c0841] | 81 | self.graph.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
[1bf33c1] | 82 | self.graph.render(self) |
---|
[6d727ae] | 83 | |
---|
| 84 | # In resizing event |
---|
| 85 | self.resizing = False |
---|
| 86 | self.canvas.set_resizing(self.resizing) |
---|
| 87 | self.Bind(wx.EVT_SIZE, self._OnReSize) |
---|
[c5a769e] | 88 | self._add_more_tool() |
---|
[a07e72f] | 89 | |
---|
| 90 | def get_symbol_label(self): |
---|
[52b8b74] | 91 | """ |
---|
[a07e72f] | 92 | Associates label to symbol |
---|
[52b8b74] | 93 | """ |
---|
[a07e72f] | 94 | _labels = {} |
---|
| 95 | i = 0 |
---|
[6d727ae] | 96 | _labels['Circle'] = i |
---|
[a07e72f] | 97 | i += 1 |
---|
[6d727ae] | 98 | _labels['Cross X '] = i |
---|
[a07e72f] | 99 | i += 1 |
---|
| 100 | _labels['Triangle Down'] = i |
---|
| 101 | i += 1 |
---|
| 102 | _labels['Triangle Up'] = i |
---|
| 103 | i += 1 |
---|
| 104 | _labels['Triangle Left'] = i |
---|
| 105 | i += 1 |
---|
| 106 | _labels['Triangle Right'] = i |
---|
| 107 | i += 1 |
---|
[6d727ae] | 108 | _labels['Cross +'] = i |
---|
[a07e72f] | 109 | i += 1 |
---|
| 110 | _labels['Square'] = i |
---|
| 111 | i += 1 |
---|
[6d727ae] | 112 | _labels['Diamond'] = i |
---|
[a07e72f] | 113 | i += 1 |
---|
| 114 | _labels['Diamond'] = i |
---|
| 115 | i += 1 |
---|
| 116 | _labels['Hexagon1'] = i |
---|
| 117 | i += 1 |
---|
| 118 | _labels['Hexagon2'] = i |
---|
| 119 | i += 1 |
---|
| 120 | _labels['Pentagon'] = i |
---|
| 121 | i += 1 |
---|
[6d727ae] | 122 | _labels['Line'] = i |
---|
[a07e72f] | 123 | return _labels |
---|
| 124 | |
---|
[52b8b74] | 125 | |
---|
[32c0841] | 126 | def set_data(self, list=None): |
---|
[3c44c66] | 127 | """ |
---|
| 128 | """ |
---|
| 129 | pass |
---|
| 130 | |
---|
[1bf33c1] | 131 | def _reset(self): |
---|
| 132 | """ |
---|
[d955bf19] | 133 | Resets internal data and graph |
---|
[1bf33c1] | 134 | """ |
---|
| 135 | self.graph.reset() |
---|
| 136 | self.plots = {} |
---|
[6d727ae] | 137 | |
---|
| 138 | def _OnReSize(self, event): |
---|
| 139 | """ |
---|
| 140 | On response of the resize of a panel, set axes_visiable False |
---|
| 141 | """ |
---|
[66718a1] | 142 | # It was found that wx >= 2.9.3 sends an event even if no size changed. |
---|
| 143 | # So manually recode the size (=x_size) and compare here. |
---|
| 144 | if self.x_size != None: |
---|
| 145 | if self.x_size == self.GetSize(): |
---|
[71fa9bb9] | 146 | self.resizing = False |
---|
| 147 | self.canvas.set_resizing(self.resizing) |
---|
[66718a1] | 148 | return |
---|
| 149 | self.x_size = self.GetSize() |
---|
[0f815f9] | 150 | |
---|
[ad9e5e1] | 151 | # Ready for another event |
---|
[4520830] | 152 | # Do not remove this Skip. Otherwise it will get runtime error on wx>=2.9. |
---|
[ad9e5e1] | 153 | event.Skip() |
---|
[6d727ae] | 154 | # set the resizing flag |
---|
| 155 | self.resizing = True |
---|
| 156 | self.canvas.set_resizing(self.resizing) |
---|
| 157 | self.parent.set_schedule(True) |
---|
[adf44c2] | 158 | pos_x, pos_y = self.GetPositionTuple() |
---|
| 159 | if pos_x != 0 and pos_y != 0: |
---|
| 160 | self.size, _ = self.GetClientSizeTuple() |
---|
[6d727ae] | 161 | |
---|
| 162 | def set_resizing(self, resizing=False): |
---|
| 163 | """ |
---|
| 164 | Set the resizing (True/False) |
---|
| 165 | """ |
---|
| 166 | self.resizing = resizing |
---|
| 167 | #self.canvas.set_resizing(resizing) |
---|
[1bf33c1] | 168 | |
---|
[6d727ae] | 169 | def schedule_full_draw(self, func='append'): |
---|
| 170 | """ |
---|
| 171 | Put self in schedule to full redraw list |
---|
| 172 | """ |
---|
| 173 | # append/del this panel in the schedule list |
---|
| 174 | self.parent.set_schedule_full_draw(self, func) |
---|
| 175 | |
---|
| 176 | |
---|
[a07e72f] | 177 | def remove_data_by_id(self, id): |
---|
| 178 | """' |
---|
| 179 | remove data from plot |
---|
| 180 | """ |
---|
| 181 | if id in self.plots.keys(): |
---|
| 182 | data = self.plots[id] |
---|
| 183 | self.graph.delete(data) |
---|
| 184 | data_manager = self._manager.parent.get_data_manager() |
---|
[df22224] | 185 | data_list, theory_list = data_manager.get_by_id(id_list=[id]) |
---|
| 186 | |
---|
| 187 | if id in data_list.keys(): |
---|
| 188 | data = data_list[id] |
---|
[248b918] | 189 | if id in theory_list.keys(): |
---|
[df22224] | 190 | data = theory_list[id] |
---|
| 191 | |
---|
[a07e72f] | 192 | del self.plots[id] |
---|
| 193 | self.graph.render(self) |
---|
| 194 | self.subplot.figure.canvas.draw_idle() |
---|
| 195 | if len(self.graph.plottables) == 0: |
---|
[df22224] | 196 | #onRemove: graph is empty must be the panel must be destroyed |
---|
| 197 | self.parent.delete_panel(self.uid) |
---|
[a07e72f] | 198 | |
---|
| 199 | def plot_data(self, data): |
---|
[1bf33c1] | 200 | """ |
---|
[d955bf19] | 201 | Data is ready to be displayed |
---|
| 202 | |
---|
| 203 | :param event: data event |
---|
[1bf33c1] | 204 | """ |
---|
[a07e72f] | 205 | if data.id in self.plots.keys(): |
---|
| 206 | #replace |
---|
| 207 | self.graph.replace(data) |
---|
| 208 | self.plots[data.id] = data |
---|
| 209 | else: |
---|
| 210 | self.plots[data.id] = data |
---|
| 211 | self.graph.add(self.plots[data.id]) |
---|
[9f5c8bb] | 212 | |
---|
[6c0568b] | 213 | ## Set the view scale for all plots |
---|
[1bf33c1] | 214 | self._onEVT_FUNC_PROPERTY() |
---|
[6d727ae] | 215 | ## render the graph<=No need this done in canvas |
---|
| 216 | #self.graph.render(self) |
---|
| 217 | #self.subplot.figure.canvas.draw_idle() |
---|
| 218 | |
---|
| 219 | def draw_plot(self): |
---|
| 220 | """ |
---|
| 221 | Draw plot |
---|
| 222 | """ |
---|
| 223 | self.draw() |
---|
| 224 | |
---|
[a07e72f] | 225 | |
---|
| 226 | |
---|
[1bf33c1] | 227 | def onLeftDown(self,event): |
---|
[6c0568b] | 228 | """ |
---|
[d955bf19] | 229 | left button down and ready to drag |
---|
| 230 | Display the position of the mouse on the statusbar |
---|
[6c0568b] | 231 | """ |
---|
[1bf33c1] | 232 | PlotPanel.onLeftDown(self, event) |
---|
| 233 | ax = event.inaxes |
---|
| 234 | if ax != None: |
---|
| 235 | position = "x: %8.3g y: %8.3g" % (event.xdata, event.ydata) |
---|
| 236 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
[0275276] | 237 | # unfocus all |
---|
| 238 | self.parent.set_plot_unfocus() |
---|
[4ed210f4] | 239 | #post nd event to notify guiframe that this panel is on focus |
---|
[a45037aa] | 240 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
[df68da1] | 241 | |
---|
[4ed210f4] | 242 | |
---|
[a07e72f] | 243 | def _ontoggle_hide_error(self, event): |
---|
| 244 | """ |
---|
| 245 | Toggle error display to hide or show |
---|
| 246 | """ |
---|
| 247 | |
---|
| 248 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 249 | if self.hide_menu.GetText() == "Hide Error": |
---|
| 250 | selected_plot.hide_error = True |
---|
| 251 | else: |
---|
| 252 | selected_plot.hide_error = False |
---|
| 253 | ## increment graph color |
---|
| 254 | self.graph.render(self) |
---|
| 255 | self.subplot.figure.canvas.draw_idle() |
---|
| 256 | |
---|
[1bf33c1] | 257 | def _onRemove(self, event): |
---|
| 258 | """ |
---|
[d955bf19] | 259 | Remove a plottable from the graph and render the graph |
---|
| 260 | |
---|
| 261 | :param event: Menu event |
---|
| 262 | |
---|
[1bf33c1] | 263 | """ |
---|
[6c0568b] | 264 | ## Check if there is a selected graph to remove |
---|
[a07e72f] | 265 | if self.graph.selected_plottable in self.plots.keys(): |
---|
| 266 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 267 | id = self.graph.selected_plottable |
---|
[df22224] | 268 | self.remove_data_by_id(id) |
---|
| 269 | |
---|
[1bf33c1] | 270 | def onContextMenu(self, event): |
---|
| 271 | """ |
---|
[d955bf19] | 272 | 1D plot context menu |
---|
| 273 | |
---|
| 274 | :param event: wx context event |
---|
| 275 | |
---|
[1bf33c1] | 276 | """ |
---|
[52b8b74] | 277 | self._slicerpop = PanelMenu() |
---|
| 278 | self._slicerpop.set_plots(self.plots) |
---|
| 279 | self._slicerpop.set_graph(self.graph) |
---|
[9a585d0] | 280 | # Various plot options |
---|
| 281 | id = wx.NewId() |
---|
[6d727ae] | 282 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
[9a585d0] | 283 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
| 284 | id = wx.NewId() |
---|
[6d727ae] | 285 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
[18eba35] | 286 | wx.EVT_MENU(self, id, self.onPrint) |
---|
| 287 | id = wx.NewId() |
---|
[6d727ae] | 288 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
[18eba35] | 289 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
[52b8b74] | 290 | |
---|
[6d727ae] | 291 | id = wx.NewId() |
---|
| 292 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
| 293 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
| 294 | |
---|
| 295 | self._slicerpop.AppendSeparator() |
---|
| 296 | |
---|
[52b8b74] | 297 | #add menu of other plugins |
---|
[a07e72f] | 298 | item_list = self.parent.get_context_menu(self) |
---|
[6d727ae] | 299 | |
---|
[32c0841] | 300 | if (not item_list == None) and (not len(item_list) == 0): |
---|
[9a585d0] | 301 | for item in item_list: |
---|
| 302 | try: |
---|
| 303 | id = wx.NewId() |
---|
[52b8b74] | 304 | self._slicerpop.Append(id, item[0], item[1]) |
---|
[9a585d0] | 305 | wx.EVT_MENU(self, id, item[2]) |
---|
| 306 | except: |
---|
[32c0841] | 307 | msg = "ModelPanel1D.onContextMenu: " |
---|
[a07e72f] | 308 | msg += "bad menu item %s" % sys.exc_value |
---|
[32c0841] | 309 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[9a585d0] | 310 | pass |
---|
[52b8b74] | 311 | self._slicerpop.AppendSeparator() |
---|
[6d727ae] | 312 | #id = wx.NewId() |
---|
| 313 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
[1bf33c1] | 314 | if self.graph.selected_plottable in self.plots: |
---|
| 315 | plot = self.plots[self.graph.selected_plottable] |
---|
[a436b2e] | 316 | |
---|
[6d727ae] | 317 | id = wx.NewId() |
---|
| 318 | name = plot.name |
---|
| 319 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
[a436b2e] | 320 | self._slicerpop.AppendSeparator() |
---|
| 321 | if plot.name != 'SLD': |
---|
| 322 | wx.EVT_MENU(self, id, self._onSave) |
---|
[e6a93df] | 323 | id = wx.NewId() |
---|
[a436b2e] | 324 | self._slicerpop.Append(id, '&Linear Fit') |
---|
| 325 | wx.EVT_MENU(self, id, self.onFitting) |
---|
| 326 | self._slicerpop.AppendSeparator() |
---|
| 327 | |
---|
[a3c96f7a] | 328 | id = wx.NewId() |
---|
[a436b2e] | 329 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
| 330 | wx.EVT_MENU(self, id, self._onRemove) |
---|
| 331 | if not plot.is_data: |
---|
| 332 | id = wx.NewId() |
---|
| 333 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
| 334 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
| 335 | symbol_menu = wx.Menu() |
---|
| 336 | for label in self._symbol_labels: |
---|
| 337 | id = wx.NewId() |
---|
| 338 | symbol_menu.Append(id, str(label), str(label)) |
---|
| 339 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
| 340 | id = wx.NewId() |
---|
| 341 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
| 342 | self._slicerpop.AppendSeparator() |
---|
| 343 | |
---|
| 344 | id = wx.NewId() |
---|
| 345 | self.hide_menu = self._slicerpop.Append(id, "Hide Error") |
---|
| 346 | |
---|
| 347 | if plot.dy is not None and plot.dy != []: |
---|
| 348 | if plot.hide_error : |
---|
| 349 | self.hide_menu.SetText('Show Error') |
---|
| 350 | else: |
---|
| 351 | self.hide_menu.SetText('Hide Error') |
---|
[a07e72f] | 352 | else: |
---|
[a436b2e] | 353 | self.hide_menu.Enable(False) |
---|
| 354 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
| 355 | |
---|
| 356 | self._slicerpop.AppendSeparator() |
---|
| 357 | # Option to hide |
---|
| 358 | #TODO: implement functionality to hide a plottable (legend click) |
---|
[df22224] | 359 | |
---|
[a07e72f] | 360 | |
---|
[1bf33c1] | 361 | id = wx.NewId() |
---|
[52b8b74] | 362 | self._slicerpop.Append(id, '&Change scale') |
---|
[1bf33c1] | 363 | wx.EVT_MENU(self, id, self._onProperties) |
---|
| 364 | id = wx.NewId() |
---|
[52b8b74] | 365 | self._slicerpop.Append(id, '&Reset Graph') |
---|
[d468daa] | 366 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
[1bf33c1] | 367 | pos = event.GetPosition() |
---|
| 368 | pos = self.ScreenToClient(pos) |
---|
[52b8b74] | 369 | self.PopupMenu(self._slicerpop, pos) |
---|
[a07e72f] | 370 | |
---|
[e6a93df] | 371 | def onFreeze(self, event): |
---|
| 372 | """ |
---|
| 373 | """ |
---|
| 374 | plot = self.plots[self.graph.selected_plottable] |
---|
| 375 | self.parent.onfreeze([plot.id]) |
---|
| 376 | |
---|
[a07e72f] | 377 | def onChangeSymbol(self, event): |
---|
[6c0568b] | 378 | """ |
---|
| 379 | """ |
---|
[a07e72f] | 380 | menu = event.GetEventObject() |
---|
| 381 | id = event.GetId() |
---|
| 382 | label = menu.GetLabel(id) |
---|
| 383 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 384 | selected_plot.symbol = self._symbol_labels[label] |
---|
| 385 | ## Set the view scale for all plots |
---|
| 386 | self._onEVT_FUNC_PROPERTY() |
---|
| 387 | ## render the graph |
---|
[6d727ae] | 388 | #self.graph.render(self) |
---|
| 389 | #self.subplot.figure.canvas.draw_idle() |
---|
[c81140c] | 390 | |
---|
[42d27f2] | 391 | def _onsaveTXT(self, path): |
---|
| 392 | """ |
---|
[d955bf19] | 393 | Save file as txt |
---|
[1abcb04] | 394 | |
---|
[d955bf19] | 395 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
| 396 | |
---|
[42d27f2] | 397 | """ |
---|
| 398 | data = self.plots[self.graph.selected_plottable] |
---|
| 399 | |
---|
| 400 | if not path == None: |
---|
| 401 | out = open(path, 'w') |
---|
| 402 | has_errors = True |
---|
[32c0841] | 403 | if data.dy == None or data.dy == []: |
---|
[42d27f2] | 404 | has_errors = False |
---|
| 405 | # Sanity check |
---|
| 406 | if has_errors: |
---|
| 407 | try: |
---|
| 408 | if len(data.y) != len(data.dy): |
---|
| 409 | has_errors = False |
---|
| 410 | except: |
---|
| 411 | has_errors = False |
---|
| 412 | if has_errors: |
---|
[6d727ae] | 413 | if data.dx != None: |
---|
| 414 | out.write("<X> <Y> <dY> <dX>\n") |
---|
| 415 | else: |
---|
| 416 | out.write("<X> <Y> <dY>\n") |
---|
[42d27f2] | 417 | else: |
---|
| 418 | out.write("<X> <Y>\n") |
---|
| 419 | |
---|
| 420 | for i in range(len(data.x)): |
---|
| 421 | if has_errors: |
---|
[6d727ae] | 422 | if data.dx != None: |
---|
| 423 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
| 424 | data.y[i], |
---|
| 425 | data.dy[i], |
---|
| 426 | data.dx[i])) |
---|
| 427 | else: |
---|
| 428 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 429 | data.y[i], |
---|
| 430 | data.dy[i])) |
---|
[42d27f2] | 431 | else: |
---|
| 432 | out.write("%g %g\n" % (data.x[i], |
---|
| 433 | data.y[i])) |
---|
| 434 | out.close() |
---|
[6063b16] | 435 | try: |
---|
| 436 | self._default_save_location = os.path.dirname(path) |
---|
| 437 | except: |
---|
| 438 | pass |
---|
[8bd764d] | 439 | |
---|
[1bf33c1] | 440 | def _onSave(self, evt): |
---|
| 441 | """ |
---|
[d955bf19] | 442 | Save a data set to a text file |
---|
| 443 | |
---|
| 444 | :param evt: Menu event |
---|
| 445 | |
---|
[1bf33c1] | 446 | """ |
---|
[a07e72f] | 447 | |
---|
| 448 | path = None |
---|
| 449 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
| 450 | "CanSAS 1D files(*.xml)|*.xml" |
---|
| 451 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 452 | self._default_save_location, |
---|
| 453 | "", wildcard , wx.SAVE) |
---|
| 454 | |
---|
| 455 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 456 | path = dlg.GetPath() |
---|
[ad1e49c] | 457 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
| 458 | # This is MAC Fix |
---|
| 459 | ext_num = dlg.GetFilterIndex() |
---|
| 460 | if ext_num == 0: |
---|
| 461 | format = '.txt' |
---|
| 462 | else: |
---|
| 463 | format = '.xml' |
---|
| 464 | path = os.path.splitext(path)[0] + format |
---|
[a07e72f] | 465 | mypath = os.path.basename(path) |
---|
[1bf33c1] | 466 | |
---|
[a07e72f] | 467 | #TODO: This is bad design. The DataLoader is designed |
---|
| 468 | #to recognize extensions. |
---|
| 469 | # It should be a simple matter of calling the . |
---|
| 470 | #save(file, data, '.xml') method |
---|
| 471 | # of the DataLoader.loader.Loader class. |
---|
| 472 | from DataLoader.loader import Loader |
---|
| 473 | #Instantiate a loader |
---|
| 474 | loader = Loader() |
---|
| 475 | data = self.plots[self.graph.selected_plottable] |
---|
| 476 | format = ".txt" |
---|
| 477 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 478 | # Make sure the ext included in the file name |
---|
| 479 | # especially on MAC |
---|
| 480 | fName = os.path.splitext(path)[0] + format |
---|
| 481 | self._onsaveTXT(fName) |
---|
[a07e72f] | 482 | format = ".xml" |
---|
| 483 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 484 | # Make sure the ext included in the file name |
---|
| 485 | # especially on MAC |
---|
| 486 | fName = os.path.splitext(path)[0] + format |
---|
| 487 | loader.save(fName, data, format) |
---|
[a07e72f] | 488 | try: |
---|
| 489 | self._default_save_location = os.path.dirname(path) |
---|
| 490 | except: |
---|
| 491 | pass |
---|
| 492 | dlg.Destroy() |
---|
[c5a769e] | 493 | |
---|
| 494 | def _add_more_tool(self): |
---|
| 495 | """ |
---|
| 496 | Add refresh button in the tool bar |
---|
| 497 | """ |
---|
| 498 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
| 499 | return |
---|
| 500 | self.toolbar.AddSeparator() |
---|
| 501 | id_delete = wx.NewId() |
---|
| 502 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
| 503 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
[fb40dbb] | 504 | 'Delete', 'permanently Delete') |
---|
[c5a769e] | 505 | |
---|
| 506 | self.toolbar.Realize() |
---|
| 507 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
| 508 | |
---|
| 509 | def _on_delete(self, event): |
---|
| 510 | """ |
---|
| 511 | Refreshes the plotpanel on refresh tollbar button |
---|
| 512 | """ |
---|
| 513 | |
---|
| 514 | if self.parent is not None: |
---|
| 515 | wx.PostEvent(self.parent, |
---|
| 516 | NewPlotEvent(group_id=self.group_id, |
---|
| 517 | action="delete")) |
---|
| 518 | |
---|