From bbc149791eb16c30cc1e0340dee7dfdb98e3e284 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 4 Jun 2019 22:49:50 -0500 Subject: [PATCH 1/4] Fixing bounding box for QT5. --- lib/matplotlib/backends/backend_qt5agg.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 4dec3b9cb0c4..ac5558add2d3 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -38,16 +38,15 @@ def paintEvent(self, event): painter = QtGui.QPainter(self) + # get bounding box scaled to the figure rect = event.rect() - left = rect.left() - top = rect.top() - width = rect.width() - height = rect.height() + left, top = self.mouseEventCoords(rect.bottomLeft()) + width = rect.width() * self._dpi_ratio + height = rect.height() * self._dpi_ratio # See documentation of QRect: bottom() and right() are off by 1, so use # left() + width() and top() + height(). - bbox = Bbox( - [[left, self.renderer.height - (top + height * self._dpi_ratio)], - [left + width * self._dpi_ratio, self.renderer.height - top]]) + bbox = Bbox([[left, top], [left + width, top + height]]) + # create a buffer using this bounding box reg = self.copy_from_bbox(bbox) buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32( memoryview(reg)) @@ -60,8 +59,8 @@ def paintEvent(self, event): if hasattr(qimage, 'setDevicePixelRatio'): # Not available on Qt4 or some older Qt5. qimage.setDevicePixelRatio(self._dpi_ratio) - origin = QtCore.QPoint(left, top) - painter.drawImage(origin / self._dpi_ratio, qimage) + origin = QtCore.QPoint(rect.left(), rect.top()) + painter.drawImage(origin, qimage) # Adjust the buf reference count to work around a memory # leak bug in QImage under PySide on Python 3. if QT_API in ('PySide', 'PySide2'): From 7db54c0ab554d64adba1542c0e2548e5b9497757 Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 4 Jun 2019 23:05:51 -0500 Subject: [PATCH 2/4] Using different corner to avoid off-by-one issue. --- lib/matplotlib/backends/backend_qt5agg.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index ac5558add2d3..6db30ee0d47d 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -38,13 +38,15 @@ def paintEvent(self, event): painter = QtGui.QPainter(self) - # get bounding box scaled to the figure + # See documentation of QRect: bottom() and right() are off by 1, so use + # left() + width() and top() + height(). rect = event.rect() - left, top = self.mouseEventCoords(rect.bottomLeft()) width = rect.width() * self._dpi_ratio height = rect.height() * self._dpi_ratio - # See documentation of QRect: bottom() and right() are off by 1, so use - # left() + width() and top() + height(). + left, top = self.mouseEventCoords(rect.topLeft()) + # shift the "top" by the height of the image to get the + # correct corner for our coordinate system + top -= height bbox = Bbox([[left, top], [left + width, top + height]]) # create a buffer using this bounding box reg = self.copy_from_bbox(bbox) @@ -59,6 +61,7 @@ def paintEvent(self, event): if hasattr(qimage, 'setDevicePixelRatio'): # Not available on Qt4 or some older Qt5. qimage.setDevicePixelRatio(self._dpi_ratio) + # set origin using original QT coordinates origin = QtCore.QPoint(rect.left(), rect.top()) painter.drawImage(origin, qimage) # Adjust the buf reference count to work around a memory From 3c5cbd87f6e1dc830bf41892afed0ff234000f1f Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Wed, 5 Jun 2019 11:17:56 -0500 Subject: [PATCH 3/4] Adding comment on scaling of coords. --- lib/matplotlib/backends/backend_qt5agg.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 6db30ee0d47d..61ca6bdf4ea2 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -41,6 +41,8 @@ def paintEvent(self, event): # See documentation of QRect: bottom() and right() are off by 1, so use # left() + width() and top() + height(). rect = event.rect() + # scale rect dimensions using the screen dpi ratio to get + # correct values for the Figure coordinates (rather than QT5's coords) width = rect.width() * self._dpi_ratio height = rect.height() * self._dpi_ratio left, top = self.mouseEventCoords(rect.topLeft()) From 13237c2f116dbfafec67940d1f71a159ced20a1f Mon Sep 17 00:00:00 2001 From: Patrick Shriwise Date: Tue, 27 Aug 2019 12:49:47 -0500 Subject: [PATCH 4/4] Added clarity on bounding box construction. --- lib/matplotlib/backends/backend_qt5agg.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 61ca6bdf4ea2..f1fc3555863d 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -48,9 +48,11 @@ def paintEvent(self, event): left, top = self.mouseEventCoords(rect.topLeft()) # shift the "top" by the height of the image to get the # correct corner for our coordinate system - top -= height - bbox = Bbox([[left, top], [left + width, top + height]]) - # create a buffer using this bounding box + bottom = top - height + # same with the right side of the image + right = left + width + # create a buffer using the image bounding box + bbox = Bbox([[left, bottom], [right, top]]) reg = self.copy_from_bbox(bbox) buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32( memoryview(reg))