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