source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/plotting.py @ 951701c

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 951701c was ca224b1, checked in by krzywon, 8 years ago

Delete all plots when resetting state.

  • Property mode set to 100644
File size: 12.1 KB
RevLine 
[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)
[c039589]7#project funded by the US National Science Foundation.
[d955bf19]8#
9#See the license text in license.txt
10#
11#copyright 2008, University of Tennessee
12################################################################################
[41d466f]13
14import wx
15import sys
[d85c194]16from sas.sasgui.guiframe.events import EVT_NEW_PLOT
17from sas.sasgui.guiframe.events import EVT_PLOT_QRANGE
[6ffa0dd]18from sas.sasgui.guiframe.events import EVT_PLOT_LIM
[d85c194]19from sas.sasgui.guiframe.events import DeletePlotPanelEvent
20from sas.sasgui.guiframe.plugin_base import PluginBase
21from sas.sasgui.guiframe.dataFitting import Data1D
22from sas.sasgui.guiframe.dataFitting import Data2D
23from sas.sasgui.guiframe.gui_manager import MDIFrame
[52f3c98]24DEFAULT_MENU_ITEM_LABEL = "No graph available"
[00b76931]25DEFAULT_MENU_ITEM_ID = wx.NewId()
26
[c039589]27IS_WIN = True
28if 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]34class Plugin(PluginBase):
[41d466f]35    """
[d955bf19]36    Plug-in class to be instantiated by the GUI manager
[41d466f]37    """
[c039589]38
[f21d496]39    def __init__(self):
40        PluginBase.__init__(self, name="Plotting")
[c039589]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
[c039589]49
[a07e72f]50    def set_panel_on_focus(self, panel):
51        """
52        """
53        self._panel_on_focus = panel
[c039589]54
[b7c7a1c]55    def is_always_active(self):
56        """
[c039589]57        return True is this plugin is always active even if the user is
[b7c7a1c]58        switching between perspectives
59        """
60        return True
[c039589]61
[bf4402c3]62    def populate_menu(self, parent):
[41d466f]63        """
[d955bf19]64        Create a 'Plot' menu to list the panels
65        available for displaying
[c039589]66
[d955bf19]67        :param id: next available unique ID for wx events
68        :param parent: parent window
[c039589]69
[41d466f]70        """
[ae84427]71        return []
[c039589]72
[41d466f]73    def get_panels(self, parent):
74        """
[d955bf19]75        Create and return a list of panel objects
[41d466f]76        """
77        ## Save a reference to the parent
78        self.parent = parent
79        # Connect to plotting events
80        self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event)
[3e001f9]81        self.parent.Bind(EVT_PLOT_QRANGE, self._on_plot_qrange)
[6ffa0dd]82        self.parent.Bind(EVT_PLOT_LIM, self._on_plot_lim)
[41d466f]83        # We have no initial panels for this plug-in
84        return []
[c039589]85
86    def _on_plot_qrange(self, event=None):
[3e001f9]87        """
88        On Qmin Qmax vertical line event
89        """
90        if event == None:
91            return
92        if event.id in self.plot_panels.keys():
93            panel = self.plot_panels[event.id]
94        elif event.group_id in self.plot_panels.keys():
95            panel = self.plot_panels[event.group_id]
96        else:
97            return
98        panel.on_plot_qrange(event)
[c039589]99
[6ffa0dd]100    def _on_plot_lim(self, event=None):
101        if event == None:
102            return
103        if event.id in self.plot_panels.keys():
104            panel = self.plot_panels[event.id]
105        elif event.group_id in self.plot_panels.keys():
106            panel = self.plot_panels[event.group_id]
107        else:
108            return
109        if hasattr(event, 'xlim'):
110            panel.subplot.set_xlim(event.xlim)
111        if hasattr(event, 'ylim'):
112            panel.subplot.set_ylim(event.ylim)
113
114
[41d466f]115    def _on_show_panel(self, event):
[6c0568b]116        """show plug-in panel"""
117        pass
[c039589]118
[ae83ad3]119    def remove_plot(self, group_id, id):
120        """
121        remove plot of ID = id from a panel of group ID =group_id
122        """
[c039589]123
[ae83ad3]124        if group_id in self.plot_panels.keys():
125            panel = self.plot_panels[group_id]
126            panel.remove_data_by_id(id=id)
[c039589]127
[ae83ad3]128            return True
[783940c]129        return False
[c039589]130
[957723f]131    def clear_panel(self):
132        """
[168a845]133        Clear and Hide all plot panels, and remove them from menu
[957723f]134        """
[4bee68d]135        for group_id in self.plot_panels.keys():
[ca224b1]136            self.clear_panel_by_id(group_id)
[168a845]137        self.plot_panels = {}
[c039589]138
[957723f]139    def clear_panel_by_id(self, group_id):
[783940c]140        """
141        clear the graph
142        """
143        if group_id in self.plot_panels.keys():
144            panel = self.plot_panels[group_id]
[fd51a7c]145            for plottable in panel.graph.plottables.keys():
146                self.remove_plot(group_id, plottable.id)
[783940c]147            panel.graph.reset()
148            return True
149        return False
[c039589]150
[ae83ad3]151    def hide_panel(self, group_id):
152        """
153        hide panel with group ID = group_id
154        """
[ae84427]155        # Not implemeted
[ae83ad3]156        return False
[c039589]157
[2bdb52b]158    def create_panel_helper(self, new_panel, data, group_id, title=None):
[ae83ad3]159        """
160        """
[6e75ed0]161        ## Set group ID if available
[ae83ad3]162        ## Assign data properties to the new create panel
163        new_panel.set_manager(self)
164        new_panel.group_id = group_id
[e88ebfd]165        if group_id not in data.list_group_id:
166            data.list_group_id.append(group_id)
[2bdb52b]167        if title is None:
168            title = data.title
[ae83ad3]169        new_panel.window_caption = title
[2bdb52b]170        new_panel.window_name = data.title
[ae83ad3]171        event_id = self.parent.popup_panel(new_panel)
[ae84427]172
[ae83ad3]173        # Set UID to allow us to reference the panel later
174        new_panel.uid = event_id
175        # Ship the plottable to its panel
[c039589]176        wx.CallAfter(new_panel.plot_data, data)
[ae84427]177        #new_panel.canvas.set_resizing(new_panel.resizing)
[ae83ad3]178        self.plot_panels[new_panel.group_id] = new_panel
[c039589]179
[ae83ad3]180    def create_1d_panel(self, data, group_id):
181        """
182        """
[c039589]183        # Create a new plot panel if none was available
[ae83ad3]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
[ae84427]191            win = MDIFrame(self.parent, None, 'None', (100, 200))
192            new_panel = ModelPanel1D(win, -1, xtransform=xtransform,
[c039589]193                                     ytransform=ytransform, style=wx.RAISED_BORDER)
[ae84427]194            win.set_panel(new_panel)
195            win.Show(False)
196            new_panel.frame = win
197            #win.Show(True)
[ae83ad3]198            return  new_panel
[c039589]199
[ae83ad3]200        msg = "1D Panel of group ID %s could not be created" % str(group_id)
201        raise ValueError, msg
[c039589]202
[ae83ad3]203    def create_2d_panel(self, data, group_id):
204        """
205        """
206        if issubclass(data.__class__, Data2D):
207            ##Create a new plotpanel for 2D data
208            from Plotter2D import ModelPanel2D
209            scale = data.scale
[ae84427]210            win = MDIFrame(self.parent, None, 'None', (200, 150))
211            win.Show(False)
[c039589]212            new_panel = ModelPanel2D(win, id=-1,
213                                     data2d=data, scale=scale,
214                                     style=wx.RAISED_BORDER)
[ae84427]215            win.set_panel(new_panel)
216            new_panel.frame = win
[ae83ad3]217            return new_panel
218        msg = "2D Panel of group ID %s could not be created" % str(group_id)
219        raise ValueError, msg
[c039589]220
[ae83ad3]221    def update_panel(self, data, panel):
222        """
223        update the graph of a given panel
224        """
225        # Check whether we already have a graph with the same units
[c039589]226        # as the plottable we just received.
227        _, x_unit = data.get_xaxis()
228        _, y_unit = data.get_yaxis()
[783940c]229        flag_x = (panel.graph.prop["xunit"] is not None) and \
230                    (panel.graph.prop["xunit"].strip() != "") and\
[2365c0b]231                    (x_unit != panel.graph.prop["xunit"]) and False
[783940c]232        flag_y = (panel.graph.prop["yunit"] is not None) and \
233                    (panel.graph.prop["yunit"].strip() != "") and\
[2365c0b]234                    (y_unit != panel.graph.prop["yunit"]) and False
[c039589]235        if flag_x and flag_y:
[ae83ad3]236            msg = "Cannot add %s" % str(data.name)
237            msg += " to panel %s\n" % str(panel.window_caption)
238            msg += "Please edit %s's units, labels" % str(data.name)
239            raise ValueError, msg
240        else:
[3658717e]241            if panel.group_id not in data.list_group_id:
242                data.list_group_id.append(panel.group_id)
[4f70cc8]243            wx.CallAfter(panel.plot_data, data)
[ae84427]244
[00b76931]245    def delete_panel(self, group_id):
246        """
247        """
248        if group_id in self.plot_panels.keys():
249            panel = self.plot_panels[group_id]
[e26d0db]250            uid = panel.uid
[c039589]251            wx.PostEvent(self.parent,
[13a63ab]252                         DeletePlotPanelEvent(name=panel.window_caption,
[c039589]253                                              caption=panel.window_caption))
[00b76931]254            del self.plot_panels[group_id]
[cdf515f]255            if uid in self.parent.plot_panels.keys():
256                del self.parent.plot_panels[uid]
[ae84427]257                panel.frame.Destroy()
[00b76931]258            return True
[d97dd6f]259
[00b76931]260        return False
[c039589]261
[41d466f]262    def _on_plot_event(self, event):
263        """
[d955bf19]264        A new plottable is being shipped to the plotting plug-in.
265        Check whether we have a panel to put in on, or create
266        a new one
[c039589]267
[d955bf19]268        :param event: EVT_NEW_PLOT event
[c039589]269
[41d466f]270        """
[940aca7]271        action_check = False
[ae83ad3]272        if hasattr(event, 'action'):
[940aca7]273            action_string = event.action.lower().strip()
274            if action_string == 'check':
275                action_check = True
276            else:
277                group_id = event.group_id
278                if group_id in self.plot_panels.keys():
279                    #remove data from panel
280                    if action_string == 'remove':
[c039589]281                        return self.remove_plot(group_id, event.id)
[940aca7]282                    if action_string == 'hide':
283                        return self.hide_panel(group_id)
284                    if action_string == 'delete':
285                        panel = self.plot_panels[group_id]
286                        uid = panel.uid
287                        return self.parent.delete_panel(uid)
288                    if action_string == "clear":
289                        return self.clear_panel_by_id(group_id)
[c039589]290
291        if not hasattr(event, 'plot'):
[0f17dd9]292            return
[2bdb52b]293        title = None
294        if hasattr(event, 'title'):
[c039589]295            title = 'Graph'  #event.title
[a07e72f]296        data = event.plot
[c039589]297        group_id = data.group_id
[ae83ad3]298        if group_id in self.plot_panels.keys():
[940aca7]299            if action_check:
300                # Check if the plot already exist. if it does, do nothing.
301                if data.id in self.plot_panels[group_id].plots.keys():
[c039589]302                    return
303            #update a panel graph
[ae83ad3]304            panel = self.plot_panels[group_id]
305            self.update_panel(data, panel)
[a07e72f]306        else:
[ae83ad3]307            #create a new panel
[a07e72f]308            if issubclass(data.__class__, Data1D):
[ae83ad3]309                new_panel = self.create_1d_panel(data, group_id)
[a07e72f]310            else:
[940aca7]311                # Need to make the group_id consistent with 1D thus no if below
312                if len(self.plot_panels.values()) > 0:
313                    for p_group_id in self.plot_panels.keys():
314                        p_plot = self.plot_panels[p_group_id]
315                        if data.id in p_plot.plots.keys():
316                            p_plot.plots[data.id] = data
317                            self.plot_panels[group_id] = p_plot
318                            if group_id != p_group_id:
319                                del self.plot_panels[p_group_id]
320                                if p_group_id in data.list_group_id:
321                                    data.list_group_id.remove(p_group_id)
322                                if group_id not in data.list_group_id:
323                                    data.list_group_id.append(group_id)
324                            p_plot.group_id = group_id
325                            return
[c039589]326
[ae83ad3]327                new_panel = self.create_2d_panel(data, group_id)
[c039589]328            self.create_panel_helper(new_panel, data, group_id, title)
[6ffa0dd]329            if hasattr(event, 'xlim'):
330                new_panel.subplot.set_xlim(event.xlim)
331            if hasattr(event, 'ylim'):
332                new_panel.subplot.set_ylim(event.ylim)
[41d466f]333        return
Note: See TracBrowser for help on using the repository browser.