Make Magnifying glass shortcuts work with non-Latin layouts

Viewer::keyPressEvent()'s and MagnifyingGlass::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.

The Magnifying glass actions are now enabled/disabled when
loadedComicActions are enabled/disabled - for the same reason (see the
recent "Reader: make 12 keyboard shortcuts work with non-Latin layouts"
commit). In addition, Viewer::keyPressEvent() propagated the Magnifying
glass shortcuts to MagnifyingGlass only when it was visible. Therefore
showing/hiding Magnifying glass also enables/disables these actions.
Note that Viewer::showMagnifyingGlass() shows Magnifying glass only if
render->hasLoadedComic() returns true, so
MainWindowViewer::setMglassActionsEnabled slot can be connected directly
to Viewer::magnifyingGlassVisibilityChanged signal without checking this
condition again.
This commit is contained in:
Igor Kushnir
2021-03-08 17:44:30 +02:00
parent 5254e66da3
commit 24e42f76d2
6 changed files with 48 additions and 82 deletions

View File

@ -280,46 +280,3 @@ bool MagnifyingGlass::shrinkHeight(int &h) const
h -= heightStep; h -= heightStep;
return true; return true;
} }
void MagnifyingGlass::keyPressEvent(QKeyEvent *event)
{
bool validKey = false;
int _key = event->key();
Qt::KeyboardModifiers modifiers = event->modifiers();
if (modifiers & Qt::ShiftModifier)
_key |= Qt::SHIFT;
if (modifiers & Qt::ControlModifier)
_key |= Qt::CTRL;
if (modifiers & Qt::MetaModifier)
_key |= Qt::META;
if (modifiers & Qt::AltModifier)
_key |= Qt::ALT;
QKeySequence key(_key);
if (key == ShortcutsManager::getShortcutsManager().getShortcut(SIZE_UP_MGLASS_ACTION_Y)) {
sizeUp();
validKey = true;
}
else if (key == ShortcutsManager::getShortcutsManager().getShortcut(SIZE_DOWN_MGLASS_ACTION_Y)) {
sizeDown();
validKey = true;
}
else if (key == ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_IN_MGLASS_ACTION_Y)) {
zoomIn();
validKey = true;
}
else if (key == ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_OUT_MGLASS_ACTION_Y)) {
zoomOut();
validKey = true;
}
if (validKey) {
event->setAccepted(true);
}
}

View File

@ -22,8 +22,6 @@ private:
bool growHeight(int &h) const; bool growHeight(int &h) const;
bool shrinkHeight(int &h) const; bool shrinkHeight(int &h) const;
void keyPressEvent(QKeyEvent *event) override;
public: public:
MagnifyingGlass(int width, int height, QWidget *parent); MagnifyingGlass(int width, int height, QWidget *parent);
MagnifyingGlass(const QSize &size, QWidget *parent); MagnifyingGlass(const QSize &size, QWidget *parent);

View File

