Warn when assigning a keyboard shortcut that is already assigned to something else. Fixes #517.

This commit is contained in:
Leonardo 2020-12-10 14:58:22 -08:00
parent 14f891bd08
commit caed332b3f
2 changed files with 58 additions and 9 deletions

View file

@ -308,7 +308,7 @@ void lcPreviewWidget::OnButtonDown(lcTrackButton TrackButton)
StartTracking(TrackButton); StartTracking(TrackButton);
break; break;
case lcTrackTool::Count: default:
break; break;
} }
} }
@ -520,7 +520,7 @@ void lcPreviewWidget::OnMouseMove()
Redraw(); Redraw();
break; break;
case lcTrackTool::Count: default:
break; break;
} }
} }

View file

@ -863,7 +863,7 @@ void lcQPreferencesDialog::updateCommandList()
ui->commandList->clear(); ui->commandList->clear();
QMap<QString, QTreeWidgetItem*> sections; QMap<QString, QTreeWidgetItem*> sections;
for (int actionIdx = 0; actionIdx < LC_NUM_COMMANDS; actionIdx++) for (unsigned int actionIdx = 0; actionIdx < LC_NUM_COMMANDS; actionIdx++)
{ {
if (!gCommands[actionIdx].ID[0]) if (!gCommands[actionIdx].ID[0])
continue; continue;
@ -989,17 +989,66 @@ void lcQPreferencesDialog::on_KeyboardFilterEdit_textEdited(const QString& Text)
void lcQPreferencesDialog::on_shortcutAssign_clicked() void lcQPreferencesDialog::on_shortcutAssign_clicked()
{ {
QTreeWidgetItem *current = ui->commandList->currentItem(); QTreeWidgetItem* CurrentItem = ui->commandList->currentItem();
if (!current || !current->data(0, Qt::UserRole).isValid()) if (!CurrentItem || !CurrentItem->data(0, Qt::UserRole).isValid())
return; return;
int shortcutIndex = qvariant_cast<int>(current->data(0, Qt::UserRole)); uint ShortcutIndex = CurrentItem->data(0, Qt::UserRole).toUInt();
mOptions->KeyboardShortcuts.mShortcuts[shortcutIndex] = ui->shortcutEdit->text(); QString (&Shortcuts)[LC_NUM_COMMANDS] = mOptions->KeyboardShortcuts.mShortcuts;
current->setText(1, ui->shortcutEdit->text()); if (ShortcutIndex >= LC_ARRAY_COUNT(Shortcuts))
return;
setShortcutModified(current, mOptions->KeyboardShortcuts.mShortcuts[shortcutIndex] != gCommands[shortcutIndex].DefaultShortcut); QString NewShortcut = ui->shortcutEdit->text();
if (!NewShortcut.isEmpty())
{
for (uint ExistingIndex = 0; ExistingIndex < LC_ARRAY_COUNT(Shortcuts); ExistingIndex++)
{
if (NewShortcut == Shortcuts[ExistingIndex])
{
QString QuestionText = tr("The shortcut '%1' is already assigned to '%2'. Do you want to replace it?").arg(NewShortcut, gCommands[ExistingIndex].ID);
if (QMessageBox::question(this, tr("Duplicate Shortcut"), QuestionText, QMessageBox::Yes | QMessageBox::No) != QMessageBox::Yes)
return;
mOptions->KeyboardShortcuts.mShortcuts[ExistingIndex].clear();
std::function<QTreeWidgetItem* (QTreeWidgetItem*)> FindItem = [&FindItem, ExistingIndex](QTreeWidgetItem* ParentItem) -> QTreeWidgetItem*
{
for (int ChildIdx = 0; ChildIdx < ParentItem->childCount(); ChildIdx++)
{
QTreeWidgetItem* ChildItem = ParentItem->child(ChildIdx);
uint ChildIndex = ChildItem->data(0, Qt::UserRole).toUInt();
if (ChildIndex == ExistingIndex)
return ChildItem;
QTreeWidgetItem* ExistingItem = FindItem(ChildItem);
if (ExistingItem)
return ExistingItem;
}
return nullptr;
};
QTreeWidgetItem* ExistingItem = FindItem(ui->commandList->invisibleRootItem());
if (ExistingItem)
{
ExistingItem->setText(1, QString());
setShortcutModified(ExistingItem, gCommands[ShortcutIndex].DefaultShortcut[0] != 0);
}
}
}
}
mOptions->KeyboardShortcuts.mShortcuts[ShortcutIndex] = NewShortcut;
CurrentItem->setText(1, NewShortcut);
setShortcutModified(CurrentItem, mOptions->KeyboardShortcuts.mShortcuts[ShortcutIndex] != gCommands[ShortcutIndex].DefaultShortcut);
mOptions->KeyboardShortcutsModified = true; mOptions->KeyboardShortcutsModified = true;
mOptions->KeyboardShortcutsDefault = false; mOptions->KeyboardShortcutsDefault = false;