diff --git a/include/ui/shortcutseditor.h b/include/ui/shortcutseditor.h index e44c8896..0654c2a2 100644 --- a/include/ui/shortcutseditor.h +++ b/include/ui/shortcutseditor.h @@ -40,6 +40,7 @@ private: QHash> multiKeyEdits_objects; void parseObjectList(const QObjectList &objectList); + void parseObject(const QObject *object, QMap *objects_labels, QMap *objects_prefixes); QString getLabel(const QObject *object) const; bool stringPropertyIsNotEmpty(const QObject *object, const char *name) const; void populateMainContainer(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index ac7c2385..8ba318b9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2271,7 +2271,12 @@ void MainWindow::initShortcutsEditor() { connectSubEditorsToShortcutsEditor(); - shortcutsEditor->setShortcutableObjects(shortcutableObjects()); + auto objectList = shortcutableObjects(); + for (auto *menu : findChildren()) { + if (!menu->objectName().isEmpty()) + objectList.append(qobject_cast(menu)); + } + shortcutsEditor->setShortcutableObjects(objectList); } void MainWindow::connectSubEditorsToShortcutsEditor() { diff --git a/src/ui/shortcutseditor.cpp b/src/ui/shortcutseditor.cpp index d0ef943d..8ae1f67d 100644 --- a/src/ui/shortcutseditor.cpp +++ b/src/ui/shortcutseditor.cpp @@ -12,6 +12,7 @@ #include #include #include +#include ShortcutsEditor::ShortcutsEditor(QWidget *parent) : @@ -78,11 +79,43 @@ void ShortcutsEditor::resetShortcuts() { } } +void ShortcutsEditor::parseObject(const QObject *object, QMap *objects_labels, QMap *objects_prefixes) { + auto menu = dynamic_cast(object); + if (menu) { + // If a menu is provided we'll use it to create prefixes for any of the menu's actions, + // and automatically insert its actions in the shortcut list (if they weren't present already). + // The prefixing assumes the provided object list is in inheritance order. + // These prefixes are important for differentiating actions that may have the same display text + // but appear in different menus. + for (const auto &action : menu->actions()) { + if (!menu->title().isEmpty()) { + auto prefix = QString("%1%2 > ") + .arg(objects_prefixes->value(menu->menuAction())) // If this is a sub-menu, it may itself have a prefix. + .arg(menu->title()); + objects_prefixes->insert(action, prefix); + } + parseObject(action, objects_labels, objects_prefixes); + } + } else if (object && !object->objectName().isEmpty() && !object->objectName().startsWith("_q_")) { + QString label = getLabel(object); + if (!label.isEmpty()) { + objects_labels->insert(object, label); + } + } +} + void ShortcutsEditor::parseObjectList(const QObjectList &objectList) { - for (auto *object : objectList) { - const auto label = getLabel(object); - if (!label.isEmpty() && !object->objectName().isEmpty() && !object->objectName().startsWith("_q_")) - labels_objects.insert(label, object); + QMap objects_labels; + QMap objects_prefixes; + for (const auto &object : objectList) { + parseObject(object, &objects_labels, &objects_prefixes); + } + + // Sort alphabetically by label + this->labels_objects.clear(); + for (auto it = objects_labels.constBegin(); it != objects_labels.constEnd(); it++) { + QString fullLabel = objects_prefixes.value(it.key()) + it.value(); + this->labels_objects.insert(fullLabel, it.key()); } } @@ -164,7 +197,9 @@ QList ShortcutsEditor::siblings(MultiKeyEdit *multiKeyEdit) cons void ShortcutsEditor::promptUserOnDuplicateFound(MultiKeyEdit *sender, MultiKeyEdit *sibling) { const auto duplicateKeySequence = sender->keySequences().last(); - const auto siblingLabel = getLabel(multiKeyEdits_objects.value(sibling)); + const auto siblingLabel = this->labels_objects.key(multiKeyEdits_objects.value(sibling)); + if (siblingLabel.isEmpty()) + return; const auto message = QString( "Shortcut '%1' is already used by '%2', would you like to replace it?") .arg(duplicateKeySequence.toString()).arg(siblingLabel);