[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 |
---|
[7f84e22] | 18 | from sans.guiframe.plugin_base import PluginBase |
---|
[a07e72f] | 19 | from sans.guiframe.dataFitting import Data1D |
---|
| 20 | from sans.guiframe.dataFitting import Data2D |
---|
[7764b41] | 21 | |
---|
[00b76931] | 22 | DEFAULT_MENU_ITEM_LABEL = "No plot available" |
---|
| 23 | DEFAULT_MENU_ITEM_ID = wx.NewId() |
---|
| 24 | |
---|
[7f84e22] | 25 | class Plugin(PluginBase): |
---|
[41d466f] | 26 | """ |
---|
[d955bf19] | 27 | Plug-in class to be instantiated by the GUI manager |
---|
[41d466f] | 28 | """ |
---|
| 29 | |
---|
[7f84e22] | 30 | def __init__(self, standalone=False): |
---|
| 31 | PluginBase.__init__(self, name="Plotting", standalone=standalone) |
---|
| 32 | |
---|
[41d466f] | 33 | ## Plot panels |
---|
[ae83ad3] | 34 | self.plot_panels = {} |
---|
[a07e72f] | 35 | self._panel_on_focus = None |
---|
[00b76931] | 36 | self.menu_default_id = None |
---|
[a07e72f] | 37 | |
---|
| 38 | def set_panel_on_focus(self, panel): |
---|
| 39 | """ |
---|
| 40 | """ |
---|
| 41 | self._panel_on_focus = panel |
---|
| 42 | |
---|
[b7c7a1c] | 43 | def is_always_active(self): |
---|
| 44 | """ |
---|
| 45 | return True is this plugin is always active even if the user is |
---|
| 46 | switching between perspectives |
---|
| 47 | """ |
---|
| 48 | return True |
---|
| 49 | |
---|
[bf4402c3] | 50 | def populate_menu(self, parent): |
---|
[41d466f] | 51 | """ |
---|
[d955bf19] | 52 | Create a 'Plot' menu to list the panels |
---|
| 53 | available for displaying |
---|
| 54 | |
---|
| 55 | :param id: next available unique ID for wx events |
---|
| 56 | :param parent: parent window |
---|
| 57 | |
---|
[41d466f] | 58 | """ |
---|
| 59 | self.menu = wx.Menu() |
---|
[00b76931] | 60 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
[2fc9243] | 61 | "No plot available") |
---|
| 62 | self.menu.FindItemByPosition(0).Enable(False) |
---|
[bf4402c3] | 63 | return [(self.menu, "Plot")] |
---|
[41d466f] | 64 | |
---|
| 65 | def get_panels(self, parent): |
---|
| 66 | """ |
---|
[d955bf19] | 67 | Create and return a list of panel objects |
---|
[41d466f] | 68 | """ |
---|
| 69 | ## Save a reference to the parent |
---|
| 70 | self.parent = parent |
---|
| 71 | # Connect to plotting events |
---|
| 72 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
| 73 | # We have no initial panels for this plug-in |
---|
| 74 | return [] |
---|
[54cc36a] | 75 | |
---|
[41d466f] | 76 | def _on_show_panel(self, event): |
---|
[6c0568b] | 77 | """show plug-in panel""" |
---|
| 78 | pass |
---|
| 79 | |
---|
[ae83ad3] | 80 | def remove_plot(self, group_id, id): |
---|
| 81 | """ |
---|
| 82 | remove plot of ID = id from a panel of group ID =group_id |
---|
| 83 | """ |
---|
| 84 | |
---|
| 85 | if group_id in self.plot_panels.keys(): |
---|
| 86 | panel = self.plot_panels[group_id] |
---|
| 87 | panel.remove_data_by_id(id=id) |
---|
| 88 | return True |
---|
[783940c] | 89 | return False |
---|
| 90 | |
---|
[957723f] | 91 | def clear_panel(self): |
---|
| 92 | """ |
---|
| 93 | """ |
---|
| 94 | pass |
---|
| 95 | |
---|
| 96 | def clear_panel_by_id(self, group_id): |
---|
[783940c] | 97 | """ |
---|
| 98 | clear the graph |
---|
| 99 | """ |
---|
| 100 | if group_id in self.plot_panels.keys(): |
---|
| 101 | panel = self.plot_panels[group_id] |
---|
| 102 | panel.graph.reset() |
---|
| 103 | return True |
---|
| 104 | return False |
---|
| 105 | |
---|
[ae83ad3] | 106 | |
---|
| 107 | def hide_panel(self, group_id): |
---|
| 108 | """ |
---|
| 109 | hide panel with group ID = group_id |
---|
| 110 | """ |
---|
| 111 | if group_id in self.plot_panels.keys(): |
---|
| 112 | panel = self.plot_panels[group_id] |
---|
| 113 | self.parent.hide_panel(panel.uid) |
---|
| 114 | return True |
---|
| 115 | return False |
---|
| 116 | |
---|
[2bdb52b] | 117 | def create_panel_helper(self, new_panel, data, group_id, title=None): |
---|
[ae83ad3] | 118 | """ |
---|
| 119 | """ |
---|
| 120 | ## Set group ID if available |
---|
| 121 | ## Assign data properties to the new create panel |
---|
| 122 | new_panel.set_manager(self) |
---|
| 123 | new_panel.group_id = group_id |
---|
[e88ebfd] | 124 | if group_id not in data.list_group_id: |
---|
| 125 | data.list_group_id.append(group_id) |
---|
[2bdb52b] | 126 | if title is None: |
---|
| 127 | title = data.title |
---|
[ae83ad3] | 128 | new_panel.window_caption = title |
---|
[2bdb52b] | 129 | new_panel.window_name = data.title |
---|
[ae83ad3] | 130 | event_id = self.parent.popup_panel(new_panel) |
---|
| 131 | #remove the default item in the menu |
---|
| 132 | if len(self.plot_panels) == 0: |
---|
[00b76931] | 133 | pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL) |
---|
| 134 | if pos != -1: |
---|
| 135 | self.menu.Delete(DEFAULT_MENU_ITEM_ID) |
---|
| 136 | |
---|
[ae83ad3] | 137 | self.menu.Append(event_id, new_panel.window_caption, |
---|
| 138 | "Show %s plot panel" % new_panel.window_caption) |
---|
| 139 | # Set UID to allow us to reference the panel later |
---|
| 140 | new_panel.uid = event_id |
---|
| 141 | # Ship the plottable to its panel |
---|
| 142 | new_panel.plot_data(data) |
---|
| 143 | self.plot_panels[new_panel.group_id] = new_panel |
---|
[00b76931] | 144 | |
---|
[ae83ad3] | 145 | |
---|
| 146 | def create_1d_panel(self, data, group_id): |
---|
| 147 | """ |
---|
| 148 | """ |
---|
| 149 | # Create a new plot panel if none was available |
---|
| 150 | if issubclass(data.__class__, Data1D): |
---|
| 151 | from Plotter1D import ModelPanel1D |
---|
| 152 | ## get the data representation label of the data to plot |
---|
| 153 | ## when even the user select "change scale" |
---|
| 154 | xtransform = data.xtransform |
---|
| 155 | ytransform = data.ytransform |
---|
| 156 | ## create a plotpanel for 1D Data |
---|
| 157 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
| 158 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
| 159 | return new_panel |
---|
| 160 | |
---|
| 161 | msg = "1D Panel of group ID %s could not be created" % str(group_id) |
---|
| 162 | raise ValueError, msg |
---|
| 163 | |
---|
| 164 | def create_2d_panel(self, data, group_id): |
---|
| 165 | """ |
---|
| 166 | """ |
---|
| 167 | if issubclass(data.__class__, Data2D): |
---|
| 168 | ##Create a new plotpanel for 2D data |
---|
| 169 | from Plotter2D import ModelPanel2D |
---|
| 170 | scale = data.scale |
---|
| 171 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
| 172 | data2d=data, scale = scale, |
---|
| 173 | style=wx.RAISED_BORDER) |
---|
| 174 | return new_panel |
---|
| 175 | msg = "2D Panel of group ID %s could not be created" % str(group_id) |
---|
| 176 | raise ValueError, msg |
---|
| 177 | |
---|
| 178 | def update_panel(self, data, panel): |
---|
| 179 | """ |
---|
| 180 | update the graph of a given panel |
---|
| 181 | """ |
---|
| 182 | # Check whether we already have a graph with the same units |
---|
| 183 | # as the plottable we just received. |
---|
| 184 | _, x_unit = data.get_xaxis() |
---|
| 185 | _, y_unit = data.get_yaxis() |
---|
[783940c] | 186 | flag_x = (panel.graph.prop["xunit"] is not None) and \ |
---|
| 187 | (panel.graph.prop["xunit"].strip() != "") and\ |
---|
| 188 | (x_unit != panel.graph.prop["xunit"]) |
---|
| 189 | flag_y = (panel.graph.prop["yunit"] is not None) and \ |
---|
| 190 | (panel.graph.prop["yunit"].strip() != "") and\ |
---|
| 191 | (x_unit != panel.graph.prop["yunit"]) |
---|
| 192 | if (flag_x and flag_y): |
---|
[ae83ad3] | 193 | msg = "Cannot add %s" % str(data.name) |
---|
| 194 | msg += " to panel %s\n" % str(panel.window_caption) |
---|
| 195 | msg += "Please edit %s's units, labels" % str(data.name) |
---|
| 196 | raise ValueError, msg |
---|
| 197 | else: |
---|
[3658717e] | 198 | if panel.group_id not in data.list_group_id: |
---|
| 199 | data.list_group_id.append(panel.group_id) |
---|
[ae83ad3] | 200 | panel.plot_data( data) |
---|
| 201 | self.parent.show_panel(panel.uid) |
---|
| 202 | |
---|
[df22224] | 203 | def delete_menu_item(self, name, uid): |
---|
| 204 | """ |
---|
| 205 | """ |
---|
| 206 | #remove menu item |
---|
| 207 | pos = self.menu.FindItem(name) |
---|
| 208 | if pos != -1: |
---|
| 209 | self.menu.Delete(uid) |
---|
| 210 | if self.menu.GetMenuItemCount() == 0: |
---|
| 211 | self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, |
---|
| 212 | "No plot available") |
---|
| 213 | self.menu.FindItemByPosition(0).Enable(False) |
---|
| 214 | |
---|
[00b76931] | 215 | def delete_panel(self, group_id): |
---|
| 216 | """ |
---|
| 217 | """ |
---|
| 218 | if group_id in self.plot_panels.keys(): |
---|
| 219 | panel = self.plot_panels[group_id] |
---|
| 220 | #remove menu item |
---|
[df22224] | 221 | self.delete_menu_item(panel.window_caption, panel.uid) |
---|
[00b76931] | 222 | del self.plot_panels[group_id] |
---|
| 223 | return True |
---|
| 224 | return False |
---|
| 225 | |
---|
[41d466f] | 226 | def _on_plot_event(self, event): |
---|
| 227 | """ |
---|
[d955bf19] | 228 | A new plottable is being shipped to the plotting plug-in. |
---|
| 229 | Check whether we have a panel to put in on, or create |
---|
| 230 | a new one |
---|
| 231 | |
---|
| 232 | :param event: EVT_NEW_PLOT event |
---|
| 233 | |
---|
[41d466f] | 234 | """ |
---|
[ae83ad3] | 235 | if hasattr(event, 'action'): |
---|
[a07e72f] | 236 | group_id = event.group_id |
---|
[ae83ad3] | 237 | #remove data from panel |
---|
| 238 | if event.action.lower() == 'remove': |
---|
| 239 | id = event.id |
---|
| 240 | return self.remove_plot(group_id, id) |
---|
| 241 | if event.action.lower() == 'hide': |
---|
| 242 | return self.hide_panel(group_id) |
---|
[00b76931] | 243 | if event.action.lower() == 'delete': |
---|
| 244 | return self.delete_panel(group_id) |
---|
[783940c] | 245 | if event.action.lower() == "clear": |
---|
[957723f] | 246 | return self.clear_panel_by_id(group_id) |
---|
[783940c] | 247 | |
---|
[2bdb52b] | 248 | title = None |
---|
| 249 | if hasattr(event, 'title'): |
---|
| 250 | title = event.title |
---|
[00b76931] | 251 | |
---|
[a07e72f] | 252 | data = event.plot |
---|
[e88ebfd] | 253 | group_id = data.group_id |
---|
[ae83ad3] | 254 | |
---|
| 255 | if group_id in self.plot_panels.keys(): |
---|
| 256 | #update a panel graph |
---|
| 257 | panel = self.plot_panels[group_id] |
---|
| 258 | self.update_panel(data, panel) |
---|
[a07e72f] | 259 | else: |
---|
[ae83ad3] | 260 | #create a new panel |
---|
[a07e72f] | 261 | if issubclass(data.__class__, Data1D): |
---|
[ae83ad3] | 262 | new_panel = self.create_1d_panel(data, group_id) |
---|
[a07e72f] | 263 | else: |
---|
[ae83ad3] | 264 | new_panel = self.create_2d_panel(data, group_id) |
---|
[2bdb52b] | 265 | self.create_panel_helper(new_panel, data, group_id, title) |
---|
[4158af4] | 266 | |
---|
[41d466f] | 267 | return |
---|
[a07e72f] | 268 | |
---|