[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 |
---|
[37c36d9] | 52 | window_caption = "Graph" |
---|
[1bf33c1] | 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 = {} |
---|
[7022fdc] | 161 | if self.is_zoomed: |
---|
| 162 | self.is_zoomed = False |
---|
[6d727ae] | 163 | |
---|
| 164 | def _OnReSize(self, event): |
---|
| 165 | """ |
---|
| 166 | On response of the resize of a panel, set axes_visiable False |
---|
| 167 | """ |
---|
[66718a1] | 168 | # It was found that wx >= 2.9.3 sends an event even if no size changed. |
---|
| 169 | # So manually recode the size (=x_size) and compare here. |
---|
| 170 | if self.x_size != None: |
---|
| 171 | if self.x_size == self.GetSize(): |
---|
[71fa9bb9] | 172 | self.resizing = False |
---|
| 173 | self.canvas.set_resizing(self.resizing) |
---|
[66718a1] | 174 | return |
---|
| 175 | self.x_size = self.GetSize() |
---|
[0f815f9] | 176 | |
---|
[ad9e5e1] | 177 | # Ready for another event |
---|
[4520830] | 178 | # Do not remove this Skip. Otherwise it will get runtime error on wx>=2.9. |
---|
[ad9e5e1] | 179 | event.Skip() |
---|
[6d727ae] | 180 | # set the resizing flag |
---|
| 181 | self.resizing = True |
---|
| 182 | self.canvas.set_resizing(self.resizing) |
---|
| 183 | self.parent.set_schedule(True) |
---|
[adf44c2] | 184 | pos_x, pos_y = self.GetPositionTuple() |
---|
| 185 | if pos_x != 0 and pos_y != 0: |
---|
| 186 | self.size, _ = self.GetClientSizeTuple() |
---|
[6d727ae] | 187 | |
---|
| 188 | def set_resizing(self, resizing=False): |
---|
| 189 | """ |
---|
| 190 | Set the resizing (True/False) |
---|
| 191 | """ |
---|
| 192 | self.resizing = resizing |
---|
| 193 | #self.canvas.set_resizing(resizing) |
---|
[1bf33c1] | 194 | |
---|
[6d727ae] | 195 | def schedule_full_draw(self, func='append'): |
---|
| 196 | """ |
---|
| 197 | Put self in schedule to full redraw list |
---|
| 198 | """ |
---|
| 199 | # append/del this panel in the schedule list |
---|
| 200 | self.parent.set_schedule_full_draw(self, func) |
---|
| 201 | |
---|
| 202 | |
---|
[a07e72f] | 203 | def remove_data_by_id(self, id): |
---|
| 204 | """' |
---|
| 205 | remove data from plot |
---|
| 206 | """ |
---|
| 207 | if id in self.plots.keys(): |
---|
| 208 | data = self.plots[id] |
---|
| 209 | self.graph.delete(data) |
---|
| 210 | data_manager = self._manager.parent.get_data_manager() |
---|
[df22224] | 211 | data_list, theory_list = data_manager.get_by_id(id_list=[id]) |
---|
| 212 | |
---|
| 213 | if id in data_list.keys(): |
---|
| 214 | data = data_list[id] |
---|
[248b918] | 215 | if id in theory_list.keys(): |
---|
[df22224] | 216 | data = theory_list[id] |
---|
| 217 | |
---|
[a07e72f] | 218 | del self.plots[id] |
---|
| 219 | self.graph.render(self) |
---|
| 220 | self.subplot.figure.canvas.draw_idle() |
---|
| 221 | if len(self.graph.plottables) == 0: |
---|
[df22224] | 222 | #onRemove: graph is empty must be the panel must be destroyed |
---|
| 223 | self.parent.delete_panel(self.uid) |
---|
[a07e72f] | 224 | |
---|
| 225 | def plot_data(self, data): |
---|
[1bf33c1] | 226 | """ |
---|
[d955bf19] | 227 | Data is ready to be displayed |
---|
| 228 | |
---|
| 229 | :param event: data event |
---|
[1bf33c1] | 230 | """ |
---|
[a07e72f] | 231 | if data.id in self.plots.keys(): |
---|
[e4a703a] | 232 | #Recover panel prop.s |
---|
| 233 | xlo, xhi = self.subplot.get_xlim() |
---|
| 234 | ylo, yhi = self.subplot.get_ylim() |
---|
[5eede4e] | 235 | old_data = self.plots[data.id] |
---|
| 236 | data.label = old_data.label |
---|
| 237 | data.custom_color = old_data.custom_color |
---|
[e4a703a] | 238 | # Replace data |
---|
[a07e72f] | 239 | self.graph.replace(data) |
---|
| 240 | self.plots[data.id] = data |
---|
[e4a703a] | 241 | ## Set the view scale for all plots |
---|
| 242 | self._onEVT_FUNC_PROPERTY() |
---|
[7022fdc] | 243 | # Check if zoomed |
---|
| 244 | toolbar_zoomed = self.toolbar.GetToolEnabled(self.toolbar._NTB2_BACK) |
---|
| 245 | if self.is_zoomed or toolbar_zoomed: |
---|
| 246 | # Recover the x,y limits |
---|
| 247 | self.subplot.set_xlim((xlo, xhi)) |
---|
| 248 | self.subplot.set_ylim((ylo, yhi)) |
---|
[53cf669] | 249 | # Update Graph menu and help string |
---|
| 250 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
| 251 | helpString = 'Show/Hide Graph: ' |
---|
| 252 | for plot in self.plots.itervalues(): |
---|
| 253 | helpString += (' ' + plot.label +';') |
---|
| 254 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
[a07e72f] | 255 | else: |
---|
| 256 | self.plots[data.id] = data |
---|
| 257 | self.graph.add(self.plots[data.id]) |
---|
[e4a703a] | 258 | ## Set the view scale for all plots |
---|
| 259 | self._onEVT_FUNC_PROPERTY() |
---|
[fe48fcc] | 260 | self.toolbar.update() |
---|
[7022fdc] | 261 | if self.is_zoomed: |
---|
| 262 | self.is_zoomed = False |
---|
[5eede4e] | 263 | |
---|
[e4a703a] | 264 | |
---|
[6d727ae] | 265 | def draw_plot(self): |
---|
| 266 | """ |
---|
| 267 | Draw plot |
---|
| 268 | """ |
---|
| 269 | self.draw() |
---|
| 270 | |
---|
[a07e72f] | 271 | |
---|
| 272 | |
---|
[1bf33c1] | 273 | def onLeftDown(self,event): |
---|
[6c0568b] | 274 | """ |
---|
[d955bf19] | 275 | left button down and ready to drag |
---|
| 276 | Display the position of the mouse on the statusbar |
---|
[6c0568b] | 277 | """ |
---|
[e85e2bc] | 278 | PlotPanel.onLeftDown(self, event) |
---|
| 279 | ax = event.inaxes |
---|
| 280 | if ax != None: |
---|
[902f373] | 281 | try: |
---|
| 282 | pos_x = float(event.xdata)# / size_x |
---|
| 283 | pos_y = float(event.ydata)# / size_y |
---|
| 284 | pos_x = "%8.3g"% pos_x |
---|
| 285 | pos_y = "%8.3g"% pos_y |
---|
| 286 | self.position = str(pos_x), str(pos_y) |
---|
| 287 | wx.PostEvent(self.parent, StatusEvent(status=self.position)) |
---|
| 288 | except: |
---|
| 289 | self.position = None |
---|
[0275276] | 290 | # unfocus all |
---|
| 291 | self.parent.set_plot_unfocus() |
---|
[4ed210f4] | 292 | #post nd event to notify guiframe that this panel is on focus |
---|
[a45037aa] | 293 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
[df68da1] | 294 | |
---|
[4ed210f4] | 295 | |
---|
[a07e72f] | 296 | def _ontoggle_hide_error(self, event): |
---|
| 297 | """ |
---|
| 298 | Toggle error display to hide or show |
---|
| 299 | """ |
---|
| 300 | |
---|
| 301 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
[5637362] | 302 | if self.hide_menu.GetText() == "Hide Error Bar": |
---|
[a07e72f] | 303 | selected_plot.hide_error = True |
---|
| 304 | else: |
---|
| 305 | selected_plot.hide_error = False |
---|
| 306 | ## increment graph color |
---|
| 307 | self.graph.render(self) |
---|
| 308 | self.subplot.figure.canvas.draw_idle() |
---|
| 309 | |
---|
[1bf33c1] | 310 | def _onRemove(self, event): |
---|
| 311 | """ |
---|
[d955bf19] | 312 | Remove a plottable from the graph and render the graph |
---|
| 313 | |
---|
| 314 | :param event: Menu event |
---|
| 315 | |
---|
[1bf33c1] | 316 | """ |
---|
[6c0568b] | 317 | ## Check if there is a selected graph to remove |
---|
[a07e72f] | 318 | if self.graph.selected_plottable in self.plots.keys(): |
---|
| 319 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 320 | id = self.graph.selected_plottable |
---|
[df22224] | 321 | self.remove_data_by_id(id) |
---|
| 322 | |
---|
[1bf33c1] | 323 | def onContextMenu(self, event): |
---|
| 324 | """ |
---|
[d955bf19] | 325 | 1D plot context menu |
---|
| 326 | |
---|
| 327 | :param event: wx context event |
---|
| 328 | |
---|
[1bf33c1] | 329 | """ |
---|
[52b8b74] | 330 | self._slicerpop = PanelMenu() |
---|
| 331 | self._slicerpop.set_plots(self.plots) |
---|
| 332 | self._slicerpop.set_graph(self.graph) |
---|
[9a585d0] | 333 | # Various plot options |
---|
| 334 | id = wx.NewId() |
---|
[6d727ae] | 335 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
[9a585d0] | 336 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
| 337 | id = wx.NewId() |
---|
[6d727ae] | 338 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
[18eba35] | 339 | wx.EVT_MENU(self, id, self.onPrint) |
---|
| 340 | id = wx.NewId() |
---|
[6d727ae] | 341 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
[18eba35] | 342 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
[52b8b74] | 343 | |
---|
[6d727ae] | 344 | id = wx.NewId() |
---|
| 345 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
| 346 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
[e1714a9] | 347 | |
---|
[6d727ae] | 348 | self._slicerpop.AppendSeparator() |
---|
| 349 | |
---|
[52b8b74] | 350 | #add menu of other plugins |
---|
[a07e72f] | 351 | item_list = self.parent.get_context_menu(self) |
---|
[6d727ae] | 352 | |
---|
[32c0841] | 353 | if (not item_list == None) and (not len(item_list) == 0): |
---|
[9a585d0] | 354 | for item in item_list: |
---|
| 355 | try: |
---|
| 356 | id = wx.NewId() |
---|
[52b8b74] | 357 | self._slicerpop.Append(id, item[0], item[1]) |
---|
[9a585d0] | 358 | wx.EVT_MENU(self, id, item[2]) |
---|
| 359 | except: |
---|
[32c0841] | 360 | msg = "ModelPanel1D.onContextMenu: " |
---|
[a07e72f] | 361 | msg += "bad menu item %s" % sys.exc_value |
---|
[32c0841] | 362 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[9a585d0] | 363 | pass |
---|
[52b8b74] | 364 | self._slicerpop.AppendSeparator() |
---|
[6d727ae] | 365 | #id = wx.NewId() |
---|
| 366 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
[1bf33c1] | 367 | if self.graph.selected_plottable in self.plots: |
---|
| 368 | plot = self.plots[self.graph.selected_plottable] |
---|
[a436b2e] | 369 | |
---|
[6d727ae] | 370 | id = wx.NewId() |
---|
| 371 | name = plot.name |
---|
| 372 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
[a436b2e] | 373 | self._slicerpop.AppendSeparator() |
---|
| 374 | if plot.name != 'SLD': |
---|
| 375 | wx.EVT_MENU(self, id, self._onSave) |
---|
[e6a93df] | 376 | id = wx.NewId() |
---|
[a436b2e] | 377 | self._slicerpop.Append(id, '&Linear Fit') |
---|
| 378 | wx.EVT_MENU(self, id, self.onFitting) |
---|
| 379 | self._slicerpop.AppendSeparator() |
---|
| 380 | |
---|
[a3c96f7a] | 381 | id = wx.NewId() |
---|
[a436b2e] | 382 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
| 383 | wx.EVT_MENU(self, id, self._onRemove) |
---|
| 384 | if not plot.is_data: |
---|
| 385 | id = wx.NewId() |
---|
| 386 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
| 387 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
[e1714a9] | 388 | self._slicerpop.AppendSeparator() |
---|
[a436b2e] | 389 | symbol_menu = wx.Menu() |
---|
| 390 | for label in self._symbol_labels: |
---|
| 391 | id = wx.NewId() |
---|
| 392 | symbol_menu.Append(id, str(label), str(label)) |
---|
| 393 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
| 394 | id = wx.NewId() |
---|
| 395 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
[2636188] | 396 | |
---|
| 397 | color_menu = wx.Menu() |
---|
| 398 | for label in self._color_labels: |
---|
| 399 | id = wx.NewId() |
---|
| 400 | color_menu.Append(id, str(label), str(label)) |
---|
| 401 | wx.EVT_MENU(self, id, self.onChangeColor) |
---|
| 402 | id = wx.NewId() |
---|
| 403 | self._slicerpop.AppendMenu(id, '&Modify Symbol Color', color_menu) |
---|
| 404 | |
---|
| 405 | size_menu = wx.Menu() |
---|
| 406 | for i in range(10): |
---|
| 407 | id = wx.NewId() |
---|
| 408 | size_menu.Append(id, str(i), str(i)) |
---|
| 409 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
| 410 | id = wx.NewId() |
---|
| 411 | size_menu.Append(id, '&Custom', 'Custom') |
---|
| 412 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
| 413 | id = wx.NewId() |
---|
| 414 | self._slicerpop.AppendMenu(id, '&Modify Symbol Size', size_menu) |
---|
| 415 | |
---|
[5637362] | 416 | self.hide_menu = self._slicerpop.Append(id, "Hide Error Bar") |
---|
[a436b2e] | 417 | |
---|
| 418 | if plot.dy is not None and plot.dy != []: |
---|
| 419 | if plot.hide_error : |
---|
[5637362] | 420 | self.hide_menu.SetText('Show Error Bar') |
---|
[a436b2e] | 421 | else: |
---|
[5637362] | 422 | self.hide_menu.SetText('Hide Error Bar') |
---|
[a07e72f] | 423 | else: |
---|
[a436b2e] | 424 | self.hide_menu.Enable(False) |
---|
| 425 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
| 426 | |
---|
| 427 | self._slicerpop.AppendSeparator() |
---|
[e1714a9] | 428 | id = wx.NewId() |
---|
| 429 | self._slicerpop.Append(id, '&Edit Legend Label', 'Edit Legend Label') |
---|
| 430 | wx.EVT_MENU(self, id, self.onEditLabels) |
---|
[a436b2e] | 431 | # Option to hide |
---|
| 432 | #TODO: implement functionality to hide a plottable (legend click) |
---|
[e1714a9] | 433 | |
---|
| 434 | loc_menu = wx.Menu() |
---|
| 435 | for label in self._loc_labels: |
---|
| 436 | id = wx.NewId() |
---|
| 437 | loc_menu.Append(id, str(label), str(label)) |
---|
| 438 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
| 439 | id = wx.NewId() |
---|
| 440 | self._slicerpop.AppendMenu(id, '&Modify Legend Location', loc_menu) |
---|
[df22224] | 441 | |
---|
[e1714a9] | 442 | id = wx.NewId() |
---|
| 443 | self._slicerpop.Append(id, '&Toggle Legend On/Off', 'Toggle Legend On/Off') |
---|
| 444 | wx.EVT_MENU(self, id, self.onLegend) |
---|
| 445 | self._slicerpop.AppendSeparator() |
---|
[a07e72f] | 446 | |
---|
[857d00f] | 447 | id = wx.NewId() |
---|
[5637362] | 448 | self._slicerpop.Append(id, '&Edit Y Axis Label') |
---|
[857d00f] | 449 | wx.EVT_MENU(self, id, self._on_yaxis_label) |
---|
| 450 | id = wx.NewId() |
---|
[5637362] | 451 | self._slicerpop.Append(id, '&Edit X Axis Label') |
---|
[857d00f] | 452 | wx.EVT_MENU(self, id, self._on_xaxis_label) |
---|
[5637362] | 453 | |
---|
| 454 | id = wx.NewId() |
---|
| 455 | self._slicerpop.Append(id, '&Toggle Grid On/Off', 'Toggle Grid On/Off') |
---|
| 456 | wx.EVT_MENU(self, id, self.onGridOnOff) |
---|
[857d00f] | 457 | self._slicerpop.AppendSeparator() |
---|
| 458 | |
---|
[ef4a4ea] | 459 | if self.position != None: |
---|
| 460 | id = wx.NewId() |
---|
[c17760d] | 461 | self._slicerpop.Append(id, '&Add Text') |
---|
[ef4a4ea] | 462 | wx.EVT_MENU(self, id, self._on_addtext) |
---|
| 463 | id = wx.NewId() |
---|
[c17760d] | 464 | self._slicerpop.Append(id, '&Remove Text') |
---|
[ef4a4ea] | 465 | wx.EVT_MENU(self, id, self._on_removetext) |
---|
[857d00f] | 466 | self._slicerpop.AppendSeparator() |
---|
[886657f] | 467 | id = wx.NewId() |
---|
[c17760d] | 468 | self._slicerpop.Append(id, '&Change Scale') |
---|
[1bf33c1] | 469 | wx.EVT_MENU(self, id, self._onProperties) |
---|
[857d00f] | 470 | self._slicerpop.AppendSeparator() |
---|
[1bf33c1] | 471 | id = wx.NewId() |
---|
[2259920] | 472 | self._slicerpop.Append(id, '&Reset Graph Range') |
---|
[d468daa] | 473 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
[dc51a7f] | 474 | try: |
---|
| 475 | pos_evt = event.GetPosition() |
---|
| 476 | pos = self.ScreenToClient(pos_evt) |
---|
| 477 | except: |
---|
| 478 | pos_x, pos_y = self.toolbar.GetPositionTuple() |
---|
| 479 | pos = (pos_x, pos_y + 5) |
---|
[37c36d9] | 480 | |
---|
| 481 | self._slicerpop.AppendSeparator() |
---|
| 482 | id = wx.NewId() |
---|
| 483 | self._slicerpop.Append(id, '&Window Title') |
---|
| 484 | wx.EVT_MENU(self, id, self.onChangeCaption) |
---|
| 485 | |
---|
[52b8b74] | 486 | self.PopupMenu(self._slicerpop, pos) |
---|
[dc51a7f] | 487 | |
---|
[e6a93df] | 488 | def onFreeze(self, event): |
---|
| 489 | """ |
---|
| 490 | """ |
---|
| 491 | plot = self.plots[self.graph.selected_plottable] |
---|
| 492 | self.parent.onfreeze([plot.id]) |
---|
[d7d1255e] | 493 | |
---|
[6d52f21d] | 494 | def onEditLabels(self, event): |
---|
| 495 | """ |
---|
[d7d1255e] | 496 | Edit legend label |
---|
[6d52f21d] | 497 | """ |
---|
| 498 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
[d7d1255e] | 499 | label = selected_plot.label |
---|
| 500 | dial = LabelDialog(None, -1, 'Change Legend Label', label) |
---|
[6d52f21d] | 501 | if dial.ShowModal() == wx.ID_OK: |
---|
[d7d1255e] | 502 | newLabel = dial.getText() |
---|
| 503 | selected_plot.label = newLabel |
---|
[53cf669] | 504 | # Updata Graph menu help string |
---|
| 505 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
| 506 | helpString = 'Show/Hide Graph: ' |
---|
| 507 | for plot in self.plots.itervalues(): |
---|
| 508 | helpString += (' ' + plot.label +';') |
---|
| 509 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
| 510 | #break |
---|
[6d52f21d] | 511 | dial.Destroy() |
---|
[53cf669] | 512 | |
---|
[d7d1255e] | 513 | ## render the graph |
---|
| 514 | self._onEVT_FUNC_PROPERTY() |
---|
[6d52f21d] | 515 | |
---|
[2636188] | 516 | def onChangeColor(self, event): |
---|
| 517 | """ |
---|
| 518 | Changes the color of the graph when selected |
---|
| 519 | """ |
---|
| 520 | menu = event.GetEventObject() |
---|
| 521 | id = event.GetId() |
---|
| 522 | label = menu.GetLabel(id) |
---|
| 523 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 524 | selected_plot.custom_color = self._color_labels[label] |
---|
| 525 | ## Set the view scale for all plots |
---|
| 526 | self._onEVT_FUNC_PROPERTY() |
---|
| 527 | ## render the graph |
---|
| 528 | #self.graph.render(self) |
---|
| 529 | #self.subplot.figure.canvas.draw_idle() |
---|
| 530 | print "PARENT: ", self.parent |
---|
| 531 | wx.PostEvent(self.parent, |
---|
| 532 | NewColorEvent(color=selected_plot.custom_color, |
---|
| 533 | id=selected_plot.id)) |
---|
| 534 | |
---|
| 535 | def onChangeSize(self, event): |
---|
| 536 | |
---|
| 537 | menu = event.GetEventObject() |
---|
| 538 | id = event.GetId() |
---|
| 539 | label = menu.GetLabel(id) |
---|
| 540 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 541 | |
---|
| 542 | if label == "&Custom": |
---|
| 543 | sizedial = SizeDialog(None, -1, 'Change Marker Size') |
---|
| 544 | if sizedial.ShowModal() == wx.ID_OK: |
---|
| 545 | label = sizedial.getText() |
---|
| 546 | sizedial.Destroy() |
---|
| 547 | |
---|
| 548 | selected_plot.marker_size = int(label) |
---|
| 549 | self._onEVT_FUNC_PROPERTY() |
---|
| 550 | ## Set the view scale for all plots |
---|
[e6a93df] | 551 | |
---|
[2636188] | 552 | ## render the graph |
---|
| 553 | #self.graph.render(self) |
---|
| 554 | #self.subplot.figure.canvas.draw_idle() |
---|
| 555 | |
---|
| 556 | |
---|
[a07e72f] | 557 | def onChangeSymbol(self, event): |
---|
[6c0568b] | 558 | """ |
---|
| 559 | """ |
---|
[a07e72f] | 560 | menu = event.GetEventObject() |
---|
| 561 | id = event.GetId() |
---|
| 562 | label = menu.GetLabel(id) |
---|
| 563 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 564 | selected_plot.symbol = self._symbol_labels[label] |
---|
| 565 | ## Set the view scale for all plots |
---|
| 566 | self._onEVT_FUNC_PROPERTY() |
---|
| 567 | ## render the graph |
---|
[6d727ae] | 568 | #self.graph.render(self) |
---|
| 569 | #self.subplot.figure.canvas.draw_idle() |
---|
[c81140c] | 570 | |
---|
[2636188] | 571 | |
---|
| 572 | |
---|
[42d27f2] | 573 | def _onsaveTXT(self, path): |
---|
| 574 | """ |
---|
[d955bf19] | 575 | Save file as txt |
---|
[1abcb04] | 576 | |
---|
[d955bf19] | 577 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
| 578 | |
---|
[42d27f2] | 579 | """ |
---|
| 580 | data = self.plots[self.graph.selected_plottable] |
---|
| 581 | |
---|
| 582 | if not path == None: |
---|
| 583 | out = open(path, 'w') |
---|
| 584 | has_errors = True |
---|
[32c0841] | 585 | if data.dy == None or data.dy == []: |
---|
[42d27f2] | 586 | has_errors = False |
---|
| 587 | # Sanity check |
---|
| 588 | if has_errors: |
---|
| 589 | try: |
---|
| 590 | if len(data.y) != len(data.dy): |
---|
| 591 | has_errors = False |
---|
| 592 | except: |
---|
| 593 | has_errors = False |
---|
| 594 | if has_errors: |
---|
[6d727ae] | 595 | if data.dx != None: |
---|
| 596 | out.write("<X> <Y> <dY> <dX>\n") |
---|
| 597 | else: |
---|
| 598 | out.write("<X> <Y> <dY>\n") |
---|
[42d27f2] | 599 | else: |
---|
| 600 | out.write("<X> <Y>\n") |
---|
| 601 | |
---|
| 602 | for i in range(len(data.x)): |
---|
| 603 | if has_errors: |
---|
[6d727ae] | 604 | if data.dx != None: |
---|
| 605 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
| 606 | data.y[i], |
---|
| 607 | data.dy[i], |
---|
| 608 | data.dx[i])) |
---|
| 609 | else: |
---|
| 610 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 611 | data.y[i], |
---|
| 612 | data.dy[i])) |
---|
[42d27f2] | 613 | else: |
---|
| 614 | out.write("%g %g\n" % (data.x[i], |
---|
| 615 | data.y[i])) |
---|
| 616 | out.close() |
---|
[6063b16] | 617 | try: |
---|
| 618 | self._default_save_location = os.path.dirname(path) |
---|
| 619 | except: |
---|
| 620 | pass |
---|
[8bd764d] | 621 | |
---|
[1bf33c1] | 622 | def _onSave(self, evt): |
---|
| 623 | """ |
---|
[d955bf19] | 624 | Save a data set to a text file |
---|
| 625 | |
---|
| 626 | :param evt: Menu event |
---|
| 627 | |
---|
[1bf33c1] | 628 | """ |
---|
[a07e72f] | 629 | |
---|
| 630 | path = None |
---|
| 631 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
| 632 | "CanSAS 1D files(*.xml)|*.xml" |
---|
| 633 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 634 | self._default_save_location, |
---|
| 635 | "", wildcard , wx.SAVE) |
---|
| 636 | |
---|
| 637 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 638 | path = dlg.GetPath() |
---|
[ad1e49c] | 639 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
| 640 | # This is MAC Fix |
---|
| 641 | ext_num = dlg.GetFilterIndex() |
---|
| 642 | if ext_num == 0: |
---|
| 643 | format = '.txt' |
---|
| 644 | else: |
---|
| 645 | format = '.xml' |
---|
| 646 | path = os.path.splitext(path)[0] + format |
---|
[a07e72f] | 647 | mypath = os.path.basename(path) |
---|
[1bf33c1] | 648 | |
---|
[a07e72f] | 649 | #TODO: This is bad design. The DataLoader is designed |
---|
| 650 | #to recognize extensions. |
---|
| 651 | # It should be a simple matter of calling the . |
---|
| 652 | #save(file, data, '.xml') method |
---|
[fe857e2] | 653 | # of the sans.dataloader.loader.Loader class. |
---|
| 654 | from sans.dataloader.loader import Loader |
---|
[a07e72f] | 655 | #Instantiate a loader |
---|
| 656 | loader = Loader() |
---|
| 657 | data = self.plots[self.graph.selected_plottable] |
---|
| 658 | format = ".txt" |
---|
| 659 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 660 | # Make sure the ext included in the file name |
---|
| 661 | # especially on MAC |
---|
| 662 | fName = os.path.splitext(path)[0] + format |
---|
| 663 | self._onsaveTXT(fName) |
---|
[a07e72f] | 664 | format = ".xml" |
---|
| 665 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 666 | # Make sure the ext included in the file name |
---|
| 667 | # especially on MAC |
---|
| 668 | fName = os.path.splitext(path)[0] + format |
---|
| 669 | loader.save(fName, data, format) |
---|
[a07e72f] | 670 | try: |
---|
| 671 | self._default_save_location = os.path.dirname(path) |
---|
| 672 | except: |
---|
| 673 | pass |
---|
| 674 | dlg.Destroy() |
---|
[c5a769e] | 675 | |
---|
| 676 | def _add_more_tool(self): |
---|
| 677 | """ |
---|
[6d52f21d] | 678 | Add refresh, add/delete button in the tool bar |
---|
[c5a769e] | 679 | """ |
---|
| 680 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
| 681 | return |
---|
| 682 | self.toolbar.AddSeparator() |
---|
| 683 | id_delete = wx.NewId() |
---|
| 684 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
| 685 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
[fb40dbb] | 686 | 'Delete', 'permanently Delete') |
---|
[c5a769e] | 687 | |
---|
| 688 | self.toolbar.Realize() |
---|
| 689 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
[6d52f21d] | 690 | |
---|
[dc51a7f] | 691 | """ |
---|
[6d52f21d] | 692 | self.toolbar.AddSeparator() |
---|
| 693 | id_text = wx.NewId() |
---|
| 694 | text = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) |
---|
| 695 | self.toolbar.AddSimpleTool(id_text, text, |
---|
| 696 | 'Remove Text from Plot', 'Removes text from plot') |
---|
| 697 | |
---|
| 698 | self.toolbar.Realize() |
---|
| 699 | wx.EVT_TOOL(self, id_text, self._on_removetext) |
---|
[886657f] | 700 | """ |
---|
[c5a769e] | 701 | def _on_delete(self, event): |
---|
| 702 | """ |
---|
| 703 | Refreshes the plotpanel on refresh tollbar button |
---|
| 704 | """ |
---|
| 705 | |
---|
| 706 | if self.parent is not None: |
---|
| 707 | wx.PostEvent(self.parent, |
---|
| 708 | NewPlotEvent(group_id=self.group_id, |
---|
| 709 | action="delete")) |
---|
[6d52f21d] | 710 | |
---|
[c5a769e] | 711 | |
---|