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