Skip to content
Snippets Groups Projects
circleEditorGraphicsView.py 1.89 KiB
Newer Older
  • Learn to ignore specific revisions
  • from PyQt5.QtWidgets import QGraphicsView
    from panZoomGraphicsView import PanZoomGraphicsView
    from PyQt5.QtCore import Qt
    from draggableCircleItem import DraggableCircleItem
    
    # A specialized PanZoomGraphicsView for the circle editor
    class CircleEditorGraphicsView(PanZoomGraphicsView):
        def __init__(self, circle_editor_widget, parent=None):
            super().__init__(parent)
            self._circle_editor_widget = circle_editor_widget
    
        def mousePressEvent(self, event):
            if event.button() == Qt.LeftButton:
                # Check if user clicked on the circle item
                clicked_item = self.itemAt(event.pos())
                if clicked_item is not None:
                    # climb up parent chain
                    it = clicked_item
                    while it is not None and not hasattr(it, "boundingRect"):
                        it = it.parentItem()
    
                    if isinstance(it, DraggableCircleItem):
                        # Let normal item-dragging occur, no pan
                        return QGraphicsView.mousePressEvent(self, event)
            super().mousePressEvent(event)
    
        def wheelEvent(self, event):
            """
            If the mouse is hovering over the circle, we adjust the circle's radius
            instead of zooming the image.
            """
            pos_in_widget = event.pos()
            item_under = self.itemAt(pos_in_widget)
            if item_under is not None:
                it = item_under
                while it is not None and not hasattr(it, "boundingRect"):
                    it = it.parentItem()
    
                if isinstance(it, DraggableCircleItem):
                    delta = event.angleDelta().y()
                    step = 1 if delta > 0 else -1
                    old_r = it.radius()
                    new_r = max(1, old_r + step)
                    it.set_radius(new_r)
                    self._circle_editor_widget.update_slider_value(new_r)
                    event.accept()
                    return
    
            super().wheelEvent(event)