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