[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: |
---|
[a48ecbd] | 28 | if int(wx.__version__.split('.')[0]) == 2: |
---|
| 29 | if int(wx.__version__.split('.')[1]) < 9: |
---|
[846c724] | 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) |
---|
[fec4939] | 99 | |
---|
[ae83ad3] | 100 | return True |
---|
[783940c] | 101 | return False |
---|
| 102 | |
---|
[957723f] | 103 | def clear_panel(self): |
---|
| 104 | """ |
---|
[168a845] | 105 | Clear and Hide all plot panels, and remove them from menu |
---|
[957723f] | 106 | """ |
---|
[4bee68d] | 107 | for group_id in self.plot_panels.keys(): |
---|
| 108 | panel = self.plot_panels[group_id] |
---|
| 109 | panel.graph.reset() |
---|
| 110 | self.hide_panel(group_id) |
---|
[168a845] | 111 | self.plot_panels = {} |
---|
| 112 | item = self.menu.FindItemByPosition(0) |
---|
| 113 | while item != None: |
---|
| 114 | self.menu.DeleteItem(item) |
---|
| 115 | try: |
---|
| 116 | item = self.menu.FindItemByPosition(0) |
---|
| 117 | except: |
---|
| 118 | item = None |
---|
| 119 | |
---|
[957723f] | 120 | |
---|
| 121 | def clear_panel_by_id(self, group_id): |
---|
[783940c] | 122 | """ |
---|
| 123 | clear the graph |
---|
| 124 | """ |
---|
| 125 | if group_id in self.plot_panels.keys(): |
---|
| 126 | panel = self.plot_panels[group_id] |
---|
| 127 | panel.graph.reset() |
---|
| 128 | return True |
---|
| 129 | return False |
---|
| 130 | |
---|
[ae83ad3] | 131 | |
---|
| 132 | def hide_panel(self, group_id): |
---|
| 133 | """ |
---|
| 134 | hide panel with group ID = group_id |
---|
| 135 | """ |
---|
| 136 | if group_id in self.plot_panels.keys(): |
---|
| 137 | panel = self.plot_panels[group_id] |
---|
| 138 | self.parent.hide_panel(panel.uid) |
---|
| 139 | return True |
---|
| 140 | return False |
---|
| 141 | |
---|
[2bdb52b] | 142 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
[ae83ad3] | 143 | """ |
---|
| 144 | """ |
---|
| 145 | ## Set group ID if available |
---|
| 146 | ## Assign data properties to the new create panel |
---|
| 147 | new_panel.set_manager(self) |
---|
| 148 | new_panel.group_id = group_id |
---|
[e88ebfd] | 149 | if group_id not in data.list_group_id: |
---|
| 150 | data.list_group_id.append(group_id) |
---|
[2bdb52b] | 151 | if title is None: |
---|
| 152 | title = data.title |
---|
[ae83ad3] | 153 | new_panel.window_caption = title |
---|
[2bdb52b] | 154 | new_panel.window_name = data.title |
---|
[ae83ad3] | 155 | event_id = self.parent.popup_panel(new_panel) |
---|
| 156 | #remove the default item in the menu |
---|
| 157 | if len(self.plot_panels) == 0: |
---|
[00b76931] | 158 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
| 159 | if pos != -1: |
---|
| 160 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
[ae83ad3] | 161 | # Set UID to allow us to reference the panel later |
---|
| 162 | new_panel.uid = event_id |
---|
| 163 | # Ship the plottable to its panel |
---|
| 164 | new_panel.plot_data(data) |
---|
| 165 | self.plot_panels[new_panel.group_id] = new_panel |
---|
[00b76931] | 166 | |
---|
[53cf669] | 167 | # Set Graph menu and help string |
---|
| 168 | helpString = 'Show/Hide Graph: ' |
---|
| 169 | for plot in new_panel.plots.itervalues(): |
---|
| 170 | helpString += (' ' + plot.label + ';') |
---|
| 171 | self.menu.AppendCheckItem(event_id, new_panel.window_caption, |
---|
| 172 | helpString) |
---|
| 173 | self.menu.Check(event_id, IS_WIN) |
---|
| 174 | wx.EVT_MENU(self.parent, event_id, self._on_check_menu) |
---|
| 175 | |
---|
| 176 | wx.EVT_SHOW(new_panel, self._on_close_panel) |
---|
| 177 | |
---|
[ae83ad3] | 178 | |
---|
| 179 | def create_1d_panel(self, data, group_id): |
---|
| 180 | """ |
---|
| 181 | """ |
---|
| 182 | # Create a new plot panel if none was available |
---|
| 183 | if issubclass(data.__class__, Data1D): |
---|
| 184 | from Plotter1D import ModelPanel1D |
---|
| 185 | ## get the data representation label of the data to plot |
---|
| 186 | ## when even the user select "change scale" |
---|
| 187 | xtransform = data.xtransform |
---|
| 188 | ytransform = data.ytransform |
---|
| 189 | ## create a plotpanel for 1D Data |
---|
| 190 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
| 191 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
| 192 | return new_panel |
---|
| 193 | |
---|
| 194 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
| 195 | raise ValueError, msg |
---|
| 196 | |
---|
| 197 | def create_2d_panel(self, data, group_id): |
---|
| 198 | """ |
---|
| 199 | """ |
---|
| 200 | if issubclass(data.__class__, Data2D): |
---|
| 201 | ##Create a new plotpanel for 2D data |
---|
| 202 | from Plotter2D import ModelPanel2D |
---|
| 203 | scale = data.scale |
---|
| 204 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
| 205 | data2d=data, scale = scale, |
---|
| 206 | style=wx.RAISED_BORDER) |
---|
| 207 | return new_panel |
---|
| 208 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
| 209 | raise ValueError, msg |
---|
| 210 | |
---|
| 211 | def update_panel(self, data, panel): |
---|
| 212 | """ |
---|
| 213 | update the graph of a given panel |
---|
| 214 | """ |
---|
| 215 | # Check whether we already have a graph with the same units |
---|
| 216 | # as the plottable we just received. |
---|
| 217 | _, x_unit = data.get_xaxis() |
---|
| 218 | _, y_unit = data.get_yaxis() |
---|
[783940c] | 219 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
| 220 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
| 221 | (x_unit != panel.graph.prop["xunit"]) |
---|
| 222 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
| 223 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
[0145a25] | 224 | (y_unit != panel.graph.prop["yunit"]) |
---|
[783940c] | 225 | if (flag_x and flag_y): |
---|
[ae83ad3] | 226 | msg = "Cannot add %s" % str(data.name) |
---|
| 227 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
| 228 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
| 229 | raise ValueError, msg |
---|
| 230 | else: |
---|
[3658717e] | 231 | if panel.group_id not in data.list_group_id: |
---|
| 232 | data.list_group_id.append(panel.group_id) |
---|
[ae83ad3] | 233 | panel.plot_data( data) |
---|
| 234 | self.parent.show_panel(panel.uid) |
---|
| 235 | |
---|
[df22224] | 236 | def delete_menu_item(self, name, uid): |
---|
| 237 | """ |
---|
| 238 | """ |
---|
| 239 | #remove menu item |
---|
| 240 | pos = self.menu.FindItem(name) |
---|
| 241 | if pos != -1: |
---|
| 242 | self.menu.Delete(uid) |
---|
| 243 | if self.menu.GetMenuItemCount() == 0: |
---|
| 244 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
[52f3c98] | 245 | "No graph available") |
---|
[df22224] | 246 | self.menu.FindItemByPosition(0).Enable(False) |
---|
| 247 | |
---|
[00b76931] | 248 | def delete_panel(self, group_id): |
---|
| 249 | """ |
---|
| 250 | """ |
---|
| 251 | if group_id in self.plot_panels.keys(): |
---|
| 252 | panel = self.plot_panels[group_id] |
---|
[e26d0db] | 253 | uid = panel.uid |
---|
[13a63ab] | 254 | wx.PostEvent(self.parent, |
---|
| 255 | DeletePlotPanelEvent(name=panel.window_caption, |
---|
| 256 | caption=panel.window_caption)) |
---|
[00b76931] | 257 | #remove menu item |
---|
[df22224] | 258 | self.delete_menu_item(panel.window_caption, panel.uid) |
---|
[00b76931] | 259 | del self.plot_panels[group_id] |
---|
[e26d0db] | 260 | self.parent.delete_panel(uid) |
---|
[00b76931] | 261 | return True |
---|
| 262 | return False |
---|
| 263 | |
---|
[41d466f] | 264 | def _on_plot_event(self, event): |
---|
| 265 | """ |
---|
[d955bf19] | 266 | A new plottable is being shipped to the plotting plug-in. |
---|
| 267 | Check whether we have a panel to put in on, or create |
---|
| 268 | a new one |
---|
| 269 | |
---|
| 270 | :param event: EVT_NEW_PLOT event |
---|
| 271 | |
---|
[41d466f] | 272 | """ |
---|
[ae83ad3] | 273 | if hasattr(event, 'action'): |
---|
[a07e72f] | 274 | group_id = event.group_id |
---|
[ae83ad3] | 275 | #remove data from panel |
---|
| 276 | if event.action.lower() == 'remove': |
---|
| 277 | id = event.id |
---|
| 278 | return self.remove_plot(group_id, id) |
---|
| 279 | if event.action.lower() == 'hide': |
---|
| 280 | return self.hide_panel(group_id) |
---|
[00b76931] | 281 | if event.action.lower() == 'delete': |
---|
| 282 | return self.delete_panel(group_id) |
---|
[783940c] | 283 | if event.action.lower() == "clear": |
---|
[957723f] | 284 | return self.clear_panel_by_id(group_id) |
---|
[783940c] | 285 | |
---|
[2bdb52b] | 286 | title = None |
---|
| 287 | if hasattr(event, 'title'): |
---|
[53cf669] | 288 | title = 'Graph'#event.title |
---|
[00b76931] | 289 | |
---|
[a07e72f] | 290 | data = event.plot |
---|
[e88ebfd] | 291 | group_id = data.group_id |
---|
[ae83ad3] | 292 | |
---|
| 293 | if group_id in self.plot_panels.keys(): |
---|
| 294 | #update a panel graph |
---|
| 295 | panel = self.plot_panels[group_id] |
---|
| 296 | self.update_panel(data, panel) |
---|
[a07e72f] | 297 | else: |
---|
[ae83ad3] | 298 | #create a new panel |
---|
[a07e72f] | 299 | if issubclass(data.__class__, Data1D): |
---|
[ae83ad3] | 300 | new_panel = self.create_1d_panel(data, group_id) |
---|
[a07e72f] | 301 | else: |
---|
[ae83ad3] | 302 | new_panel = self.create_2d_panel(data, group_id) |
---|
[3603b2e] | 303 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
[60063f3] | 304 | |
---|
[41d466f] | 305 | return |
---|
[6d727ae] | 306 | |
---|
| 307 | def _on_check_menu(self, event): |
---|
| 308 | """ |
---|
| 309 | Check mark on menu |
---|
| 310 | """ |
---|
[d264f0da] | 311 | #event.Skip() |
---|
[6d727ae] | 312 | event_id = event.GetId() |
---|
| 313 | |
---|
[d264f0da] | 314 | if self.menu.IsChecked(event_id): |
---|
[56d5562] | 315 | self.parent.on_view(event) |
---|
[c501ea5] | 316 | self.menu.Check(event_id, IS_WIN) |
---|
[d264f0da] | 317 | else: |
---|
| 318 | self.parent.hide_panel(event_id) |
---|
| 319 | self.menu.Check(event_id, False) |
---|
[6d727ae] | 320 | |
---|
[df8a13e] | 321 | def help(self, evt): |
---|
| 322 | """ |
---|
| 323 | Show a general help dialog. |
---|
| 324 | """ |
---|
| 325 | from help_panel import HelpWindow |
---|
| 326 | frame = HelpWindow(None, -1, 'HelpWindow') |
---|
| 327 | frame.Show(True) |
---|
| 328 | |
---|
[6d727ae] | 329 | def _on_close_panel(self, event): |
---|
| 330 | """ |
---|
| 331 | Update check mark on panel's show and hide event |
---|
| 332 | """ |
---|
| 333 | event.Skip() |
---|
| 334 | panel = event.GetEventObject() |
---|
| 335 | id = panel.uid |
---|
| 336 | if event.GetShow() and self.parent._mgr.GetPane(panel).IsShown(): |
---|
[ead78a4] | 337 | try: |
---|
| 338 | if self.menu.FindItemById(id) != None: |
---|
| 339 | if not self.menu.IsChecked(id): |
---|
| 340 | self.menu.Check(id, True) |
---|
| 341 | #self.menu.Enable(id, False) |
---|
| 342 | except: |
---|
| 343 | # when no such item; may happens after delete |
---|
| 344 | pass |
---|
[6d727ae] | 345 | else: |
---|
| 346 | try: |
---|
[ead78a4] | 347 | if self.menu.FindItemById(id) != None: |
---|
| 348 | if self.menu.IsChecked(id): |
---|
| 349 | self.menu.Check(id, False) |
---|
| 350 | #self.menu.Enable(id, True) |
---|
[6d727ae] | 351 | except: |
---|
| 352 | pass |
---|