[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 |
---|
[a4964b8e] | 250 | try: |
---|
| 251 | self._onEVT_FUNC_PROPERTY() |
---|
| 252 | except: |
---|
| 253 | msg=" Encountered sigular points..." |
---|
| 254 | wx.PostEvent(self, StatusEvent(status=\ |
---|
| 255 | "Plotting Erorr: %s"%msg)) |
---|
[7022fdc] | 256 | # Check if zoomed |
---|
| 257 | toolbar_zoomed = self.toolbar.GetToolEnabled(self.toolbar._NTB2_BACK) |
---|
| 258 | if self.is_zoomed or toolbar_zoomed: |
---|
| 259 | # Recover the x,y limits |
---|
| 260 | self.subplot.set_xlim((xlo, xhi)) |
---|
| 261 | self.subplot.set_ylim((ylo, yhi)) |
---|
[53cf669] | 262 | # Update Graph menu and help string |
---|
| 263 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
| 264 | helpString = 'Show/Hide Graph: ' |
---|
| 265 | for plot in self.plots.itervalues(): |
---|
[d74fbff] | 266 | helpString += (' ' + str(plot.label) +';') |
---|
[53cf669] | 267 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
[a07e72f] | 268 | else: |
---|
| 269 | self.plots[data.id] = data |
---|
| 270 | self.graph.add(self.plots[data.id]) |
---|
[e4a703a] | 271 | ## Set the view scale for all plots |
---|
[a4964b8e] | 272 | try: |
---|
| 273 | self._onEVT_FUNC_PROPERTY() |
---|
| 274 | except: |
---|
| 275 | msg=" Encountered sigular points..." |
---|
| 276 | wx.PostEvent(self, StatusEvent(status=\ |
---|
| 277 | "Plotting Erorr: %s"%msg)) |
---|
[fe48fcc] | 278 | self.toolbar.update() |
---|
[7022fdc] | 279 | if self.is_zoomed: |
---|
| 280 | self.is_zoomed = False |
---|
[5eede4e] | 281 | |
---|
[e4a703a] | 282 | |
---|
[6d727ae] | 283 | def draw_plot(self): |
---|
| 284 | """ |
---|
| 285 | Draw plot |
---|
| 286 | """ |
---|
| 287 | self.draw() |
---|
| 288 | |
---|
[a07e72f] | 289 | |
---|
| 290 | |
---|
[1bf33c1] | 291 | def onLeftDown(self,event): |
---|
[6c0568b] | 292 | """ |
---|
[d955bf19] | 293 | left button down and ready to drag |
---|
| 294 | Display the position of the mouse on the statusbar |
---|
[6c0568b] | 295 | """ |
---|
[e85e2bc] | 296 | PlotPanel.onLeftDown(self, event) |
---|
| 297 | ax = event.inaxes |
---|
| 298 | if ax != None: |
---|
[902f373] | 299 | try: |
---|
| 300 | pos_x = float(event.xdata)# / size_x |
---|
| 301 | pos_y = float(event.ydata)# / size_y |
---|
| 302 | pos_x = "%8.3g"% pos_x |
---|
| 303 | pos_y = "%8.3g"% pos_y |
---|
| 304 | self.position = str(pos_x), str(pos_y) |
---|
| 305 | wx.PostEvent(self.parent, StatusEvent(status=self.position)) |
---|
| 306 | except: |
---|
| 307 | self.position = None |
---|
[0275276] | 308 | # unfocus all |
---|
| 309 | self.parent.set_plot_unfocus() |
---|
[4ed210f4] | 310 | #post nd event to notify guiframe that this panel is on focus |
---|
[a45037aa] | 311 | wx.PostEvent(self.parent, PanelOnFocusEvent(panel=self)) |
---|
[df68da1] | 312 | |
---|
[4ed210f4] | 313 | |
---|
[a07e72f] | 314 | def _ontoggle_hide_error(self, event): |
---|
| 315 | """ |
---|
| 316 | Toggle error display to hide or show |
---|
| 317 | """ |
---|
[ebf422a] | 318 | # Check zoom |
---|
| 319 | xlo, xhi = self.subplot.get_xlim() |
---|
| 320 | ylo, yhi = self.subplot.get_ylim() |
---|
| 321 | |
---|
[a07e72f] | 322 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
[5637362] | 323 | if self.hide_menu.GetText() == "Hide Error Bar": |
---|
[a07e72f] | 324 | selected_plot.hide_error = True |
---|
| 325 | else: |
---|
| 326 | selected_plot.hide_error = False |
---|
| 327 | ## increment graph color |
---|
| 328 | self.graph.render(self) |
---|
| 329 | self.subplot.figure.canvas.draw_idle() |
---|
[ebf422a] | 330 | # Check if zoomed |
---|
| 331 | toolbar_zoomed = self.toolbar.GetToolEnabled(self.toolbar._NTB2_BACK) |
---|
| 332 | if self.is_zoomed or toolbar_zoomed: |
---|
| 333 | # Recover the x,y limits |
---|
| 334 | self.subplot.set_xlim((xlo, xhi)) |
---|
| 335 | self.subplot.set_ylim((ylo, yhi)) |
---|
| 336 | |
---|
[a07e72f] | 337 | |
---|
[1bf33c1] | 338 | def _onRemove(self, event): |
---|
| 339 | """ |
---|
[d955bf19] | 340 | Remove a plottable from the graph and render the graph |
---|
| 341 | |
---|
| 342 | :param event: Menu event |
---|
| 343 | |
---|
[1bf33c1] | 344 | """ |
---|
[6c0568b] | 345 | ## Check if there is a selected graph to remove |
---|
[a07e72f] | 346 | if self.graph.selected_plottable in self.plots.keys(): |
---|
| 347 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 348 | id = self.graph.selected_plottable |
---|
[df22224] | 349 | self.remove_data_by_id(id) |
---|
| 350 | |
---|
[1bf33c1] | 351 | def onContextMenu(self, event): |
---|
| 352 | """ |
---|
[d955bf19] | 353 | 1D plot context menu |
---|
| 354 | |
---|
| 355 | :param event: wx context event |
---|
| 356 | |
---|
[1bf33c1] | 357 | """ |
---|
[52b8b74] | 358 | self._slicerpop = PanelMenu() |
---|
| 359 | self._slicerpop.set_plots(self.plots) |
---|
| 360 | self._slicerpop.set_graph(self.graph) |
---|
[9a585d0] | 361 | # Various plot options |
---|
| 362 | id = wx.NewId() |
---|
[6d727ae] | 363 | self._slicerpop.Append(id, '&Save Image', 'Save image as PNG') |
---|
[9a585d0] | 364 | wx.EVT_MENU(self, id, self.onSaveImage) |
---|
| 365 | id = wx.NewId() |
---|
[6d727ae] | 366 | self._slicerpop.Append(id, '&Print Image', 'Print image ') |
---|
[18eba35] | 367 | wx.EVT_MENU(self, id, self.onPrint) |
---|
| 368 | id = wx.NewId() |
---|
[6d727ae] | 369 | self._slicerpop.Append(id, '&Print Preview', 'Print preview') |
---|
[18eba35] | 370 | wx.EVT_MENU(self, id, self.onPrinterPreview) |
---|
[52b8b74] | 371 | |
---|
[6d727ae] | 372 | id = wx.NewId() |
---|
| 373 | self._slicerpop.Append(id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
| 374 | wx.EVT_MENU(self, id, self.OnCopyFigureMenu) |
---|
[e1714a9] | 375 | |
---|
[6d727ae] | 376 | self._slicerpop.AppendSeparator() |
---|
| 377 | |
---|
[52b8b74] | 378 | #add menu of other plugins |
---|
[a07e72f] | 379 | item_list = self.parent.get_context_menu(self) |
---|
[6d727ae] | 380 | |
---|
[32c0841] | 381 | if (not item_list == None) and (not len(item_list) == 0): |
---|
[9a585d0] | 382 | for item in item_list: |
---|
| 383 | try: |
---|
| 384 | id = wx.NewId() |
---|
[52b8b74] | 385 | self._slicerpop.Append(id, item[0], item[1]) |
---|
[9a585d0] | 386 | wx.EVT_MENU(self, id, item[2]) |
---|
| 387 | except: |
---|
[32c0841] | 388 | msg = "ModelPanel1D.onContextMenu: " |
---|
[a07e72f] | 389 | msg += "bad menu item %s" % sys.exc_value |
---|
[32c0841] | 390 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[9a585d0] | 391 | pass |
---|
[52b8b74] | 392 | self._slicerpop.AppendSeparator() |
---|
[6d727ae] | 393 | #id = wx.NewId() |
---|
| 394 | #self._slicerpop.Append(id, '&Print image', 'Print image') |
---|
[1bf33c1] | 395 | if self.graph.selected_plottable in self.plots: |
---|
| 396 | plot = self.plots[self.graph.selected_plottable] |
---|
[a436b2e] | 397 | |
---|
[6d727ae] | 398 | id = wx.NewId() |
---|
| 399 | name = plot.name |
---|
| 400 | self._slicerpop.Append(id, "&Save Points as a File") |
---|
[2d443fd] | 401 | wx.EVT_MENU(self, id, self._onSave) |
---|
[a436b2e] | 402 | self._slicerpop.AppendSeparator() |
---|
[73eb92fc] | 403 | if self.parent.ClassName.count('wxDialog') == 0: |
---|
[e6a93df] | 404 | id = wx.NewId() |
---|
[a436b2e] | 405 | self._slicerpop.Append(id, '&Linear Fit') |
---|
| 406 | wx.EVT_MENU(self, id, self.onFitting) |
---|
| 407 | self._slicerpop.AppendSeparator() |
---|
| 408 | |
---|
[a3c96f7a] | 409 | id = wx.NewId() |
---|
[a436b2e] | 410 | self._slicerpop.Append(id, "Remove %s Curve" % name) |
---|
| 411 | wx.EVT_MENU(self, id, self._onRemove) |
---|
| 412 | if not plot.is_data: |
---|
| 413 | id = wx.NewId() |
---|
| 414 | self._slicerpop.Append(id, '&Freeze', 'Freeze') |
---|
| 415 | wx.EVT_MENU(self, id, self.onFreeze) |
---|
[e1714a9] | 416 | self._slicerpop.AppendSeparator() |
---|
[a436b2e] | 417 | symbol_menu = wx.Menu() |
---|
| 418 | for label in self._symbol_labels: |
---|
| 419 | id = wx.NewId() |
---|
| 420 | symbol_menu.Append(id, str(label), str(label)) |
---|
| 421 | wx.EVT_MENU(self, id, self.onChangeSymbol) |
---|
| 422 | id = wx.NewId() |
---|
| 423 | self._slicerpop.AppendMenu(id,'&Modify Symbol', symbol_menu) |
---|
[2636188] | 424 | |
---|
| 425 | color_menu = wx.Menu() |
---|
| 426 | for label in self._color_labels: |
---|
| 427 | id = wx.NewId() |
---|
| 428 | color_menu.Append(id, str(label), str(label)) |
---|
| 429 | wx.EVT_MENU(self, id, self.onChangeColor) |
---|
| 430 | id = wx.NewId() |
---|
| 431 | self._slicerpop.AppendMenu(id, '&Modify Symbol Color', color_menu) |
---|
| 432 | |
---|
| 433 | size_menu = wx.Menu() |
---|
| 434 | for i in range(10): |
---|
| 435 | id = wx.NewId() |
---|
| 436 | size_menu.Append(id, str(i), str(i)) |
---|
| 437 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
| 438 | id = wx.NewId() |
---|
| 439 | size_menu.Append(id, '&Custom', 'Custom') |
---|
| 440 | wx.EVT_MENU(self, id, self.onChangeSize) |
---|
| 441 | id = wx.NewId() |
---|
| 442 | self._slicerpop.AppendMenu(id, '&Modify Symbol Size', size_menu) |
---|
| 443 | |
---|
[5637362] | 444 | self.hide_menu = self._slicerpop.Append(id, "Hide Error Bar") |
---|
[a436b2e] | 445 | |
---|
| 446 | if plot.dy is not None and plot.dy != []: |
---|
| 447 | if plot.hide_error : |
---|
[5637362] | 448 | self.hide_menu.SetText('Show Error Bar') |
---|
[a436b2e] | 449 | else: |
---|
[5637362] | 450 | self.hide_menu.SetText('Hide Error Bar') |
---|
[a07e72f] | 451 | else: |
---|
[a436b2e] | 452 | self.hide_menu.Enable(False) |
---|
| 453 | wx.EVT_MENU(self, id, self._ontoggle_hide_error) |
---|
| 454 | |
---|
| 455 | self._slicerpop.AppendSeparator() |
---|
[e1714a9] | 456 | id = wx.NewId() |
---|
| 457 | self._slicerpop.Append(id, '&Edit Legend Label', 'Edit Legend Label') |
---|
| 458 | wx.EVT_MENU(self, id, self.onEditLabels) |
---|
[a436b2e] | 459 | # Option to hide |
---|
| 460 | #TODO: implement functionality to hide a plottable (legend click) |
---|
[e1714a9] | 461 | |
---|
| 462 | loc_menu = wx.Menu() |
---|
| 463 | for label in self._loc_labels: |
---|
| 464 | id = wx.NewId() |
---|
| 465 | loc_menu.Append(id, str(label), str(label)) |
---|
| 466 | wx.EVT_MENU(self, id, self.onChangeLegendLoc) |
---|
| 467 | id = wx.NewId() |
---|
| 468 | self._slicerpop.AppendMenu(id, '&Modify Legend Location', loc_menu) |
---|
[df22224] | 469 | |
---|
[e1714a9] | 470 | id = wx.NewId() |
---|
| 471 | self._slicerpop.Append(id, '&Toggle Legend On/Off', 'Toggle Legend On/Off') |
---|
| 472 | wx.EVT_MENU(self, id, self.onLegend) |
---|
| 473 | self._slicerpop.AppendSeparator() |
---|
[a07e72f] | 474 | |
---|
[857d00f] | 475 | id = wx.NewId() |
---|
[5637362] | 476 | self._slicerpop.Append(id, '&Edit Y Axis Label') |
---|
[857d00f] | 477 | wx.EVT_MENU(self, id, self._on_yaxis_label) |
---|
| 478 | id = wx.NewId() |
---|
[5637362] | 479 | self._slicerpop.Append(id, '&Edit X Axis Label') |
---|
[857d00f] | 480 | wx.EVT_MENU(self, id, self._on_xaxis_label) |
---|
[5637362] | 481 | |
---|
| 482 | id = wx.NewId() |
---|
| 483 | self._slicerpop.Append(id, '&Toggle Grid On/Off', 'Toggle Grid On/Off') |
---|
| 484 | wx.EVT_MENU(self, id, self.onGridOnOff) |
---|
[857d00f] | 485 | self._slicerpop.AppendSeparator() |
---|
| 486 | |
---|
[ef4a4ea] | 487 | if self.position != None: |
---|
| 488 | id = wx.NewId() |
---|
[c17760d] | 489 | self._slicerpop.Append(id, '&Add Text') |
---|
[ef4a4ea] | 490 | wx.EVT_MENU(self, id, self._on_addtext) |
---|
| 491 | id = wx.NewId() |
---|
[c17760d] | 492 | self._slicerpop.Append(id, '&Remove Text') |
---|
[ef4a4ea] | 493 | wx.EVT_MENU(self, id, self._on_removetext) |
---|
[857d00f] | 494 | self._slicerpop.AppendSeparator() |
---|
[886657f] | 495 | id = wx.NewId() |
---|
[c17760d] | 496 | self._slicerpop.Append(id, '&Change Scale') |
---|
[1bf33c1] | 497 | wx.EVT_MENU(self, id, self._onProperties) |
---|
[857d00f] | 498 | self._slicerpop.AppendSeparator() |
---|
[1bf33c1] | 499 | id = wx.NewId() |
---|
[2259920] | 500 | self._slicerpop.Append(id, '&Reset Graph Range') |
---|
[d468daa] | 501 | wx.EVT_MENU(self, id, self.onResetGraph) |
---|
[dc51a7f] | 502 | try: |
---|
| 503 | pos_evt = event.GetPosition() |
---|
| 504 | pos = self.ScreenToClient(pos_evt) |
---|
| 505 | except: |
---|
| 506 | pos_x, pos_y = self.toolbar.GetPositionTuple() |
---|
| 507 | pos = (pos_x, pos_y + 5) |
---|
[fba201a0] | 508 | |
---|
| 509 | if self.parent.ClassName.count('wxDialog') == 0: |
---|
| 510 | self._slicerpop.AppendSeparator() |
---|
| 511 | id = wx.NewId() |
---|
| 512 | self._slicerpop.Append(id, '&Window Title') |
---|
| 513 | wx.EVT_MENU(self, id, self.onChangeCaption) |
---|
[37c36d9] | 514 | |
---|
[52b8b74] | 515 | self.PopupMenu(self._slicerpop, pos) |
---|
[dc51a7f] | 516 | |
---|
[e6a93df] | 517 | def onFreeze(self, event): |
---|
| 518 | """ |
---|
| 519 | """ |
---|
| 520 | plot = self.plots[self.graph.selected_plottable] |
---|
| 521 | self.parent.onfreeze([plot.id]) |
---|
[d7d1255e] | 522 | |
---|
[6d52f21d] | 523 | def onEditLabels(self, event): |
---|
| 524 | """ |
---|
[d7d1255e] | 525 | Edit legend label |
---|
[6d52f21d] | 526 | """ |
---|
| 527 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
[d7d1255e] | 528 | label = selected_plot.label |
---|
| 529 | dial = LabelDialog(None, -1, 'Change Legend Label', label) |
---|
[6d52f21d] | 530 | if dial.ShowModal() == wx.ID_OK: |
---|
[d7d1255e] | 531 | newLabel = dial.getText() |
---|
| 532 | selected_plot.label = newLabel |
---|
[53cf669] | 533 | # Updata Graph menu help string |
---|
| 534 | pos = self.parent._window_menu.FindItem(self.window_caption) |
---|
| 535 | helpString = 'Show/Hide Graph: ' |
---|
| 536 | for plot in self.plots.itervalues(): |
---|
[e2160ab] | 537 | helpString += (' ' + str(plot.label) +';') |
---|
[53cf669] | 538 | self.parent._window_menu.SetHelpString(pos, helpString) |
---|
[36288ca] | 539 | self._is_changed_legend_label = True |
---|
[53cf669] | 540 | #break |
---|
[6d52f21d] | 541 | dial.Destroy() |
---|
[53cf669] | 542 | |
---|
[d7d1255e] | 543 | ## render the graph |
---|
[a4964b8e] | 544 | try: |
---|
| 545 | self._onEVT_FUNC_PROPERTY() |
---|
| 546 | except: |
---|
| 547 | msg=" Encountered sigular points..." |
---|
| 548 | wx.PostEvent(self, StatusEvent(status=\ |
---|
| 549 | "Plotting Erorr: %s"%msg)) |
---|
| 550 | |
---|
[2636188] | 551 | def onChangeColor(self, event): |
---|
| 552 | """ |
---|
| 553 | Changes the color of the graph when selected |
---|
| 554 | """ |
---|
| 555 | menu = event.GetEventObject() |
---|
| 556 | id = event.GetId() |
---|
| 557 | label = menu.GetLabel(id) |
---|
| 558 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 559 | selected_plot.custom_color = self._color_labels[label] |
---|
| 560 | ## Set the view scale for all plots |
---|
[e6c6b67] | 561 | self._check_zoom_plot() |
---|
| 562 | #self._onEVT_FUNC_PROPERTY() |
---|
| 563 | #wx.PostEvent(self.parent, |
---|
| 564 | # NewColorEvent(color=selected_plot.custom_color, |
---|
| 565 | # id=selected_plot.id)) |
---|
[2636188] | 566 | |
---|
| 567 | def onChangeSize(self, event): |
---|
| 568 | |
---|
| 569 | menu = event.GetEventObject() |
---|
| 570 | id = event.GetId() |
---|
| 571 | label = menu.GetLabel(id) |
---|
| 572 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 573 | |
---|
| 574 | if label == "&Custom": |
---|
| 575 | sizedial = SizeDialog(None, -1, 'Change Marker Size') |
---|
| 576 | if sizedial.ShowModal() == wx.ID_OK: |
---|
[30ccad1] | 577 | try: |
---|
| 578 | label = sizedial.getText() |
---|
| 579 | selected_plot.markersize = int(label) |
---|
| 580 | self._check_zoom_plot() |
---|
| 581 | except: |
---|
| 582 | msg = 'Symbol Size: Got an invalid Value.' |
---|
| 583 | wx.PostEvent( self.parent, |
---|
| 584 | StatusEvent(status= msg, info='error')) |
---|
[2636188] | 585 | sizedial.Destroy() |
---|
| 586 | |
---|
| 587 | |
---|
| 588 | |
---|
[a07e72f] | 589 | def onChangeSymbol(self, event): |
---|
[6c0568b] | 590 | """ |
---|
| 591 | """ |
---|
[a07e72f] | 592 | menu = event.GetEventObject() |
---|
| 593 | id = event.GetId() |
---|
| 594 | label = menu.GetLabel(id) |
---|
| 595 | selected_plot = self.plots[self.graph.selected_plottable] |
---|
| 596 | selected_plot.symbol = self._symbol_labels[label] |
---|
| 597 | ## Set the view scale for all plots |
---|
[e6c6b67] | 598 | self._check_zoom_plot() |
---|
| 599 | #self._onEVT_FUNC_PROPERTY() |
---|
[a07e72f] | 600 | ## render the graph |
---|
[6d727ae] | 601 | #self.graph.render(self) |
---|
| 602 | #self.subplot.figure.canvas.draw_idle() |
---|
[c81140c] | 603 | |
---|
[2636188] | 604 | |
---|
| 605 | |
---|
[42d27f2] | 606 | def _onsaveTXT(self, path): |
---|
| 607 | """ |
---|
[d955bf19] | 608 | Save file as txt |
---|
[1abcb04] | 609 | |
---|
[d955bf19] | 610 | :TODO: Refactor and remove this method. See TODO in _onSave. |
---|
| 611 | |
---|
[42d27f2] | 612 | """ |
---|
| 613 | data = self.plots[self.graph.selected_plottable] |
---|
| 614 | |
---|
| 615 | if not path == None: |
---|
| 616 | out = open(path, 'w') |
---|
| 617 | has_errors = True |
---|
[32c0841] | 618 | if data.dy == None or data.dy == []: |
---|
[42d27f2] | 619 | has_errors = False |
---|
| 620 | # Sanity check |
---|
| 621 | if has_errors: |
---|
| 622 | try: |
---|
| 623 | if len(data.y) != len(data.dy): |
---|
| 624 | has_errors = False |
---|
| 625 | except: |
---|
| 626 | has_errors = False |
---|
| 627 | if has_errors: |
---|
[6d727ae] | 628 | if data.dx != None: |
---|
| 629 | out.write("<X> <Y> <dY> <dX>\n") |
---|
| 630 | else: |
---|
| 631 | out.write("<X> <Y> <dY>\n") |
---|
[42d27f2] | 632 | else: |
---|
| 633 | out.write("<X> <Y>\n") |
---|
| 634 | |
---|
| 635 | for i in range(len(data.x)): |
---|
| 636 | if has_errors: |
---|
[6d727ae] | 637 | if data.dx != None: |
---|
| 638 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
| 639 | data.y[i], |
---|
| 640 | data.dy[i], |
---|
| 641 | data.dx[i])) |
---|
| 642 | else: |
---|
| 643 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 644 | data.y[i], |
---|
| 645 | data.dy[i])) |
---|
[42d27f2] | 646 | else: |
---|
| 647 | out.write("%g %g\n" % (data.x[i], |
---|
| 648 | data.y[i])) |
---|
| 649 | out.close() |
---|
[6063b16] | 650 | try: |
---|
| 651 | self._default_save_location = os.path.dirname(path) |
---|
[c553b18] | 652 | self.parent._default_save_location = self._default_save_location |
---|
[6063b16] | 653 | except: |
---|
| 654 | pass |
---|
[8bd764d] | 655 | |
---|
[1bf33c1] | 656 | def _onSave(self, evt): |
---|
| 657 | """ |
---|
[d955bf19] | 658 | Save a data set to a text file |
---|
| 659 | |
---|
| 660 | :param evt: Menu event |
---|
| 661 | |
---|
[1bf33c1] | 662 | """ |
---|
[a07e72f] | 663 | |
---|
| 664 | path = None |
---|
| 665 | wildcard = "Text files (*.txt)|*.txt|"\ |
---|
[c553b18] | 666 | "CanSAS 1D files(*.xml)|*.xml" |
---|
| 667 | default_name = self.plots[self.graph.selected_plottable].label |
---|
| 668 | if default_name.count('.') > 0: |
---|
| 669 | default_name = default_name.split('.')[0] |
---|
| 670 | default_name += "_out" |
---|
| 671 | if self.parent != None: |
---|
| 672 | self._default_save_location = self.parent._default_save_location |
---|
[a07e72f] | 673 | dlg = wx.FileDialog(self, "Choose a file", |
---|
| 674 | self._default_save_location, |
---|
[c553b18] | 675 | default_name, wildcard , wx.SAVE) |
---|
[a07e72f] | 676 | |
---|
| 677 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 678 | path = dlg.GetPath() |
---|
[ad1e49c] | 679 | # ext_num = 0 for .txt, ext_num = 1 for .xml |
---|
| 680 | # This is MAC Fix |
---|
| 681 | ext_num = dlg.GetFilterIndex() |
---|
| 682 | if ext_num == 0: |
---|
| 683 | format = '.txt' |
---|
| 684 | else: |
---|
| 685 | format = '.xml' |
---|
| 686 | path = os.path.splitext(path)[0] + format |
---|
[a07e72f] | 687 | mypath = os.path.basename(path) |
---|
[1bf33c1] | 688 | |
---|
[a07e72f] | 689 | #TODO: This is bad design. The DataLoader is designed |
---|
| 690 | #to recognize extensions. |
---|
| 691 | # It should be a simple matter of calling the . |
---|
| 692 | #save(file, data, '.xml') method |
---|
[fe857e2] | 693 | # of the sans.dataloader.loader.Loader class. |
---|
| 694 | from sans.dataloader.loader import Loader |
---|
[a07e72f] | 695 | #Instantiate a loader |
---|
| 696 | loader = Loader() |
---|
| 697 | data = self.plots[self.graph.selected_plottable] |
---|
| 698 | format = ".txt" |
---|
| 699 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 700 | # Make sure the ext included in the file name |
---|
| 701 | # especially on MAC |
---|
| 702 | fName = os.path.splitext(path)[0] + format |
---|
| 703 | self._onsaveTXT(fName) |
---|
[a07e72f] | 704 | format = ".xml" |
---|
| 705 | if os.path.splitext(mypath)[1].lower() == format: |
---|
[959981b] | 706 | # Make sure the ext included in the file name |
---|
| 707 | # especially on MAC |
---|
| 708 | fName = os.path.splitext(path)[0] + format |
---|
| 709 | loader.save(fName, data, format) |
---|
[a07e72f] | 710 | try: |
---|
| 711 | self._default_save_location = os.path.dirname(path) |
---|
[c553b18] | 712 | self.parent._default_save_location = self._default_save_location |
---|
[a07e72f] | 713 | except: |
---|
| 714 | pass |
---|
| 715 | dlg.Destroy() |
---|
[c5a769e] | 716 | |
---|
| 717 | def _add_more_tool(self): |
---|
| 718 | """ |
---|
[6d52f21d] | 719 | Add refresh, add/delete button in the tool bar |
---|
[c5a769e] | 720 | """ |
---|
| 721 | if self.parent.__class__.__name__ != 'ViewerFrame': |
---|
| 722 | return |
---|
| 723 | self.toolbar.AddSeparator() |
---|
| 724 | id_delete = wx.NewId() |
---|
[9149431] | 725 | delete = wx.ArtProvider.GetBitmap(wx.ART_DELETE, wx.ART_TOOLBAR) |
---|
[c5a769e] | 726 | self.toolbar.AddSimpleTool(id_delete, delete, |
---|
[fb40dbb] | 727 | 'Delete', 'permanently Delete') |
---|
[c5a769e] | 728 | |
---|
| 729 | self.toolbar.Realize() |
---|
| 730 | wx.EVT_TOOL(self, id_delete, self._on_delete) |
---|
[6d52f21d] | 731 | |
---|
[dc51a7f] | 732 | """ |
---|
[6d52f21d] | 733 | self.toolbar.AddSeparator() |
---|
| 734 | id_text = wx.NewId() |
---|
| 735 | text = wx.ArtProvider.GetBitmap(wx.ART_CUT, wx.ART_TOOLBAR) |
---|
| 736 | self.toolbar.AddSimpleTool(id_text, text, |
---|
| 737 | 'Remove Text from Plot', 'Removes text from plot') |
---|
| 738 | |
---|
| 739 | self.toolbar.Realize() |
---|
| 740 | wx.EVT_TOOL(self, id_text, self._on_removetext) |
---|
[886657f] | 741 | """ |
---|
[c5a769e] | 742 | def _on_delete(self, event): |
---|
| 743 | """ |
---|
| 744 | Refreshes the plotpanel on refresh tollbar button |
---|
| 745 | """ |
---|
| 746 | |
---|
| 747 | if self.parent is not None: |
---|
| 748 | wx.PostEvent(self.parent, |
---|
| 749 | NewPlotEvent(group_id=self.group_id, |
---|
| 750 | action="delete")) |
---|
[6d52f21d] | 751 | |
---|
[c5a769e] | 752 | |
---|