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