@ -139,7 +139,12 @@ void MainWindowViewer::setupUI()
// setUnifiedTitleAndToolBarOnMac(true); // setUnifiedTitleAndToolBarOnMac(true);
viewer = new Viewer(this); viewer = new Viewer(this);
connect(viewer, &Viewer::comicLoaded, this, [this] { setLoadedComicActionsEnabled(true); }); connect(viewer, &Viewer::comicLoaded, this, [this] {
if (viewer->magnifyingGlassIsVisible())
setMglassActionsEnabled(true);
setLoadedComicActionsEnabled(true);
});
connect(viewer, &Viewer::magnifyingGlassVisibilityChanged, this, &MainWindowViewer::setMglassActionsEnabled);
connect(viewer, &Viewer::reset, this, &MainWindowViewer::processReset); connect(viewer, &Viewer::reset, this, &MainWindowViewer::processReset);
// detected end of comic // detected end of comic
connect(viewer, &Viewer::openNextComic, this, &MainWindowViewer::openNextComic); connect(viewer, &Viewer::openNextComic, this, &MainWindowViewer::openNextComic);
@ -961,6 +966,7 @@ void MainWindowViewer::enableActions()
void MainWindowViewer::disableActions() void MainWindowViewer::disableActions()
{ {
setActionsEnabled(false); setActionsEnabled(false);
setMglassActionsEnabled(false);
setLoadedComicActionsEnabled(false); setLoadedComicActionsEnabled(false);
setBookmarkAction->setEnabled(false); setBookmarkAction->setEnabled(false);
} }
@ -1187,30 +1193,34 @@ void MainWindowViewer::setUpShortcutsManagement()
allActions << tmpList; allActions << tmpList;
// keys without actions (MGlass)
auto sizeUpMglassAction = new QAction(tr("Size up magnifying glass"), orphanActions); auto sizeUpMglassAction = new QAction(tr("Size up magnifying glass"), orphanActions);
sizeUpMglassAction->setData(SIZE_UP_MGLASS_ACTION_Y); sizeUpMglassAction->setData(SIZE_UP_MGLASS_ACTION_Y);
sizeUpMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(SIZE_UP_MGLASS_ACTION_Y)); sizeUpMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(SIZE_UP_MGLASS_ACTION_Y));
connect(sizeUpMglassAction, &QAction::triggered, viewer, &Viewer::magnifyingGlassSizeUp);
auto sizeDownMglassAction = new QAction(tr("Size down magnifying glass"), orphanActions); auto sizeDownMglassAction = new QAction(tr("Size down magnifying glass"), orphanActions);
sizeDownMglassAction->setData(SIZE_DOWN_MGLASS_ACTION_Y); sizeDownMglassAction->setData(SIZE_DOWN_MGLASS_ACTION_Y);
sizeDownMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(SIZE_DOWN_MGLASS_ACTION_Y)); sizeDownMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(SIZE_DOWN_MGLASS_ACTION_Y));
connect(sizeDownMglassAction, &QAction::triggered, viewer, &Viewer::magnifyingGlassSizeDown);
auto zoomInMglassAction = new QAction(tr("Zoom in magnifying glass"), orphanActions); auto zoomInMglassAction = new QAction(tr("Zoom in magnifying glass"), orphanActions);
zoomInMglassAction->setData(ZOOM_IN_MGLASS_ACTION_Y); zoomInMglassAction->setData(ZOOM_IN_MGLASS_ACTION_Y);
zoomInMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_IN_MGLASS_ACTION_Y)); zoomInMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_IN_MGLASS_ACTION_Y));
connect(zoomInMglassAction, &QAction::triggered, viewer, &Viewer::magnifyingGlassZoomIn);
auto zoomOutMglassAction = new QAction(tr("Zoom out magnifying glass"), orphanActions); auto zoomOutMglassAction = new QAction(tr("Zoom out magnifying glass"), orphanActions);
zoomOutMglassAction->setData(ZOOM_OUT_MGLASS_ACTION_Y); zoomOutMglassAction->setData(ZOOM_OUT_MGLASS_ACTION_Y);
zoomOutMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_OUT_MGLASS_ACTION_Y)); zoomOutMglassAction->setShortcut(ShortcutsManager::getShortcutsManager().getShortcut(ZOOM_OUT_MGLASS_ACTION_Y));
connect(zoomOutMglassAction, &QAction::triggered, viewer, &Viewer::magnifyingGlassZoomOut);
mglassActions = { sizeUpMglassAction, sizeDownMglassAction,
zoomInMglassAction, zoomOutMglassAction };
addActions(mglassActions);
editShortcutsDialog->addActionsGroup(tr("Magnifiying glass"), QIcon(":/images/shortcuts_group_mglass.png"), editShortcutsDialog->addActionsGroup(tr("Magnifiying glass"), QIcon(":/images/shortcuts_group_mglass.png"),
tmpList = QList<QAction *>() tmpList = QList<QAction *>()
<< showMagnifyingGlassAction << showMagnifyingGlassAction
<< sizeUpMglassAction << mglassActions);
<< sizeDownMglassAction
<< zoomInMglassAction
<< zoomOutMglassAction);
allActions << tmpList; allActions << tmpList;
@ -1542,6 +1552,12 @@ void MainWindowViewer::setActionsEnabled(bool enabled)
a->setEnabled(enabled); a->setEnabled(enabled);
} }
void MainWindowViewer::setMglassActionsEnabled(bool enabled)
{
for (auto *a : std::as_const(mglassActions))
a->setEnabled(enabled);
}
void MainWindowViewer::setLoadedComicActionsEnabled(bool enabled) void MainWindowViewer::setLoadedComicActionsEnabled(bool enabled)
{ {
for (auto *a : std::as_const(loadedComicActions)) for (auto *a : std::as_const(loadedComicActions))

View File

@ -148,6 +148,7 @@ private:
QAction *showEditShortcutsAction; QAction *showEditShortcutsAction;
QList<QAction *> mglassActions;
QList<QAction *> loadedComicActions; QList<QAction *> loadedComicActions;
YACReaderSlider *zoomSliderAction; YACReaderSlider *zoomSliderAction;
@ -163,6 +164,7 @@ private:
void clearRecentFiles(); void clearRecentFiles();
void getSiblingComics(QString path, QString currentComic); void getSiblingComics(QString path, QString currentComic);
void setActionsEnabled(bool enabled); void setActionsEnabled(bool enabled);
void setMglassActionsEnabled(bool enabled);
void setLoadedComicActionsEnabled(bool enabled); void setLoadedComicActionsEnabled(bool enabled);
//! Manejadores de evento: //! Manejadores de evento:

View File

@ -20,6 +20,7 @@
#include "opengl_checker.h" #include "opengl_checker.h"
#include <QFile> #include <QFile>
#include <QKeyEvent>
#include <QsLog.h> #include <QsLog.h>
@ -154,6 +155,11 @@ void Viewer::createConnections()
// magnifyingGlass (update mg after a background change // magnifyingGlass (update mg after a background change
connect(this, &Viewer::backgroundChanges, mglass, QOverload<>::of(&MagnifyingGlass::updateImage)); connect(this, &Viewer::backgroundChanges, mglass, QOverload<>::of(&MagnifyingGlass::updateImage));
connect(this, &Viewer::magnifyingGlassSizeUp, mglass, &MagnifyingGlass::sizeUp);
connect(this, &Viewer::magnifyingGlassSizeDown, mglass, &MagnifyingGlass::sizeDown);
connect(this, &Viewer::magnifyingGlassZoomIn, mglass, &MagnifyingGlass::zoomIn);
connect(this, &Viewer::magnifyingGlassZoomOut, mglass, &MagnifyingGlass::zoomOut);
// goToDialog // goToDialog
connect(goToDialog, &GoToDialog::goToPage, this, &Viewer::goTo); connect(goToDialog, &GoToDialog::goToPage, this, &Viewer::goTo);
@ -608,33 +614,6 @@ void Viewer::scrollTo(int x, int y)
emit backgroundChanges(); emit backgroundChanges();
} }
void Viewer::keyPressEvent(QKeyEvent *event)
{
if (render->hasLoadedComic()) {
int _key = event->key();
Qt::KeyboardModifiers modifiers = event->modifiers();
if (modifiers & Qt::ShiftModifier)
_key |= Qt::SHIFT;
if (modifiers & Qt::ControlModifier)
_key |= Qt::CTRL;
if (modifiers & Qt::MetaModifier)
_key |= Qt::META;
if (modifiers & Qt::AltModifier)
_key |= Qt::ALT;
QKeySequence key(_key);
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);
}
} else
QAbstractScrollArea::keyPressEvent(event);
}
void Viewer::moveView(Qt::Key directionKey) void Viewer::moveView(Qt::Key directionKey)
{ {
QKeyEvent event(QEvent::KeyPress, directionKey, Qt::NoModifier); QKeyEvent event(QEvent::KeyPress, directionKey, Qt::NoModifier);
@ -770,14 +749,22 @@ void Viewer::showMagnifyingGlass()
mglass->move(static_cast<int>(p.x() - float(mglass->width()) / 2), static_cast<int>(p.y() - float(mglass->height()) / 2)); mglass->move(static_cast<int>(p.x() - float(mglass->width()) / 2), static_cast<int>(p.y() - float(mglass->height()) / 2));
mglass->show(); mglass->show();
mglass->updateImage(mglass->x() + mglass->width() / 2, mglass->y() + mglass->height() / 2); mglass->updateImage(mglass->x() + mglass->width() / 2, mglass->y() + mglass->height() / 2);
magnifyingGlassShown = true; setMagnifyingGlassShown(true);
} }
} }
void Viewer::hideMagnifyingGlass() void Viewer::hideMagnifyingGlass()
{ {
mglass->hide(); mglass->hide();
magnifyingGlassShown = false; setMagnifyingGlassShown(false);
}
void Viewer::setMagnifyingGlassShown(bool shown)
{
if (magnifyingGlassShown != shown) {
magnifyingGlassShown = shown;
emit magnifyingGlassVisibilityChanged(magnifyingGlassShown);
}
} }
void Viewer::informationSwitch() void Viewer::informationSwitch()

