From 26befecbf935d3819aefc125066c17082729f664 Mon Sep 17 00:00:00 2001 From: Peter Iannucci Date: Mon, 9 May 2016 11:38:52 -0400 Subject: [PATCH] Initial HiDPI support for qt5agg backend --- lib/matplotlib/backends/backend_qt5.py | 31 ++++++++++------------- lib/matplotlib/backends/backend_qt5agg.py | 3 +++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/backends/backend_qt5.py b/lib/matplotlib/backends/backend_qt5.py index 66aed1371e8a..8476792ee5cc 100644 --- a/lib/matplotlib/backends/backend_qt5.py +++ b/lib/matplotlib/backends/backend_qt5.py @@ -249,10 +249,14 @@ def leaveEvent(self, event): QtWidgets.QApplication.restoreOverrideCursor() FigureCanvasBase.leave_notify_event(self, guiEvent=event) + def mouseEventCoords(self, pos): + x = pos.x() * self.devicePixelRatio() + # flip y so y=0 is bottom of canvas + y = self.figure.bbox.height - pos.y() * self.devicePixelRatio() + return x, y + def mousePressEvent(self, event): - x = event.pos().x() - # flipy so y=0 is bottom of canvas - y = self.figure.bbox.height - event.pos().y() + x, y = self.mouseEventCoords(event.pos()) button = self.buttond.get(event.button()) if button is not None: FigureCanvasBase.button_press_event(self, x, y, button, @@ -261,9 +265,7 @@ def mousePressEvent(self, event): print('button pressed:', event.button()) def mouseDoubleClickEvent(self, event): - x = event.pos().x() - # flipy so y=0 is bottom of canvas - y = self.figure.bbox.height - event.pos().y() + x, y = self.mouseEventCoords(event.pos()) button = self.buttond.get(event.button()) if button is not None: FigureCanvasBase.button_press_event(self, x, y, @@ -273,16 +275,12 @@ def mouseDoubleClickEvent(self, event): print('button doubleclicked:', event.button()) def mouseMoveEvent(self, event): - x = event.x() - # flipy so y=0 is bottom of canvas - y = self.figure.bbox.height - event.y() + x, y = self.mouseEventCoords(event) FigureCanvasBase.motion_notify_event(self, x, y, guiEvent=event) # if DEBUG: print('mouse move') def mouseReleaseEvent(self, event): - x = event.x() - # flipy so y=0 is bottom of canvas - y = self.figure.bbox.height - event.y() + x, y = self.mouseEventCoords(event) button = self.buttond.get(event.button()) if button is not None: FigureCanvasBase.button_release_event(self, x, y, button, @@ -291,9 +289,7 @@ def mouseReleaseEvent(self, event): print('button released') def wheelEvent(self, event): - x = event.x() - # flipy so y=0 is bottom of canvas - y = self.figure.bbox.height - event.y() + x, y = self.mouseEventCoords(event) # from QWheelEvent::delta doc if event.pixelDelta().x() == 0 and event.pixelDelta().y() == 0: steps = event.angleDelta().y() / 120 @@ -323,8 +319,9 @@ def keyReleaseEvent(self, event): print('key release', key) def resizeEvent(self, event): - w = event.size().width() - h = event.size().height() + dpi_ratio = getattr(self, '_dpi_ratio', 1) + w = event.size().width() * dpi_ratio + h = event.size().height() * dpi_ratio if DEBUG: print('resize (%d x %d)' % (w, h)) print("FigureCanvasQt.resizeEvent(%d, %d)" % (w, h)) diff --git a/lib/matplotlib/backends/backend_qt5agg.py b/lib/matplotlib/backends/backend_qt5agg.py index 81addae00d27..f1549701513e 100644 --- a/lib/matplotlib/backends/backend_qt5agg.py +++ b/lib/matplotlib/backends/backend_qt5agg.py @@ -100,6 +100,7 @@ def paintEvent(self, e): qImage = QtGui.QImage(stringBuffer, self.renderer.width, self.renderer.height, QtGui.QImage.Format_ARGB32) + qImage.setDevicePixelRatio(self._dpi_ratio) # get the rectangle for the image rect = qImage.rect() p = QtGui.QPainter(self) @@ -135,6 +136,7 @@ def paintEvent(self, e): stringBuffer = reg.to_string_argb() qImage = QtGui.QImage(stringBuffer, w, h, QtGui.QImage.Format_ARGB32) + qImage.setDevicePixelRatio(self._dpi_ratio) # Adjust the stringBuffer reference count to work # around a memory leak bug in QImage() under PySide on # Python 3.x @@ -222,6 +224,7 @@ def __init__(self, figure): super(FigureCanvasQTAgg, self).__init__(figure=figure) self._drawRect = None self.blitbox = [] + self._dpi_ratio = self.devicePixelRatio() self.setAttribute(QtCore.Qt.WA_OpaquePaintEvent)