source: sasview/src/sas/sasgui/guiframe/local_perspectives/plotting/plotting.py @ 6ffa0dd

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 6ffa0dd was 6ffa0dd, checked in by lewis, 8 years ago

Plot entire extrapolation

  • Property mode set to 100644
File size: 12.2 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():
136            panel = self.plot_panels[group_id]
137            panel.graph.reset()
138            self.hide_panel(group_id)
[168a845]139        self.plot_panels = {}
[c039589]140
[957723f]141    def clear_panel_by_id(self, group_id):
[783940c]142        """
143        clear the graph
144        """
145        if group_id in self.plot_panels.keys():
146            panel = self.plot_panels[group_id]
[fd51a7c]147            for plottable in panel.graph.plottables.keys():
148                self.remove_plot(group_id, plottable.id)
[783940c]149            panel.graph.reset()
150            return True
151        return False
[c039589]152
[ae83ad3]153    def hide_panel(self, group_id):
154        """
155        hide panel with group ID = group_id
156        """
[ae84427]157        # Not implemeted
[ae83ad3]158        return False
[c039589]159
[2bdb52b]160    def create_panel_helper(self, new_panel, data, group_id, title=None):
[ae83ad3]161        """
162        """
[6e75ed0]163        ## Set group ID if available
[ae83ad3]164        ## Assign data properties to the new create panel
165        new_panel.set_manager(self)
166        new_panel.group_id = group_id
[e88ebfd]167        if group_id not in data.list_group_id:
168            data.list_group_id.append(group_id)
[2bdb52b]169        if title is None:
170            title = data.title
[ae83ad3]171        new_panel.window_caption = title
[2bdb52b]172        new_panel.window_name = data.title
[ae83ad3]173        event_id = self.parent.popup_panel(new_panel)
[ae84427]174
[ae83ad3]175        # Set UID to allow us to reference the panel later
176        new_panel.uid = event_id
177        # Ship the plottable to its panel
[c039589]178        wx.CallAfter(new_panel.plot_data, data)
[ae84427]179        #new_panel.canvas.set_resizing(new_panel.resizing)
[ae83ad3]180        self.plot_panels[new_panel.group_id] = new_panel
[c039589]181
[ae83ad3]182    def create_1d_panel(self, data, group_id):
183        """
184        """
[c039589]185        # Create a new plot panel if none was available
[ae83ad3]186        if issubclass(data.__class__, Data1D):
187            from Plotter1D import ModelPanel1D
188            ## get the data representation label of the data to plot
189            ## when even the user select "change scale"
190            xtransform = data.xtransform
191            ytransform = data.ytransform
192            ## create a plotpanel for 1D Data
[ae84427]193            win = MDIFrame(self.parent, None, 'None', (100, 200))
194            new_panel = ModelPanel1D(win, -1, xtransform=xtransform,
[c039589]195                                     ytransform=ytransform, style=wx.RAISED_BORDER)
[ae84427]196            win.set_panel(new_panel)
197            win.Show(False)
198            new_panel.frame = win
199            #win.Show(True)
[ae83ad3]200            return  new_panel
[c039589]201
[ae83ad3]202        msg = "1D Panel of group ID %s could not be created" % str(group_id)
203        raise ValueError, msg
[c039589]204
[ae83ad3]205    def create_2d_panel(self, data, group_id):
206        """
207        """
208        if issubclass(data.__class__, Data2D):
209            ##Create a new plotpanel for 2D data
210            from Plotter2D import ModelPanel2D
211            scale = data.scale
[ae84427]212            win = MDIFrame(self.parent, None, 'None', (200, 150))
213            win.Show(False)
[c039589]214            new_panel = ModelPanel2D(win, id=-1,
215                                     data2d=data, scale=scale,
216                                     style=wx.RAISED_BORDER)
[ae84427]217            win.set_panel(new_panel)
218            new_panel.frame = win
[ae83ad3]219            return new_panel
220        msg = "2D Panel of group ID %s could not be created" % str(group_id)
221        raise ValueError, msg
[c039589]222
[ae83ad3]223    def update_panel(self, data, panel):
224        """
225        update the graph of a given panel
226        """
227        # Check whether we already have a graph with the same units
[c039589]228        # as the plottable we just received.
229        _, x_unit = data.get_xaxis()
230        _, y_unit = data.get_yaxis()
[783940c]231        flag_x = (panel.graph.prop["xunit"] is not None) and \
232                    (panel.graph.prop["xunit"].strip() != "") and\
[2365c0b]233                    (x_unit != panel.graph.prop["xunit"]) and False
[783940c]234        flag_y = (panel.graph.prop["yunit"] is not None) and \
235                    (panel.graph.prop["yunit"].strip() != "") and\
[2365c0b]236                    (y_unit != panel.graph.prop["yunit"]) and False
[c039589]237        if flag_x and flag_y:
[ae83ad3]238            msg = "Cannot add %s" % str(data.name)
239            msg += " to panel %s\n" % str(panel.window_caption)
240            msg += "Please edit %s's units, labels" % str(data.name)
241            raise ValueError, msg
242        else:
[3658717e]243            if panel.group_id not in data.list_group_id:
244                data.list_group_id.append(panel.group_id)
[4f70cc8]245            wx.CallAfter(panel.plot_data, data)
[ae84427]246
[00b76931]247    def delete_panel(self, group_id):
248        """
249        """
250        if group_id in self.plot_panels.keys():
251            panel = self.plot_panels[group_id]
[e26d0db]252            uid = panel.uid
[c039589]253            wx.PostEvent(self.parent,
[13a63ab]254                         DeletePlotPanelEvent(name=panel.window_caption,
[c039589]255                                              caption=panel.window_caption))
[00b76931]256            del self.plot_panels[group_id]
[cdf515f]257            if uid in self.parent.plot_panels.keys():
258                del self.parent.plot_panels[uid]
[ae84427]259                panel.frame.Destroy()
[00b76931]260            return True
[d97dd6f]261
[00b76931]262        return False
[c039589]263
[41d466f]264    def _on_plot_event(self, event):
265        """
[d955bf19]266        A new plottable is being shipped to the plotting plug-in.
267        Check whether we have a panel to put in on, or create
268        a new one
[c039589]269
[d955bf19]270        :param event: EVT_NEW_PLOT event
[c039589]271
[41d466f]272        """
[940aca7]273        action_check = False
[ae83ad3]274        if hasattr(event, 'action'):
[940aca7]275            action_string = event.action.lower().strip()
276            if action_string == 'check':
277                action_check = True
278            else:
279                group_id = event.group_id
280                if group_id in self.plot_panels.keys():
281                    #remove data from panel
282                    if action_string == 'remove':
[c039589]283                        return self.remove_plot(group_id, event.id)
[940aca7]284                    if action_string == 'hide':
285                        return self.hide_panel(group_id)
286                    if action_string == 'delete':
287                        panel = self.plot_panels[group_id]
288                        uid = panel.uid
289                        return self.parent.delete_panel(uid)
290                    if action_string == "clear":
291                        return self.clear_panel_by_id(group_id)
[c039589]292
293        if not hasattr(event, 'plot'):
[0f17dd9]294            return
[2bdb52b]295        title = None
296        if hasattr(event, 'title'):
[c039589]297            title = 'Graph'  #event.title
[a07e72f]298        data = event.plot
[c039589]299        group_id = data.group_id
[ae83ad3]300        if group_id in self.plot_panels.keys():
[940aca7]301            if action_check:
302                # Check if the plot already exist. if it does, do nothing.
303                if data.id in self.plot_panels[group_id].plots.keys():
[c039589]304                    return
305            #update a panel graph
[ae83ad3]306            panel = self.plot_panels[group_id]
307            self.update_panel(data, panel)
[a07e72f]308        else:
[ae83ad3]309            #create a new panel
[a07e72f]310            if issubclass(data.__class__, Data1D):
[ae83ad3]311                new_panel = self.create_1d_panel(data, group_id)
[a07e72f]312            else:
[940aca7]313                # Need to make the group_id consistent with 1D thus no if below
314                if len(self.plot_panels.values()) > 0:
315                    for p_group_id in self.plot_panels.keys():
316                        p_plot = self.plot_panels[p_group_id]
317                        if data.id in p_plot.plots.keys():
318                            p_plot.plots[data.id] = data
319                            self.plot_panels[group_id] = p_plot
320                            if group_id != p_group_id:
321                                del self.plot_panels[p_group_id]
322                                if p_group_id in data.list_group_id:
323                                    data.list_group_id.remove(p_group_id)
324                                if group_id not in data.list_group_id:
325                                    data.list_group_id.append(group_id)
326                            p_plot.group_id = group_id
327                            return
[c039589]328
[ae83ad3]329                new_panel = self.create_2d_panel(data, group_id)
[c039589]330            self.create_panel_helper(new_panel, data, group_id, title)
[6ffa0dd]331            if hasattr(event, 'xlim'):
332                new_panel.subplot.set_xlim(event.xlim)
333            if hasattr(event, 'ylim'):
334                new_panel.subplot.set_ylim(event.ylim)
[41d466f]335        return
Note: See TracBrowser for help on using the repository browser.