source: sasview/guiframe/local_perspectives/plotting/plotting.py @ ddeeb02

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 ddeeb02 was 6d727ae, checked in by Jae Cho <jhjcho@…>, 14 years ago

moving a new feature (fast reacting plot panels) from branch

  • Property mode set to 100644
File size: 10.5 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)
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################################################################################
[41d466f]13
14import wx
15import sys
[f444b20]16from sans.guiframe.events import EVT_NEW_PLOT
17from sans.guiframe.events import StatusEvent
[7f84e22]18from sans.guiframe.plugin_base import PluginBase
[a07e72f]19from sans.guiframe.dataFitting import Data1D
20from sans.guiframe.dataFitting import Data2D
[7764b41]21
[00b76931]22DEFAULT_MENU_ITEM_LABEL = "No plot available"
23DEFAULT_MENU_ITEM_ID = wx.NewId()
24
[7f84e22]25class Plugin(PluginBase):
[41d466f]26    """
[d955bf19]27    Plug-in class to be instantiated by the GUI manager
[41d466f]28    """
29   
[7f84e22]30    def __init__(self, standalone=False):
31        PluginBase.__init__(self, name="Plotting", standalone=standalone)
32     
[41d466f]33        ## Plot panels
[ae83ad3]34        self.plot_panels = {}
[a07e72f]35        self._panel_on_focus = None
[00b76931]36        self.menu_default_id = None
[6d727ae]37        # Plot menu
38        self.menu = None
39
[a07e72f]40     
41    def set_panel_on_focus(self, panel):
42        """
43        """
44        self._panel_on_focus = panel
45       
[b7c7a1c]46    def is_always_active(self):
47        """
48        return True is this plugin is always active even if the user is
49        switching between perspectives
50        """
51        return True
52   
[bf4402c3]53    def populate_menu(self, parent):
[41d466f]54        """
[d955bf19]55        Create a 'Plot' menu to list the panels
56        available for displaying
57       
58        :param id: next available unique ID for wx events
59        :param parent: parent window
60       
[41d466f]61        """
62        self.menu = wx.Menu()
[00b76931]63        self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, 
[6d727ae]64                             "No graph available")
[2fc9243]65        self.menu.FindItemByPosition(0).Enable(False)
[6d727ae]66        return [(self.menu, "Graph")]
[41d466f]67   
68    def get_panels(self, parent):
69        """
[d955bf19]70        Create and return a list of panel objects
[41d466f]71        """
72        ## Save a reference to the parent
73        self.parent = parent
74        # Connect to plotting events
75        self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event)
76        # We have no initial panels for this plug-in
77        return []
[54cc36a]78   
[41d466f]79    def _on_show_panel(self, event):
[6c0568b]80        """show plug-in panel"""
81        pass
82   
[ae83ad3]83    def remove_plot(self, group_id, id):
84        """
85        remove plot of ID = id from a panel of group ID =group_id
86        """
87       
88        if group_id in self.plot_panels.keys():
89            panel = self.plot_panels[group_id]
90            panel.remove_data_by_id(id=id)
91            return True
[783940c]92        return False
93       
[957723f]94    def clear_panel(self):
95        """
96        """
97        pass
98   
99    def clear_panel_by_id(self, group_id):
[783940c]100        """
101        clear the graph
102        """
103        if group_id in self.plot_panels.keys():
104            panel = self.plot_panels[group_id]
105            panel.graph.reset()
106            return True
107        return False
108           
[ae83ad3]109       
110    def hide_panel(self, group_id):
111        """
112        hide panel with group ID = group_id
113        """
114        if group_id in self.plot_panels.keys():
115            panel = self.plot_panels[group_id]
116            self.parent.hide_panel(panel.uid)
117            return True
118        return False
119   
[2bdb52b]120    def create_panel_helper(self, new_panel, data, group_id, title=None):
[ae83ad3]121        """
122        """
123         ## Set group ID if available
124        ## Assign data properties to the new create panel
125        new_panel.set_manager(self)
126        new_panel.group_id = group_id
[e88ebfd]127        if group_id not in data.list_group_id:
128            data.list_group_id.append(group_id)
[2bdb52b]129        if title is None:
130            title = data.title
[ae83ad3]131        new_panel.window_caption = title
[2bdb52b]132        new_panel.window_name = data.title
[ae83ad3]133        event_id = self.parent.popup_panel(new_panel)
134        #remove the default item in the menu
135        if len(self.plot_panels) == 0:
[00b76931]136            pos = self.menu.FindItem(DEFAULT_MENU_ITEM_LABEL)
137            if pos != -1:
138                self.menu.Delete(DEFAULT_MENU_ITEM_ID)
139           
[6d727ae]140        self.menu.AppendCheckItem(event_id, new_panel.window_caption, 
[ae83ad3]141                         "Show %s plot panel" % new_panel.window_caption)
[6d727ae]142        self.menu.Check(event_id, True)
143        wx.EVT_MENU(self.parent, event_id, self._on_check_menu)
144        wx.EVT_SHOW(new_panel, self._on_close_panel)
[ae83ad3]145        # Set UID to allow us to reference the panel later
146        new_panel.uid = event_id
147        # Ship the plottable to its panel
148        new_panel.plot_data(data) 
149        self.plot_panels[new_panel.group_id] = new_panel
[00b76931]150       
[ae83ad3]151       
152    def create_1d_panel(self, data, group_id):
153        """
154        """
155        # Create a new plot panel if none was available       
156        if issubclass(data.__class__, Data1D):
157            from Plotter1D import ModelPanel1D
158            ## get the data representation label of the data to plot
159            ## when even the user select "change scale"
160            xtransform = data.xtransform
161            ytransform = data.ytransform
162            ## create a plotpanel for 1D Data
163            new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform,
164                     ytransform=ytransform, style=wx.RAISED_BORDER)
165            return  new_panel
166       
167        msg = "1D Panel of group ID %s could not be created" % str(group_id)
168        raise ValueError, msg
169   
170    def create_2d_panel(self, data, group_id):
171        """
172        """
173        if issubclass(data.__class__, Data2D):
174            ##Create a new plotpanel for 2D data
175            from Plotter2D import ModelPanel2D
176            scale = data.scale
177            new_panel = ModelPanel2D(self.parent, id = -1,
178                                data2d=data, scale = scale, 
179                                style=wx.RAISED_BORDER)
180            return new_panel
181        msg = "2D Panel of group ID %s could not be created" % str(group_id)
182        raise ValueError, msg
183   
184    def update_panel(self, data, panel):
185        """
186        update the graph of a given panel
187        """
188        # Check whether we already have a graph with the same units
189        # as the plottable we just received.
190        _, x_unit =  data.get_xaxis()
191        _, y_unit =  data.get_yaxis()
[783940c]192        flag_x = (panel.graph.prop["xunit"] is not None) and \
193                    (panel.graph.prop["xunit"].strip() != "") and\
194                    (x_unit != panel.graph.prop["xunit"])
195        flag_y = (panel.graph.prop["yunit"] is not None) and \
196                    (panel.graph.prop["yunit"].strip() != "") and\
197                    (x_unit != panel.graph.prop["yunit"])
198        if (flag_x and flag_y):
[ae83ad3]199            msg = "Cannot add %s" % str(data.name)
200            msg += " to panel %s\n" % str(panel.window_caption)
201            msg += "Please edit %s's units, labels" % str(data.name)
202            raise ValueError, msg
203        else:
[3658717e]204            if panel.group_id not in data.list_group_id:
205                data.list_group_id.append(panel.group_id)
[ae83ad3]206            panel.plot_data( data)
207            self.parent.show_panel(panel.uid)   
208   
[df22224]209    def delete_menu_item(self, name, uid):
210        """
211        """
212        #remove menu item
213        pos = self.menu.FindItem(name) 
214        if pos != -1:
215            self.menu.Delete(uid)
216        if self.menu.GetMenuItemCount() == 0:
217            self.menu.Append(DEFAULT_MENU_ITEM_ID, DEFAULT_MENU_ITEM_LABEL, 
218                             "No plot available")
219            self.menu.FindItemByPosition(0).Enable(False)
220       
[00b76931]221    def delete_panel(self, group_id):
222        """
223        """
224        if group_id in self.plot_panels.keys():
225            panel = self.plot_panels[group_id]
226            #remove menu item
[df22224]227            self.delete_menu_item(panel.window_caption, panel.uid)
[00b76931]228            del self.plot_panels[group_id]
229            return True
230        return False
231   
[41d466f]232    def _on_plot_event(self, event):
233        """
[d955bf19]234        A new plottable is being shipped to the plotting plug-in.
235        Check whether we have a panel to put in on, or create
236        a new one
237       
238        :param event: EVT_NEW_PLOT event
239       
[41d466f]240        """
[ae83ad3]241        if hasattr(event, 'action'):
[a07e72f]242            group_id = event.group_id
[ae83ad3]243            #remove data from panel
244            if event.action.lower() == 'remove':
245                id = event.id
246                return self.remove_plot(group_id, id)
247            if event.action.lower() == 'hide':
248                return self.hide_panel(group_id)
[00b76931]249            if event.action.lower() == 'delete':
250                return self.delete_panel(group_id)
[783940c]251            if event.action.lower() == "clear":
[957723f]252                return self.clear_panel_by_id(group_id)
[783940c]253           
[2bdb52b]254        title = None
255        if hasattr(event, 'title'):
256            title = event.title
[00b76931]257               
[a07e72f]258        data = event.plot
[e88ebfd]259        group_id = data.group_id
[ae83ad3]260           
261        if group_id in self.plot_panels.keys():
262            #update a panel graph
263            panel = self.plot_panels[group_id]
264            self.update_panel(data, panel)
[a07e72f]265        else:
[ae83ad3]266            #create a new panel
[a07e72f]267            if issubclass(data.__class__, Data1D):
[ae83ad3]268                new_panel = self.create_1d_panel(data, group_id)
[a07e72f]269            else:
[ae83ad3]270                new_panel = self.create_2d_panel(data, group_id)
[2bdb52b]271            self.create_panel_helper(new_panel, data, group_id, title)
[4158af4]272     
[41d466f]273        return
[6d727ae]274
275    def _on_check_menu(self, event):
276        """
277        Check mark on menu
278        """
279        event.Skip()
280        event_id = event.GetId()
281       
282        #self.menu.Enable(event_id, False)
283        self.parent._on_view(event)
284        if not self.menu.IsChecked(event_id):
285            self.menu.Check(event_id, True)
286
287       
288    def _on_close_panel(self, event):
289        """
290        Update check mark on panel's show and hide event
291        """
292        event.Skip()
293        panel = event.GetEventObject()
294        id = panel.uid
295        if event.GetShow() and self.parent._mgr.GetPane(panel).IsShown():
296            if not self.menu.IsChecked(id):
297                self.menu.Check(id, True)
298                #self.menu.Enable(id, False)
299        else:
300            try:
301                if self.menu.IsChecked(id):
302                    self.menu.Check(id, False)
303                    #self.menu.Enable(id, True)
304            except:
305                pass 
Note: See TracBrowser for help on using the repository browser.