feat: support forward/back button on mouse for image navigation

This commit is contained in:
Gary Wang 2021-10-08 14:06:18 +08:00
parent a4416cd77c
commit 3a442b35f6
4 changed files with 42 additions and 11 deletions

View File

@ -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)

View File

@ -23,6 +23,9 @@ public:
QAction *actionToggleCheckerboard;
QAction *actionRotateClockwise;
QAction *actionPrevPicture;
QAction *actionNextPicture;
QAction *actionHorizontalFlip;
QAction *actionFitInView;
QAction *actionFitByWidth;

View File

@ -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();

View File

@ -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();