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