I feel this should be possible more concisely, less code repetition, with a loop and a two-dimensional array or something. But not sure how exactly. Any best practice for review?
// Activate shortcuts for editor menu items like Ctrl+S for "Save Item" etc.
// Deactivate instead with optional "false" - to allow these for keybindings
void dlgTriggerEditor::setShortcuts(const bool active)
{
QList<QAction*> actionList = toolBar->actions();
QString actionText;
// 3/3 save button texts need to be kept in sync
const QStringList saveButtonNames = {qsl("Save Item"), tr("Save Trigger"), tr("Save Timer"), tr("Save Alias"), tr("Save Script"), tr("Save Button"), tr("Save Key"), tr("Save Variable")};
for (auto& action : actionList) {
actionText = action->text();
if (saveButtonNames.contains(actionText)) {
action->setShortcut((active) ? tr("Ctrl+S") : QString());
} else if (actionText == tr("Save Profile")) {
action->setShortcut((active) ? tr("Ctrl+Shift+S") : QString());
}
}
actionList = toolBar2->actions();
QString actionText;
for (auto& action : actionList) {
actionText = action->text();
if (actionText == tr("Triggers")) {
action->setShortcut((active) ? tr("Ctrl+1") : QString());
} else if (actionText == tr("Aliases")) {
action->setShortcut((active) ? tr("Ctrl+2") : QString());
} else if (actionText == tr("Scripts")) {
action->setShortcut((active) ? tr("Ctrl+3") : QString());
} else if (actionText == tr("Timers")) {
action->setShortcut((active) ? tr("Ctrl+4") : QString());
} else if (actionText == tr("Keys")) {
action->setShortcut((active) ? tr("Ctrl+5") : QString());
} else if (actionText == tr("Variables")) {
action->setShortcut((active) ? tr("Ctrl+6") : QString());
} else if (actionText == tr("Buttons")) {
action->setShortcut((active) ? tr("Ctrl+7") : QString());
} else if (actionText == tr("Errors")) {
action->setShortcut((active) ? tr("Ctrl+8") : QString());
} else if (actionText == tr("Statistics")) {
action->setShortcut((active) ? tr("Ctrl+9") : QString());
} else if (actionText == tr("Debug")) {
action->setShortcut((active) ? tr("Ctrl+0") : QString());
}
}
}
Here is a python mock-up of the bottom (not yet a) loop for comparison:
texts_and_actions = (
("Statistics", "Ctrl+9"),
("Debug", "Ctrl+0")
)
for text, action in texts_and_actions:
if actionText == text:
setShortcut(action)