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

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 b9dbd6b was b9dbd6b, checked in by Mathieu Doucet <doucetm@…>, 9 years ago

pylint fixes and remove old code

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