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