View File

@ -8,7 +8,6 @@
#include <QTimer> #include <QTimer>
#include <QLabel> #include <QLabel>
#include <QPixmap> #include <QPixmap>
#include <QKeyEvent>
#include <QResizeEvent> #include <QResizeEvent>
#include <QWheelEvent> #include <QWheelEvent>
#include <QMouseEvent> #include <QMouseEvent>
@ -159,9 +158,9 @@ private:
MagnifyingGlass *mglass; MagnifyingGlass *mglass;
bool magnifyingGlassShown; bool magnifyingGlassShown;
bool restoreMagnifyingGlass; bool restoreMagnifyingGlass;
void setMagnifyingGlassShown(bool shown);
//! Manejadores de evento: //! Manejadores de evento:
void keyPressEvent(QKeyEvent *event) override;
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
void wheelEvent(QWheelEvent *event) override; void wheelEvent(QWheelEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
@ -198,6 +197,13 @@ signals:
void openNextComic(); void openNextComic();
void openPreviousComic(); void openPreviousComic();
void zoomUpdated(int); void zoomUpdated(int);
void magnifyingGlassVisibilityChanged(bool visible);
// The following signals are emitted by users of this class and propagated to mglass.
void magnifyingGlassSizeUp();
void magnifyingGlassSizeDown();
void magnifyingGlassZoomIn();
void magnifyingGlassZoomOut();
}; };
#endif #endif