[959eb01] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | ################################################################################ |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 7 | #project funded by the US National Science Foundation. |
---|
| 8 | # |
---|
| 9 | #See the license text in license.txt |
---|
| 10 | # |
---|
| 11 | #copyright 2008, University of Tennessee |
---|
| 12 | ################################################################################ |
---|
| 13 | |
---|
| 14 | import sys |
---|
[2d9526d] | 15 | from copy import deepcopy |
---|
[5251ec6] | 16 | |
---|
| 17 | import wx |
---|
| 18 | |
---|
[959eb01] | 19 | from sas.sasgui.guiframe.events import EVT_NEW_PLOT |
---|
| 20 | from sas.sasgui.guiframe.events import EVT_PLOT_QRANGE |
---|
| 21 | from sas.sasgui.guiframe.events import EVT_PLOT_LIM |
---|
| 22 | from sas.sasgui.guiframe.events import DeletePlotPanelEvent |
---|
| 23 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
| 24 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 25 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
| 26 | from sas.sasgui.guiframe.gui_manager import MDIFrame |
---|
[5251ec6] | 27 | |
---|
[959eb01] | 28 | DEFAULT_MENU_ITEM_LABEL = "No graph available" |
---|
| 29 | DEFAULT_MENU_ITEM_ID = wx.NewId() |
---|
| 30 | |
---|
| 31 | IS_WIN = True |
---|
| 32 | if sys.platform.count("win32") == 0: |
---|
| 33 | if int(str(wx.__version__).split('.')[0]) == 2: |
---|
| 34 | if int(str(wx.__version__).split('.')[1]) < 9: |
---|
| 35 | IS_WIN = False |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | class Plugin(PluginBase): |
---|
| 39 | """ |
---|
| 40 | Plug-in class to be instantiated by the GUI manager |
---|
| 41 | """ |
---|
| 42 | |
---|
| 43 | def __init__(self): |
---|
| 44 | PluginBase.__init__(self, name="Plotting") |
---|
| 45 | |
---|
| 46 | ## Plot panels |
---|
| 47 | self.plot_panels = {} |
---|
| 48 | self._panel_on_focus = None |
---|
| 49 | self.menu_default_id = None |
---|
| 50 | # Plot menu |
---|
| 51 | self.menu = None |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | def set_panel_on_focus(self, panel): |
---|
| 55 | """ |
---|
| 56 | """ |
---|
| 57 | self._panel_on_focus = panel |
---|
| 58 | |
---|
| 59 | def is_always_active(self): |
---|
| 60 | """ |
---|
| 61 | return True is this plugin is always active even if the user is |
---|
| 62 | switching between perspectives |
---|
| 63 | """ |
---|
| 64 | return True |
---|
| 65 | |
---|
| 66 | def populate_menu(self, parent): |
---|
| 67 | """ |
---|
| 68 | Create a 'Plot' menu to list the panels |
---|
| 69 | available for displaying |
---|
| 70 | |
---|
| 71 | :param id: next available unique ID for wx events |
---|
| 72 | :param parent: parent window |
---|
| 73 | |
---|
| 74 | """ |
---|
| 75 | return [] |
---|
| 76 | |
---|
| 77 | def get_panels(self, parent): |
---|
| 78 | """ |
---|
| 79 | Create and return a list of panel objects |
---|
| 80 | """ |
---|
| 81 | ## Save a reference to the parent |
---|
| 82 | self.parent = parent |
---|
| 83 | # Connect to plotting events |
---|
| 84 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
| 85 | self.parent.Bind(EVT_PLOT_QRANGE, self._on_plot_qrange) |
---|
| 86 | self.parent.Bind(EVT_PLOT_LIM, self._on_plot_lim) |
---|
| 87 | # We have no initial panels for this plug-in |
---|
| 88 | return [] |
---|
| 89 | |
---|
| 90 | def _on_plot_qrange(self, event=None): |
---|
| 91 | """ |
---|
| 92 | On Qmin Qmax vertical line event |
---|
| 93 | """ |
---|
[235f514] | 94 | if event is None: |
---|
[959eb01] | 95 | return |
---|
[5251ec6] | 96 | if event.id in self.plot_panels: |
---|
[959eb01] | 97 | panel = self.plot_panels[event.id] |
---|
[5251ec6] | 98 | elif event.group_id in self.plot_panels: |
---|
[959eb01] | 99 | panel = self.plot_panels[event.group_id] |
---|
| 100 | else: |
---|
| 101 | return |
---|
| 102 | panel.on_plot_qrange(event) |
---|
| 103 | |
---|
| 104 | def _on_plot_lim(self, event=None): |
---|
[235f514] | 105 | if event is None: |
---|
[959eb01] | 106 | return |
---|
[5251ec6] | 107 | if event.id in self.plot_panels: |
---|
[959eb01] | 108 | panel = self.plot_panels[event.id] |
---|
[5251ec6] | 109 | elif event.group_id in self.plot_panels: |
---|
[959eb01] | 110 | panel = self.plot_panels[event.group_id] |
---|
| 111 | else: |
---|
| 112 | return |
---|
| 113 | if hasattr(event, 'xlim'): |
---|
| 114 | panel.subplot.set_xlim(event.xlim) |
---|
| 115 | if hasattr(event, 'ylim'): |
---|
| 116 | panel.subplot.set_ylim(event.ylim) |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | def _on_show_panel(self, event): |
---|
| 120 | """show plug-in panel""" |
---|
| 121 | pass |
---|
| 122 | |
---|
| 123 | def remove_plot(self, group_id, id): |
---|
| 124 | """ |
---|
| 125 | remove plot of ID = id from a panel of group ID =group_id |
---|
| 126 | """ |
---|
| 127 | |
---|
[5251ec6] | 128 | if group_id in self.plot_panels: |
---|
[959eb01] | 129 | panel = self.plot_panels[group_id] |
---|
| 130 | panel.remove_data_by_id(id=id) |
---|
| 131 | |
---|
| 132 | return True |
---|
| 133 | return False |
---|
| 134 | |
---|
| 135 | def clear_panel(self): |
---|
| 136 | """ |
---|
| 137 | Clear and Hide all plot panels, and remove them from menu |
---|
| 138 | """ |
---|
[5251ec6] | 139 | for group_id in self.plot_panels: |
---|
[959eb01] | 140 | self.clear_panel_by_id(group_id) |
---|
| 141 | self.plot_panels = {} |
---|
| 142 | |
---|
| 143 | def clear_panel_by_id(self, group_id): |
---|
| 144 | """ |
---|
| 145 | clear the graph |
---|
| 146 | """ |
---|
[5251ec6] | 147 | if group_id in self.plot_panels: |
---|
[959eb01] | 148 | panel = self.plot_panels[group_id] |
---|
| 149 | for plottable in panel.graph.plottables.keys(): |
---|
| 150 | self.remove_plot(group_id, plottable.id) |
---|
| 151 | panel.graph.reset() |
---|
| 152 | return True |
---|
| 153 | return False |
---|
| 154 | |
---|
| 155 | def hide_panel(self, group_id): |
---|
| 156 | """ |
---|
| 157 | hide panel with group ID = group_id |
---|
| 158 | """ |
---|
| 159 | # Not implemeted |
---|
| 160 | return False |
---|
| 161 | |
---|
| 162 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
| 163 | """ |
---|
| 164 | """ |
---|
| 165 | ## Set group ID if available |
---|
| 166 | ## Assign data properties to the new create panel |
---|
| 167 | new_panel.set_manager(self) |
---|
| 168 | new_panel.group_id = group_id |
---|
| 169 | if group_id not in data.list_group_id: |
---|
| 170 | data.list_group_id.append(group_id) |
---|
| 171 | if title is None: |
---|
| 172 | title = data.title |
---|
| 173 | new_panel.window_caption = title |
---|
| 174 | new_panel.window_name = data.title |
---|
| 175 | event_id = self.parent.popup_panel(new_panel) |
---|
| 176 | |
---|
| 177 | # Set UID to allow us to reference the panel later |
---|
| 178 | new_panel.uid = event_id |
---|
| 179 | # Ship the plottable to its panel |
---|
| 180 | wx.CallAfter(new_panel.plot_data, data) |
---|
| 181 | #new_panel.canvas.set_resizing(new_panel.resizing) |
---|
| 182 | self.plot_panels[new_panel.group_id] = new_panel |
---|
| 183 | |
---|
| 184 | def create_1d_panel(self, data, group_id): |
---|
| 185 | """ |
---|
| 186 | """ |
---|
| 187 | # Create a new plot panel if none was available |
---|
| 188 | if issubclass(data.__class__, Data1D): |
---|
[5251ec6] | 189 | from .Plotter1D import ModelPanel1D |
---|
[959eb01] | 190 | ## get the data representation label of the data to plot |
---|
| 191 | ## when even the user select "change scale" |
---|
| 192 | xtransform = data.xtransform |
---|
| 193 | ytransform = data.ytransform |
---|
| 194 | ## create a plotpanel for 1D Data |
---|
| 195 | win = MDIFrame(self.parent, None, 'None', (100, 200)) |
---|
| 196 | new_panel = ModelPanel1D(win, -1, xtransform=xtransform, |
---|
| 197 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
| 198 | win.set_panel(new_panel) |
---|
| 199 | win.Show(False) |
---|
| 200 | new_panel.frame = win |
---|
| 201 | #win.Show(True) |
---|
| 202 | return new_panel |
---|
| 203 | |
---|
| 204 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
[5251ec6] | 205 | raise ValueError(msg) |
---|
[959eb01] | 206 | |
---|
| 207 | def create_2d_panel(self, data, group_id): |
---|
| 208 | """ |
---|
| 209 | """ |
---|
| 210 | if issubclass(data.__class__, Data2D): |
---|
| 211 | ##Create a new plotpanel for 2D data |
---|
[5251ec6] | 212 | from .Plotter2D import ModelPanel2D |
---|
[959eb01] | 213 | scale = data.scale |
---|
| 214 | win = MDIFrame(self.parent, None, 'None', (200, 150)) |
---|
| 215 | win.Show(False) |
---|
| 216 | new_panel = ModelPanel2D(win, id=-1, |
---|
| 217 | data2d=data, scale=scale, |
---|
| 218 | style=wx.RAISED_BORDER) |
---|
| 219 | win.set_panel(new_panel) |
---|
| 220 | new_panel.frame = win |
---|
| 221 | return new_panel |
---|
| 222 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
[5251ec6] | 223 | raise ValueError(msg) |
---|
[959eb01] | 224 | |
---|
| 225 | def update_panel(self, data, panel): |
---|
| 226 | """ |
---|
| 227 | update the graph of a given panel |
---|
| 228 | """ |
---|
| 229 | # Check whether we already have a graph with the same units |
---|
| 230 | # as the plottable we just received. |
---|
| 231 | _, x_unit = data.get_xaxis() |
---|
| 232 | _, y_unit = data.get_yaxis() |
---|
| 233 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
| 234 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
| 235 | (x_unit != panel.graph.prop["xunit"]) and False |
---|
| 236 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
| 237 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
| 238 | (y_unit != panel.graph.prop["yunit"]) and False |
---|
| 239 | if flag_x and flag_y: |
---|
| 240 | msg = "Cannot add %s" % str(data.name) |
---|
| 241 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
| 242 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
[5251ec6] | 243 | raise ValueError(msg) |
---|
[959eb01] | 244 | else: |
---|
| 245 | if panel.group_id not in data.list_group_id: |
---|
| 246 | data.list_group_id.append(panel.group_id) |
---|
| 247 | wx.CallAfter(panel.plot_data, data) |
---|
| 248 | |
---|
| 249 | def delete_panel(self, group_id): |
---|
| 250 | """ |
---|
| 251 | """ |
---|
[5251ec6] | 252 | if group_id in self.plot_panels: |
---|
[959eb01] | 253 | panel = self.plot_panels[group_id] |
---|
| 254 | uid = panel.uid |
---|
| 255 | wx.PostEvent(self.parent, |
---|
| 256 | DeletePlotPanelEvent(name=panel.window_caption, |
---|
| 257 | caption=panel.window_caption)) |
---|
| 258 | del self.plot_panels[group_id] |
---|
[5251ec6] | 259 | if uid in self.parent.plot_panels: |
---|
[959eb01] | 260 | del self.parent.plot_panels[uid] |
---|
| 261 | panel.frame.Destroy() |
---|
| 262 | return True |
---|
| 263 | |
---|
| 264 | return False |
---|
| 265 | |
---|
| 266 | def _on_plot_event(self, event): |
---|
| 267 | """ |
---|
| 268 | A new plottable is being shipped to the plotting plug-in. |
---|
| 269 | Check whether we have a panel to put in on, or create |
---|
| 270 | a new one |
---|
| 271 | |
---|
| 272 | :param event: EVT_NEW_PLOT event |
---|
| 273 | |
---|
| 274 | """ |
---|
| 275 | action_check = False |
---|
| 276 | if hasattr(event, 'action'): |
---|
| 277 | action_string = event.action.lower().strip() |
---|
| 278 | if action_string == 'check': |
---|
| 279 | action_check = True |
---|
| 280 | else: |
---|
[2d9526d] | 281 | if action_string == 'update': |
---|
| 282 | # Update all existing plots of data with this ID |
---|
| 283 | for data in event.plots: |
---|
| 284 | for panel in self.plot_panels.values(): |
---|
[5251ec6] | 285 | if data.id in panel.plots: |
---|
[2d9526d] | 286 | plot_exists = True |
---|
| 287 | # Pass each panel it's own copy of the data |
---|
| 288 | # that's being updated, otherwise things like |
---|
| 289 | # colour and line thickness are unintentionally |
---|
| 290 | # synced across panels |
---|
| 291 | self.update_panel(deepcopy(data), panel) |
---|
| 292 | return |
---|
| 293 | |
---|
[959eb01] | 294 | group_id = event.group_id |
---|
[2d9526d] | 295 | if group_id in self.plot_panels: |
---|
[959eb01] | 296 | #remove data from panel |
---|
| 297 | if action_string == 'remove': |
---|
| 298 | return self.remove_plot(group_id, event.id) |
---|
| 299 | if action_string == 'hide': |
---|
| 300 | return self.hide_panel(group_id) |
---|
| 301 | if action_string == 'delete': |
---|
| 302 | panel = self.plot_panels[group_id] |
---|
| 303 | uid = panel.uid |
---|
| 304 | return self.parent.delete_panel(uid) |
---|
| 305 | if action_string == "clear": |
---|
| 306 | return self.clear_panel_by_id(group_id) |
---|
| 307 | |
---|
| 308 | if not hasattr(event, 'plot'): |
---|
| 309 | return |
---|
| 310 | title = None |
---|
| 311 | if hasattr(event, 'title'): |
---|
| 312 | title = 'Graph' #event.title |
---|
| 313 | data = event.plot |
---|
| 314 | group_id = data.group_id |
---|
[5251ec6] | 315 | if group_id in self.plot_panels: |
---|
[959eb01] | 316 | if action_check: |
---|
| 317 | # Check if the plot already exist. if it does, do nothing. |
---|
[5251ec6] | 318 | if data.id in self.plot_panels[group_id].plots: |
---|
[959eb01] | 319 | return |
---|
| 320 | #update a panel graph |
---|
| 321 | panel = self.plot_panels[group_id] |
---|
| 322 | self.update_panel(data, panel) |
---|
| 323 | else: |
---|
| 324 | #create a new panel |
---|
| 325 | if issubclass(data.__class__, Data1D): |
---|
| 326 | new_panel = self.create_1d_panel(data, group_id) |
---|
| 327 | else: |
---|
| 328 | # Need to make the group_id consistent with 1D thus no if below |
---|
| 329 | if len(self.plot_panels.values()) > 0: |
---|
| 330 | for p_group_id in self.plot_panels.keys(): |
---|
| 331 | p_plot = self.plot_panels[p_group_id] |
---|
[5251ec6] | 332 | if data.id in p_plot.plots: |
---|
[959eb01] | 333 | p_plot.plots[data.id] = data |
---|
| 334 | self.plot_panels[group_id] = p_plot |
---|
| 335 | if group_id != p_group_id: |
---|
| 336 | del self.plot_panels[p_group_id] |
---|
| 337 | if p_group_id in data.list_group_id: |
---|
| 338 | data.list_group_id.remove(p_group_id) |
---|
| 339 | if group_id not in data.list_group_id: |
---|
| 340 | data.list_group_id.append(group_id) |
---|
| 341 | p_plot.group_id = group_id |
---|
| 342 | return |
---|
| 343 | |
---|
| 344 | new_panel = self.create_2d_panel(data, group_id) |
---|
| 345 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
| 346 | if hasattr(event, 'xlim'): |
---|
| 347 | new_panel.subplot.set_xlim(event.xlim) |
---|
| 348 | if hasattr(event, 'ylim'): |
---|
| 349 | new_panel.subplot.set_ylim(event.ylim) |
---|
| 350 | return |
---|