make keyboard shortcut handling consistent across the entire app

This commit is contained in:
Axel Kohlmeyer
2023-10-13 05:59:20 -04:00
parent 506de7015d
commit 6195be5af7
5 changed files with 39 additions and 13 deletions

View File

@ -19,6 +19,7 @@
#include <QApplication> #include <QApplication>
#include <QFileDialog> #include <QFileDialog>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QKeySequence>
#include <QLabel> #include <QLabel>
#include <QLayout> #include <QLayout>
#include <QLineSeries> #include <QLineSeries>
@ -61,13 +62,13 @@ ChartWindow::ChartWindow(const QString &_filename, QWidget *parent) :
file->addSeparator(); file->addSeparator();
stopAct = file->addAction("Stop &Run", this, &ChartWindow::stop_run); stopAct = file->addAction("Stop &Run", this, &ChartWindow::stop_run);
stopAct->setIcon(QIcon(":/icons/process-stop.png")); 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 = file->addAction("&Close", this, &QWidget::close);
closeAct->setIcon(QIcon(":/icons/window-close.png")); 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 = file->addAction("&Quit", this, &ChartWindow::quit);
quitAct->setIcon(QIcon(":/icons/application-exit.png")); 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; auto *layout = new QVBoxLayout;
layout->addLayout(top); layout->addLayout(top);
setLayout(layout); setLayout(layout);

View File

@ -23,6 +23,7 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QImage> #include <QImage>
#include <QImageReader> #include <QImageReader>
#include <QKeySequence>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QMenuBar> #include <QMenuBar>
@ -558,10 +559,10 @@ void ImageViewer::createActions()
fileMenu->addSeparator(); fileMenu->addSeparator();
QAction *exitAct = fileMenu->addAction("&Close", this, &QWidget::close); QAction *exitAct = fileMenu->addAction("&Close", this, &QWidget::close);
exitAct->setIcon(QIcon(":/icons/window-close.png")); 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); QAction *quitAct = fileMenu->addAction("&Quit", this, &ImageViewer::quit);
quitAct->setIcon(QIcon(":/icons/application-exit.png")); quitAct->setIcon(QIcon(":/icons/application-exit.png"));
quitAct->setShortcut(QKeySequence::fromString("Ctrl+Q")); quitAct->setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Q));
} }
void ImageViewer::updateActions() void ImageViewer::updateActions()

View File

@ -35,12 +35,14 @@ LogWindow::LogWindow(const QString &_filename, QWidget *parent) :
QSettings settings; QSettings settings;
resize(settings.value("logx", 500).toInt(), settings.value("logy", 320).toInt()); 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); 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); 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); connect(action, &QShortcut::activated, this, &LogWindow::stop_run);
installEventFilter(this);
} }
void LogWindow::closeEvent(QCloseEvent *event) void LogWindow::closeEvent(QCloseEvent *event)
@ -99,15 +101,35 @@ void LogWindow::contextMenuEvent(QContextMenuEvent *event)
menu->addSeparator(); menu->addSeparator();
auto action = menu->addAction(QString("Save Log to File ...")); auto action = menu->addAction(QString("Save Log to File ..."));
action->setIcon(QIcon(":/icons/document-save-as.png")); 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); connect(action, &QAction::triggered, this, &LogWindow::save_as);
action = menu->addAction("&Close Window", this, &QWidget::close); action = menu->addAction("&Close Window", this, &QWidget::close);
action->setIcon(QIcon(":/icons/window-close.png")); 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()); menu->exec(event->globalPos());
delete menu; 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<QKeyEvent *>(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: // Local Variables:
// c-basic-offset: 4 // c-basic-offset: 4
// End: // End:

View File

@ -30,6 +30,7 @@ private slots:
protected: protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
void contextMenuEvent(QContextMenuEvent *event) override; void contextMenuEvent(QContextMenuEvent *event) override;
bool eventFilter(QObject *watched, QEvent *event) override;
private: private:
QString filename; QString filename;

View File

@ -25,6 +25,7 @@
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QImage> #include <QImage>
#include <QImageReader> #include <QImageReader>
#include <QKeySequence>
#include <QLabel> #include <QLabel>
#include <QPalette> #include <QPalette>
#include <QProcess> #include <QProcess>
@ -50,11 +51,11 @@ SlideShow::SlideShow(const QString &fileName, QWidget *parent) :
imageName->setAlignment(Qt::AlignCenter); imageName->setAlignment(Qt::AlignCenter);
imageName->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 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); 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); 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); QObject::connect(shortcut, &QShortcut::activated, this, &SlideShow::quit);
buttonBox = new QDialogButtonBox(QDialogButtonBox::Close); buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);