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