Commit ab589f1f authored by Norman Specificus's avatar Norman Specificus
Browse files

Fix for QVTKRenderWindowInteractor for better support of retina displays

Resolves issues #17046 and #17049 (and possibly more).
Make use of QCursor.pos() and QApplication().devicePixelRatio()
to find the devicePixelRatio() of the current screen, then scale all
cursor positions with this factor.
parent 10e8cdc3
......@@ -80,6 +80,7 @@ if PyQtImpl == "PyQt5":
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QTimer
from PyQt5.QtCore import QObject
......@@ -379,16 +380,43 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
return ctrl, shift
@staticmethod
def _getPixelRatio():
if PyQtImpl == "PyQt5":
# Source: https://stackoverflow.com/a/40053864/3388962
pos = QCursor.pos()
for screen in QApplication.screens():
rect = screen.geometry()
if rect.contains(pos):
return screen.devicePixelRatio()
# Should never happen, but try to find a good fallback.
return QApplication.devicePixelRatio()
else:
# Qt4 seems not to provide any cross-platform means to get the
# pixel ratio.
return 1.
def _setEventInformation(self, x, y, ctrl, shift, key, repeat=0, keysum=None):
if False:
# Original behavior
self._Iren.SetEventInformationFlipY(x, y, ctrl, shift, key, repeat, keysum)
else:
# Fixed
scale = self._getPixelRatio()
self._Iren.SetEventInformation(int(round(x*scale)),
int(round((self.height()-y-1)*scale)),
ctrl, shift, key, repeat, keysum)
def enterEvent(self, ev):
ctrl, shift = self._GetCtrlShift(ev)
self._Iren.SetEventInformationFlipY(self.__saveX, self.__saveY,
ctrl, shift, chr(0), 0, None)
self._setEventInformation(self.__saveX, self.__saveY,
ctrl, shift, chr(0), 0, None)
self._Iren.EnterEvent()
def leaveEvent(self, ev):
ctrl, shift = self._GetCtrlShift(ev)
self._Iren.SetEventInformationFlipY(self.__saveX, self.__saveY,
ctrl, shift, chr(0), 0, None)
self._setEventInformation(self.__saveX, self.__saveY,
ctrl, shift, chr(0), 0, None)
self._Iren.LeaveEvent()
def mousePressEvent(self, ev):
......@@ -396,8 +424,8 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
repeat = 0
if ev.type() == QEvent.MouseButtonDblClick:
repeat = 1
self._Iren.SetEventInformationFlipY(ev.x(), ev.y(),
ctrl, shift, chr(0), repeat, None)
self._setEventInformation(ev.x(), ev.y(),
ctrl, shift, chr(0), repeat, None)
self._ActiveButton = ev.button()
......@@ -410,8 +438,8 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
def mouseReleaseEvent(self, ev):
ctrl, shift = self._GetCtrlShift(ev)
self._Iren.SetEventInformationFlipY(ev.x(), ev.y(),
ctrl, shift, chr(0), 0, None)
self._setEventInformation(ev.x(), ev.y(),
ctrl, shift, chr(0), 0, None)
if self._ActiveButton == Qt.LeftButton:
self._Iren.LeftButtonReleaseEvent()
......@@ -427,8 +455,8 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
self.__saveY = ev.y()
ctrl, shift = self._GetCtrlShift(ev)
self._Iren.SetEventInformationFlipY(ev.x(), ev.y(),
ctrl, shift, chr(0), 0, None)
self._setEventInformation(ev.x(), ev.y(),
ctrl, shift, chr(0), 0, None)
self._Iren.MouseMoveEvent()
def keyPressEvent(self, ev):
......@@ -442,8 +470,8 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
if shift and len(keySym) == 1 and keySym.isalpha():
keySym = keySym.upper()
self._Iren.SetEventInformationFlipY(self.__saveX, self.__saveY,
ctrl, shift, key, 0, keySym)
self._setEventInformation(self.__saveX, self.__saveY,
ctrl, shift, key, 0, keySym)
self._Iren.KeyPressEvent()
self._Iren.CharEvent()
......@@ -454,8 +482,8 @@ class QVTKRenderWindowInteractor(QVTKRWIBaseClass):
else:
key = chr(0)
self._Iren.SetEventInformationFlipY(self.__saveX, self.__saveY,
ctrl, shift, key, 0, None)
self._setEventInformation(self.__saveX, self.__saveY,
ctrl, shift, key, 0, None)
self._Iren.KeyReleaseEvent()
def wheelEvent(self, ev):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment