@@ -58,23 +58,23 @@ class FigureCanvasQTAggBase(object):
5858
5959 Public attribute
6060
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
6367
6468 def drawRectangle (self , rect ):
6569 self ._drawRect = rect
66- self .draw_idle ()
70+ self .update ()
6771
6872 def paintEvent (self , e ):
6973 """
7074 Copy the image from the Agg canvas to the qt.drawable.
7175 In Qt, all drawing should be done inside of here when a widget is
7276 shown onscreen.
7377 """
74- # If we have not rendered the Agg backend yet, do so now.
75- if not hasattr (self , 'renderer' ):
76- FigureCanvasAgg .draw (self )
77-
7878 # FigureCanvasQT.paintEvent(self, e)
7979 if DEBUG :
8080 print ('FigureCanvasQtAgg.paintEvent: ' , self ,
@@ -136,21 +136,44 @@ def paintEvent(self, e):
136136 pixmap = QtGui .QPixmap .fromImage (qImage )
137137 p = QtGui .QPainter (self )
138138 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 )
139145 p .end ()
140146 self .blitbox = None
141- self ._drawRect = None
142147
143148 def draw (self ):
144149 """
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.
147151 """
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.
151154 FigureCanvasAgg .draw (self )
152155 self .update ()
153156
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+
154177 def blit (self , bbox = None ):
155178 """
156179 Blit the region in bbox
@@ -186,8 +209,7 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase,
186209 def __init__ (self , figure ):
187210 if DEBUG :
188211 print ('FigureCanvasQtAgg: ' , figure )
189- FigureCanvasQT .__init__ (self , figure )
190- FigureCanvasAgg .__init__ (self , figure )
212+ super (FigureCanvasQTAgg , self ).__init__ (figure = figure )
191213 self ._drawRect = None
192214 self .blitbox = None
193215 self .setAttribute (QtCore .Qt .WA_OpaquePaintEvent )
0 commit comments