From b60766527f0634566d2cd64458132be37da4ee27 Mon Sep 17 00:00:00 2001 From: tetektoza Date: Sun, 22 Oct 2023 17:59:09 +0200 Subject: [PATCH] CelView: Add dragging for mouse This patch adds dragging for mouse, so by pressing shift dragging mode is being changed to scroll hand drag, basically it allows to navigate on the image without pressing scroll bars. --- source/celview.cpp | 41 ++++++++++++++++++++++++++++++++++------- source/celview.h | 1 + 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/source/celview.cpp b/source/celview.cpp index 4564469da..c20a36114 100644 --- a/source/celview.cpp +++ b/source/celview.cpp @@ -22,16 +22,43 @@ CelScene::CelScene(QWidget *v) void CelScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { - if (event->button() != Qt::LeftButton) { - return; - } + switch (event->button()) { + case Qt::LeftButton: { + // return if currently set drag mode is scroll hand drag + if (views()[0]->dragMode() == QGraphicsView::ScrollHandDrag) + return; - int x = event->scenePos().x(); - int y = event->scenePos().y(); + int x = event->scenePos().x(); + int y = event->scenePos().y(); - qDebug() << "Clicked: " << x << "," << y; + qDebug() << "Clicked: " << x << "," << y; - emit this->framePixelClicked(x, y); + emit this->framePixelClicked(x, y); + break; + } + case Qt::MiddleButton: { + views()[0]->setDragMode(QGraphicsView::ScrollHandDrag); + + // after middle button has been pressed - send the mouse press event to QGraphicsView that holds this scene, since + // it will toggle on dragging on ScrollHandDrag + QMouseEvent eventGraphicsView(QEvent::MouseButtonPress, event->scenePos(), event->scenePos(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); + QApplication::sendEvent(views()[0]->viewport(), &eventGraphicsView); + break; + } + } +} + +void CelScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + switch (event->button()) { + case Qt::LeftButton: { + break; + } + case Qt::MiddleButton: { + views()[0]->setDragMode(QGraphicsView::NoDrag); + break; + } + } } void CelScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event) diff --git a/source/celview.h b/source/celview.h index abf5864ea..d5154892f 100644 --- a/source/celview.h +++ b/source/celview.h @@ -28,6 +28,7 @@ class CelScene : public QGraphicsScene { CelScene(QWidget *view); private slots: + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event); void dragEnterEvent(QGraphicsSceneDragDropEvent *event); void dragMoveEvent(QGraphicsSceneDragDropEvent *event);