From 8cce48e6b06a971c5bb3a9a74857a1012d53608e Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 3 Nov 2024 00:46:21 +0800 Subject: [PATCH] feat: check conflict shortcuts before saving --- app/settings.cpp | 20 ++++++++++++++------ app/settingsdialog.cpp | 12 +++++++++--- app/shortcutedit.cpp | 6 +++--- app/shortcutedit.h | 1 + 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/app/settings.cpp b/app/settings.cpp index e44cdd2..7c70244 100644 --- a/app/settings.cpp +++ b/app/settings.cpp @@ -133,23 +133,31 @@ void Settings::applyUserShortcuts(QWidget *widget) bool Settings::setShortcutsForAction(QWidget *widget, const QString &objectName, QList shortcuts, bool writeConfig) { - bool result = false; + QAction * targetAction = nullptr; for (QAction * action : widget->actions()) { if (action->objectName() == objectName) { - action->setShortcuts(shortcuts); - result = true; - break; + targetAction = action; + } else { + for (const QKeySequence & shortcut : std::as_const(shortcuts)) { + if (action->shortcuts().contains(shortcut)) { + return false; + } + } } } - if (result && writeConfig) { + if (targetAction) { + targetAction->setShortcuts(shortcuts); + } + + if (targetAction && writeConfig) { m_qsettings->beginGroup("shortcuts"); m_qsettings->setValue(objectName, QVariant::fromValue(shortcuts)); m_qsettings->endGroup(); m_qsettings->sync(); } - return result; + return true; } #if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN) diff --git a/app/settingsdialog.cpp b/app/settingsdialog.cpp index 9360e55..f5fc716 100644 --- a/app/settingsdialog.cpp +++ b/app/settingsdialog.cpp @@ -15,6 +15,7 @@ #include #include #include +#include SettingsDialog::SettingsDialog(QWidget *parent) : QDialog(parent) @@ -60,9 +61,14 @@ SettingsDialog::SettingsDialog(QWidget *parent) shortcutEditorSplitter->setSizes({shortcutEditorSplitter->height(), 1}); oldEditor->deleteLater(); }); - connect(shortcutEdit, &ShortcutEdit::shortcutsChanged, this, [=](){ - Settings::instance()->setShortcutsForAction(parent, shortcutEdit->objectName().mid(9), - shortcutEdit->shortcuts()); + connect(shortcutEdit, &ShortcutEdit::applyShortcutsRequested, this, [=](QList newShortcuts){ + bool succ = Settings::instance()->setShortcutsForAction(parent, shortcutEdit->objectName().mid(9), + newShortcuts); + if (!succ) { + QMessageBox::warning(this, tr("Failed to set shortcuts"), + tr("Please check if shortcuts are duplicated with existing shortcuts.")); + } + shortcutEdit->setShortcuts(action->shortcuts()); }); } diff --git a/app/shortcutedit.cpp b/app/shortcutedit.cpp index 3f4f82e..14b8f5a 100644 --- a/app/shortcutedit.cpp +++ b/app/shortcutedit.cpp @@ -33,6 +33,7 @@ ShortcutEditor::ShortcutEditor(ShortcutEdit * shortcutEdit, QWidget * parent) reloadShortcuts(); } }); + connect(shortcutEdit, &ShortcutEdit::shortcutsChanged, this, &ShortcutEditor::reloadShortcuts); setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum); @@ -81,12 +82,11 @@ void ShortcutEditor::applyShortcuts() { QList shortcuts; for (const QKeySequenceEdit * keyseqEdit : m_keySequenceEdits) { - if (!keyseqEdit->keySequence().isEmpty()) { + if (!keyseqEdit->keySequence().isEmpty() && !shortcuts.contains(keyseqEdit->keySequence())) { shortcuts.append(keyseqEdit->keySequence()); } } - m_shortcutEdit->setShortcuts(shortcuts); - reloadShortcuts(); + emit m_shortcutEdit->applyShortcutsRequested(shortcuts); } // ---------------------------------------- diff --git a/app/shortcutedit.h b/app/shortcutedit.h index ef8da03..1b70cf4 100644 --- a/app/shortcutedit.h +++ b/app/shortcutedit.h @@ -46,6 +46,7 @@ public: signals: void shortcutsChanged(); void editButtonClicked(); + void applyShortcutsRequested(QList newShortcuts); private: QList m_shortcuts;