8000 Qt5Agg HiDPI support by QuLogic · Pull Request #7507 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Qt5Agg HiDPI support #7507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 2, 2017
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
Prev Previous commit
Next Next commit
Fix backwards compatibility with Qt4.
  • Loading branch information
QuLogic committed Dec 1, 2016
commit d1d2c66d1a70f9fabdac17e219bb617458d6ceb4
20 changes: 13 additions & 7 deletions lib/matplotlib/backends/backend_qt5.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,17 @@ def __init__(self, figure):
w, h = self.get_width_height()
self.resize(w, h)

@property
def _dpi_ratio(self):
# Not available on Qt4 or some older Qt5.
try:
return self.devicePixelRatio()
except AttributeError:
return 1

def get_width_height(self):
dpi_ratio = self.devicePixelRatio()
w, h = FigureCanvasBase.get_width_height(self)
return int(w / dpi_ratio), int(h / dpi_ratio)
return int(w / self._dpi_ratio), int(h / self._dpi_ratio)

def enterEvent(self, event):
FigureCanvasBase.enter_notify_event(self, guiEvent=event)
Expand All @@ -256,9 +263,9 @@ def leaveEvent(self, event):
FigureCanvasBase.leave_notify_event(self, guiEvent=event)

def mouseEventCoords(self, pos):
x = pos.x() * self.devicePixelRatio()
x = pos.x() * self._dpi_ratio
# flip y so y=0 is bottom of canvas
y = self.figure.bbox.height - pos.y() * self.devicePixelRatio()
y = self.figure.bbox.height - pos.y() * self._dpi_ratio
return x, y

def mousePressEvent(self, event):
Expand Down Expand Up @@ -325,9 +332,8 @@ def keyReleaseEvent(self, event):
print('key release', key)

def resizeEvent(self, event):
dpi_ratio = self.devicePixelRatio()
w = event.size().width() * dpi_ratio
h = event.size().height() * dpi_ratio
w = event.size().width() * self._dpi_ratio
h = event.size().height() * self._dpi_ratio
if DEBUG:
print('resize (%d x %d)' % (w, h))
print("FigureCanvasQt.resizeEvent(%d, %d)" % (w, h))
Expand Down
9 changes: 6 additions & 3 deletions lib/matplotlib/backends/backend_qt5agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ def paintEvent(self, e):
qImage = QtGui.QImage(stringBuffer, self.renderer.width,
self.renderer.height,
QtGui.QImage.Format_ARGB32)
qImage.setDevicePixelRatio(self._dpi_ratio)
if hasattr(qImage, 'setDevicePixelRatio'):
# Not available on Qt4 or some older Qt5.
qImage.setDevicePixelRatio(self._dpi_ratio)
# get the rectangle for the image
rect = qImage.rect()
p = QtGui.QPainter(self)
Expand Down Expand Up @@ -142,7 +144,9 @@ def paintEvent(self, e):
stringBuffer = reg.to_string_argb()
qImage = QtGui.QImage(stringBuffer, w, h,
QtGui.QImage.Format_ARGB32)
qImage.setDevicePixelRatio(self._dpi_ratio)
if hasattr(qImage, 'setDevicePixelRatio'):
# Not available on Qt4 or some older Qt5.
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
Expand Down Expand Up @@ -238,7 +242,6 @@ def __init__(self, figure):
super(FigureCanvasQTAgg, self).__init__(figure=figure)
self._drawRect = None
self.blitbox = []
self._dpi_ratio = self.devicePixelRatio()
# We don't want to scale up the figure DPI more than once.
# Note, we don't handle a signal for changing DPI yet.
if not hasattr(self.figure, '_original_dpi'):
Expand Down
0