First pass at implementing the diff from PR307

This commit is contained in:
Ogi Moore 2020-06-24 22:20:47 -07:00
parent b41c4a71e5
commit ce6da3e93f
3 changed files with 65 additions and 35 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import time
import weakref
import warnings
@ -8,6 +9,9 @@ from .. import functions as fn
from .. import ptime as ptime
from .mouseEvents import *
from .. import debug as debug
from .. import getConfigOption
getMillis = lambda: int(round(time.time() * 1000))
if hasattr(QtCore, 'PYQT_VERSION'):
@ -114,6 +118,7 @@ class GraphicsScene(QtGui.QGraphicsScene):
self.contextMenu[0].triggered.connect(self.showExportDialog)
self.exportDialog = None
self._lastMoveEventTime = 0
def render(self, *args):
self.prepareForPaint()
@ -162,16 +167,33 @@ class GraphicsScene(QtGui.QGraphicsScene):
i.setFocus(QtCore.Qt.MouseFocusReason)
break
def _moveEventIsAllowed(self):
# For ignoring events that are too close together
# Max number of events per second
rateLimit = getConfigOption('mouseRateLimit')
if rateLimit <= 0:
return True
# Delay between events (in milliseconds)
delay = 1000.0 / rateLimit
if getMillis() - self._lastMoveEventTime >= delay:
return True
return False
def mouseMoveEvent(self, ev):
# ignore high frequency events
if self._moveEventIsAllowed():
self._lastMoveEventTime = getMillis()
self.sigMouseMoved.emit(ev.scenePos())
## First allow QGraphicsScene to deliver hoverEnter/Move/ExitEvents
# First allow QGraphicsScene to eliver hoverEvent/Move/Exit Events
QtGui.QGraphicsScene.mouseMoveEvent(self, ev)
## Next deliver our own HoverEvents
# Next Deliver our own Hover Events
self.sendHoverEvents(ev)
if int(ev.buttons()) != 0: ## button is pressed; send mouseMoveEvents and mouseDragEvents
if int(ev.buttons()) != 0:
# button is pressed' send mouseMoveEvents and mouseDragEvents
QtGui.QGraphicsScene.mouseMoveEvent(self, ev)
if self.mouseGrabberItem() is None:
now = ptime.time()
@ -189,12 +211,16 @@ class GraphicsScene(QtGui.QGraphicsScene):
continue
init = init or (len(self.dragButtons) == 0) ## If this is the first button to be dragged, then init=True
self.dragButtons.append(int(btn))
## If we have dragged buttons, deliver a drag event
## if we have dragged buttons, deliver a drag event
if len(self.dragButtons) > 0:
if self.sendDragEvent(ev, init=init):
ev.accept()
else:
QtGui.QGraphicsScene.mouseMoveEvent(self, ev)
# if you do not accept event (which is ignored) then cursor will disappear
ev.accept()
def leaveEvent(self, ev): ## inform items that mouse is gone
if len(self.dragButtons) == 0:
self.sendHoverEvents(ev, exitOnly=True)

View File

@ -56,6 +56,7 @@ CONFIG_OPTIONS = {
'exitCleanup': True, ## Attempt to work around some exit crash bugs in PyQt and PySide
'enableExperimental': False, ## Enable experimental features (the curious can search for this key in the code)
'crashWarning': False, # If True, print warnings about situations that may result in a crash
'mouseRateLimit': 100, # For ignoring frequent mouse events, max number of mouse move events per second, if <= 0, then it is switched off
'imageAxisOrder': 'col-major', # For 'row-major', image data is expected in the standard (row, col) order.
# For 'col-major', image data is expected in reversed (col, row) order.
# The default is 'col-major' for backward compatibility, but this may

View File

@ -5,6 +5,9 @@ pg.mkQApp()
def test_InfiniteLine():
# disable delay of mouse move events because events is called immediately in test
pg.setConfigOption('mouseRateLimit', -1)
# Test basic InfiniteLine API
plt = pg.plot()
plt.setXRange(-10, 10)