From a112bcf896b413c6d01d488d1e3fa3bafbe1f6f9 Mon Sep 17 00:00:00 2001 From: Anton Yablokov Date: Thu, 23 Feb 2023 08:21:08 +0300 Subject: [PATCH] PR: Add mappings for QMouseEvent methods (#408) * Might close https://github.com/spyder-ide/qtpy/issues/394 * Rephrase a comment, as @CAM-Gerlach suggested * Test the fix for https://github.com/spyder-ide/qtpy/issues/394 * Format the docstring as suggested by @CAM-Gerlach * Ensure the created window is of sufficient size to point at * Don't wait before moving and clicking the mouse. `QMainWindow.show()` finishes when the window appears. So, no extra waiting needed. * Don't close the window at the end Co-authored-by: C.A.M. Gerlach --- qtpy/QtGui.py | 13 +++++++++++++ qtpy/tests/test_qtgui.py | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/qtpy/QtGui.py b/qtpy/QtGui.py index 662b84cd..86502fd2 100644 --- a/qtpy/QtGui.py +++ b/qtpy/QtGui.py @@ -72,3 +72,16 @@ def movePositionPatched( ) -> bool: return movePosition(self, operation, mode, n) QTextCursor.movePosition = 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())) +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() diff --git a/qtpy/tests/test_qtgui.py b/qtpy/tests/test_qtgui.py index 802129c9..d16f08a2 100644 --- a/qtpy/tests/test_qtgui.py +++ b/qtpy/tests/test_qtgui.py @@ -4,7 +4,7 @@ import pytest -from qtpy import PYQT5, PYQT_VERSION, PYSIDE2, PYSIDE6, QtGui +from qtpy import PYQT5, PYQT_VERSION, PYSIDE2, PYSIDE6, QtCore, QtGui, QtWidgets from qtpy.tests.utils import not_using_conda @@ -61,6 +61,41 @@ def test_enum_access(): assert QtGui.QIcon.Normal == QtGui.QIcon.Mode.Normal assert QtGui.QImage.Format_Invalid == QtGui.QImage.Format.Format_Invalid + +@pytest.mark.skipif( + sys.platform.startswith('linux') and not_using_conda(), + reason="Fatal Python error: Aborted on Linux CI when not using conda") +@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): + """ + Test `QMouseEvent.pos` and related functions removed in Qt 6, + and `QMouseEvent.position`, etc., missing from Qt 5. + """ + + class Window(QtWidgets.QMainWindow): + def mouseDoubleClickEvent(self, event: QtGui.QMouseEvent) -> None: + assert event.globalPos() - event.pos() == self.mapToParent(QtCore.QPoint(0, 0)) + assert event.pos().x() == event.x() + assert event.pos().y() == event.y() + assert event.globalPos().x() == event.globalX() + assert event.globalPos().y() == event.globalY() + assert event.position().x() == event.pos().x() + assert event.position().y() == event.pos().y() + assert event.globalPosition().x() == event.globalPos().x() + assert event.globalPosition().y() == event.globalPos().y() + + event.accept() + + window = Window() + 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) + + @pytest.mark.skipif(not (PYSIDE2 or PYSIDE6), reason="PySide{2,6} specific test") def test_qtextcursor_moveposition(): """Test monkeypatched QTextCursor.movePosition"""