Skip to content

Commit

Permalink
Fix Qt6 mouse event code (#916)
Browse files Browse the repository at this point in the history
* Fix Qt6 mouse event code

This:
- uses the new API for position for Qt6 mouse events
- fixes the test code that generates synthetic mouse events for Qt6

* Use keyword arguments and QPointF where appropriate.

* Use comments instead.

* Revert QPointF change.
  • Loading branch information
corranwebster committed Mar 29, 2022
1 parent f2fcc5c commit a3a5164
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 52 deletions.
10 changes: 7 additions & 3 deletions enable/qt4/base_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import warnings

# Qt imports.
from pyface.qt import QtCore, QtGui, QtOpenGL, is_qt4, qt_api
from pyface.qt import QtCore, QtGui, QtOpenGL, is_qt4, is_qt5, qt_api

# Enthought library imports.
from enable.abstract_window import AbstractWindow
Expand Down Expand Up @@ -457,8 +457,12 @@ def _create_mouse_event(self, event):
# If the event (if there is one) doesn't contain the mouse position,
# modifiers and buttons then get sensible defaults.
try:
x = event.x()
y = event.y()
if is_qt4 or is_qt5:
x = event.x()
y = event.y()
else:
x = event.position().x()
y = event.position().y()
modifiers = event.modifiers()
buttons = event.buttons()
except AttributeError:
Expand Down
133 changes: 84 additions & 49 deletions enable/tests/qt4/test_mouse_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,40 @@ def setUp(self):
self.window._size = (600, 600)

def test_vertical_mouse_wheel(self):
from pyface.qt import QtCore, QtGui
from pyface.qt import QtCore, QtGui, is_qt4, is_qt5

is_qt4 = QtCore.__version_info__[0] <= 4

# create and mock a mouse wheel event
if is_qt4:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0),
200,
QtCore.Qt.NoButton,
QtCore.Qt.NoModifier,
QtCore.Qt.Vertical,
QtCore.QPoint(0, 0), # pos
200, # delta
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.Vertical, # orient
)
elif is_qt5:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0), # pos
self.window.control.mapToGlobal(QtCore.QPoint(0, 0)), # globalPos
QtCore.QPoint(0, 200), # pixelDelta
QtCore.QPoint(0, 200), # angleDelta
200, # qt4Delta
QtCore.Qt.Vertical, # qt4Orietation
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.ScrollUpdate, # phase
)
else:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0),
self.window.control.mapToGlobal(QtCore.QPoint(0, 0)),
QtCore.QPoint(0, 200),
QtCore.QPoint(0, 200),
200,
QtCore.Qt.Vertical,
QtCore.Qt.NoButton,
QtCore.Qt.NoModifier,
QtCore.Qt.ScrollUpdate,
QtCore.QPointF(0, 0), # pos
self.window.control.mapToGlobal(QtCore.QPointF(0, 0)), # globalPos
QtCore.QPoint(0, 200), # pixelDelta
QtCore.QPoint(0, 200), # angleDelta
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.ScrollUpdate, # phase
False, # inverted
)

# dispatch event
Expand All @@ -79,30 +89,39 @@ def test_vertical_mouse_wheel(self):
self.assertEqual(self.tool.event.mouse_wheel_delta, (0, 200))

def test_horizontal_mouse_wheel(self):
from pyface.qt import QtCore, QtGui

is_qt4 = QtCore.__version_info__[0] <= 4
from pyface.qt import QtCore, QtGui, is_qt4, is_qt5

# create and mock a mouse wheel event
if is_qt4:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0),
200,
QtCore.Qt.NoButton,
QtCore.Qt.NoModifier,
QtCore.Qt.Horizontal,
QtCore.QPoint(0, 0), # pos
200, # delta
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.Horizontal, # orient
)
elif is_qt5:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0), # pos
self.window.control.mapToGlobal(QtCore.QPoint(0, 0)), # globalPos
QtCore.QPoint(200, 0), # pixelDelta
QtCore.QPoint(200, 0), # angleDelta
200, # qt4Delta
QtCore.Qt.Horizontal, # qt4Orietation
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.ScrollUpdate, # phase
)
else:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0),
self.window.control.mapToGlobal(QtCore.QPoint(0, 0)),
QtCore.QPoint(200, 0),
QtCore.QPoint(200, 0),
200,
QtCore.Qt.Vertical,
QtCore.Qt.NoButton,
QtCore.Qt.NoModifier,
QtCore.Qt.ScrollUpdate,
QtCore.QPointF(0, 0), # pos
self.window.control.mapToGlobal(QtCore.QPointF(0, 0)), # globalPos
QtCore.QPoint(200, 0), # pixelDelta
QtCore.QPoint(200, 0), # angleDelta
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.ScrollUpdate, # phase
False, # inverted
)

# dispatch event
Expand All @@ -114,24 +133,40 @@ def test_horizontal_mouse_wheel(self):
self.assertEqual(self.tool.event.mouse_wheel_delta, (200, 0))

def test_vertical_mouse_wheel_without_pixel_delta(self):
from pyface.qt import QtCore, QtGui

is_qt4 = QtCore.__version_info__[0] <= 4
if is_qt4:
self.skipTest("Not directly applicable in Qt4")
from pyface.qt import QtCore, QtGui, is_qt4, is_qt5

# create and mock a mouse wheel event
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0),
self.window.control.mapToGlobal(QtCore.QPoint(0, 0)),
QtCore.QPoint(0, 0),
QtCore.QPoint(0, 200),
200,
QtCore.Qt.Vertical,
QtCore.Qt.NoButton,
QtCore.Qt.NoModifier,
QtCore.Qt.ScrollUpdate,
)
if is_qt4:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0), # pos
200, # delta
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.Vertical, # orient
)
elif is_qt5:
qt_event = QtGui.QWheelEvent(
QtCore.QPoint(0, 0), # pos
self.window.control.mapToGlobal(QtCore.QPoint(0, 0)), # globalPos
QtCore.QPoint(0, 0), # pixelDelta
QtCore.QPoint(0, 200), # angleDelta
200, # qt4Delta
QtCore.Qt.Horizontal, # qt4Orientation
QtCore.Qt.NoButton, # buttons
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.ScrollUpdate, # phase
)
else:
qt_event = QtGui.QWheelEvent(
QtCore.QPointF(0, 0), # pos
self.window.control.mapToGlobal(QtCore.QPointF(0, 0)), # globalPos
QtCore.QPoint(0, 0), # pixelDelta
QtCore.QPoint(0, 200), # angleDelta
QtCore.Qt.NoButton, # buttos
QtCore.Qt.NoModifier, # modifiers
QtCore.Qt.ScrollUpdate, # phase
False, # inverted
)

# dispatch event
self.window._on_mouse_wheel(qt_event)
Expand Down

0 comments on commit a3a5164

Please sign in to comment.