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