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