Skip to content

Commit

Permalink
Only pass wheelEvent to children that can handle it
Browse files Browse the repository at this point in the history
  • Loading branch information
roomrys committed Jul 26, 2024
1 parent 28c34e2 commit 8185aa2
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions sleap/gui/widgets/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,29 +1150,24 @@ def mouseDoubleClickEvent(self, event: QMouseEvent):
QGraphicsView.mouseDoubleClickEvent(self, event)

def wheelEvent(self, event):
"""Custom event handler. Zoom in/out based on scroll wheel change."""
# zoom on wheel when no mouse buttons are pressed
"""Custom event handler to zoom in/out based on scroll wheel change.
We cannot use the default QGraphicsView.wheelEvent behavior since that will
scroll the view.
"""

# Zoom on wheel when no mouse buttons are pressed
if event.buttons() == Qt.NoButton:
angle = event.angleDelta().y()
factor = 1.1 if angle > 0 else 0.9

self.zoomFactor = max(factor * self.zoomFactor, 1)
self.updateViewer()

# Trigger wheelEvent for all child elements. This is a bit of a hack.
# We can't use QGraphicsView.wheelEvent(self, event) since that will scroll
# view.
# We want to trigger for all children, since wheelEvent should continue rotating
# an skeleton even if the skeleton node/node label is no longer under the
# cursor.
# Note that children expect a QGraphicsSceneWheelEvent event, which is why we're
# explicitly ignoring TypeErrors. Everything seems to work fine since we don't
# care about the mouse position; if we did, we'd need to map pos to scene.
# Trigger only for rotation-relevant children (otherwise GUI crashes)
for child in self.items():
try:
if isinstance(child, (QtNode, QtNodeLabel)):
child.wheelEvent(event)
except TypeError:
pass

def keyPressEvent(self, event):
"""Custom event hander, disables default QGraphicsView behavior."""
Expand Down Expand Up @@ -1590,7 +1585,9 @@ def mouseReleaseEvent(self, event):
def wheelEvent(self, event):
"""Custom event handler for mouse scroll wheel."""
if self.dragParent:
angle = event.delta() / 20 + self.parentObject().rotation()
angle = (
event.angleDelta().x() + event.angleDelta().y()
) / 20 + self.parentObject().rotation()
self.parentObject().setRotation(angle)
event.accept()

Expand Down

0 comments on commit 8185aa2

Please sign in to comment.