From 4af15ab532bc6d01fdb79cb752976aa8772ba774 Mon Sep 17 00:00:00 2001 From: Alex Okonechnikov Date: Wed, 31 Dec 2025 07:46:38 -0500 Subject: [PATCH] pr comments --- .../src/game/zones/view_zone_widget.cpp | 38 +++++++++++-------- cockatrice/src/game/zones/view_zone_widget.h | 21 +++++++++- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/cockatrice/src/game/zones/view_zone_widget.cpp b/cockatrice/src/game/zones/view_zone_widget.cpp index 84d74e21c..43ad0faea 100644 --- a/cockatrice/src/game/zones/view_zone_widget.cpp +++ b/cockatrice/src/game/zones/view_zone_widget.cpp @@ -257,9 +257,24 @@ void ZoneViewWidget::stopWindowDrag() ungrabMouse(); } -QPointF ZoneViewWidget::draggedWindowPos(const QPoint &screenPos, - const QPointF &scenePos, - const QPointF &buttonDownScenePos) const +QGraphicsView *ZoneViewWidget::findDragView(QWidget *eventWidget) const +{ + QWidget *current = eventWidget; + while (current) { + if (auto *view = qobject_cast(current)) + return view; + current = current->parentWidget(); + } + + if (scene() && !scene()->views().isEmpty()) + return scene()->views().constFirst(); + + return nullptr; +} + +QPointF ZoneViewWidget::calcDraggedWindowPos(const QPoint &screenPos, + const QPointF &scenePos, + const QPointF &buttonDownScenePos) const { if (dragView && dragView->viewport()) { const QPoint vpStart = dragView->viewport()->mapFromGlobal(dragStartScreenPos); @@ -285,6 +300,7 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event) if (me->button() == Qt::LeftButton && section == Qt::TitleBarArea) { // avoid drag on close button const QRectF frameRectF = windowFrameRect(); + // square at right end of the title bar is the close button const QRectF closeRect(frameRectF.right() - kTitleBarHeight, frameRectF.top(), kTitleBarHeight, kTitleBarHeight); if (closeRect.contains(me->pos())) { @@ -297,17 +313,7 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event) dragStartItemPos = pos(); dragStartScreenPos = me->screenPos(); - dragView = nullptr; - if (me->widget()) { - QWidget *current = me->widget(); - while (current && !dragView) { - dragView = qobject_cast(current); - current = current->parentWidget(); - } - } - if (!dragView && scene() && !scene()->views().isEmpty()) { - dragView = scene()->views().constFirst(); - } + dragView = findDragView(me->widget()); // need to grab mouse to receive events and not miss initial movement grabMouse(); @@ -326,7 +332,7 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event) return true; } - setPos(draggedWindowPos(me->screenPos(), me->scenePos(), me->buttonDownScenePos(Qt::LeftButton))); + setPos(calcDraggedWindowPos(me->screenPos(), me->scenePos(), me->buttonDownScenePos(Qt::LeftButton))); me->accept(); return true; } @@ -352,7 +358,7 @@ void ZoneViewWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { // move if the scene routes moves while dragging if (draggingWindow && (event->buttons() & Qt::LeftButton)) { - setPos(draggedWindowPos(event->screenPos(), event->scenePos(), event->buttonDownScenePos(Qt::LeftButton))); + setPos(calcDraggedWindowPos(event->screenPos(), event->scenePos(), event->buttonDownScenePos(Qt::LeftButton))); event->accept(); return; } diff --git a/cockatrice/src/game/zones/view_zone_widget.h b/cockatrice/src/game/zones/view_zone_widget.h index 07bd122c4..c2801af7a 100644 --- a/cockatrice/src/game/zones/view_zone_widget.h +++ b/cockatrice/src/game/zones/view_zone_widget.h @@ -3,7 +3,6 @@ * @ingroup GameGraphicsZones * @brief TODO: Document this. */ - #ifndef ZONEVIEWWIDGET_H #define ZONEVIEWWIDGET_H @@ -30,6 +29,7 @@ class QGraphicsSceneMouseEvent; class QGraphicsSceneWheelEvent; class QStyleOption; class QGraphicsView; +class QWidget; class ScrollableGraphicsProxyWidget : public QGraphicsProxyWidget { @@ -74,7 +74,24 @@ private: QPointer dragView; void stopWindowDrag(); - QPointF draggedWindowPos(const QPoint &screenPos, const QPointF &scenePos, const QPointF &buttonDownScenePos) const; + /** + * @brief Resolves the QGraphicsView to use for drag coordinate mapping + * + * @param eventWidget QWidget that originated the mouse event + * @return The resolved QGraphicsView + */ + QGraphicsView *findDragView(QWidget *eventWidget) const; + /** + * @brief Calculates the desired widget position while dragging + * + * @param screenPos Global screen coordinates of the current mouse position + * @param scenePos Scene coordinates of the current mouse position + * @param buttonDownScenePos Scene coordinates of the initial mouse press position + * + * @return The new widget position in scene coordinates + */ + QPointF + calcDraggedWindowPos(const QPoint &screenPos, const QPointF &scenePos, const QPointF &buttonDownScenePos) const; void resizeScrollbar(qreal newZoneHeight); signals: