From feb16f1836787b0d0f75e89afa9ed0971d5c88c3 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 4 Jun 2020 00:18:57 -0400 Subject: [PATCH 1/3] FIX: support Qt 5.15 - QPainter.drawLine is now picky about float vs int - be more careful that we close the Qpainter out --- lib/matplotlib/backends/backend_qt5.py | 2 +- lib/matplotlib/backends/backend_qt5agg.py | 78 +++++++++++------------ 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 1265479d2ead..2b80993a993a 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -486,7 +486,7 @@ def drawRectangle(self, rect): # Draw the zoom rectangle to the QPainter. _draw_rect_callback needs # to be called at the end of paintEvent. if rect is not None: - x0, y0, w, h = [pt / self._dpi_ratio for pt in rect] + x0, y0, w, h = map(int, [pt / self._dpi_ratio for pt in rect]) x1 = x0 + w y1 = y0 + h def _draw_rect_callback(painter): diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 2d4d636d4018..d16ec96f4e20 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -38,45 +38,45 @@ def paintEvent(self, event): return painter = QtGui.QPainter(self) - - # 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()) - # shift the "top" by the height of the image to get the - # correct corner for our coordinate system - 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)) - - # clear the widget canvas - painter.eraseRect(rect) - - qimage = QtGui.QImage(buf, buf.shape[1], buf.shape[0], - QtGui.QImage.Format_ARGB32_Premultiplied) - 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 - # leak bug in QImage under PySide on Python 3. - if QT_API in ('PySide', 'PySide2'): - ctypes.c_long.from_address(id(buf)).value = 1 - - self._draw_rect_callback(painter) - - painter.end() + try: + # 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()) + # shift the "top" by the height of the image to get the + # correct corner for our coordinate system + 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)) + + # clear the widget canvas + painter.eraseRect(rect) + + qimage = QtGui.QImage(buf, buf.shape[1], buf.shape[0], + QtGui.QImage.Format_ARGB32_Premultiplied) + 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 + # leak bug in QImage under PySide on Python 3. + if QT_API in ('PySide', 'PySide2'): + ctypes.c_long.from_address(id(buf)).value = 1 + + self._draw_rect_callback(painter) + finally: + painter.end() def print_figure(self, *args, **kwargs): super().print_figure(*args, **kwargs) From 472a046eaee98417930f71560ebd9c7d8b5e3acc Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 4 Jun 2020 01:47:13 -0400 Subject: [PATCH 2/3] MNT: simplify logic No need for map when a single list comprehension will do. Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/backends/backend_qt5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 2b80993a993a..0642dcfab87a 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -486,7 +486,7 @@ def drawRectangle(self, rect): # Draw the zoom rectangle to the QPainter. _draw_rect_callback needs # to be called at the end of paintEvent. if rect is not None: - x0, y0, w, h = map(int, [pt / self._dpi_ratio for pt in rect]) + x0, y0, w, h = [int(pt / self._dpi_ratio) for pt in rect] x1 = x0 + w y1 = y0 + h def _draw_rect_callback(painter): From 258832aa8d85a6713e3fcc8767829aba6841288e Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Thu, 4 Jun 2020 00:18:57 -0400 Subject: [PATCH 3/3] STY: linewrap docstrings --- lib/matplotlib/backends/backend_qt5agg.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index d16ec96f4e20..11b60378ac39 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -39,11 +39,12 @@ def paintEvent(self, event): painter = QtGui.QPainter(self) try: - # See documentation of QRect: bottom() and right() are off by 1, so use - # left() + width() and top() + height(). + # 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) + # 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()) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy