From 8430ad217038833ce5094f781ac56947957962b0 Mon Sep 17 00:00:00 2001 From: tetektoza Date: Sun, 22 Oct 2023 17:59:09 +0200 Subject: [PATCH] View: Add dragging for mouse This patch adds dragging for mouse, so by pressing middle mouse button dragging mode is being changed to scroll hand drag, basically it allows to navigate on the image without pressing scroll bars. To do that, View class had to be introduced which has been introduced to override some of QGraphicsView mouse events and to promote QGraphicsView widget to it. --- CMakeLists.txt | 4 ++++ source/celview.cpp | 4 ++++ source/view.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ source/view.h | 16 ++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 source/view.cpp create mode 100644 source/view.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 518dcad6f..ec1d49ea0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) + +include_directories(source/) set(PROJECT_SOURCES source/celview.cpp source/celview.h @@ -49,6 +51,8 @@ set(PROJECT_SOURCES source/exportdialog.cpp source/exportdialog.h source/exportdialog.ui + source/view.cpp + source/view.h source/levelcelview.cpp source/levelcelview.h source/levelcelview.ui diff --git a/source/celview.cpp b/source/celview.cpp index 4564469da..ef9d05e89 100644 --- a/source/celview.cpp +++ b/source/celview.cpp @@ -22,6 +22,10 @@ CelScene::CelScene(QWidget *v) void CelScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { + // return if currently set drag mode is scroll hand drag + if (views()[0]->dragMode() == QGraphicsView::ScrollHandDrag) + return; + if (event->button() != Qt::LeftButton) { return; } diff --git a/source/view.cpp b/source/view.cpp new file mode 100644 index 000000000..4c036b03c --- /dev/null +++ b/source/view.cpp @@ -0,0 +1,45 @@ +#include "view.h" + +View::View(QWidget *parent) + : QGraphicsView(parent) +{ +} + +void View::mousePressEvent(QMouseEvent *event) +{ + switch (event->button()) { + case Qt::LeftButton: { + QGraphicsView::mousePressEvent(event); + break; + } + case Qt::MiddleButton: { + this->setDragMode(QGraphicsView::ScrollHandDrag); + + // after middle button has been pressed - send the mouse press event to base + // class that holds this scene, since it will toggle on dragging on ScrollHandDrag + QMouseEvent *pressEvent = new QMouseEvent(QEvent::MouseButtonPress, + event->pos(), event->globalPosition(), Qt::MouseButton::LeftButton, + Qt::MouseButton::LeftButton, Qt::KeyboardModifier::NoModifier); + + QGraphicsView::mousePressEvent(pressEvent); + break; + } + default: { + QGraphicsView::mousePressEvent(event); + break; + } + } +} + +void View::mouseReleaseEvent(QMouseEvent *event) +{ + switch (event->button()) { + case Qt::LeftButton: { + break; + } + case Qt::MiddleButton: { + this->setDragMode(QGraphicsView::NoDrag); + break; + } + } +} diff --git a/source/view.h b/source/view.h new file mode 100644 index 000000000..62de97ddf --- /dev/null +++ b/source/view.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include +#include + +class View : public QGraphicsView { + Q_OBJECT + +public: + View(QWidget *parent = nullptr); + +private slots: + void mouseReleaseEvent(QMouseEvent *event); + void mousePressEvent(QMouseEvent *event); +};