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