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

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 d85c194 was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 8 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 11.4 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.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
23DEFAULT_MENU_ITEM_LABEL = "No graph available"
24DEFAULT_MENU_ITEM_ID = wx.NewId()
25
26IS_WIN = True
27if sys.platform.count("win32") == 0:
28    if int(str(wx.__version__).split('.')[0]) == 2:
29        if int(str(wx.__version__).split('.')[1]) < 9:
30            IS_WIN = False
31
32
33class Plugin(PluginBase):
34    """
35    Plug-in class to be instantiated by the GUI manager
36    """
37
38    def __init__(self):
39        PluginBase.__init__(self, name="Plotting")
40
41        ## Plot panels
42        self.plot_panels = {}
43        self._panel_on_focus = None
44        self.menu_default_id = None
45        # Plot menu
46        self.menu = None
47
48
49    def set_panel_on_focus(self, panel):
50        """
51        """
52        self._panel_on_focus = panel
53
54    def is_always_active(self):
55        """
56        return True is this plugin is always active even if the user is
57        switching between perspectives
58        """
59        return True
60
61    def populate_menu(self, parent):
62        """
63        Create a 'Plot' menu to list the panels
64        available for displaying
65
66        :param id: next available unique ID for wx events
67        :param parent: parent window
68
69        """
70        return []
71
72    def get_panels(self, parent):
73        """
74        Create and return a list of panel objects
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)
80        self.parent.Bind(EVT_PLOT_QRANGE, self._on_plot_qrange)
81        # We have no initial panels for this plug-in
82        return []
83
84    def _on_plot_qrange(self, event=None):
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)
97
98    def _on_show_panel(self, event):
99        """show plug-in panel"""
100        pass
101
102    def remove_plot(self, group_id, id):
103        """
104        remove plot of ID = id from a panel of group ID =group_id
105        """
106
107        if group_id in self.plot_panels.keys():
108            panel = self.plot_panels[group_id]
109            panel.remove_data_by_id(id=id)
110
111            return True
112        return False
113
114    def clear_panel(self):
115        """
116        Clear and Hide all plot panels, and remove them from menu
117        """
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)
122        self.plot_panels = {}
123
124    def clear_panel_by_id(self, group_id):
125        """
126        clear the graph
127        """
128        if group_id in self.plot_panels.keys():
129            panel = self.plot_panels[group_id]
130            for plottable in panel.graph.plottables.keys():
131                self.remove_plot(group_id, plottable.id)
132            panel.graph.reset()
133            return True
134        return False
135
136    def hide_panel(self, group_id):
137        """
138        hide panel with group ID = group_id
139        """
140        # Not implemeted
141        return False
142
143    def create_panel_helper(self, new_panel, data, group_id, title=None):
144        """
145        """
146        ## Set group ID if available
147        ## Assign data properties to the new create panel
148        new_panel.set_manager(self)
149        new_panel.group_id = group_id
150        if group_id not in data.list_group_id:
151            data.list_group_id.append(group_id)
152        if title is None:
153            title = data.title
154        new_panel.window_caption = title
155        new_panel.window_name = data.title
156        event_id = self.parent.popup_panel(new_panel)
157
158        # Set UID to allow us to reference the panel later
159        new_panel.uid = event_id
160        # Ship the plottable to its panel
161        wx.CallAfter(new_panel.plot_data, data)
162        #new_panel.canvas.set_resizing(new_panel.resizing)
163        self.plot_panels[new_panel.group_id] = new_panel
164
165    def create_1d_panel(self, data, group_id):
166        """
167        """
168        # Create a new plot panel if none was available
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
176            win = MDIFrame(self.parent, None, 'None', (100, 200))
177            new_panel = ModelPanel1D(win, -1, xtransform=xtransform,
178                                     ytransform=ytransform, style=wx.RAISED_BORDER)
179            win.set_panel(new_panel)
180            win.Show(False)
181            new_panel.frame = win
182            #win.Show(True)
183            return  new_panel
184
185        msg = "1D Panel of group ID %s could not be created" % str(group_id)
186        raise ValueError, msg
187
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
195            win = MDIFrame(self.parent, None, 'None', (200, 150))
196            win.Show(False)
197            new_panel = ModelPanel2D(win, id=-1,
198                                     data2d=data, scale=scale,
199                                     style=wx.RAISED_BORDER)
200            win.set_panel(new_panel)
201            new_panel.frame = win
202            return new_panel
203        msg = "2D Panel of group ID %s could not be created" % str(group_id)
204        raise ValueError, msg
205
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
211        # as the plottable we just received.
212        _, x_unit = data.get_xaxis()
213        _, y_unit = data.get_yaxis()
214        flag_x = (panel.graph.prop["xunit"] is not None) and \
215                    (panel.graph.prop["xunit"].strip() != "") and\
216                    (x_unit != panel.graph.prop["xunit"]) and False
217        flag_y = (panel.graph.prop["yunit"] is not None) and \
218                    (panel.graph.prop["yunit"].strip() != "") and\
219                    (y_unit != panel.graph.prop["yunit"]) and False
220        if flag_x and flag_y:
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:
226            if panel.group_id not in data.list_group_id:
227                data.list_group_id.append(panel.group_id)
228            wx.CallAfter(panel.plot_data, data)
229
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]
235            uid = panel.uid
236            wx.PostEvent(self.parent,
237                         DeletePlotPanelEvent(name=panel.window_caption,
238                                              caption=panel.window_caption))
239            del self.plot_panels[group_id]
240            if uid in self.parent.plot_panels.keys():
241                del self.parent.plot_panels[uid]
242                panel.frame.Destroy()
243            return True
244
245        return False
246
247    def _on_plot_event(self, event):
248        """
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
252
253        :param event: EVT_NEW_PLOT event
254
255        """
256        action_check = False
257        if hasattr(event, 'action'):
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':
266                        return self.remove_plot(group_id, event.id)
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)
275
276        if not hasattr(event, 'plot'):
277            return
278        title = None
279        if hasattr(event, 'title'):
280            title = 'Graph'  #event.title
281        data = event.plot
282        group_id = data.group_id
283        if group_id in self.plot_panels.keys():
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():
287                    return
288            #update a panel graph
289            panel = self.plot_panels[group_id]
290            self.update_panel(data, panel)
291        else:
292            #create a new panel
293            if issubclass(data.__class__, Data1D):
294                new_panel = self.create_1d_panel(data, group_id)
295            else:
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
311
312                new_panel = self.create_2d_panel(data, group_id)
313            self.create_panel_helper(new_panel, data, group_id, title)
314        return
Note: See TracBrowser for help on using the repository browser.