From 76afb30acfc395923bcef3512d45318d3c193926 Mon Sep 17 00:00:00 2001 From: Christian <s224389@dtu.dk> Date: Thu, 16 Jan 2025 18:43:58 +0100 Subject: [PATCH] Added mouse cursor changes disk size functionality --- GUI_draft_live.py | 58 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/GUI_draft_live.py b/GUI_draft_live.py index e4af0fe..064c80d 100644 --- a/GUI_draft_live.py +++ b/GUI_draft_live.py @@ -72,25 +72,66 @@ class PanZoomGraphicsView(QGraphicsView): # ------------------------------------------------------------------------ # A specialized PanZoomGraphicsView for the circle editor -# Only pan if user did NOT click on the draggable circle +# - Only pan if user did NOT click on the draggable circle +# - If the mouse is over the circle item, scrolling changes radius # ------------------------------------------------------------------------ class CircleEditorGraphicsView(PanZoomGraphicsView): + def __init__(self, circle_editor_widget, parent=None): + """ + :param circle_editor_widget: Reference to the parent CircleEditorWidget + so we can communicate (e.g. update slider). + """ + super().__init__(parent) + self._circle_editor_widget = circle_editor_widget + def mousePressEvent(self, event): if event.button() == Qt.LeftButton: # Check if the user clicked on the circle item - clicked_item = self.itemAt(event.pos().x(), event.pos().y()) + clicked_item = self.itemAt(event.pos()) if clicked_item is not None: # Walk up parent chain to see if it is our DraggableCircleItem it = clicked_item while it is not None and not hasattr(it, "boundingRect"): it = it.parentItem() - from PyQt5.QtWidgets import QGraphicsEllipseItem + if isinstance(it, DraggableCircleItem): # Let normal item-dragging occur, don't initiate panning return QGraphicsView.mousePressEvent(self, event) # Otherwise proceed with normal panning logic super().mousePressEvent(event) + def wheelEvent(self, event): + """ + Overridden so that 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: + # climb up the chain to find if it's our DraggableCircleItem + it = item_under + while it is not None and not hasattr(it, "boundingRect"): + it = it.parentItem() + + if isinstance(it, DraggableCircleItem): + # Scroll up -> increase radius, scroll down -> decrease + delta = event.angleDelta().y() + # each wheel "notch" is typically 120, so let's do small steps + step = 1 if delta > 0 else -1 + + old_r = it.radius() + new_r = max(1, old_r + step) + it.set_radius(new_r) + + # Also update the slider in the parent CircleEditorWidget + self._circle_editor_widget.update_slider_value(new_r) + + event.accept() + return + + # else do normal pan/zoom + super().wheelEvent(event) + # ------------------------------------------------------------------------ # Draggable circle item (centered at (x, y) with radius) @@ -141,7 +182,7 @@ class CircleEditorWidget(QWidget): self.setLayout(layout) # Use specialized CircleEditorGraphicsView - self._graphics_view = CircleEditorGraphicsView() + self._graphics_view = CircleEditorGraphicsView(circle_editor_widget=self) self._scene = QGraphicsScene(self) self._graphics_view.setScene(self._scene) layout.addWidget(self._graphics_view) @@ -186,6 +227,15 @@ class CircleEditorWidget(QWidget): if self._done_callback is not None: self._done_callback(final_radius) + def update_slider_value(self, new_radius): + """ + Called by CircleEditorGraphicsView when the user scrolls on the circle item. + We sync the slider to the new radius. + """ + self._slider.blockSignals(True) # to avoid recursively calling set_radius + self._slider.setValue(new_radius) + self._slider.blockSignals(False) + def sizeHint(self): return QSize(800, 600) -- GitLab