1 | import wx |
---|
2 | from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg, _convert_agg_to_wx_bitmap |
---|
3 | from matplotlib.backends.backend_agg import FigureCanvasAgg |
---|
4 | from matplotlib.backend_bases import MouseEvent |
---|
5 | |
---|
6 | class FigureCanvas(FigureCanvasWxAgg): |
---|
7 | """ |
---|
8 | Add features to the wx agg canvas for better support of AUI and |
---|
9 | faster plotting. |
---|
10 | """ |
---|
11 | |
---|
12 | def __init__(self, *args, **kw): |
---|
13 | super(FigureCanvas,self).__init__(*args, **kw) |
---|
14 | self._isRendered = False |
---|
15 | |
---|
16 | # Create an timer for handling draw_idle requests |
---|
17 | # If there are events pending when the timer is |
---|
18 | # complete, reset the timer and continue. The |
---|
19 | # alternative approach, binding to wx.EVT_IDLE, |
---|
20 | # doesn't behave as nicely. |
---|
21 | self.idletimer = wx.CallLater(1,self._onDrawIdle) |
---|
22 | # Support for mouse wheel |
---|
23 | self.Bind(wx.EVT_MOUSEWHEEL, self._onMouseWheel) |
---|
24 | |
---|
25 | |
---|
26 | def draw_idle(self, *args, **kwargs): |
---|
27 | """ |
---|
28 | Render after a delay if no other render requests have been made. |
---|
29 | """ |
---|
30 | self.idletimer.Restart(5, *args, **kwargs) # Delay by 5 ms |
---|
31 | |
---|
32 | def _onDrawIdle(self, *args, **kwargs): |
---|
33 | if False and wx.GetApp().Pending(): |
---|
34 | self.idletimer.Restart(5, *args, **kwargs) |
---|
35 | else: |
---|
36 | self.draw(*args, **kwargs) |
---|
37 | |
---|
38 | def draw(self, drawDC=None): |
---|
39 | """ |
---|
40 | Render the figure using agg. |
---|
41 | """ |
---|
42 | |
---|
43 | # Only draw if window is shown, otherwise graph will bleed through |
---|
44 | # on the notebook style AUI widgets. |
---|
45 | if self.IsShownOnScreen(): |
---|
46 | self._isRendered = True |
---|
47 | FigureCanvasWxAgg.draw(self) |
---|
48 | self.bitmap = _convert_agg_to_wx_bitmap(self.get_renderer(), None) |
---|
49 | self.gui_repaint(drawDC=drawDC) |
---|
50 | else: |
---|
51 | self._isRendered = False |
---|
52 | def _onMouseWheel(self, evt): |
---|
53 | """Translate mouse wheel events into matplotlib events""" |
---|
54 | # Determine mouse location |
---|
55 | w,h = self.figure.canvas.get_width_height() |
---|
56 | x = evt.GetX() |
---|
57 | y = h - evt.GetY() |
---|
58 | |
---|
59 | # Convert delta/rotation/rate into a floating point step size |
---|
60 | delta = evt.GetWheelDelta() |
---|
61 | rotation = evt.GetWheelRotation() |
---|
62 | rate = evt.GetLinesPerAction() |
---|
63 | #print "delta,rotation,rate",delta,rotation,rate |
---|
64 | step = rate*float(rotation)/delta |
---|
65 | |
---|
66 | # Convert to mpl event |
---|
67 | evt.Skip() |
---|
68 | self.scroll_event(x, y, step, guiEvent=evt) |
---|
69 | |
---|
70 | def scroll_event(self, x, y, step=1, guiEvent=None): |
---|
71 | """ |
---|
72 | Backend derived classes should call this function on any |
---|
73 | scroll wheel event. x,y are the canvas coords: 0,0 is lower, |
---|
74 | left. button and key are as defined in MouseEvent |
---|
75 | """ |
---|
76 | button = 'up' if step >= 0 else 'down' |
---|
77 | self._button = button |
---|
78 | s = 'scroll_event' |
---|
79 | event = MouseEvent(s, self, x, y, button, self._key, guiEvent=guiEvent) |
---|
80 | setattr(event,'step',step) |
---|
81 | self.callbacks.process(s, event) |
---|
82 | |
---|
83 | |
---|
84 | def _onPaint(self, evt): |
---|
85 | """ |
---|
86 | Called when wxPaintEvt is generated |
---|
87 | """ |
---|
88 | |
---|
89 | if not self._isRealized: |
---|
90 | self.realize() |
---|
91 | |
---|
92 | # Need to draw the graph the first time it is shown otherwise |
---|
93 | # it is a black canvas. After that we can use the rendered |
---|
94 | # bitmap for updates. |
---|
95 | if self._isRendered: |
---|
96 | self.gui_repaint(drawDC=wx.PaintDC(self)) |
---|
97 | else: |
---|
98 | self.draw(drawDC=wx.PaintDC(self)) |
---|
99 | |
---|
100 | evt.Skip() |
---|
101 | |
---|