From 3a442b35f6f33b7d20abd7a3cfba99eda94c73e6 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Fri, 8 Oct 2021 14:06:18 +0800 Subject: [PATCH] feat: support forward/back button on mouse for image navigation --- app/actionmanager.cpp | 14 +++++++++++--- app/actionmanager.h | 3 +++ app/mainwindow.cpp | 33 +++++++++++++++++++++++++-------- app/mainwindow.h | 3 +++ 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/app/actionmanager.cpp b/app/actionmanager.cpp index 9401b30..875d9bc 100644 --- a/app/actionmanager.cpp +++ b/app/actionmanager.cpp @@ -38,6 +38,9 @@ void ActionManager::setupAction(MainWindow *mainWindow) CREATE_NEW_ICON_ACTION(mainWindow, actionToggleCheckerboard, view-background-checkerboard); CREATE_NEW_ICON_ACTION(mainWindow, actionRotateClockwise, object-rotate-right); + CREATE_NEW_ACTION(mainWindow, actionPrevPicture); + CREATE_NEW_ACTION(mainWindow, actionNextPicture); + CREATE_NEW_ACTION(mainWindow, actionHorizontalFlip); CREATE_NEW_ACTION(mainWindow, actionFitInView); CREATE_NEW_ACTION(mainWindow, actionFitByWidth); @@ -67,6 +70,9 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) actionToggleCheckerboard->setText(QCoreApplication::translate("MainWindow", "Toggle Checkerboard", nullptr)); actionRotateClockwise->setText(QCoreApplication::translate("MainWindow", "Rotate right", nullptr)); + actionPrevPicture->setText(QCoreApplication::translate("MainWindow", "Previous image", nullptr)); + actionNextPicture->setText(QCoreApplication::translate("MainWindow", "Next image", nullptr)); + actionHorizontalFlip->setText(QCoreApplication::translate("MainWindow", "Flip &Horizontally", nullptr)); actionFitInView->setText("Fit in view"); // TODO: what should it called? actionFitByWidth->setText("Fit by width"); // TODO: what should it called? @@ -83,15 +89,17 @@ void ActionManager::retranslateUi(MainWindow *mainWindow) void ActionManager::setupShortcuts() { - actionActualSize->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_0)); + actionActualSize->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_0)); actionZoomIn->setShortcut(QKeySequence(QKeySequence::ZoomIn)); actionZoomOut->setShortcut(QKeySequence(QKeySequence::ZoomOut)); - actionHorizontalFlip->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R)); + actionPrevPicture->setShortcut(QKeySequence(Qt::Key_PageUp)); + actionNextPicture->setShortcut(QKeySequence(Qt::Key_PageDown)); + actionHorizontalFlip->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_R)); actionCopyPixmap->setShortcut(QKeySequence(QKeySequence::Copy)); actionPaste->setShortcut(QKeySequence::Paste); actionHelp->setShortcut(QKeySequence::HelpContents); actionSettings->setShortcut(QKeySequence::Preferences); - actionProperties->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_I)); + actionProperties->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_I)); actionQuitApp->setShortcuts({ QKeySequence(Qt::Key_Space), QKeySequence(Qt::Key_Escape) diff --git a/app/actionmanager.h b/app/actionmanager.h index e848db4..f2aacfb 100644 --- a/app/actionmanager.h +++ b/app/actionmanager.h @@ -23,6 +23,9 @@ public: QAction *actionToggleCheckerboard; QAction *actionRotateClockwise; + QAction *actionPrevPicture; + QAction *actionNextPicture; + QAction *actionHorizontalFlip; QAction *actionFitInView; QAction *actionFitByWidth; diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index a657b88..e5e87fb 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -126,14 +126,6 @@ MainWindow::MainWindow(QWidget *parent) m_nextButton->setVisible(galleryFileCount > 1); }); - QShortcut * prevPictureShorucut = new QShortcut(QKeySequence(Qt::Key_PageUp), this); - connect(prevPictureShorucut, &QShortcut::activated, - this, &MainWindow::galleryPrev); - - QShortcut * nextPictureShorucut = new QShortcut(QKeySequence(Qt::Key_PageDown), this); - connect(nextPictureShorucut, &QShortcut::activated, - this, &MainWindow::galleryNext); - QShortcut * fullscreenShorucut = new QShortcut(QKeySequence(QKeySequence::FullScreen), this); connect(fullscreenShorucut, &QShortcut::activated, this, &MainWindow::toggleFullscreen); @@ -280,6 +272,15 @@ void MainWindow::mousePressEvent(QMouseEvent *event) event->accept(); } + // It seems the forward/back mouse button won't generate a key event [1] so we can't use + // QShortcut or QKeySequence to indicate these shortcuts, so we do it here. + // Reference: + // [1]: https://codereview.qt-project.org/c/qt/qtbase/+/177475 + if (event->buttons() & Qt::ForwardButton || event->buttons() & Qt::BackButton) { + event->buttons() & Qt::BackButton ? galleryNext() : galleryPrev(); + event->accept(); + } + return FramelessWindow::mousePressEvent(event); } @@ -308,6 +309,12 @@ void MainWindow::mouseReleaseEvent(QMouseEvent *event) void MainWindow::mouseDoubleClickEvent(QMouseEvent *event) { + // The forward/back mouse button can also used to trigger a mouse double-click event + // Since we use that for gallery navigation so we ignore these two buttons. + if (event->buttons() & Qt::ForwardButton || event->buttons() & Qt::BackButton) { + return; + } + switch (Settings::instance()->doubleClickBehavior()) { case ActionCloseWindow: quitAppAction(); @@ -631,6 +638,16 @@ void MainWindow::on_actionRotateClockwise_triggered() m_gv->setVisible(false); } +void MainWindow::on_actionPrevPicture_triggered() +{ + galleryPrev(); +} + +void MainWindow::on_actionNextPicture_triggered() +{ + galleryNext(); +} + void MainWindow::on_actionToggleStayOnTop_triggered() { toggleStayOnTop(); diff --git a/app/mainwindow.h b/app/mainwindow.h index d97224e..29c9d1a 100644 --- a/app/mainwindow.h +++ b/app/mainwindow.h @@ -75,6 +75,9 @@ private slots: void on_actionToggleCheckerboard_triggered(); void on_actionRotateClockwise_triggered(); + void on_actionPrevPicture_triggered(); + void on_actionNextPicture_triggered(); + void on_actionHorizontalFlip_triggered(); void on_actionFitInView_triggered(); void on_actionFitByWidth_triggered();