1 | """ |
---|
2 | Simple Plot Frame : supporting only copy, print, scale |
---|
3 | """ |
---|
4 | import wx |
---|
5 | from sas.guiframe.local_perspectives.plotting.Plotter2D import ModelPanel2D as PlotPanel |
---|
6 | from sas.plottools.toolbar import NavigationToolBar |
---|
7 | from sas.plottools.plottables import Graph |
---|
8 | from sas.guiframe.utils import PanelMenu |
---|
9 | from sas.guiframe.events import StatusEvent |
---|
10 | |
---|
11 | class SimplePlotPanel(PlotPanel): |
---|
12 | """ |
---|
13 | PlotPanel for 1d and 2d |
---|
14 | """ |
---|
15 | _window_caption = 'Simple Plot' |
---|
16 | def __init__(self, parent, id=-1, color=None, |
---|
17 | dpi=None, style=wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs): |
---|
18 | """ |
---|
19 | Init |
---|
20 | """ |
---|
21 | PlotPanel.__init__(self, parent, id=id, style=style, **kwargs) |
---|
22 | |
---|
23 | self.SetColor(wx.WHITE) |
---|
24 | |
---|
25 | self.toolbar = NavigationToolBar(parent=self, canvas=self.canvas) |
---|
26 | self.toolbar.Show(False) |
---|
27 | self.scale = parent.scale |
---|
28 | self.window_caption = self._window_caption |
---|
29 | |
---|
30 | def draw(self): |
---|
31 | """ |
---|
32 | """ |
---|
33 | self.resizing = False |
---|
34 | self.canvas.set_resizing(self.resizing) |
---|
35 | self.canvas.draw() |
---|
36 | |
---|
37 | def add_toolbar(self): |
---|
38 | """ |
---|
39 | """ |
---|
40 | pass |
---|
41 | |
---|
42 | def onContextMenu(self, event): |
---|
43 | """ |
---|
44 | 2D plot context menu |
---|
45 | |
---|
46 | :param event: wx context event |
---|
47 | |
---|
48 | """ |
---|
49 | slicerpop = PanelMenu() |
---|
50 | slicerpop.set_plots(self.plots) |
---|
51 | slicerpop.set_graph(self.graph) |
---|
52 | |
---|
53 | wx_id = wx.NewId() |
---|
54 | slicerpop.Append(wx_id, '&Save Image') |
---|
55 | wx.EVT_MENU(self, wx_id, self.onSaveImage) |
---|
56 | |
---|
57 | wx_id = wx.NewId() |
---|
58 | slicerpop.Append(wx_id, '&Print Image', 'Print image') |
---|
59 | wx.EVT_MENU(self, wx_id, self.onPrint) |
---|
60 | |
---|
61 | wx_id = wx.NewId() |
---|
62 | slicerpop.Append(wx_id, '&Copy to Clipboard', 'Copy to the clipboard') |
---|
63 | wx.EVT_MENU(self, wx_id, self.OnCopyFigureMenu) |
---|
64 | |
---|
65 | if self.dimension != 3: |
---|
66 | slicerpop.AppendSeparator() |
---|
67 | wx_id = wx.NewId() |
---|
68 | slicerpop.Append(wx_id, '&Toggle Grid On/Off', 'Toggle Grid On/Off') |
---|
69 | wx.EVT_MENU(self, wx_id, self.on_grid_onoff) |
---|
70 | |
---|
71 | if self.data.__class__.__name__ == 'Data1D': |
---|
72 | slicerpop.AppendSeparator() |
---|
73 | wx_id = wx.NewId() |
---|
74 | slicerpop.Append(wx_id, '&Change Scale') |
---|
75 | wx.EVT_MENU(self, wx_id, self._onProperties) |
---|
76 | elif self.data2D.__class__.__name__ == 'Data2D': |
---|
77 | slicerpop.AppendSeparator() |
---|
78 | wx_id = wx.NewId() |
---|
79 | slicerpop.Append(wx_id, '&Toggle Linear/Log Scale') |
---|
80 | wx.EVT_MENU(self, wx_id, self._onToggleScale) |
---|
81 | |
---|
82 | try: |
---|
83 | pos_evt = event.GetPosition() |
---|
84 | pos = self.ScreenToClient(pos_evt) |
---|
85 | except: |
---|
86 | pos_x, pos_y = self.toolbar.GetPositionTuple() |
---|
87 | pos = (pos_x, pos_y + 5) |
---|
88 | self.PopupMenu(slicerpop, pos) |
---|
89 | if self.scale != None: |
---|
90 | self.parent.scale2d = self.scale |
---|
91 | |
---|
92 | def on_grid_onoff(self, event): |
---|
93 | """ |
---|
94 | On grid on/off |
---|
95 | """ |
---|
96 | switch = (not self.grid_on) |
---|
97 | self.onGridOnOff(switch) |
---|
98 | |
---|
99 | def onLeftDown(self, event): |
---|
100 | """ |
---|
101 | left button down and ready to drag |
---|
102 | |
---|
103 | """ |
---|
104 | # Check that the LEFT button was pressed |
---|
105 | if event.button == 1: |
---|
106 | self.leftdown = True |
---|
107 | ax = event.inaxes |
---|
108 | if ax != None: |
---|
109 | self.xInit, self.yInit = event.xdata, event.ydata |
---|
110 | try: |
---|
111 | pos_x = float(event.xdata) # / size_x |
---|
112 | pos_y = float(event.ydata) # / size_y |
---|
113 | pos_x = "%8.3g" % pos_x |
---|
114 | pos_y = "%8.3g" % pos_y |
---|
115 | self.position = str(pos_x), str(pos_y) |
---|
116 | wx.PostEvent(self.parent, StatusEvent(status=self.position)) |
---|
117 | except: |
---|
118 | self.position = None |
---|
119 | |
---|
120 | def _OnReSize(self, event): |
---|
121 | """ |
---|
122 | On response of the resize of a panel, set axes_visiable False |
---|
123 | """ |
---|
124 | self.resizing = False |
---|
125 | if self.x_size != None: |
---|
126 | if self.x_size == self.GetSize(): |
---|
127 | self.canvas.set_resizing(self.resizing) |
---|
128 | return |
---|
129 | self.x_size = self.GetSize() |
---|
130 | |
---|
131 | # Ready for another event |
---|
132 | # Do not remove this Skip. |
---|
133 | # Otherwise it will get runtime error on wx>=2.9. |
---|
134 | event.Skip() |
---|
135 | # set the resizing flag |
---|
136 | self.canvas.set_resizing(self.resizing) |
---|
137 | pos_x, pos_y = self.GetPositionTuple() |
---|
138 | if pos_x != 0 and pos_y != 0: |
---|
139 | self.size, _ = self.GetClientSizeTuple() |
---|
140 | self.SetSizer(self.sizer) |
---|
141 | |
---|
142 | def on_set_focus(self, event): |
---|
143 | """ |
---|
144 | By pass default boundary blue color drawing |
---|
145 | """ |
---|
146 | pass |
---|
147 | |
---|
148 | def on_kill_focus(self, event): |
---|
149 | """ |
---|
150 | Reset the panel color |
---|
151 | """ |
---|
152 | pass |
---|
153 | |
---|
154 | def show_plot(self, plot): |
---|
155 | """ |
---|
156 | Show the plot |
---|
157 | """ |
---|
158 | _yaxis, _yunit = plot.get_yaxis() |
---|
159 | _xaxis, _xunit = plot.get_xaxis() |
---|
160 | self.data = plot |
---|
161 | self.plots[plot.name] = plot |
---|
162 | # Axis scales |
---|
163 | if plot.xtransform != None: |
---|
164 | self.xLabel = plot.xtransform |
---|
165 | if plot.ytransform != None: |
---|
166 | self.yLabel = plot.ytransform |
---|
167 | # Init graph |
---|
168 | self.graph = Graph() |
---|
169 | # Add plot: Handles both 1D and 2D |
---|
170 | self.graph.add(self.data) |
---|
171 | self.canvas.set_resizing(False) |
---|
172 | if self.data.__class__.__name__ == 'Data2D': |
---|
173 | self.data2D = plot |
---|
174 | elif self.data.__class__.__name__ == 'Data1D': |
---|
175 | self._onEVT_FUNC_PROPERTY(show=False) |
---|
176 | # Axes |
---|
177 | self.graph.xaxis(_xaxis, _xunit) |
---|
178 | self.graph.yaxis(_yaxis, _yunit) |
---|
179 | self.xaxis(_xaxis, _xunit) |
---|
180 | self.yaxis(_yaxis, _yunit) |
---|
181 | self.set_xscale(self.xscale) |
---|
182 | self.set_yscale(self.yscale) |
---|
183 | self.graph.render(self) |
---|
184 | |
---|
185 | class PlotFrame(wx.Frame): |
---|
186 | """ |
---|
187 | Frame for simple plot |
---|
188 | """ |
---|
189 | def __init__(self, parent, id, title, scale='log_{10}', |
---|
190 | size=wx.Size(550, 470)): |
---|
191 | """ |
---|
192 | comment |
---|
193 | :param parent: parent panel/container |
---|
194 | """ |
---|
195 | # Initialize the Frame object |
---|
196 | wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, size) |
---|
197 | |
---|
198 | # Panel for 1D plot |
---|
199 | self.parent = parent |
---|
200 | self._mgr = None |
---|
201 | self.menu_bar = None |
---|
202 | self._default_save_location = None |
---|
203 | self.scale = scale |
---|
204 | self.plotpanel = SimplePlotPanel(self, -1) |
---|
205 | self._build_menubar() |
---|
206 | |
---|
207 | def _build_menubar(self): |
---|
208 | """ |
---|
209 | Build menubar |
---|
210 | """ |
---|
211 | tsize = (13, 13) |
---|
212 | save_bmp = wx.ArtProvider.GetBitmap(wx.ART_FILE_SAVE_AS, wx.ART_TOOLBAR, tsize) |
---|
213 | quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR, tsize) |
---|
214 | print_bmp = wx.ArtProvider.GetBitmap(wx.ART_PRINT, wx.ART_TOOLBAR, tsize) |
---|
215 | preview_bmp = wx.ArtProvider.GetBitmap(wx.ART_REPORT_VIEW, wx.ART_TOOLBAR, tsize) |
---|
216 | copy_bmp = wx.ArtProvider.GetBitmap(wx.ART_COPY, wx.ART_TOOLBAR, tsize) |
---|
217 | menu_bar = wx.MenuBar() |
---|
218 | |
---|
219 | menu = wx.Menu() |
---|
220 | id = wx.NewId() |
---|
221 | item = wx.MenuItem(menu, id, "&Save Image") |
---|
222 | item.SetBitmap(save_bmp) |
---|
223 | menu.AppendItem(item) |
---|
224 | wx.EVT_MENU(self, id, self.on_save_file) |
---|
225 | |
---|
226 | id = wx.NewId() |
---|
227 | item = wx.MenuItem(menu, id, "&Print Image") |
---|
228 | item.SetBitmap(print_bmp) |
---|
229 | menu.AppendItem(item) |
---|
230 | wx.EVT_MENU(self, id, self.on_print_image) |
---|
231 | |
---|
232 | menu.AppendSeparator() |
---|
233 | id = wx.NewId() |
---|
234 | item = wx.MenuItem(menu, id, "&Quit") |
---|
235 | item.SetBitmap(quit_bmp) |
---|
236 | menu.AppendItem(item) |
---|
237 | |
---|
238 | menu_bar.Append(menu, "&File") |
---|
239 | wx.EVT_MENU(self, id, self.on_close) |
---|
240 | |
---|
241 | menu_edit = wx.Menu() |
---|
242 | id = wx.NewId() |
---|
243 | item = wx.MenuItem(menu_edit, id, "&Copy") |
---|
244 | item.SetBitmap(copy_bmp) |
---|
245 | menu_edit.AppendItem(item) |
---|
246 | wx.EVT_MENU(self, id, self.on_copy_image) |
---|
247 | |
---|
248 | menu_bar.Append(menu_edit, "&Edit") |
---|
249 | self.menu_bar = menu_bar |
---|
250 | self.SetMenuBar(self.menu_bar) |
---|
251 | |
---|
252 | def set_plot_unfocus(self): |
---|
253 | """ |
---|
254 | un focusing |
---|
255 | """ |
---|
256 | pass |
---|
257 | |
---|
258 | def add_plot(self, plot): |
---|
259 | """ |
---|
260 | Add Image |
---|
261 | """ |
---|
262 | plotpanel = self.plotpanel |
---|
263 | plotpanel.scale = self.scale |
---|
264 | plotpanel.show_plot(plot) |
---|
265 | |
---|
266 | def set_schedule_full_draw(self, panel, func): |
---|
267 | """ |
---|
268 | """ |
---|
269 | self.plotpanel.resizing = False |
---|
270 | |
---|
271 | def im_show(self, img): |
---|
272 | """ |
---|
273 | Show background image |
---|
274 | :Param img: [imread(path) from matplotlib.pyplot] |
---|
275 | """ |
---|
276 | self.plotpanel.subplot.imshow(img) |
---|
277 | |
---|
278 | def set_schedule(self, schedule=False): |
---|
279 | """ |
---|
280 | """ |
---|
281 | #Not implemented |
---|
282 | |
---|
283 | def disable_app_menu(self, panel): |
---|
284 | """ |
---|
285 | """ |
---|
286 | #Not implemented |
---|
287 | |
---|
288 | def get_current_context_menu(self, plotpanel): |
---|
289 | """ |
---|
290 | """ |
---|
291 | #Not implemented |
---|
292 | |
---|
293 | def on_save_file(self, event): |
---|
294 | """ |
---|
295 | Save image |
---|
296 | """ |
---|
297 | self.plotpanel.onSaveImage(event) |
---|
298 | |
---|
299 | def on_print_image(self, event): |
---|
300 | """ |
---|
301 | Save image |
---|
302 | """ |
---|
303 | self.plotpanel.onPrint(event) |
---|
304 | |
---|
305 | def on_print_preview(self, event): |
---|
306 | """ |
---|
307 | Save image |
---|
308 | """ |
---|
309 | self.plotpanel.onPrinterPreview(event) |
---|
310 | |
---|
311 | def on_copy_image(self, event): |
---|
312 | """ |
---|
313 | Save image |
---|
314 | """ |
---|
315 | self.plotpanel.OnCopyFigureMenu(event) |
---|
316 | |
---|
317 | def on_close(self, event): |
---|
318 | """ |
---|
319 | On Close |
---|
320 | """ |
---|
321 | try: |
---|
322 | self.parent.set_scale2d(self.scale) |
---|
323 | self.parent.on_panel_close(event) |
---|
324 | except: |
---|
325 | self.Destroy() |
---|
326 | |
---|