Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Qt: Redraw agg buffer in main thread. Implement FigureCanvasQTAggBase…
… init.
  • Loading branch information
pwuertz committed Aug 27, 2015
commit 6a74cda44d8f467b4d5d2ac25e6ce7ac1efe3852
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_qt4agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def __init__(self, figure):
if DEBUG:
print('FigureCanvasQtAgg: ', figure)
FigureCanvasQT.__init__(self, figure)
FigureCanvasQTAggBase.__init__(self, figure)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and not using super here. This was a change between qt4 and qt5 that I am not 100% sure we are handling correctly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also a bit confused concerning that change from qt4 to qt5. They prefer cooperative multiple inheritance now, while I got used to explicitly/manually calling all init functions, which is required when using PyQt4 I guess.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok this looks right to me now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With MEP27 we begin to introduce super into the backends, as we work to standardise the backends. Currently we do it for all of the GUI elements, apart from the canvas. Wx has an odd call signature for the constructor which makes super in FigureCanvas impossible until I have that fixed on master. I do have it fixed in my Wx branch of MEP27 but naturally that will wait until we have the main MEP27 PR into master.

FigureCanvasAgg.__init__(self, figure)
self._drawRect = None
self.blitbox = None
Expand Down
18 changes: 0 additions & 18 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ def __init__(self, figure):
super(FigureCanvasQT, self).__init__(figure=figure)
self.figure = figure
self.setMouseTracking(True)
self._idle = True
w, h = self.get_width_height()
self.resize(w, h)

Expand Down Expand Up @@ -415,23 +414,6 @@ def stop_event_loop(self):

stop_event_loop.__doc__ = FigureCanvasBase.stop_event_loop_default.__doc__

def draw_idle(self):
# This cannot be a call to 'update', we need a slightly longer
# delay, otherwise mouse releases from zooming, panning, or
# lassoing might not finish processing and will not redraw properly.
# We use the guard flag to prevent infinite calls to 'draw_idle' which
# happens with the 'stale' figure & axes callbacks.
d = self._idle
self._idle = False

def idle_draw(*args):
try:
self.draw()
finally:
self._idle = True
if d:
QtCore.QTimer.singleShot(0, idle_draw)


class MainWindow(QtWidgets.QMainWindow):
closing = QtCore.Signal()
Expand Down
43 changes: 30 additions & 13 deletions lib/matplotlib/backends/backend_qt5agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ class FigureCanvasQTAggBase(object):

Public attribute

figure - A Figure instance
"""
figure - A Figure instance
"""

def __init__(self, figure):
super(FigureCanvasQTAggBase, self).__init__(figure=figure)
self._agg_draw_pending = False

def drawRectangle(self, rect):
self._drawRect = rect
Expand All @@ -71,10 +75,6 @@ def paintEvent(self, e):
In Qt, all drawing should be done inside of here when a widget is
shown onscreen.
"""
# If we have not rendered the Agg backend yet, do so now.
if not hasattr(self, 'renderer'):
FigureCanvasAgg.draw(self)

# FigureCanvasQT.paintEvent(self, e)
if DEBUG:
print('FigureCanvasQtAgg.paintEvent: ', self,
Expand Down Expand Up @@ -142,15 +142,33 @@ def paintEvent(self, e):

def draw(self):
"""
Draw the figure with Agg, and queue a request
for a Qt draw.
Draw the figure with Agg, and queue a request for a Qt draw.
"""
# The Agg draw is done here; delaying it until the paintEvent
# causes problems with code that uses the result of the
# draw() to update plot elements.
# The Agg draw is done here; delaying causes problems with code that
# uses the result of the draw() to update plot elements.
FigureCanvasAgg.draw(self)
self.update()

def draw_idle(self):
"""
Queue redraw of the Agg buffer and request Qt paintEvent.
"""
# The Agg draw needs to be handled by the same thread matplotlib
# modifies the scene graph from. Post Agg draw request to the
# current event loop in order to ensure thread affinity and to
# accumulate multiple draw requests from event handling.
# TODO: queued signal connection might be safer than singleShot
if not self._agg_draw_pending:
self._agg_draw_pending = True
QtCore.QTimer.singleShot(0, self.__draw_idle_agg)

def __draw_idle_agg(self, *args):
try:
FigureCanvasAgg.draw(self)
self.update()
finally:
self._agg_draw_pending = False

def blit(self, bbox=None):
"""
Blit the region in bbox
Expand Down Expand Up @@ -186,8 +204,7 @@ class FigureCanvasQTAgg(FigureCanvasQTAggBase,
def __init__(self, figure):
if DEBUG:
print('FigureCanvasQtAgg: ', figure)
FigureCanvasQT.__init__(self, figure)
FigureCanvasAgg.__init__(self, figure)
super(FigureCanvasQTAgg, self).__init__(figure=figure)
self._drawRect = None
self.blitbox = None
self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)
Expand Down