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 | |
---|
14 | import wx |
---|
15 | import sys |
---|
16 | from sans.guicomm.events import EVT_NEW_PLOT |
---|
17 | from sans.guicomm.events import StatusEvent |
---|
18 | |
---|
19 | from sans.guiframe.plugin_base import PluginBase |
---|
20 | |
---|
21 | class Plugin(PluginBase): |
---|
22 | """ |
---|
23 | Plug-in class to be instantiated by the GUI manager |
---|
24 | """ |
---|
25 | |
---|
26 | def __init__(self, standalone=False): |
---|
27 | PluginBase.__init__(self, name="Plotting", standalone=standalone) |
---|
28 | |
---|
29 | ## Plot panels |
---|
30 | self.plot_panels = [] |
---|
31 | |
---|
32 | def is_always_active(self): |
---|
33 | """ |
---|
34 | return True is this plugin is always active even if the user is |
---|
35 | switching between perspectives |
---|
36 | """ |
---|
37 | return True |
---|
38 | |
---|
39 | def populate_menu(self, id, parent): |
---|
40 | """ |
---|
41 | Create a 'Plot' menu to list the panels |
---|
42 | available for displaying |
---|
43 | |
---|
44 | :param id: next available unique ID for wx events |
---|
45 | :param parent: parent window |
---|
46 | |
---|
47 | """ |
---|
48 | self.menu = wx.Menu() |
---|
49 | |
---|
50 | self.menu.Append(wx.NewId(), "No plot available", |
---|
51 | "No plot available") |
---|
52 | self.menu.FindItemByPosition(0).Enable(False) |
---|
53 | return [(id, self.menu, "Plot")] |
---|
54 | |
---|
55 | def get_panels(self, parent): |
---|
56 | """ |
---|
57 | Create and return a list of panel objects |
---|
58 | """ |
---|
59 | ## Save a reference to the parent |
---|
60 | self.parent = parent |
---|
61 | # Connect to plotting events |
---|
62 | self.parent.Bind(EVT_NEW_PLOT, self._on_plot_event) |
---|
63 | # We have no initial panels for this plug-in |
---|
64 | return [] |
---|
65 | |
---|
66 | def _on_show_panel(self, event): |
---|
67 | """show plug-in panel""" |
---|
68 | pass |
---|
69 | |
---|
70 | def _on_plot_event(self, event): |
---|
71 | """ |
---|
72 | A new plottable is being shipped to the plotting plug-in. |
---|
73 | Check whether we have a panel to put in on, or create |
---|
74 | a new one |
---|
75 | |
---|
76 | :param event: EVT_NEW_PLOT event |
---|
77 | |
---|
78 | """ |
---|
79 | # Check whether we already have a graph with the same units |
---|
80 | # as the plottable we just received. |
---|
81 | |
---|
82 | is_available = False |
---|
83 | for panel in self.plot_panels: |
---|
84 | if event.plot._xunit == panel.graph.prop["xunit_base"] \ |
---|
85 | and event.plot._yunit == panel.graph.prop["yunit_base"]: |
---|
86 | if hasattr(event.plot, "group_id"): |
---|
87 | ## if same group_id used the same panel to plot |
---|
88 | if not event.plot.group_id == None \ |
---|
89 | and event.plot.group_id == panel.group_id: |
---|
90 | is_available = True |
---|
91 | panel._onEVT_1DREPLOT(event) |
---|
92 | self.parent.show_panel(panel.uid) |
---|
93 | else: |
---|
94 | # Check that the plot panel has no group ID |
---|
95 | ## Use a panel with group_id ==None to plot |
---|
96 | if panel.group_id == None: |
---|
97 | is_available = True |
---|
98 | panel._onEVT_1DREPLOT(event) |
---|
99 | self.parent.show_panel(panel.uid) |
---|
100 | # Create a new plot panel if none was available |
---|
101 | if not is_available: |
---|
102 | #print"event.plot",hasattr(event.plot,'data') |
---|
103 | if not hasattr(event.plot, 'data'): |
---|
104 | from Plotter1D import ModelPanel1D |
---|
105 | ## get the data representation label of the data to plot |
---|
106 | ## when even the user select "change scale" |
---|
107 | if hasattr(event.plot, "xtransform"): |
---|
108 | xtransform = event.plot.xtransform |
---|
109 | else: |
---|
110 | xtransform = None |
---|
111 | |
---|
112 | if hasattr(event.plot, "ytransform"): |
---|
113 | ytransform = event.plot.ytransform |
---|
114 | else: |
---|
115 | ytransform = None |
---|
116 | ## create a plotpanel for 1D Data |
---|
117 | new_panel = ModelPanel1D(self.parent, -1, xtransform=xtransform, |
---|
118 | ytransform=ytransform, style=wx.RAISED_BORDER) |
---|
119 | else: |
---|
120 | ##Create a new plotpanel for 2D data |
---|
121 | from Plotter2D import ModelPanel2D |
---|
122 | new_panel = ModelPanel2D(self.parent, id = -1, |
---|
123 | data2d=event.plot, style=wx.RAISED_BORDER) |
---|
124 | ## Set group ID if available |
---|
125 | ## Assign data properties to the new create panel |
---|
126 | group_id_str = '' |
---|
127 | if hasattr(event.plot, "group_id"): |
---|
128 | if not event.plot.group_id == None: |
---|
129 | new_panel.group_id = event.plot.group_id |
---|
130 | group_id_str = ' [%s]' % event.plot.group_id |
---|
131 | if hasattr(event, "title"): |
---|
132 | new_panel.window_caption = event.title |
---|
133 | new_panel.window_name = event.title |
---|
134 | event_id = self.parent.popup_panel(new_panel) |
---|
135 | #remove the default item in the menu |
---|
136 | if len(self.plot_panels) == 0: |
---|
137 | self.menu.RemoveItem(self.menu.FindItemByPosition(0)) |
---|
138 | self.menu.Append(event_id, new_panel.window_caption, |
---|
139 | "Show %s plot panel" % new_panel.window_caption) |
---|
140 | # Set UID to allow us to reference the panel later |
---|
141 | new_panel.uid = event_id |
---|
142 | # Ship the plottable to its panel |
---|
143 | new_panel._onEVT_1DREPLOT(event) |
---|
144 | self.plot_panels.append(new_panel) |
---|
145 | |
---|
146 | return |
---|
147 | |
---|