2016-12-14 16:35:33 +00:00
|
|
|
import time
|
2021-05-28 04:57:07 +00:00
|
|
|
from pyqtgraph.Qt import QtCore, QtGui, QtTest
|
2016-12-14 16:35:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def resizeWindow(win, w, h, timeout=2.0):
|
|
|
|
"""Resize a window and wait until it has the correct size.
|
|
|
|
|
|
|
|
This is required for unit testing on some platforms that do not guarantee
|
|
|
|
immediate response from the windowing system.
|
|
|
|
"""
|
|
|
|
QtGui.QApplication.processEvents()
|
|
|
|
# Sometimes the window size will switch multiple times before settling
|
2021-02-04 03:27:17 +00:00
|
|
|
# on its final size. Adding qWaitForWindowExposed seems to help with this.
|
|
|
|
QtTest.QTest.qWaitForWindowExposed(win)
|
2016-12-14 16:35:33 +00:00
|
|
|
win.resize(w, h)
|
|
|
|
start = time.time()
|
|
|
|
while True:
|
|
|
|
w1, h1 = win.width(), win.height()
|
|
|
|
if (w,h) == (w1,h1):
|
|
|
|
return
|
|
|
|
QtTest.QTest.qWait(10)
|
|
|
|
if time.time()-start > timeout:
|
|
|
|
raise TimeoutError("Window resize failed (requested %dx%d, got %dx%d)" % (w, h, w1, h1))
|
|
|
|
|
2016-02-04 07:13:58 +00:00
|
|
|
|
|
|
|
# Functions for generating user input events.
|
|
|
|
# We would like to use QTest for this purpose, but it seems to be broken.
|
|
|
|
# See: http://stackoverflow.com/questions/16299779/qt-qgraphicsview-unit-testing-how-to-keep-the-mouse-in-a-pressed-state
|
|
|
|
|
|
|
|
def mousePress(widget, pos, button, modifier=None):
|
|
|
|
if isinstance(widget, QtGui.QGraphicsView):
|
|
|
|
widget = widget.viewport()
|
|
|
|
if modifier is None:
|
2021-06-06 01:17:43 +00:00
|
|
|
modifier = QtCore.Qt.KeyboardModifier.NoModifier
|
|
|
|
event = QtGui.QMouseEvent(QtCore.QEvent.Type.MouseButtonPress, pos, button, QtCore.Qt.MouseButton.NoButton, modifier)
|
2016-02-04 07:13:58 +00:00
|
|
|
QtGui.QApplication.sendEvent(widget, event)
|
|
|
|
|
|
|
|
|
|
|
|
def mouseRelease(widget, pos, button, modifier=None):
|
|
|
|
if isinstance(widget, QtGui.QGraphicsView):
|
|
|
|
widget = widget.viewport()
|
|
|
|
if modifier is None:
|
2021-06-06 01:17:43 +00:00
|
|
|
modifier = QtCore.Qt.KeyboardModifier.NoModifier
|
|
|
|
event = QtGui.QMouseEvent(QtCore.QEvent.Type.MouseButtonRelease, pos, button, QtCore.Qt.MouseButton.NoButton, modifier)
|
2016-02-04 07:13:58 +00:00
|
|
|
QtGui.QApplication.sendEvent(widget, event)
|
|
|
|
|
|
|
|
|
|
|
|
def mouseMove(widget, pos, buttons=None, modifier=None):
|
|
|
|
if isinstance(widget, QtGui.QGraphicsView):
|
|
|
|
widget = widget.viewport()
|
|
|
|
if modifier is None:
|
2021-06-06 01:17:43 +00:00
|
|
|
modifier = QtCore.Qt.KeyboardModifier.NoModifier
|
2016-02-04 07:13:58 +00:00
|
|
|
if buttons is None:
|
2021-06-06 01:17:43 +00:00
|
|
|
buttons = QtCore.Qt.MouseButton.NoButton
|
|
|
|
event = QtGui.QMouseEvent(QtCore.QEvent.Type.MouseMove, pos, QtCore.Qt.MouseButton.NoButton, buttons, modifier)
|
2016-02-04 07:13:58 +00:00
|
|
|
QtGui.QApplication.sendEvent(widget, event)
|
|
|
|
|
|
|
|
|
|
|
|
def mouseDrag(widget, pos1, pos2, button, modifier=None):
|
|
|
|
mouseMove(widget, pos1)
|
|
|
|
mousePress(widget, pos1, button, modifier)
|
|
|
|
mouseMove(widget, pos2, button, modifier)
|
|
|
|
mouseRelease(widget, pos2, button, modifier)
|
|
|
|
|
|
|
|
|
|
|
|
def mouseClick(widget, pos, button, modifier=None):
|
|
|
|
mouseMove(widget, pos)
|
|
|
|
mousePress(widget, pos, button, modifier)
|
|
|
|
mouseRelease(widget, pos, button, modifier)
|