Skip to content
Snippets Groups Projects
circleEditorGraphicsView.py 2.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • Christian's avatar
    Christian committed
    from PyQt5.QtWidgets import QGraphicsView, QWidget
    
    from panZoomGraphicsView import PanZoomGraphicsView
    from PyQt5.QtCore import Qt
    
    Christian's avatar
    Christian committed
    from PyQt5.QtGui import QMouseEvent, QWheelEvent
    
    from draggableCircleItem import DraggableCircleItem
    
    Christian's avatar
    Christian committed
    from typing import Optional
    
    Christian's avatar
    Christian committed
    # A specialized PanZoomGraphicsView for the circle editor (disk size calibration)
    
    class CircleEditorGraphicsView(PanZoomGraphicsView):
    
    s224361's avatar
    s224361 committed
        def __init__(self, circle_editor_widget, parent: Optional[QWidget] = None):
    
    Christian's avatar
    Christian committed
            """
            Constructor.
            """
    
            super().__init__(parent)
            self._circle_editor_widget = circle_editor_widget
    
    
    Christian's avatar
    Christian committed
        def mousePressEvent(self, event: QMouseEvent):
            """
            If the user clicks on the circle, we let the circle item handle the 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)
    
    
    Christian's avatar
    Christian committed
        def wheelEvent(self, event: QWheelEvent):
    
    Christian's avatar
    Christian committed
            If the user scrolls the mouse wheel over the circle, we change the circle
    
            """
            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
    
    
    Christian's avatar
    Christian committed
            super().wheelEvent(event)