[41d466f] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[d955bf19] | 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 | ################################################################################ |
---|
[41d466f] | 13 | |
---|
| 14 | import wx |
---|
| 15 | import sys |
---|
[f444b20] | 16 | from sans.guiframe.events import EVT_NEW_PLOT |
---|
| 17 | from sans.guiframe.events import StatusEvent |
---|
[13a63ab] | 18 | from sans.guiframe.events import DeletePlotPanelEvent |
---|
[7f84e22] | 19 | from sans.guiframe.plugin_base import PluginBase |
---|
[a07e72f] | 20 | from sans.guiframe.dataFitting import Data1D |
---|
| 21 | from sans.guiframe.dataFitting import Data2D |
---|
[7764b41] | 22 | |
---|
[52f3c98] | 23 | DEFAULT_MENU_ITEM_LABEL = "No graph available" |
---|
[00b76931] | 24 | DEFAULT_MENU_ITEM_ID = wx.NewId() |
---|
| 25 | |
---|
[846c724] | 26 | IS_WIN = True |
---|
| 27 | if sys.platform.count("win32")==0: |
---|
| 28 | if wx.__version__.split('.')[0] == 2: |
---|
| 29 | if wx.__version__.split('.')[1] < 9: |
---|
| 30 | IS_WIN = False |
---|
| 31 | |
---|
[4520830] | 32 | |
---|
[7f84e22] | 33 | class Plugin(PluginBase): |
---|
[41d466f] | 34 | """ |
---|
[d955bf19] | 35 | Plug-in class to be instantiated by the GUI manager |
---|
[41d466f] | 36 | """ |
---|
| 37 | |
---|
[7f84e22] | 38 | def __init__(self, standalone=False): |
---|
| 39 | PluginBase.__init__(self, name="Plotting", standalone=standalone) |
---|
| 40 | |
---|
[41d466f] | 41 | ## Plot panels |
---|
[ae83ad3] | 42 | self.plot_panels = {} |
---|
[a07e72f] | 43 | self._panel_on_focus = None |
---|
[00b76931] | 44 | self.menu_default_id = None |
---|
[6d727ae] | 45 | # Plot menu |
---|
| 46 | self.menu = None |
---|
| 47 | |
---|
[a07e72f] | 48 | |
---|
| 49 | def set_panel_on_focus(self, panel): |
---|
| 50 | """ |
---|
| 51 | """ |
---|
| 52 | self._panel_on_focus = panel |
---|
| 53 | |
---|
[b7c7a1c] | 54 | def is_always_active(self): |
---|
| 55 | """ |
---|
| 56 | return True is this plugin is always active even if the user is |
---|
| 57 | switching between perspectives |
---|
| 58 | """ |
---|
| 59 | return True |
---|
| 60 | |
---|
[bf4402c3] | 61 | def populate_menu(self, parent): |
---|
[41d466f] | 62 | """ |
---|
[d955bf19] | 63 | Create a 'Plot' menu to list the panels |
---|
| 64 | available for displaying |
---|
| 65 | |
---|
| 66 | :param id: next available unique ID for wx events |
---|
| 67 | :param parent: parent window |
---|
| 68 | |
---|
[41d466f] | 69 | """ |
---|
| 70 | self.menu = wx.Menu() |
---|
[00b76931] | 71 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
[6d727ae] | 72 | "No graph available") |
---|
[2fc9243] | 73 | self.menu.FindItemByPosition(0).Enable(False) |
---|
[52f3c98] | 74 | return [(self.menu, "Show")] |
---|
[41d466f] | 75 | |
---|
| 76 | def get_panels(self, parent): |
---|
| 77 | """ |
---|
[d955bf19] | 78 | Create and return a list of panel objects |
---|
[41d466f] | 79 | """ |
---|
| 80 | ## Save a reference to the parent |
---|
| 81 | self.parent = parent |
---|
| 82 | # Connect to plotting events |
---|
| 83 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
| 84 | # We have no initial panels for this plug-in |
---|
| 85 | return [] |
---|
[54cc36a] | 86 | |
---|
[41d466f] | 87 | def _on_show_panel(self, event): |
---|
[6c0568b] | 88 | """show plug-in panel""" |
---|
| 89 | pass |
---|
| 90 | |
---|
[ae83ad3] | 91 | def remove_plot(self, group_id, id): |
---|
| 92 | """ |
---|
| 93 | remove plot of ID = id from a panel of group ID =group_id |
---|
| 94 | """ |
---|
| 95 | |
---|
| 96 | if group_id in self.plot_panels.keys(): |
---|
| 97 | panel = self.plot_panels[group_id] |
---|
| 98 | panel.remove_data_by_id(id=id) |
---|
| 99 | return True |
---|
[783940c] | 100 | return False |
---|
| 101 | |
---|
[957723f] | 102 | def clear_panel(self): |
---|
| 103 | """ |
---|
[168a845] | 104 | Clear and Hide all plot panels, and remove them from menu |
---|
[957723f] | 105 | """ |
---|
[4bee68d] | 106 | for group_id in self.plot_panels.keys(): |
---|
| 107 | panel = self.plot_panels[group_id] |
---|
| 108 | panel.graph.reset() |
---|
| 109 | self.hide_panel(group_id) |
---|
[168a845] | 110 | self.plot_panels = {} |
---|
| 111 | item = self.menu.FindItemByPosition(0) |
---|
| 112 | while item != None: |
---|
| 113 | self.menu.DeleteItem(item) |
---|
| 114 | try: |
---|
| 115 | item = self.menu.FindItemByPosition(0) |
---|
| 116 | except: |
---|
| 117 | item = None |
---|
| 118 | |
---|
[957723f] | 119 | |
---|
| 120 | def clear_panel_by_id(self, group_id): |
---|
[783940c] | 121 | """ |
---|
| 122 | clear the graph |
---|
| 123 | """ |
---|
| 124 | if group_id in self.plot_panels.keys(): |
---|
| 125 | panel = self.plot_panels[group_id] |
---|
| 126 | panel.graph.reset() |
---|
| 127 | return True |
---|
| 128 | return False |
---|
| 129 | |
---|
[ae83ad3] | 130 | |
---|
| 131 | def hide_panel(self, group_id): |
---|
| 132 | """ |
---|
| 133 | hide panel with group ID = group_id |
---|
| 134 | """ |
---|
| 135 | if group_id in self.plot_panels.keys(): |
---|
| 136 | panel = self.plot_panels[group_id] |
---|
| 137 | self.parent.hide_panel(panel.uid) |
---|
| 138 | return True |
---|
| 139 | return False |
---|
| 140 | |
---|
[2bdb52b] | 141 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
[ae83ad3] | 142 | """ |
---|
| 143 | """ |
---|
| 144 | ## Set group ID if available |
---|
| 145 | ## Assign data properties to the new create panel |
---|
| 146 | new_panel.set_manager(self) |
---|
| 147 | new_panel.group_id = group_id |
---|
[e88ebfd] | 148 | if group_id not in data.list_group_id: |
---|
| 149 | data.list_group_id.append(group_id) |
---|
[2bdb52b] | 150 | if title is None: |
---|
| 151 | title = data.title |
---|
[ae83ad3] | 152 | new_panel.window_caption = title |
---|
[2bdb52b] | 153 | new_panel.window_name = data.title |
---|
[ae83ad3] | 154 | event_id = self.parent.popup_panel(new_panel) |
---|
| 155 | #remove the default item in the menu |
---|
| 156 | if len(self.plot_panels) == 0: |
---|
[00b76931] | 157 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
| 158 | if pos != -1: |
---|
| 159 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
[ae83ad3] | 160 | # Set UID to allow us to reference the panel later |
---|
| 161 | new_panel.uid = event_id |
---|
| 162 | # Ship the plottable to its panel |
---|
| 163 | new_panel.plot_data(data) |
---|
| 164 | self.plot_panels[new_panel.group_id] = new_panel |
---|
[00b76931] | 165 | |
---|
[53cf669] | 166 | # Set Graph menu and help string |
---|
| 167 | helpString = 'Show/Hide Graph: ' |
---|
| 168 | for plot in new_panel.plots.itervalues(): |
---|
| 169 | helpString += (' ' + plot.label + ';') |
---|
| 170 | self.menu.AppendCheckItem(event_id, new_panel.window_caption, |
---|
| 171 | helpString) |
---|
| 172 | self.menu.Check(event_id, IS_WIN) |
---|
| 173 | wx.EVT_MENU(self.parent, event_id, self._on_check_menu) |
---|
| 174 | |
---|
| 175 | wx.EVT_SHOW(new_panel, self._on_close_panel) |
---|
| 176 | |
---|
[ae83ad3] | 177 | |
---|
| 178 | def create_1d_panel(self, data, group_id): |
---|
| 179 | """ |
---|
| 180 | """ |
---|
| 181 | # Create a new plot panel if none was available |
---|
| 182 | if issubclass(data.__class__, Data1D): |
---|
| 183 | from Plotter1D import ModelPanel1D |
---|
| 184 | ## get the data representation label of the data to plot |
---|
| 185 | ## when even the user select "change scale" |
---|
| 186 | xtransform = data.xtransform |
---|
| 187 | ytransform = data.ytransform |
---|
| 188 | ## create a plotpanel for 1D Data |
---|
| 189 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
| 190 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
| 191 | return new_panel |
---|
| 192 | |
---|
| 193 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
| 194 | raise ValueError, msg |
---|
| 195 | |
---|
| 196 | def create_2d_panel(self, data, group_id): |
---|
| 197 | """ |
---|
| 198 | """ |
---|
| 199 | if issubclass(data.__class__, Data2D): |
---|
| 200 | ##Create a new plotpanel for 2D data |
---|
| 201 | from Plotter2D import ModelPanel2D |
---|
| 202 | scale = data.scale |
---|
| 203 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
| 204 | data2d=data, scale = scale, |
---|
| 205 | style=wx.RAISED_BORDER) |
---|
| 206 | return new_panel |
---|
| 207 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
| 208 | raise ValueError, msg |
---|
| 209 | |
---|
| 210 | def update_panel(self, data, panel): |
---|
| 211 | """ |
---|
| 212 | update the graph of a given panel |
---|
| 213 | """ |
---|
| 214 | # Check whether we already have a graph with the same units |
---|
| 215 | # as the plottable we just received. |
---|
| 216 | _, x_unit = data.get_xaxis() |
---|
| 217 | _, y_unit = data.get_yaxis() |
---|
[783940c] | 218 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
| 219 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
| 220 | (x_unit != panel.graph.prop["xunit"]) |
---|
| 221 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
| 222 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
[0145a25] | 223 | (y_unit != panel.graph.prop["yunit"]) |
---|
[783940c] | 224 | if (flag_x and flag_y): |
---|
[ae83ad3] | 225 | msg = "Cannot add %s" % str(data.name) |
---|
| 226 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
| 227 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
| 228 | raise ValueError, msg |
---|
| 229 | else: |
---|
[3658717e] | 230 | if panel.group_id not in data.list_group_id: |
---|
| 231 | data.list_group_id.append(panel.group_id) |
---|
[ae83ad3] | 232 | panel.plot_data( data) |
---|
| 233 | self.parent.show_panel(panel.uid) |
---|
| 234 | |
---|
[df22224] | 235 | def delete_menu_item(self, name, uid): |
---|
| 236 | """ |
---|
| 237 | """ |
---|
| 238 | #remove menu item |
---|
| 239 | pos = self.menu.FindItem(name) |
---|
| 240 | if pos != -1: |
---|
| 241 | self.menu.Delete(uid) |
---|
| 242 | if self.menu.GetMenuItemCount() == 0: |
---|
| 243 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
[52f3c98] | 244 | "No graph available") |
---|
[df22224] | 245 | self.menu.FindItemByPosition(0).Enable(False) |
---|
| 246 | |
---|
[00b76931] | 247 | def delete_panel(self, group_id): |
---|
| 248 | """ |
---|
| 249 | """ |
---|
| 250 | if group_id in self.plot_panels.keys(): |
---|
| 251 | panel = self.plot_panels[group_id] |
---|
[e26d0db] | 252 | uid = panel.uid |
---|
[13a63ab] | 253 | wx.PostEvent(self.parent, |
---|
| 254 | DeletePlotPanelEvent(name=panel.window_caption, |
---|
| 255 | caption=panel.window_caption)) |
---|
[00b76931] | 256 | #remove menu item |
---|
[df22224] | 257 | self.delete_menu_item(panel.window_caption, panel.uid) |
---|
[00b76931] | 258 | del self.plot_panels[group_id] |
---|
[e26d0db] | 259 | self.parent.delete_panel(uid) |
---|
[00b76931] | 260 | return True |
---|
| 261 | return False |
---|
| 262 | |
---|
[41d466f] | 263 | def _on_plot_event(self, event): |
---|
| 264 | """ |
---|
[d955bf19] | 265 | A new plottable is being shipped to the plotting plug-in. |
---|
| 266 | Check whether we have a panel to put in on, or create |
---|
| 267 | a new one |
---|
| 268 | |
---|
| 269 | :param event: EVT_NEW_PLOT event |
---|
| 270 | |
---|
[41d466f] | 271 | """ |
---|
[ae83ad3] | 272 | if hasattr(event, 'action'): |
---|
[a07e72f] | 273 | group_id = event.group_id |
---|
[ae83ad3] | 274 | #remove data from panel |
---|
| 275 | if event.action.lower() == 'remove': |
---|
| 276 | id = event.id |
---|
| 277 | return self.remove_plot(group_id, id) |
---|
| 278 | if event.action.lower() == 'hide': |
---|
| 279 | return self.hide_panel(group_id) |
---|
[00b76931] | 280 | if event.action.lower() == 'delete': |
---|
| 281 | return self.delete_panel(group_id) |
---|
[783940c] | 282 | if event.action.lower() == "clear": |
---|
[957723f] | 283 | return self.clear_panel_by_id(group_id) |
---|
[783940c] | 284 | |
---|
[2bdb52b] | 285 | title = None |
---|
| 286 | if hasattr(event, 'title'): |
---|
[53cf669] | 287 | title = 'Graph'#event.title |
---|
[00b76931] | 288 | |
---|
[a07e72f] | 289 | data = event.plot |
---|
[e88ebfd] | 290 | group_id = data.group_id |
---|
[ae83ad3] | 291 | |
---|
| 292 | if group_id in self.plot_panels.keys(): |
---|
| 293 | #update a panel graph |
---|
| 294 | panel = self.plot_panels[group_id] |
---|
| 295 | self.update_panel(data, panel) |
---|
[a07e72f] | 296 | else: |
---|
[ae83ad3] | 297 | #create a new panel |
---|
[a07e72f] | 298 | if issubclass(data.__class__, Data1D): |
---|
[ae83ad3] | 299 | new_panel = self.create_1d_panel(data, group_id) |
---|
[a07e72f] | 300 | else: |
---|
[ae83ad3] | 301 | new_panel = self.create_2d_panel(data, group_id) |
---|
[3603b2e] | 302 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
[60063f3] | 303 | |
---|
[41d466f] | 304 | return |
---|
[6d727ae] | 305 | |
---|
| 306 | def _on_check_menu(self, event): |
---|
| 307 | """ |
---|
| 308 | Check mark on menu |
---|
| 309 | """ |
---|
[d264f0da] | 310 | #event.Skip() |
---|
[6d727ae] | 311 | event_id = event.GetId() |
---|
| 312 | |
---|
[d264f0da] | 313 | if self.menu.IsChecked(event_id): |
---|
[56d5562] | 314 | self.parent.on_view(event) |
---|
[c501ea5] | 315 | self.menu.Check(event_id, IS_WIN) |
---|
[d264f0da] | 316 | else: |
---|
| 317 | self.parent.hide_panel(event_id) |
---|
| 318 | self.menu.Check(event_id, False) |
---|
[6d727ae] | 319 | |
---|
[df8a13e] | 320 | def help(self, evt): |
---|
| 321 | """ |
---|
| 322 | Show a general help dialog. |
---|
| 323 | """ |
---|
| 324 | from help_panel import HelpWindow |
---|
| 325 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
| 326 | frame.Show(True) |
---|
| 327 | |
---|
[6d727ae] | 328 | def _on_close_panel(self, event): |
---|
| 329 | """ |
---|
| 330 | Update check mark on panel's show and hide event |
---|
| 331 | """ |
---|
| 332 | event.Skip() |
---|
| 333 | panel = event.GetEventObject() |
---|
| 334 | id = panel.uid |
---|
| 335 | if event.GetShow() and self.parent._mgr.GetPane(panel).IsShown(): |
---|
[ead78a4] | 336 | try: |
---|
| 337 | if self.menu.FindItemById(id) != None: |
---|
| 338 | if not self.menu.IsChecked(id): |
---|
| 339 | self.menu.Check(id, True) |
---|
| 340 | #self.menu.Enable(id, False) |
---|
| 341 | except: |
---|
| 342 | # when no such item; may happens after delete |
---|
| 343 | pass |
---|
[6d727ae] | 344 | else: |
---|
| 345 | try: |
---|
[ead78a4] | 346 | if self.menu.FindItemById(id) != None: |
---|
| 347 | if self.menu.IsChecked(id): |
---|
| 348 | self.menu.Check(id, False) |
---|
| 349 | #self.menu.Enable(id, True) |
---|
[6d727ae] | 350 | except: |
---|
| 351 | pass |
---|