feat: check conflict shortcuts before saving
This commit is contained in:
parent
d0bdc71cf5
commit
8cce48e6b0
|
@ -133,23 +133,31 @@ void Settings::applyUserShortcuts(QWidget *widget)
|
||||||
bool Settings::setShortcutsForAction(QWidget *widget, const QString &objectName,
|
bool Settings::setShortcutsForAction(QWidget *widget, const QString &objectName,
|
||||||
QList<QKeySequence> shortcuts, bool writeConfig)
|
QList<QKeySequence> shortcuts, bool writeConfig)
|
||||||
{
|
{
|
||||||
bool result = false;
|
QAction * targetAction = nullptr;
|
||||||
for (QAction * action : widget->actions()) {
|
for (QAction * action : widget->actions()) {
|
||||||
if (action->objectName() == objectName) {
|
if (action->objectName() == objectName) {
|
||||||
action->setShortcuts(shortcuts);
|
targetAction = action;
|
||||||
result = true;
|
} else {
|
||||||
break;
|
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->beginGroup("shortcuts");
|
||||||
m_qsettings->setValue(objectName, QVariant::fromValue(shortcuts));
|
m_qsettings->setValue(objectName, QVariant::fromValue(shortcuts));
|
||||||
m_qsettings->endGroup();
|
m_qsettings->endGroup();
|
||||||
m_qsettings->sync();
|
m_qsettings->sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN)
|
#if defined(FLAG_PORTABLE_MODE_SUPPORT) && defined(Q_OS_WIN)
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include <QScrollArea>
|
#include <QScrollArea>
|
||||||
#include <QSplitter>
|
#include <QSplitter>
|
||||||
#include <QStringListModel>
|
#include <QStringListModel>
|
||||||
|
#include <QMessageBox>
|
||||||
|
|
||||||
SettingsDialog::SettingsDialog(QWidget *parent)
|
SettingsDialog::SettingsDialog(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
|
@ -60,9 +61,14 @@ SettingsDialog::SettingsDialog(QWidget *parent)
|
||||||
shortcutEditorSplitter->setSizes({shortcutEditorSplitter->height(), 1});
|
shortcutEditorSplitter->setSizes({shortcutEditorSplitter->height(), 1});
|
||||||
oldEditor->deleteLater();
|
oldEditor->deleteLater();
|
||||||
});
|
});
|
||||||
connect(shortcutEdit, &ShortcutEdit::shortcutsChanged, this, [=](){
|
connect(shortcutEdit, &ShortcutEdit::applyShortcutsRequested, this, [=](QList<QKeySequence> newShortcuts){
|
||||||
Settings::instance()->setShortcutsForAction(parent, shortcutEdit->objectName().mid(9),
|
bool succ = Settings::instance()->setShortcutsForAction(parent, shortcutEdit->objectName().mid(9),
|
||||||
shortcutEdit->shortcuts());
|
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());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ ShortcutEditor::ShortcutEditor(ShortcutEdit * shortcutEdit, QWidget * parent)
|
||||||
reloadShortcuts();
|
reloadShortcuts();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
connect(shortcutEdit, &ShortcutEdit::shortcutsChanged, this, &ShortcutEditor::reloadShortcuts);
|
||||||
|
|
||||||
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
|
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
|
||||||
|
|
||||||
|
@ -81,12 +82,11 @@ void ShortcutEditor::applyShortcuts()
|
||||||
{
|
{
|
||||||
QList<QKeySequence> shortcuts;
|
QList<QKeySequence> shortcuts;
|
||||||
for (const QKeySequenceEdit * keyseqEdit : m_keySequenceEdits) {
|
for (const QKeySequenceEdit * keyseqEdit : m_keySequenceEdits) {
|
||||||
if (!keyseqEdit->keySequence().isEmpty()) {
|
if (!keyseqEdit->keySequence().isEmpty() && !shortcuts.contains(keyseqEdit->keySequence())) {
|
||||||
shortcuts.append(keyseqEdit->keySequence());
|
shortcuts.append(keyseqEdit->keySequence());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_shortcutEdit->setShortcuts(shortcuts);
|
emit m_shortcutEdit->applyShortcutsRequested(shortcuts);
|
||||||
reloadShortcuts();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
|
|
|
@ -46,6 +46,7 @@ public:
|
||||||
signals:
|
signals:
|
||||||
void shortcutsChanged();
|
void shortcutsChanged();
|
||||||
void editButtonClicked();
|
void editButtonClicked();
|
||||||
|
void applyShortcutsRequested(QList<QKeySequence> newShortcuts);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QList<QKeySequence> m_shortcuts;
|
QList<QKeySequence> m_shortcuts;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user