Skip to content

Commit

Permalink
CelView: Add dragging for mouse
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tetektoza committed Oct 31, 2023
1 parent 2e62c58 commit b607665
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
41 changes: 34 additions & 7 deletions source/celview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions source/celview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b607665

Please sign in to comment.