Skip to content

Commit

Permalink
View: Add dragging for mouse
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tetektoza committed Nov 1, 2023
1 parent 2e62c58 commit 8430ad2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions source/celview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
45 changes: 45 additions & 0 deletions source/view.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
16 changes: 16 additions & 0 deletions source/view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <QGraphicsView>
#include <QMouseEvent>
#include <QObject>

class View : public QGraphicsView {
Q_OBJECT

public:
View(QWidget *parent = nullptr);

private slots:
void mouseReleaseEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
};

0 comments on commit 8430ad2

Please sign in to comment.