Skip to content
Snippets Groups Projects
panZoomGraphicsView.py 1.77 KiB
Newer Older
  • Learn to ignore specific revisions
  • from PyQt5.QtWidgets import QGraphicsView, QSizePolicy
    from PyQt5.QtCore import Qt
    
    class PanZoomGraphicsView(QGraphicsView):
    
    s224362's avatar
    s224362 committed
        """
        A QGraphicsView subclass that supports panning and zooming with the mouse.
        """
    
        def __init__(self, parent=None):
            super().__init__(parent)
            self.setDragMode(QGraphicsView.NoDrag)  # We'll handle panning manually
            self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
            self._panning = False
            self._pan_start = None
    
    
    s224362's avatar
    s224362 committed
            # Expands layout
    
            self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
    
    
    s224361's avatar
    s224361 committed
        def wheelEvent(self, event):
    
            """ Zoom in/out with mouse wheel. """
            zoom_in_factor = 1.25
            zoom_out_factor = 1 / zoom_in_factor
            if event.angleDelta().y() > 0:
                self.scale(zoom_in_factor, zoom_in_factor)
            else:
                self.scale(zoom_out_factor, zoom_out_factor)
            event.accept()
    
    
    s224361's avatar
    s224361 committed
        def mousePressEvent(self, event):
    
            """ If left button: Start panning (unless overridden). """
            if event.button() == Qt.LeftButton:
                self._panning = True
                self._pan_start = event.pos()
                self.setCursor(Qt.ClosedHandCursor)
    
    s224361's avatar
    s224361 committed
            super().mousePressEvent(event)
    
    s224361's avatar
    s224361 committed
        def mouseMoveEvent(self, event):
    
            """ If panning, translate the scene. """
            if self._panning and self._pan_start is not None:
                delta = event.pos() - self._pan_start
                self._pan_start = event.pos()
                self.translate(delta.x(), delta.y())
    
    s224361's avatar
    s224361 committed
            super().mouseMoveEvent(event)
    
    s224361's avatar
    s224361 committed
        def mouseReleaseEvent(self, event):
    
            """ End panning. """
            if event.button() == Qt.LeftButton:
                self._panning = False
                self.setCursor(Qt.ArrowCursor)
    
    s224361's avatar
    s224361 committed
            super().mouseReleaseEvent(event)