From a1bb7735d262c573f7b677d3dedba84ecec8cada Mon Sep 17 00:00:00 2001 From: Igor Kushnir Date: Sat, 6 Mar 2021 18:48:25 +0200 Subject: [PATCH] Reader: make 12 keyboard shortcuts work with non-Latin layouts Viewer::keyPressEvent()'s custom matching of these shortcuts is the same as MainWindowViewer::keyPressEvent()'s before the recent commit "Reader: make 3 keyboard shortcuts work with non-Latin layouts". That commit's message details the issues with the custom code. render->hasLoadedComic() condition in Viewer::keyPressEvent() becomes true when Comic::_loaded is set to true. This always happens right after Comic emits its numPages() signal. That is why the 12 fixed actions are now enabled when Viewer emits its comicLoaded() signal, which is connected to Render::numPages, which in turn is connected to Comic::numPages signal. The 12 fixed actions are now disabled when most other actions are disabled: before a comic is opened and on comic opening error. --- YACReader/main_window_viewer.cpp | 49 ++++++++++++----- YACReader/main_window_viewer.h | 3 ++ YACReader/viewer.cpp | 90 +++++++++----------------------- YACReader/viewer.h | 9 +++- 4 files changed, 73 insertions(+), 78 deletions(-) diff --git a/YACReader/main_window_viewer.cpp b/YACReader/main_window_viewer.cpp index 7f0140ce..3cb01319 100644 --- a/YACReader/main_window_viewer.cpp +++ b/YACReader/main_window_viewer.cpp @@ -24,6 +24,8 @@ #include #include +#include + #include #include #include @@ -137,6 +139,7 @@ void MainWindowViewer::setupUI() // setUnifiedTitleAndToolBarOnMac(true); viewer = new Viewer(this); + connect(viewer, &Viewer::comicLoaded, this, [this] { setLoadedComicActionsEnabled(true); }); connect(viewer, &Viewer::reset, this, &MainWindowViewer::processReset); // detected end of comic connect(viewer, &Viewer::openNextComic, this, &MainWindowViewer::openNextComic); @@ -958,6 +961,7 @@ void MainWindowViewer::enableActions() void MainWindowViewer::disableActions() { setActionsEnabled(false); + setLoadedComicActionsEnabled(false); setBookmarkAction->setEnabled(false); } @@ -1236,50 +1240,76 @@ void MainWindowViewer::setUpShortcutsManagement() auto autoScrollForwardAction = new QAction(tr("Autoscroll down"), orphanActions); autoScrollForwardAction->setData(AUTO_SCROLL_FORWARD_ACTION_Y); autoScrollForwardAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_FORWARD_ACTION_Y)); + connect(autoScrollForwardAction, &QAction::triggered, viewer, &Viewer::scrollForward); auto autoScrollBackwardAction = new QAction(tr("Autoscroll up"), orphanActions); autoScrollBackwardAction->setData(AUTO_SCROLL_BACKWARD_ACTION_Y); autoScrollBackwardAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_BACKWARD_ACTION_Y)); + connect(autoScrollBackwardAction, &QAction::triggered, viewer, &Viewer::scrollBackward); auto autoScrollForwardHorizontalFirstAction = new QAction(tr("Autoscroll forward, horizontal first"), orphanActions); autoScrollForwardHorizontalFirstAction->setData(AUTO_SCROLL_FORWARD_HORIZONTAL_FIRST_ACTION_Y); autoScrollForwardHorizontalFirstAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_FORWARD_HORIZONTAL_FIRST_ACTION_Y)); + connect(autoScrollForwardHorizontalFirstAction, &QAction::triggered, viewer, &Viewer::scrollForwardHorizontalFirst); auto autoScrollBackwardHorizontalFirstAction = new QAction(tr("Autoscroll backward, horizontal first"), orphanActions); autoScrollBackwardHorizontalFirstAction->setData(AUTO_SCROLL_BACKWARD_HORIZONTAL_FIRST_ACTION_Y); autoScrollBackwardHorizontalFirstAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_BACKWARD_HORIZONTAL_FIRST_ACTION_Y)); + connect(autoScrollBackwardHorizontalFirstAction, &QAction::triggered, viewer, &Viewer::scrollBackwardHorizontalFirst); auto autoScrollForwardVerticalFirstAction = new QAction(tr("Autoscroll forward, vertical first"), orphanActions); autoScrollForwardVerticalFirstAction->setData(AUTO_SCROLL_FORWARD_VERTICAL_FIRST_ACTION_Y); autoScrollForwardVerticalFirstAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_FORWARD_VERTICAL_FIRST_ACTION_Y)); + connect(autoScrollForwardVerticalFirstAction, &QAction::triggered, viewer, &Viewer::scrollForwardVerticalFirst); auto autoScrollBackwardVerticalFirstAction = new QAction(tr("Autoscroll backward, vertical first"), orphanActions); autoScrollBackwardVerticalFirstAction->setData(AUTO_SCROLL_BACKWARD_VERTICAL_FIRST_ACTION_Y); autoScrollBackwardVerticalFirstAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_BACKWARD_VERTICAL_FIRST_ACTION_Y)); + connect(autoScrollBackwardVerticalFirstAction, &QAction::triggered, viewer, &Viewer::scrollBackwardVerticalFirst); auto moveDownAction = new QAction(tr("Move down"), orphanActions); moveDownAction->setData(MOVE_DOWN_ACTION_Y); moveDownAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(MOVE_DOWN_ACTION_Y)); + connect(moveDownAction, &QAction::triggered, viewer, [this] { viewer->moveView(Qt::Key_Down); }); auto moveUpAction = new QAction(tr("Move up"), orphanActions); moveUpAction->setData(MOVE_UP_ACTION_Y); moveUpAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(MOVE_UP_ACTION_Y)); + connect(moveUpAction, &QAction::triggered, viewer, [this] { viewer->moveView(Qt::Key_Up); }); auto moveLeftAction = new QAction(tr("Move left"), orphanActions); moveLeftAction->setData(MOVE_LEFT_ACTION_Y); moveLeftAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(MOVE_LEFT_ACTION_Y)); + connect(moveLeftAction, &QAction::triggered, viewer, [this] { viewer->moveView(Qt::Key_Left); }); auto moveRightAction = new QAction(tr("Move right"), orphanActions); moveRightAction->setData(MOVE_RIGHT_ACTION_Y); moveRightAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(MOVE_RIGHT_ACTION_Y)); + connect(moveRightAction, &QAction::triggered, viewer, [this] { viewer->moveView(Qt::Key_Right); }); auto goToFirstPageAction = new QAction(tr("Go to the first page"), orphanActions); goToFirstPageAction->setData(GO_TO_FIRST_PAGE_ACTION_Y); goToFirstPageAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(GO_TO_FIRST_PAGE_ACTION_Y)); + connect(goToFirstPageAction, &QAction::triggered, viewer, &Viewer::goToFirstPage); auto goToLastPageAction = new QAction(tr("Go to the last page"), orphanActions); goToLastPageAction->setData(GO_TO_LAST_PAGE_ACTION_Y); goToLastPageAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(GO_TO_LAST_PAGE_ACTION_Y)); + connect(goToLastPageAction, &QAction::triggered, viewer, &Viewer::goToLastPage); + + loadedComicActions = { autoScrollForwardAction, + autoScrollBackwardAction, + autoScrollForwardHorizontalFirstAction, + autoScrollBackwardHorizontalFirstAction, + autoScrollForwardVerticalFirstAction, + autoScrollBackwardVerticalFirstAction, + moveDownAction, + moveUpAction, + moveLeftAction, + moveRightAction, + goToFirstPageAction, + goToLastPageAction }; + addActions(loadedComicActions); editShortcutsDialog->addActionsGroup(tr("Reading"), QIcon(":/images/shortcuts_group_reading.png"), tmpList = QList() @@ -1287,18 +1317,7 @@ void MainWindowViewer::setUpShortcutsManagement() << goToPageOnTheLeftAction << setBookmarkAction << showBookmarksAction - << autoScrollForwardAction - << autoScrollBackwardAction - << autoScrollForwardHorizontalFirstAction - << autoScrollBackwardHorizontalFirstAction - << autoScrollForwardVerticalFirstAction - << autoScrollBackwardVerticalFirstAction - << moveDownAction - << moveUpAction - << moveLeftAction - << moveRightAction - << goToFirstPageAction - << goToLastPageAction + << loadedComicActions << goToPageAction); allActions << tmpList; @@ -1523,6 +1542,12 @@ void MainWindowViewer::setActionsEnabled(bool enabled) a->setEnabled(enabled); } +void MainWindowViewer::setLoadedComicActionsEnabled(bool enabled) +{ + for (auto *a : std::as_const(loadedComicActions)) + a->setEnabled(enabled); +} + void MainWindowViewer::dropEvent(QDropEvent *event) { QList urlList; diff --git a/YACReader/main_window_viewer.h b/YACReader/main_window_viewer.h index 23c26683..54746725 100644 --- a/YACReader/main_window_viewer.h +++ b/YACReader/main_window_viewer.h @@ -148,6 +148,8 @@ private: QAction *showEditShortcutsAction; + QList loadedComicActions; + YACReaderSlider *zoomSliderAction; HttpVersionChecker *versionChecker; @@ -161,6 +163,7 @@ private: void clearRecentFiles(); void getSiblingComics(QString path, QString currentComic); void setActionsEnabled(bool enabled); + void setLoadedComicActionsEnabled(bool enabled); //! Manejadores de evento: // void resizeEvent(QResizeEvent * event); diff --git a/YACReader/viewer.cpp b/YACReader/viewer.cpp index d024945b..edda73de 100644 --- a/YACReader/viewer.cpp +++ b/YACReader/viewer.cpp @@ -178,6 +178,7 @@ void Viewer::createConnections() connect(render, &Render::crcError, this, &Viewer::processCRCError); connect(render, QOverload::of(&Render::numPages), goToFlow, &GoToFlowWidget::setNumSlides); connect(render, QOverload::of(&Render::numPages), goToDialog, &GoToDialog::setNumPages); + connect(render, qOverload(&Render::numPages), this, &Viewer::comicLoaded); connect(render, QOverload::of(&Render::imageLoaded), goToFlow, &GoToFlowWidget::setImageReady); connect(render, &Render::currentPageReady, this, &Viewer::updatePage); connect(render, &Render::processingPage, this, &Viewer::setLoadingMessage); @@ -284,6 +285,14 @@ void Viewer::showGoToDialog() { goToDialog->open(); } +void Viewer::goToFirstPage() +{ + goTo(0); +} +void Viewer::goToLastPage() +{ + goTo(this->render->numPages() - 1); +} void Viewer::goTo(unsigned int page) { direction = 1; // in "go to" direction is always fordward @@ -456,6 +465,18 @@ void Viewer::scrollUp() } } +void Viewer::scrollForward() +{ + nextPos = verticalScrollBar()->sliderPosition() + verticalScrollStep(); + scrollDown(); +} + +void Viewer::scrollBackward() +{ + nextPos = verticalScrollBar()->sliderPosition() - verticalScrollStep(); + scrollUp(); +} + void Viewer::scrollForwardHorizontalFirst() { if (!doubleMangaPage) { @@ -603,54 +624,8 @@ void Viewer::keyPressEvent(QKeyEvent *event) _key |= Qt::ALT; QKeySequence key(_key); - /*if(goToFlow->isVisible() && event->key()!=Qt::Key_S) - QCoreApplication::sendEvent(goToFlow,event); - else*/ - if (key == ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_FORWARD_ACTION_Y)) { - nextPos = verticalScrollBar()->sliderPosition() + verticalScrollStep(); - scrollDown(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_BACKWARD_ACTION_Y)) { - nextPos = verticalScrollBar()->sliderPosition() - verticalScrollStep(); - scrollUp(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_FORWARD_HORIZONTAL_FIRST_ACTION_Y)) { - scrollForwardHorizontalFirst(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_BACKWARD_HORIZONTAL_FIRST_ACTION_Y)) { - scrollBackwardHorizontalFirst(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_FORWARD_VERTICAL_FIRST_ACTION_Y)) { - scrollForwardVerticalFirst(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(AUTO_SCROLL_BACKWARD_VERTICAL_FIRST_ACTION_Y)) { - scrollBackwardVerticalFirst(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_DOWN_ACTION_Y) || - key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_UP_ACTION_Y) || - key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_LEFT_ACTION_Y) || - key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_RIGHT_ACTION_Y)) { - moveAction(key); - emit backgroundChanges(); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(GO_TO_FIRST_PAGE_ACTION_Y)) { - goTo(0); - } - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(GO_TO_LAST_PAGE_ACTION_Y)) { - goTo(this->render->numPages() - 1); - } - - else - QAbstractScrollArea::keyPressEvent(event); + QAbstractScrollArea::keyPressEvent(event); if (mglass->isVisible() && (key == ShortcutsManager::getShortcutsManager().getShortcut(SIZE_UP_MGLASS_ACTION_Y) || key == ShortcutsManager::getShortcutsManager().getShortcut(SIZE_DOWN_MGLASS_ACTION_Y) || key == ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_IN_MGLASS_ACTION_Y) || key == ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_OUT_MGLASS_ACTION_Y))) { QCoreApplication::sendEvent(mglass, event); @@ -660,24 +635,11 @@ void Viewer::keyPressEvent(QKeyEvent *event) QAbstractScrollArea::keyPressEvent(event); } -void Viewer::moveAction(const QKeySequence &key) +void Viewer::moveView(Qt::Key directionKey) { - int _key = 0; - - if (key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_DOWN_ACTION_Y)) - _key = Qt::Key_Down; - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_UP_ACTION_Y)) - _key = Qt::Key_Up; - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_LEFT_ACTION_Y)) - _key = Qt::Key_Left; - - else if (key == ShortcutsManager::getShortcutsManager().getShortcut(MOVE_RIGHT_ACTION_Y)) - _key = Qt::Key_Right; - - QKeyEvent _event = QKeyEvent(QEvent::KeyPress, _key, Qt::NoModifier); - QAbstractScrollArea::keyPressEvent(&_event); + QKeyEvent event(QEvent::KeyPress, directionKey, Qt::NoModifier); + QAbstractScrollArea::keyPressEvent(&event); + emit backgroundChanges(); } static void animateScroll(QPropertyAnimation &scroller, const QScrollBar &scrollBar, int delta) diff --git a/YACReader/viewer.h b/YACReader/viewer.h index 7e959511..ee5bbda9 100644 --- a/YACReader/viewer.h +++ b/YACReader/viewer.h @@ -51,6 +51,8 @@ public slots: void left(); void right(); void showGoToDialog(); + void goToFirstPage(); + void goToLastPage(); void goTo(unsigned int page); void updatePage(); void updateContentSize(); @@ -58,6 +60,8 @@ public slots: void updateOptions(); void scrollDown(); void scrollUp(); + void scrollForward(); + void scrollBackward(); void scrollForwardHorizontalFirst(); void scrollBackwardHorizontalFirst(); void scrollForwardVerticalFirst(); @@ -162,8 +166,6 @@ private: void wheelEvent(QWheelEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; - void moveAction(const QKeySequence &key); - int verticalScrollStep() const; int horizontalScrollStep() const; @@ -185,10 +187,13 @@ public: // returns the current index starting in 1 [1,nPages] unsigned int getIndex(); void updateComic(ComicDB &comic); + void moveView(Qt::Key directionKey); + signals: void backgroundChanges(); void pageAvailable(bool); void pageIsBookmark(bool); + void comicLoaded(); void reset(); void openNextComic(); void openPreviousComic();