Skip to content

Commit

Permalink
PR: Add compatibility mappings between bindings for all children of `…
Browse files Browse the repository at this point in the history
…QSinglePointEvent` (#417)
  • Loading branch information
ccordoba12 authored Mar 28, 2023
2 parents 3abce78 + bd1b013 commit b8291d8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
41 changes: 33 additions & 8 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,37 @@ def movePositionPatched(
# Fix https://github.com/spyder-ide/qtpy/issues/394
if PYQT5 or PYSIDE2:
from qtpy.QtCore import QPointF as __QPointF
QMouseEvent.position = lambda self: __QPointF(float(self.x()), float(self.y()))
QMouseEvent.globalPosition = lambda self: __QPointF(float(self.globalX()), float(self.globalY()))
QNativeGestureEvent.x = lambda self: self.localPos().toPoint().x()
QNativeGestureEvent.y = lambda self: self.localPos().toPoint().y()
QNativeGestureEvent.position = lambda self: self.localPos()
QNativeGestureEvent.globalX = lambda self: self.globalPos().x()
QNativeGestureEvent.globalY = lambda self: self.globalPos().y()
QNativeGestureEvent.globalPosition = lambda self: __QPointF(
float(self.globalPos().x()), float(self.globalPos().y()))
QEnterEvent.position = lambda self: self.localPos()
QEnterEvent.globalPosition = lambda self: __QPointF(
float(self.globalX()), float(self.globalY()))
QTabletEvent.position = lambda self: self.posF()
QTabletEvent.globalPosition = lambda self: self.globalPosF()
QHoverEvent.x = lambda self: self.pos().x()
QHoverEvent.y = lambda self: self.pos().y()
QHoverEvent.position = lambda self: self.posF()
# No `QHoverEvent.globalPosition`, `QHoverEvent.globalX`,
# nor `QHoverEvent.globalY` in the Qt5 docs.
QMouseEvent.position = lambda self: self.localPos()
QMouseEvent.globalPosition = lambda self: __QPointF(
float(self.globalX()), float(self.globalY()))
if PYQT6 or PYSIDE6:
QMouseEvent.pos = lambda self: self.position().toPoint()
QMouseEvent.x = lambda self: self.position().toPoint().x()
QMouseEvent.y = lambda self: self.position().toPoint().y()
QMouseEvent.globalPos = lambda self: self.globalPosition().toPoint()
QMouseEvent.globalX = lambda self: self.globalPosition().toPoint().x()
QMouseEvent.globalY = lambda self: self.globalPosition().toPoint().y()
for _class in (QNativeGestureEvent, QEnterEvent, QTabletEvent, QHoverEvent,
QMouseEvent):
for _obsolete_function in ('pos', 'x', 'y', 'globalPos', 'globalX', 'globalY'):
if hasattr(_class, _obsolete_function):
delattr(_class, _obsolete_function)
QSinglePointEvent.pos = lambda self: self.position().toPoint()
QSinglePointEvent.posF = lambda self: self.position()
QSinglePointEvent.localPos = lambda self: self.position()
QSinglePointEvent.x = lambda self: self.position().toPoint().x()
QSinglePointEvent.y = lambda self: self.position().toPoint().y()
QSinglePointEvent.globalPos = lambda self: self.globalPosition().toPoint()
QSinglePointEvent.globalX = lambda self: self.globalPosition().toPoint().x()
QSinglePointEvent.globalY = lambda self: self.globalPosition().toPoint().y()
15 changes: 12 additions & 3 deletions qtpy/tests/test_qtgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_enum_access():
@pytest.mark.skipif(
sys.platform == 'darwin' and sys.version_info[:2] == (3, 7),
reason="Stalls on macOS CI with Python 3.7")
def test_QMouseEvent_pos_functions(qtbot):
def test_QSomethingEvent_pos_functions(qtbot):
"""
Test `QMouseEvent.pos` and related functions removed in Qt 6,
and `QMouseEvent.position`, etc., missing from Qt 5.
Expand All @@ -101,8 +101,17 @@ def mouseDoubleClickEvent(self, event: QtGui.QMouseEvent) -> None:
window.setMinimumSize(320, 240) # ensure the window is of sufficient size
window.show()

qtbot.mouseMove(window, QtCore.QPoint(42, 6 * 9))
qtbot.mouseDClick(window, QtCore.Qt.LeftButton)
with qtbot.waitExposed(window):
qtbot.mouseMove(window, QtCore.QPoint(42, 6 * 9))
qtbot.mouseDClick(window, QtCore.Qt.LeftButton)

# the rest of the functions are not actually tested
for _class in ('QNativeGestureEvent', 'QEnterEvent', 'QTabletEvent'):
for _function in ('pos', 'x', 'y', 'globalPos', 'globalX', 'globalY',
'position', 'globalPosition'):
assert hasattr(getattr(QtGui, _class), _function)
for _function in ('pos', 'x', 'y', 'position'):
assert hasattr(QtGui.QHoverEvent, _function)


@pytest.mark.skipif(not (PYSIDE2 or PYSIDE6), reason="PySide{2,6} specific test")
Expand Down

0 comments on commit b8291d8

Please sign in to comment.