pr comments

This commit is contained in:
Alex Okonechnikov 2025-12-31 07:46:38 -05:00
parent 02c6d0bf47
commit 4af15ab532
2 changed files with 41 additions and 18 deletions

View file

@ -257,9 +257,24 @@ void ZoneViewWidget::stopWindowDrag()
ungrabMouse(); ungrabMouse();
} }
QPointF ZoneViewWidget::draggedWindowPos(const QPoint &screenPos, QGraphicsView *ZoneViewWidget::findDragView(QWidget *eventWidget) const
const QPointF &scenePos, {
const QPointF &buttonDownScenePos) const QWidget *current = eventWidget;
while (current) {
if (auto *view = qobject_cast<QGraphicsView *>(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()) { if (dragView && dragView->viewport()) {
const QPoint vpStart = dragView->viewport()->mapFromGlobal(dragStartScreenPos); const QPoint vpStart = dragView->viewport()->mapFromGlobal(dragStartScreenPos);
@ -285,6 +300,7 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event)
if (me->button() == Qt::LeftButton && section == Qt::TitleBarArea) { if (me->button() == Qt::LeftButton && section == Qt::TitleBarArea) {
// avoid drag on close button // avoid drag on close button
const QRectF frameRectF = windowFrameRect(); 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, const QRectF closeRect(frameRectF.right() - kTitleBarHeight, frameRectF.top(), kTitleBarHeight,
kTitleBarHeight); kTitleBarHeight);
if (closeRect.contains(me->pos())) { if (closeRect.contains(me->pos())) {
@ -297,17 +313,7 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event)
dragStartItemPos = pos(); dragStartItemPos = pos();
dragStartScreenPos = me->screenPos(); dragStartScreenPos = me->screenPos();
dragView = nullptr; dragView = findDragView(me->widget());
if (me->widget()) {
QWidget *current = me->widget();
while (current && !dragView) {
dragView = qobject_cast<QGraphicsView *>(current);
current = current->parentWidget();
}
}
if (!dragView && scene() && !scene()->views().isEmpty()) {
dragView = scene()->views().constFirst();
}
// need to grab mouse to receive events and not miss initial movement // need to grab mouse to receive events and not miss initial movement
grabMouse(); grabMouse();
@ -326,7 +332,7 @@ bool ZoneViewWidget::windowFrameEvent(QEvent *event)
return true; return true;
} }
setPos(draggedWindowPos(me->screenPos(), me->scenePos(), me->buttonDownScenePos(Qt::LeftButton))); setPos(calcDraggedWindowPos(me->screenPos(), me->scenePos(), me->buttonDownScenePos(Qt::LeftButton)));
me->accept(); me->accept();
return true; return true;
} }
@ -352,7 +358,7 @@ void ZoneViewWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{ {
// move if the scene routes moves while dragging // move if the scene routes moves while dragging
if (draggingWindow && (event->buttons() & Qt::LeftButton)) { 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(); event->accept();
return; return;
} }

View file

@ -3,7 +3,6 @@
* @ingroup GameGraphicsZones * @ingroup GameGraphicsZones
* @brief TODO: Document this. * @brief TODO: Document this.
*/ */
#ifndef ZONEVIEWWIDGET_H #ifndef ZONEVIEWWIDGET_H
#define ZONEVIEWWIDGET_H #define ZONEVIEWWIDGET_H
@ -30,6 +29,7 @@ class QGraphicsSceneMouseEvent;
class QGraphicsSceneWheelEvent; class QGraphicsSceneWheelEvent;
class QStyleOption; class QStyleOption;
class QGraphicsView; class QGraphicsView;
class QWidget;
class ScrollableGraphicsProxyWidget : public QGraphicsProxyWidget class ScrollableGraphicsProxyWidget : public QGraphicsProxyWidget
{ {
@ -74,7 +74,24 @@ private:
QPointer<QGraphicsView> dragView; QPointer<QGraphicsView> dragView;
void stopWindowDrag(); 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); void resizeScrollbar(qreal newZoneHeight);
signals: signals: