From d77a715807045184f0ac9f5aebf01b31bb2fedba Mon Sep 17 00:00:00 2001 From: Carlo Teubner Date: Fri, 14 Jun 2024 22:35:30 +0100 Subject: [PATCH] MainWindow: move shortcuts from .cpp to .ui file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only shortcuts defined in the .cpp file are ones that can't be defined in the .ui file, because they are in some way conditional. This also reduces the number of compiler warnings of the kind warning: arithmetic between different enumeration types ‘Qt::Modifier’ and ‘Qt::Key’ is deprecated [-Wdeprecated-enum-enum-conversion] with recent GCC versions. --- src/gui/ActionCollection.cpp | 6 +-- src/gui/ActionCollection.h | 2 +- src/gui/MainWindow.cpp | 51 ++++++--------------- src/gui/MainWindow.ui | 87 ++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 44 deletions(-) diff --git a/src/gui/ActionCollection.cpp b/src/gui/ActionCollection.cpp index d18fb4ce3..3db86a899 100644 --- a/src/gui/ActionCollection.cpp +++ b/src/gui/ActionCollection.cpp @@ -61,14 +61,10 @@ void ActionCollection::setDefaultShortcut(QAction* action, const QKeySequence& s setDefaultShortcuts(action, {shortcut}); } -void ActionCollection::setDefaultShortcut(QAction* action, - QKeySequence::StandardKey standard, - const QKeySequence& fallback) +void ActionCollection::setDefaultShortcut(QAction* action, QKeySequence::StandardKey standard) { if (!QKeySequence::keyBindings(standard).isEmpty()) { setDefaultShortcuts(action, QKeySequence::keyBindings(standard)); - } else if (fallback != 0) { - setDefaultShortcut(action, QKeySequence(fallback)); } } diff --git a/src/gui/ActionCollection.h b/src/gui/ActionCollection.h index ab0d40029..c9045b552 100644 --- a/src/gui/ActionCollection.h +++ b/src/gui/ActionCollection.h @@ -44,7 +44,7 @@ public: QList defaultShortcuts(const QAction* a) const; void setDefaultShortcut(QAction* a, const QKeySequence& shortcut); - void setDefaultShortcut(QAction* a, QKeySequence::StandardKey standard, const QKeySequence& fallback); + void setDefaultShortcut(QAction* a, QKeySequence::StandardKey standard); void setDefaultShortcuts(QAction* a, const QList& shortcut); // Check if any action conflicts with @p seq and return the conflicting action diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 7229ef92b..5f605a587 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -2190,55 +2190,30 @@ void MainWindow::initActionCollection() m_ui->actionBugReport, m_ui->actionAbout}); - // Add actions whose shortcuts were set in the .ui file + // Register as default any shortcuts that were set in the .ui file for (const auto action : ac->actions()) { if (!action->shortcut().isEmpty()) { ac->setDefaultShortcut(action, action->shortcut()); } } - // Actions with standard shortcuts - ac->setDefaultShortcut(m_ui->actionDatabaseOpen, QKeySequence::Open, Qt::CTRL + Qt::Key_O); - ac->setDefaultShortcut(m_ui->actionDatabaseSave, QKeySequence::Save, Qt::CTRL + Qt::Key_S); - ac->setDefaultShortcut(m_ui->actionDatabaseSaveAs, QKeySequence::SaveAs, Qt::CTRL + Qt::SHIFT + Qt::Key_S); - ac->setDefaultShortcut(m_ui->actionDatabaseClose, QKeySequence::Close, Qt::CTRL + Qt::Key_W); - ac->setDefaultShortcut(m_ui->actionSettings, QKeySequence::Preferences, Qt::CTRL + Qt::Key_Comma); - ac->setDefaultShortcut(m_ui->actionQuit, QKeySequence::Quit, Qt::CTRL + Qt::Key_Q); - ac->setDefaultShortcut(m_ui->actionEntryNew, QKeySequence::New, Qt::CTRL + Qt::Key_N); + // Actions with standard shortcuts (if no standard shortcut exists, leave the existing + // shortcuts from the .ui file in place) + ac->setDefaultShortcut(m_ui->actionDatabaseOpen, QKeySequence::Open); + ac->setDefaultShortcut(m_ui->actionDatabaseSave, QKeySequence::Save); + ac->setDefaultShortcut(m_ui->actionDatabaseSaveAs, QKeySequence::SaveAs); + ac->setDefaultShortcut(m_ui->actionDatabaseClose, QKeySequence::Close); + ac->setDefaultShortcut(m_ui->actionSettings, QKeySequence::Preferences); + ac->setDefaultShortcut(m_ui->actionQuit, QKeySequence::Quit); + ac->setDefaultShortcut(m_ui->actionEntryNew, QKeySequence::New); // Prevent conflicts with global Mac shortcuts (force Control on all platforms) + // Note: Qt::META means Ctrl on Mac. #ifdef Q_OS_MAC - auto modifier = Qt::META; -#else - auto modifier = Qt::CTRL; + ac->setDefaultShortcut(m_ui->actionEntryAddToAgent, Qt::META + Qt::Key_H); + ac->setDefaultShortcut(m_ui->actionEntryRemoveFromAgent, Qt::META + Qt::SHIFT + Qt::Key_H); #endif - // All other actions with default shortcuts - ac->setDefaultShortcut(m_ui->actionDatabaseNew, Qt::CTRL + Qt::SHIFT + Qt::Key_N); - ac->setDefaultShortcut(m_ui->actionDatabaseSettings, Qt::CTRL + Qt::SHIFT + Qt::Key_Comma); - ac->setDefaultShortcut(m_ui->actionReports, Qt::CTRL + Qt::SHIFT + Qt::Key_R); - ac->setDefaultShortcut(m_ui->actionLockDatabase, Qt::CTRL + Qt::Key_L); - ac->setDefaultShortcut(m_ui->actionLockAllDatabases, Qt::CTRL + Qt::SHIFT + Qt::Key_L); - ac->setDefaultShortcut(m_ui->actionEntryEdit, Qt::CTRL + Qt::Key_E); - ac->setDefaultShortcut(m_ui->actionEntryDelete, Qt::CTRL + Qt::Key_D); - ac->setDefaultShortcut(m_ui->actionEntryDelete, Qt::Key_Delete); - ac->setDefaultShortcut(m_ui->actionEntryClone, Qt::CTRL + Qt::Key_K); - ac->setDefaultShortcut(m_ui->actionEntryTotp, Qt::CTRL + Qt::SHIFT + Qt::Key_T); - ac->setDefaultShortcut(m_ui->actionEntryDownloadIcon, Qt::CTRL + Qt::SHIFT + Qt::Key_D); - ac->setDefaultShortcut(m_ui->actionEntryCopyTotp, Qt::CTRL + Qt::Key_T); - ac->setDefaultShortcut(m_ui->actionEntryCopyPasswordTotp, Qt::CTRL + Qt::Key_Y); - ac->setDefaultShortcut(m_ui->actionEntryMoveUp, Qt::CTRL + Qt::ALT + Qt::Key_Up); - ac->setDefaultShortcut(m_ui->actionEntryMoveDown, Qt::CTRL + Qt::ALT + Qt::Key_Down); - ac->setDefaultShortcut(m_ui->actionEntryCopyUsername, Qt::CTRL + Qt::Key_B); - ac->setDefaultShortcut(m_ui->actionEntryCopyPassword, Qt::CTRL + Qt::Key_C); - ac->setDefaultShortcut(m_ui->actionEntryCopyTitle, Qt::CTRL + Qt::Key_I); - ac->setDefaultShortcut(m_ui->actionEntryAutoTypeSequence, Qt::CTRL + Qt::SHIFT + Qt::Key_V); - ac->setDefaultShortcut(m_ui->actionEntryOpenUrl, Qt::CTRL + Qt::SHIFT + Qt::Key_U); - ac->setDefaultShortcut(m_ui->actionEntryCopyURL, Qt::CTRL + Qt::Key_U); - ac->setDefaultShortcut(m_ui->actionEntryRestore, Qt::CTRL + Qt::Key_R); - ac->setDefaultShortcut(m_ui->actionEntryAddToAgent, modifier + Qt::Key_H); - ac->setDefaultShortcut(m_ui->actionEntryRemoveFromAgent, modifier + Qt::SHIFT + Qt::Key_H); - QTimer::singleShot(1, ac, &ActionCollection::restoreShortcuts); } diff --git a/src/gui/MainWindow.ui b/src/gui/MainWindow.ui index c1a0133a5..3c2ee1650 100644 --- a/src/gui/MainWindow.ui +++ b/src/gui/MainWindow.ui @@ -447,6 +447,9 @@ Quit Application + + Ctrl+Q + QAction::QuitRole @@ -477,6 +480,9 @@ Open Database + + Ctrl+O + @@ -485,6 +491,9 @@ &Save Database + + Ctrl+S + @@ -493,6 +502,9 @@ &Close Database + + Ctrl+W + @@ -501,6 +513,9 @@ Create Database + + Ctrl+Shift+N + @@ -520,6 +535,9 @@ Create Entry + + Ctrl+N + @@ -531,6 +549,9 @@ Edit Entry + + Ctrl+E + @@ -542,6 +563,9 @@ Delete Entry + + Del + @@ -619,6 +643,9 @@ Save Database As + + Ctrl+Shift+S + @@ -641,6 +668,9 @@ Show Database Reports + + Ctrl+Shift+R + QAction::NoRole @@ -655,6 +685,9 @@ Show Database Settings + + Ctrl+Shift+, + QAction::NoRole @@ -697,6 +730,9 @@ Clone Entry + + Ctrl+K + @@ -708,6 +744,9 @@ Move Entry Up + + Alt+Up + @@ -719,6 +758,9 @@ Move Entry Down + + Alt+Down + @@ -730,6 +772,9 @@ Copy Username + + Ctrl+B + @@ -741,6 +786,9 @@ Copy Password + + Ctrl+C + @@ -752,6 +800,9 @@ Show Application Settings + + Ctrl+, + QAction::PreferencesRole @@ -865,6 +916,9 @@ Download &Favicon + + Ctrl+Shift+D + @@ -873,6 +927,9 @@ Open &URL + + Ctrl+Shift+U + @@ -881,6 +938,9 @@ &Lock Database + + Ctrl+L + @@ -889,6 +949,9 @@ Lock &All Databases + + Ctrl+Shift+L + @@ -900,6 +963,9 @@ Copy Title + + Ctrl+I + @@ -911,6 +977,9 @@ Copy URL + + Ctrl+U + @@ -973,6 +1042,9 @@ Show TOTP + + Ctrl+Shift+T + @@ -994,11 +1066,17 @@ Copy &TOTP + + Ctrl+T + Copy Password and TOTP + + Ctrl+Y + @@ -1080,6 +1158,9 @@ SSH Agent: Add Key + + Ctrl+H + @@ -1088,6 +1169,9 @@ SSH Agent: Remove Key + + Ctrl+Shift+H + @@ -1244,6 +1328,9 @@ Perform Auto-Type: Entry Default + + Ctrl+Shift+V +