[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 |
---|
[e5664f2] | 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 |
---|
| 79 | self.graph = Graph() |
---|
| 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]) |
---|
[ffd23b5] | 212 | |
---|
[a07e72f] | 213 | x_label, x_unit = data.get_xaxis() |
---|
| 214 | y_label, y_unit = data.get_yaxis() |
---|
| 215 | self.graph.xaxis(x_unit, x_label) |
---|
| 216 | self.graph.yaxis(y_unit, y_label) |
---|
[6c0568b] | 217 | ## Set the view scale for all plots |
---|
[1bf33c1] | 218 | self._onEVT_FUNC_PROPERTY() |
---|
[6d727ae] | 219 | ## render the graph<=No need this done in canvas |
---|
| 220 | #self.graph.render(self) |
---|
| 221 | #self.subplot.figure.canvas.draw_idle() |
---|
| 222 | |
---|
| 223 | def draw_plot(self): |
---|
| 224 | """ |
---|
| 225 | Draw plot |
---|
| 226 | """ |
---|
| 227 | self.draw() |
---|
| 228 | |
---|
[a07e72f] | 229 | |
---|
| 230 | |
---|
[1bf33c1] | 231 | def onLeftDown(self,event): |
---|
[6c0568b] | 232 | """ |
---|
[d955bf19] | 233 | left button down and ready to drag |
---|
| 234 | Display the position of the mouse on the statusbar |
---|
[6c0568b] | 235 | """ |
---|
[1bf33c1] | 236 | PlotPanel.onLeftDown(self, event) |
---|
| 237 | ax = event.inaxes |
---|
| 238 | if ax != None: |
---|
| 239 | position = "x: %8.3g y: %8.3g" % (event.xdata, event.ydata) |
---|
| 240 | wx.PostEvent(self.parent, StatusEvent(status=position)) |
---|
[0275276] | 241 | # unfocus all |
---|
| 242 | self.parent.set_plot_unfocus() |
---|
[4ed210f4] | 243 | #post nd event to notify guiframe that this panel is on focus |
---|
[a45037aa] | 244 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
[df68da1] | 245 | |
---|
[4ed210f4] | 246 | |
---|
[a07e72f] | 247 | def _ontoggle_hide_error(self, event): |
---|
| 248 | """ |
---|
| 249 | Toggle error display to hide or show |
---|
| 250 | """ |
---|
| 251 | |
---|
| 252 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 253 | if self.hide_menu.GetText() == "Hide Error": |
---|
| 254 | selected_plot.hide_error = True |
---|
| 255 | else: |
---|
| 256 | selected_plot.hide_error = False |
---|
| 257 | ## increment graph color |
---|
| 258 | self.graph.render(self) |
---|
| 259 | self.subplot.figure.canvas.draw_idle() |
---|
| 260 | |
---|
[1bf33c1] | 261 | def _onRemove(self, event): |
---|
| 262 | """ |
---|
[d955bf19] | 263 | Remove a plottable from the graph and render the graph |
---|
| 264 | |
---|
| 265 | :param event: Menu event |
---|
| 266 | |
---|
[1bf33c1] | 267 | """ |
---|
[6c0568b] | 268 | ## Check if there is a selected graph to remove |
---|
[a07e72f] | 269 | if self.graph.selected_plottable in self.plots.keys(): |
---|
| 270 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 271 | id = self.graph.selected_plottable |
---|
[df22224] | 272 | self.remove_data_by_id(id) |
---|
| 273 | |
---|
[1bf33c1] | 274 | def onContextMenu(self, event): |
---|
| 275 | """ |
---|
[d955bf19] | 276 | 1D plot context menu |
---|
| 277 | |
---|
| 278 | :param event: wx context event |
---|
| 279 | |
---|
[1bf33c1] | 280 | """ |
---|
[52b8b74] | 281 | self._slicerpop = PanelMenu() |
---|
| 282 | self._slicerpop.set_plots(self.plots) |
---|
| 283 | self._slicerpop.set_graph(self.graph) |
---|
[9a585d0] | 284 | # Various plot options |
---|
| 285 | id = wx.NewId() |
---|
[6d727ae] | 286 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
[9a585d0] | 287 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
| 288 | id = wx.NewId() |
---|
[6d727ae] | 289 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
[18eba35] | 290 | wx.EVT_MENU(self, id, self.onPrint) |
---|
| 291 | id = wx.NewId() |
---|
[6d727ae] | 292 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
[18eba35] | 293 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
[52b8b74] | 294 | |
---|
[6d727ae] | 295 | id = wx.NewId() |
---|
| 296 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
| 297 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
| 298 | |
---|
| 299 | self._slicerpop.AppendSeparator() |
---|
| 300 | |
---|
[52b8b74] | 301 | #add menu of other plugins |
---|
[a07e72f] | 302 | item_list = self.parent.get_context_menu(self) |
---|
[6d727ae] | 303 | |
---|
[32c0841] | 304 | if (not item_list == None) and (not len(item_list) == 0): |
---|
[9a585d0] | 305 | for item in item_list: |
---|
| 306 | try: |
---|
| 307 | id = wx.NewId() |
---|
[52b8b74] | 308 | self._slicerpop.Append(id, item[0], item[1]) |
---|
[9a585d0] | 309 | wx.EVT_MENU(self, id, item[2]) |
---|
| 310 | except: |
---|
[32c0841] | 311 | msg = "ModelPanel1D.onContextMenu: " |
---|
[a07e72f] | 312 | msg += "bad menu item %s" % sys.exc_value |
---|
[32c0841] | 313 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[9a585d0] | 314 | pass |
---|
[52b8b74] | 315 | self._slicerpop.AppendSeparator() |
---|
[6d727ae] | 316 | #id = wx.NewId() |
---|
| 317 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
[1bf33c1] | 318 | if self.graph.selected_plottable in self.plots: |
---|
| 319 | plot = self.plots[self.graph.selected_plottable] |
---|
[a436b2e] | 320 | |
---|
[6d727ae] | 321 | id = wx.NewId() |
---|
| 322 | name = plot.name |
---|
| 323 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
[a436b2e] | 324 | self._slicerpop.AppendSeparator() |
---|
| 325 | if plot.name != 'SLD': |
---|
| 326 | wx.EVT_MENU(self, id, self._onSave) |
---|
[e6a93df] | 327 | id = wx.NewId() |
---|
[a436b2e] | 328 | self._slicerpop.Append(id, '&Linear Fit') |
---|
| 329 | wx.EVT_MENU(self, id, self.onFitting) |
---|
| 330 | self._slicerpop.AppendSeparator() |
---|
| 331 | |
---|
[a3c96f7a] | 332 | id = wx.NewId() |
---|
[a436b2e] | 333 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
| 334 | wx.EVT_MENU(self, id, self._onRemove) |
---|
| 335 | if not plot.is_data: |
---|
| 336 | id = wx.NewId() |
---|
| 337 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
| 338 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
| 339 | symbol_menu = wx.Menu() |
---|
| 340 | for label in self._symbol_labels: |
---|
| 341 | id = wx.NewId() |
---|
| 342 | symbol_menu.Append(id, str(label), str(label)) |
---|
| 343 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
| 344 | id = wx.NewId() |
---|
| 345 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
| 346 | self._slicerpop.AppendSeparator() |
---|
| 347 | |
---|
| 348 | id = wx.NewId() |
---|
| 349 | self.hide_menu = self._slicerpop.Append(id, "Hide Error") |
---|
| 350 | |
---|
| 351 | if plot.dy is not None and plot.dy != []: |
---|
| 352 | if plot.hide_error : |
---|
| 353 | self.hide_menu.SetText('Show Error') |
---|
| 354 | else: |
---|
| 355 | self.hide_menu.SetText('Hide Error') |
---|
[a07e72f] | 356 | else: |
---|
[a436b2e] | 357 | self.hide_menu.Enable(False) |
---|
| 358 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
| 359 | |
---|
| 360 | self._slicerpop.AppendSeparator() |
---|
| 361 | # Option to hide |
---|
| 362 | #TODO: implement functionality to hide a plottable (legend click) |
---|
[df22224] | 363 | |
---|
[a07e72f] | 364 | |
---|
[1bf33c1] | 365 | id = wx.NewId() |
---|
[52b8b74] | 366 | self._slicerpop.Append(id, '&Change scale') |
---|
[1bf33c1] | 367 | wx.EVT_MENU(self, id, self._onProperties) |
---|
| 368 | id = wx.NewId() |
---|
[52b8b74] | 369 | self._slicerpop.Append(id, '&Reset Graph') |
---|
[d468daa] | 370 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
[1bf33c1] | 371 | pos = event.GetPosition() |
---|
| 372 | pos = self.ScreenToClient(pos) |
---|
[52b8b74] | 373 | self.PopupMenu(self._slicerpop, pos) |
---|
[a07e72f] | 374 | |
---|
[e6a93df] | 375 | def onFreeze(self, event): |
---|
| 376 | """ |
---|
| 377 | """ |
---|
| 378 | plot = self.plots[self.graph.selected_plottable] |
---|
| 379 | self.parent.onfreeze([plot.id]) |
---|
| 380 | |
---|
[a07e72f] | 381 | def onChangeSymbol(self, event): |
---|
[6c0568b] | 382 | """ |
---|
| 383 | """ |
---|
[a07e72f] | 384 | menu = event.GetEventObject() |
---|
| 385 | id = event.GetId() |
---|
| 386 | label = menu.GetLabel(id) |
---|
| 387 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 388 | selected_plot.symbol = self._symbol_labels[label] |
---|
| 389 | ## Set the view scale for all plots |
---|
| 390 | self._onEVT_FUNC_PROPERTY() |
---|
| 391 | ## render the graph |
---|
[6d727ae] | 392 | #self.graph.render(self) |
---|
| 393 | #self.subplot.figure.canvas.draw_idle() |
---|
[c81140c] | 394 | |
---|
[42d27f2] | 395 | def _onsaveTXT(self, path): |
---|
| 396 | """ |
---|
[d955bf19] | 397 | Save file as txt |
---|
[1abcb04] | 398 | |
---|
[d955bf19] | 399 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
| 400 | |
---|
[42d27f2] | 401 | """ |
---|
| 402 | data = self.plots[self.graph.selected_plottable] |
---|
| 403 | |
---|
| 404 | if not path == None: |
---|
| 405 | out = open(path, 'w') |
---|
| 406 | has_errors = True |
---|
[32c0841] | 407 | if data.dy == None or data.dy == []: |
---|
[42d27f2] | 408 | has_errors = False |
---|
| 409 | # Sanity check |
---|
| 410 | if has_errors: |
---|
| 411 | try: |
---|
| 412 | if len(data.y) != len(data.dy): |
---|
| 413 | has_errors = False |
---|
| 414 | except: |
---|
| 415 | has_errors = False |
---|
| 416 | if has_errors: |
---|
[6d727ae] | 417 | if data.dx != None: |
---|
| 418 | out.write("<X> <Y> <dY> <dX>\n") |
---|
| 419 | else: |
---|
| 420 | out.write("<X> <Y> <dY>\n") |
---|
[42d27f2] | 421 | else: |
---|
| 422 | out.write("<X> <Y>\n") |
---|
| 423 | |
---|
| 424 | for i in range(len(data.x)): |
---|
| 425 | if has_errors: |
---|
[6d727ae] | 426 | if data.dx != None: |
---|
| 427 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
| 428 | data.y[i], |
---|
| 429 | data.dy[i], |
---|
| 430 | data.dx[i])) |
---|
| 431 | else: |
---|
| 432 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 433 | data.y[i], |
---|
| 434 | data.dy[i])) |
---|
[42d27f2] | 435 | else: |
---|
| 436 | out.write("%g %g\n" % (data.x[i], |
---|
| 437 | data.y[i])) |
---|
| 438 | out.close() |
---|
[6063b16] | 439 | try: |
---|
| 440 | self._default_save_location = os.path.dirname(path) |
---|
| 441 | except: |
---|
| 442 | pass |
---|
[8bd764d] | 443 | |
---|
[1bf33c1] | 444 | def _onSave(self, evt): |
---|
| 445 | """ |
---|
[d955bf19] | 446 | Save a data set to a text file |
---|
| 447 | |
---|
| 448 | :param evt: Menu event |
---|
| 449 | |
---|
[1bf33c1] | 450 | """ |
---|
[a07e72f] | 451 | |
---|
| 452 | path = None |
---|
| 453 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
| 454 | "CanSAS 1D files(*.xml)|*.xml" |
---|
| 455 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 456 | self._default_save_location, |
---|
| 457 | "", wildcard , wx.SAVE) |
---|
| 458 | |
---|
| 459 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 460 | path = dlg.GetPath() |
---|
[ad1e49c] | 461 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
| 462 | # This is MAC Fix |
---|
| 463 | ext_num = dlg.GetFilterIndex() |
---|
| 464 | if ext_num == 0: |
---|
| 465 | format = '.txt' |
---|
| 466 | else: |
---|
| 467 | format = '.xml' |
---|
| 468 | path = os.path.splitext(path)[0] + format |
---|
[a07e72f] | 469 | mypath = os.path.basename(path) |
---|
[1bf33c1] | 470 | |
---|
[a07e72f] | 471 | #TODO: This is bad design. The DataLoader is designed |
---|
| 472 | #to recognize extensions. |
---|
| 473 | # It should be a simple matter of calling the . |
---|
| 474 | #save(file, data, '.xml') method |
---|
| 475 | # of the DataLoader.loader.Loader class. |
---|
| 476 | from DataLoader.loader import Loader |
---|
| 477 | #Instantiate a loader |
---|
| 478 | loader = Loader() |
---|
| 479 | data = self.plots[self.graph.selected_plottable] |
---|
| 480 | format = ".txt" |
---|
| 481 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 482 | # Make sure the ext included in the file name |
---|
| 483 | # especially on MAC |
---|
| 484 | fName = os.path.splitext(path)[0] + format |
---|
| 485 | self._onsaveTXT(fName) |
---|
[a07e72f] | 486 | format = ".xml" |
---|
| 487 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 488 | # Make sure the ext included in the file name |
---|
| 489 | # especially on MAC |
---|
| 490 | fName = os.path.splitext(path)[0] + format |
---|
| 491 | loader.save(fName, data, format) |
---|
[a07e72f] | 492 | try: |
---|
| 493 | self._default_save_location = os.path.dirname(path) |
---|
| 494 | except: |
---|
| 495 | pass |
---|
| 496 | dlg.Destroy() |
---|
[c5a769e] | 497 | |
---|
| 498 | def _add_more_tool(self): |
---|
| 499 | """ |
---|
| 500 | Add refresh button in the tool bar |
---|
| 501 | """ |
---|
| 502 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
| 503 | return |
---|
| 504 | self.toolbar.AddSeparator() |
---|
| 505 | id_delete = wx.NewId() |
---|
| 506 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
| 507 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
[fb40dbb] | 508 | 'Delete', 'permanently Delete') |
---|
[c5a769e] | 509 | |
---|
| 510 | self.toolbar.Realize() |
---|
| 511 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
| 512 | |
---|
| 513 | def _on_delete(self, event): |
---|
| 514 | """ |
---|
| 515 | Refreshes the plotpanel on refresh tollbar button |
---|
| 516 | """ |
---|
| 517 | |
---|
| 518 | if self.parent is not None: |
---|
| 519 | wx.PostEvent(self.parent, |
---|
| 520 | NewPlotEvent(group_id=self.group_id, |
---|
| 521 | action="delete")) |
---|
| 522 | |
---|