@@ -58,23 +58,23 @@ class FigureCanvasQTAggBase(object):
58
58
59
59
Public attribute
60
60
61
- figure - A Figure instance
62
- """
61
+ figure - A Figure instance
62
+ """
63
+
64
+ def __init__ (self , figure ):
65
+ super (FigureCanvasQTAggBase , self ).__init__ (figure = figure )
66
+ self ._agg_draw_pending = False
63
67
64
68
def drawRectangle (self , rect ):
65
69
self ._drawRect = rect
66
- self .draw_idle ()
70
+ self .update ()
67
71
68
72
def paintEvent (self , e ):
69
73
"""
70
74
Copy the image from the Agg canvas to the qt.drawable.
71
75
In Qt, all drawing should be done inside of here when a widget is
72
76
shown onscreen.
73
77
"""
74
- # If we have not rendered the Agg backend yet, do so now.
75
- if not hasattr (self , 'renderer' ):
76
- FigureCanvasAgg .draw (self )
77
-
78
78
# FigureCanvasQT.paintEvent(self, e)
79
79
if DEBUG :
80
80
print ('FigureCanvasQtAgg.paintEvent: ' , self ,
@@ -136,21 +136,44 @@ def paintEvent(self, e):
136
136
pixmap = QtGui .QPixmap .fromImage (qImage )
137
137
p = QtGui .QPainter (self )
138
138
p .drawPixmap (QtCore .QPoint (l , self .renderer .height - t ), pixmap )
139
+
140
+ # draw the zoom rectangle to the QPainter
141
+ if self ._drawRect is not None :
142
+ p .setPen (QtGui .QPen (QtCore .Qt .black , 1 , QtCore .Qt .DotLine ))
143
+ x , y , w , h = self ._drawRect
144
+ p .drawRect (x , y , w , h )
139
145
p .end ()
140
146
self .blitbox = None
141
- self ._drawRect = None
142
147
143
148
def draw (self ):
144
149
"""
145
- Draw the figure with Agg, and queue a request
146
- for a Qt draw.
150
+ Draw the figure with Agg, and queue a request for a Qt draw.
147
151
"""
148
- # The Agg draw is done here; delaying it until the paintEvent
149
- # causes problems with code that uses the result of the
150
- # draw() to update plot elements.
152
+ # The Agg draw is done here; delaying causes problems with code that
153
+ # uses the result of the draw() to update plot elements.
151
154
FigureCanvasAgg .draw (self )
152
155
self .update ()
153
156
157
+ def draw_idle (self ):
158
+ """
159
+ Queue redraw of the Agg buffer and request Qt paintEvent.
160
+ """
161
+ # The Agg draw needs to be handled by the same thread matplotlib
162
+ # modifies the scene graph from. Post Agg draw request to the
163
+ # current event loop in order to ensure thread affinity and to
164
+ # accumulate multiple draw requests from event handling.
165
+ # TODO: queued signal connection might be safer than singleShot
166
+ if not self ._agg_draw_pending :
167
+ self ._agg_draw_pending = True
168
+ QtCore .QTimer .singleShot (0 , self .__draw_idle_agg )
169
+
170
+ def __draw_idle_agg (self , * args ):
171
+ try :
172
+ FigureCanvasAgg .draw (self )
173
+ self .update ()
174
+ finally :
175
+ self ._agg_draw_pending = False
176
+
154
177
def blit (self , bbox = None ):
155
178
"""
156
179
Blit the region in bbox
@@ -186,8 +209,7 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase,
186
209
def __init__ (self , figure ):
187
210
if DEBUG :
188
211
print ('FigureCanvasQtAgg: ' , figure )
189
- FigureCanvasQT .__init__ (self , figure )
190
- FigureCanvasAgg .__init__ (self , figure )
212
+ super (FigureCanvasQTAgg , self ).__init__ (figure = figure )
191
213
self ._drawRect = None
192
214
self .blitbox = None
193
215
self .setAttribute (QtCore .Qt .WA_OpaquePaintEvent )
0 commit comments