Skip to content

Commit

Permalink
Merge pull request #6 from Ledger-Donjon/fix-zoom
Browse files Browse the repository at this point in the history
Don't zoom at cursor when following stage
  • Loading branch information
mmouchous-ledger authored Jul 25, 2024
2 parents 44b8cf2 + 40b6ab4 commit 34c3d3b
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions laserstudio/widgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,27 +298,27 @@ def wheelEvent(self, event: Optional[QWheelEvent]):
"""
if event is None:
return
# We want to zoom relative to the current cursor position, not relative
# to the center of the widget. This involves some math...
# p is the pointed position in the scene, and we want to keep p at the
# same screen position after changing the zoom. If c1 and c2 are the
# camera positions before and after the zoom changes,
# z1 and z2 the zoom levels, then we want:
# z1 * (p - c1) = z2 * (p - c2)
# which gives:
# c2 = c1 * (z1/z2) + p * (1 - z1/z2)
# we can use zr = z2/z1, the zoom factor to apply.

# The pointed position
p = self.mapToScene(event.position().toPoint())

# Get current position and zoom factor of camera
pos, zoom = self.cam_pos_zoom
# The zoom factor to apply
zr = 2 ** (event.angleDelta().y() / (8 * 120))

# Get current position and zoom factor of camera
pos, zoom = self.cam_pos_zoom
if not self._follow_stage_sight:
# We want to zoom relative to the current cursor position, not relative
# to the center of the widget. This involves some math...
# p is the pointed position in the scene, and we want to keep p at the
# same screen position after changing the zoom. If c1 and c2 are the
# camera positions before and after the zoom changes,
# z1 and z2 the zoom levels, then we want:
# z1 * (p - c1) = z2 * (p - c2)
# which gives:
# c2 = c1 * (z1/z2) + p * (1 - z1/z2)
# we can use zr = z2/z1, the zoom factor to apply.

# The pointed position
p = self.mapToScene(event.position().toPoint())
pos = (pos / zr) + (p * (1 - (1 / zr)))

pos = (pos / zr) + (p * (1 - (1 / zr)))
zoom *= zr

# Update the position and zoom factors
Expand Down Expand Up @@ -347,18 +347,19 @@ def mousePressEvent(self, event: Optional[QMouseEvent]):

# The event is a press of the right button
if event.button() == Qt.MouseButton.RightButton:
# Scroll gesture mode
self.setDragMode(Viewer.DragMode.ScrollHandDrag)
# Transform as left press button event,
# to make the scroll by dragging actually effective.
event = QMouseEvent(
event.type(),
event.position(),
Qt.MouseButton.LeftButton,
event.buttons(),
event.modifiers(),
event.pointingDevice(),
)
if not self._follow_stage_sight:
# Scroll gesture mode
self.setDragMode(Viewer.DragMode.ScrollHandDrag)
# Transform as left press button event,
# to make the scroll by dragging actually effective.
event = QMouseEvent(
event.type(),
event.position(),
Qt.MouseButton.LeftButton,
event.buttons(),
event.modifiers(),
event.pointingDevice(),
)

super().mousePressEvent(event)

Expand Down

0 comments on commit 34c3d3b

Please sign in to comment.