From 6195be5af72baea2a7cd8870b3c0bdb615401da7 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Fri, 13 Oct 2023 05:59:20 -0400 Subject: [PATCH] make keyboard shortcut handling consistent across the entire app --- tools/lammps-gui/chartviewer.cpp | 7 ++++--- tools/lammps-gui/imageviewer.cpp | 5 +++-- tools/lammps-gui/logwindow.cpp | 32 +++++++++++++++++++++++++++----- tools/lammps-gui/logwindow.h | 1 + tools/lammps-gui/slideshow.cpp | 7 ++++--- 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/tools/lammps-gui/chartviewer.cpp b/tools/lammps-gui/chartviewer.cpp index ab03a110b5..ee29ab9540 100644 --- a/tools/lammps-gui/chartviewer.cpp +++ b/tools/lammps-gui/chartviewer.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -61,13 +62,13 @@ ChartWindow::ChartWindow(const QString &_filename, QWidget *parent) : file->addSeparator(); stopAct = file->addAction("Stop &Run", this, &ChartWindow::stop_run); stopAct->setIcon(QIcon(":/icons/process-stop.png")); - stopAct->setShortcut(QKeySequence::fromString("Ctrl+/")); + stopAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Slash)); closeAct = file->addAction("&Close", this, &QWidget::close); closeAct->setIcon(QIcon(":/icons/window-close.png")); - closeAct->setShortcut(QKeySequence::fromString("Ctrl+W")); + closeAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W)); quitAct = file->addAction("&Quit", this, &ChartWindow::quit); quitAct->setIcon(QIcon(":/icons/application-exit.png")); - quitAct->setShortcut(QKeySequence::fromString("Ctrl+Q")); + quitAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q)); auto *layout = new QVBoxLayout; layout->addLayout(top); setLayout(layout); diff --git a/tools/lammps-gui/imageviewer.cpp b/tools/lammps-gui/imageviewer.cpp index ee81f61253..7406f1860a 100644 --- a/tools/lammps-gui/imageviewer.cpp +++ b/tools/lammps-gui/imageviewer.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -558,10 +559,10 @@ void ImageViewer::createActions() fileMenu->addSeparator(); QAction *exitAct = fileMenu->addAction("&Close", this, &QWidget::close); exitAct->setIcon(QIcon(":/icons/window-close.png")); - exitAct->setShortcut(QKeySequence::fromString("Ctrl+W")); + exitAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W)); QAction *quitAct = fileMenu->addAction("&Quit", this, &ImageViewer::quit); quitAct->setIcon(QIcon(":/icons/application-exit.png")); - quitAct->setShortcut(QKeySequence::fromString("Ctrl+Q")); + quitAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q)); } void ImageViewer::updateActions() diff --git a/tools/lammps-gui/logwindow.cpp b/tools/lammps-gui/logwindow.cpp index ab1886f1bd..374c4c89b1 100644 --- a/tools/lammps-gui/logwindow.cpp +++ b/tools/lammps-gui/logwindow.cpp @@ -35,12 +35,14 @@ LogWindow::LogWindow(const QString &_filename, QWidget *parent) : QSettings settings; resize(settings.value("logx", 500).toInt(), settings.value("logy", 320).toInt()); - auto action = new QShortcut(QKeySequence::fromString("Ctrl+S"), this); + auto action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_S), this); connect(action, &QShortcut::activated, this, &LogWindow::save_as); - action = new QShortcut(QKeySequence::fromString("Ctrl+Q"), this); + action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this); connect(action, &QShortcut::activated, this, &LogWindow::quit); - action = new QShortcut(QKeySequence(Qt::Key_Slash, Qt::CTRL), this); + action = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Slash), this); connect(action, &QShortcut::activated, this, &LogWindow::stop_run); + + installEventFilter(this); } void LogWindow::closeEvent(QCloseEvent *event) @@ -99,15 +101,35 @@ void LogWindow::contextMenuEvent(QContextMenuEvent *event) menu->addSeparator(); auto action = menu->addAction(QString("Save Log to File ...")); action->setIcon(QIcon(":/icons/document-save-as.png")); - action->setShortcut(QKeySequence::fromString("Ctrl+S")); + action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_S)); connect(action, &QAction::triggered, this, &LogWindow::save_as); action = menu->addAction("&Close Window", this, &QWidget::close); action->setIcon(QIcon(":/icons/window-close.png")); - action->setShortcut(QKeySequence::fromString("Ctrl+W")); + action->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_W)); menu->exec(event->globalPos()); delete menu; } +// event filter to handle "Ambiguous shortcut override" issues +bool LogWindow::eventFilter(QObject *watched, QEvent *event) +{ + if (event->type() == QEvent::ShortcutOverride) { + QKeyEvent *keyEvent = dynamic_cast(event); + if (!keyEvent) return QWidget::eventFilter(watched, event); + if (keyEvent->modifiers().testFlag(Qt::ControlModifier) && keyEvent->key() == '/') { + stop_run(); + event->accept(); + return true; + } + if (keyEvent->modifiers().testFlag(Qt::ControlModifier) && keyEvent->key() == 'W') { + close(); + event->accept(); + return true; + } + } + return QWidget::eventFilter(watched, event); +} + // Local Variables: // c-basic-offset: 4 // End: diff --git a/tools/lammps-gui/logwindow.h b/tools/lammps-gui/logwindow.h index 8923e35ee5..ad0691d0cc 100644 --- a/tools/lammps-gui/logwindow.h +++ b/tools/lammps-gui/logwindow.h @@ -30,6 +30,7 @@ private slots: protected: void closeEvent(QCloseEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override; + bool eventFilter(QObject *watched, QEvent *event) override; private: QString filename; diff --git a/tools/lammps-gui/slideshow.cpp b/tools/lammps-gui/slideshow.cpp index de7742f22f..92eca9a530 100644 --- a/tools/lammps-gui/slideshow.cpp +++ b/tools/lammps-gui/slideshow.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -50,11 +51,11 @@ SlideShow::SlideShow(const QString &fileName, QWidget *parent) : imageName->setAlignment(Qt::AlignCenter); imageName->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); - auto *shortcut = new QShortcut(QKeySequence::fromString("Ctrl+W"), this); + auto *shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_W), this); QObject::connect(shortcut, &QShortcut::activated, this, &QWidget::close); - shortcut = new QShortcut(QKeySequence::fromString("Ctrl+/"), this); + shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Slash), this); QObject::connect(shortcut, &QShortcut::activated, this, &SlideShow::stop_run); - shortcut = new QShortcut(QKeySequence::fromString("Ctrl+Q"), this); + shortcut = new QShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q), this); QObject::connect(shortcut, &QShortcut::activated, this, &SlideShow::quit); buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);