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

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.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since c9dc8c9 was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Remaining modules refactored

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