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

Skip to content

Commit 5087c05

Browse files
committed
Qt dpi handling should be renderer-agnostic.
1 parent d96d104 commit 5087c05

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

lib/matplotlib/backends/backend_qt5.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ def _dpi_ratio(self):
270270
except AttributeError:
271271
return 1
272272

273+
def _update_dpi(self):
274+
# As described in __init__ above, we need to be careful in cases with
275+
# mixed resolution displays if dpi_ratio is changing between painting
276+
# events.
277+
# Return whether we triggered a resizeEvent (and thus a paintEvent)
278+
# from within this function.
279+
if self._dpi_ratio != self._dpi_ratio_prev:
280+
# We need to update the figure DPI.
281+
self._update_figure_dpi()
282+
self._dpi_ratio_prev = self._dpi_ratio
283+
# The easiest way to resize the canvas is to emit a resizeEvent
284+
# since we implement all the logic for resizing the canvas for
285+
# that event.
286+
event = QtGui.QResizeEvent(self.size(), self.size())
287+
self.resizeEvent(event)
288+
# resizeEvent triggers a paintEvent itself, so we exit this one
289+
# (after making sure that the event is immediately handled).
290+
return True
291+
return False
292+
273293
def get_width_height(self):
274294
w, h = FigureCanvasBase.get_width_height(self)
275295
return int(w / self._dpi_ratio), int(h / self._dpi_ratio)
@@ -489,6 +509,7 @@ def draw_idle(self):
489509
def _draw_idle(self):
490510
if self.height() < 0 or self.width() < 0:
491511
self._draw_pending = False
512+
if not self._draw_pending:
492513
return
493514
try:
494515
self.draw()

lib/matplotlib/backends/backend_qt5agg.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,13 @@ def paintEvent(self, e):
3535
In Qt, all drawing should be done inside of here when a widget is
3636
shown onscreen.
3737
"""
38-
# if there is a pending draw, run it now as we need the updated render
39-
# to paint the widget
40-
if self._agg_draw_pending:
41-
self.__draw_idle_agg()
42-
# As described in __init__ above, we need to be careful in cases with
43-
# mixed resolution displays if dpi_ratio is changing between painting
44-
# events.
45-
if self._dpi_ratio != self._dpi_ratio_prev:
46-
# We need to update the figure DPI
47-
self._update_figure_dpi()
48-
self._dpi_ratio_prev = self._dpi_ratio
49-
# The easiest way to resize the canvas is to emit a resizeEvent
50-
# since we implement all the logic for resizing the canvas for
51-
# that event.
52-
event = QtGui.QResizeEvent(self.size(), self.size())
53-
# We use self.resizeEvent here instead of QApplication.postEvent
54-
# since the latter doesn't guarantee that the event will be emitted
55-
# straight away, and this causes visual delays in the changes.
56-
self.resizeEvent(event)
57-
# resizeEvent triggers a paintEvent itself, so we exit this one.
38+
if self._update_dpi():
39+
# The dpi update triggered its own paintEvent.
5840
return
41+
self._draw_idle() # Only does something if a draw is pending.
5942

60-
# if the canvas does not have a renderer, then give up and wait for
61-
# FigureCanvasAgg.draw(self) to be called
43+
# If the canvas does not have a renderer, then give up and wait for
44+
# FigureCanvasAgg.draw(self) to be called.
6245
if not hasattr(self, 'renderer'):
6346
return
6447

lib/matplotlib/backends/backend_qt5cairo.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def __init__(self, figure):
99
self._renderer = backend_cairo.RendererCairo(self.figure.dpi)
1010

1111
def paintEvent(self, event):
12+
self._update_dpi()
1213
width = self.width()
1314
height = self.height()
1415
surface = backend_cairo.cairo.ImageSurface(

0 commit comments

Comments
 (0)