From eabd2d66185f5c16d0bc30537ca1f33068907a08 Mon Sep 17 00:00:00 2001 From: BigBahss Date: Tue, 3 Nov 2020 05:58:10 -0500 Subject: [PATCH] Convert QShortcut's to Shortcut's and add them to config --- include/config.h | 26 +++++---- src/config.cpp | 114 +++++++++++++++++++++++++++------------ src/mainwindow.cpp | 26 ++++++--- src/ui/tileseteditor.cpp | 2 +- 4 files changed, 116 insertions(+), 52 deletions(-) diff --git a/include/config.h b/include/config.h index 4c4543d4..9c802a39 100644 --- a/include/config.h +++ b/include/config.h @@ -195,21 +195,29 @@ private: extern ProjectConfig projectConfig; class QAction; +class Shortcut; class ShortcutsConfig : public KeyValueConfigBase { public: ShortcutsConfig() : - shortcuts(QMultiMap()), + userShortcuts(QMultiMap()), defaultShortcuts(QMultiMap()) - { - reset(); - } - virtual void reset() override { shortcuts.clear(); } - void setDefaultShortcuts(const QList &actions); + { } + + virtual void reset() override { userShortcuts.clear(); } + + void setDefaultShortcuts( + const QList &actions = QList(), + const QList &shortcuts = QList()); QList getDefaultShortcuts(QAction *action) const; - void setUserShortcuts(const QList &actions); + QList getDefaultShortcuts(Shortcut *shortcut) const; + + void setUserShortcuts( + const QList &actions = QList(), + const QList &shortcuts = QList()); QList getUserShortcuts(QAction *action) const; + QList getUserShortcuts(Shortcut *shortcut) const; protected: virtual QString getConfigFilepath() override; @@ -219,10 +227,10 @@ protected: virtual void setUnreadKeys() override {}; private: - QMultiMap shortcuts; + QMultiMap userShortcuts; QMultiMap defaultShortcuts; - QString getKey(QObject *object) const; + QString cfgKey(QObject *object) const; }; extern ShortcutsConfig shortcutsConfig; diff --git a/src/config.cpp b/src/config.cpp index 2c009b36..0c9cbee9 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -1,5 +1,6 @@ #include "config.h" #include "log.h" +#include "shortcut.h" #include #include #include @@ -719,69 +720,114 @@ QString ShortcutsConfig::getConfigFilepath() { } void ShortcutsConfig::parseConfigKeyValue(QString key, QString value) { - QStringList keySeqs = value.split(' '); - if (keySeqs.isEmpty()) - shortcuts.insert(key, value); - else for (auto keySeq : keySeqs) - shortcuts.insert(key, keySeq); + QStringList keySequences = value.split(' '); + for (auto keySequence : keySequences) + userShortcuts.insert(key, keySequence); } QMap ShortcutsConfig::getKeyValueMap() { QMap map; - for (auto key : shortcuts.uniqueKeys()) { - auto keySeqs = shortcuts.values(key); + for (auto cfg_key : userShortcuts.uniqueKeys()) { + auto keySequences = userShortcuts.values(cfg_key); QStringList values; - for (auto keySeq : keySeqs) - values.append(keySeq.toString()); + for (auto keySequence : keySequences) + values.append(keySequence.toString()); QString value = values.join(' '); - map.insert(key, value); + map.insert(cfg_key, value); } return map; } // Call this before applying user shortcuts to be able to restore default shortcuts. -void ShortcutsConfig::setDefaultShortcuts(const QList &actions) { +void ShortcutsConfig::setDefaultShortcuts(const QList &actions, const QList &shortcuts) { defaultShortcuts.clear(); + for (auto *action : actions) { - const QString key = getKey(action); - bool addToUserShortcuts = !shortcuts.contains(key); - for (auto shortcut : action->shortcuts()) { - defaultShortcuts.insert(key, shortcut); + if (action->text().isEmpty() || action->objectName().isEmpty()) + continue; + const QString cfg_key = cfgKey(action); + bool addToUserShortcuts = !userShortcuts.contains(cfg_key); + if (action->shortcuts().isEmpty()) { + defaultShortcuts.insert(cfg_key, QKeySequence()); if (addToUserShortcuts) - shortcuts.insert(key, shortcut); + userShortcuts.insert(cfg_key, QKeySequence()); + } else { + for (auto keySequence : action->shortcuts()) { + defaultShortcuts.insert(cfg_key, keySequence); + if (addToUserShortcuts) + userShortcuts.insert(cfg_key, keySequence); + } } } + + for (auto *shortcut : shortcuts) { + if (shortcut->objectName().isEmpty()) + continue; + const QString cfg_key = cfgKey(shortcut); + bool addToUserShortcuts = !userShortcuts.contains(cfg_key); + for (auto keySequence : shortcut->keys()) { + defaultShortcuts.insert(cfg_key, keySequence); + if (addToUserShortcuts) + userShortcuts.insert(cfg_key, keySequence); + } + } + save(); } QList ShortcutsConfig::getDefaultShortcuts(QAction *action) const { - return defaultShortcuts.values(getKey(action)); + return defaultShortcuts.values(cfgKey(action)); } -void ShortcutsConfig::setUserShortcuts(const QList &actions) { - shortcuts.clear(); +QList ShortcutsConfig::getDefaultShortcuts(Shortcut *shortcut) const { + return defaultShortcuts.values(cfgKey(shortcut)); +} + +void ShortcutsConfig::setUserShortcuts(const QList &actions, const QList &shortcuts) { + userShortcuts.clear(); + for (auto *action : actions) { - const QString key = getKey(action); + if (action->text().isEmpty() || action->objectName().isEmpty()) + continue; + const QString cfg_key = cfgKey(action); if (action->shortcuts().isEmpty()) - shortcuts.insert(key, QKeySequence()); - else for (auto shortcut : action->shortcuts()) - shortcuts.insert(key, shortcut); + userShortcuts.insert(cfg_key, QKeySequence()); + else + for (auto keySequence : action->shortcuts()) + userShortcuts.insert(cfg_key, keySequence); } + + for (auto *shortcut : shortcuts) { + if (shortcut->objectName().isEmpty()) + continue; + const QString cfg_key = cfgKey(shortcut); + if (shortcut->keys().isEmpty()) + userShortcuts.insert(cfg_key, QKeySequence()); + else + for (auto keySequence : shortcut->keys()) + userShortcuts.insert(cfg_key, keySequence); + } + save(); } QList ShortcutsConfig::getUserShortcuts(QAction *action) const { - return shortcuts.values(getKey(action)); + return userShortcuts.values(cfgKey(action)); } -QString ShortcutsConfig::getKey(QObject *object) const { - QString key = object->objectName(); - QRegularExpression re("[A-Z]"); - int i = key.indexOf(re); - while (i != -1) { - if (key.at(i - 1) != '_') - key.insert(i++, '_'); - i = key.indexOf(re, i + 1); - } - return key.toLower(); +QList ShortcutsConfig::getUserShortcuts(Shortcut *shortcut) const { + return userShortcuts.values(cfgKey(shortcut)); +} + +// Converts a camelCase object name to snake_case. +QString ShortcutsConfig::cfgKey(QObject *object) const { + QString cfg_key = object->objectName(); + QRegularExpression re("[A-Z]"); + int i = cfg_key.indexOf(re); + while (i != -1) { + if (cfg_key.at(i - 1) != '_') + cfg_key.insert(i++, '_'); + i = cfg_key.indexOf(re, i + 1); + } + return cfg_key.toLower(); } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eb4a2de9..2b242876 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -14,11 +14,11 @@ #include "draggablepixmapitem.h" #include "editcommands.h" #include "flowlayout.h" +#include "shortcut.h" #include #include #include -#include #include #include #include @@ -99,19 +99,29 @@ void MainWindow::initWindow() { } void MainWindow::initExtraShortcuts() { - new QShortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale())); - new QShortcut(QKeySequence("Ctrl+G"), ui->checkBox_ToggleGrid, SLOT(toggle())); - new QShortcut(QKeySequence("Ctrl+D"), this, SLOT(duplicate())); - new QShortcut(QKeySequence::Delete, this, SLOT(on_toolButton_deleteObject_clicked())); - new QShortcut(QKeySequence("Backspace"), this, SLOT(on_toolButton_deleteObject_clicked())); - ui->actionZoom_In->setShortcuts({QKeySequence("Ctrl++"), QKeySequence("Ctrl+=")}); + auto *shortcutReset_Zoom = new Shortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale())); + shortcutReset_Zoom->setObjectName("shortcutReset_Zoom"); + + auto *shortcutToggle_Grid = new Shortcut(QKeySequence("Ctrl+G"), ui->checkBox_ToggleGrid, SLOT(toggle())); + shortcutToggle_Grid->setObjectName("shortcutToggle_Grid"); + + auto *shortcutDuplicate_Events = new Shortcut(QKeySequence("Ctrl+D"), this, SLOT(duplicate())); + shortcutDuplicate_Events->setObjectName("shortcutDuplicate_Events"); + + auto *shortcutDelete_Object = new Shortcut( + {QKeySequence("Del"), QKeySequence("Backspace")}, this, SLOT(on_toolButton_deleteObject_clicked())); + shortcutDelete_Object->setObjectName("shortcutDelete_Object"); + + ui->actionZoom_In->setShortcuts({ui->actionZoom_In->shortcut(), QKeySequence("Ctrl+=")}); } void MainWindow::initUserShortcuts() { shortcutsConfig.load(); - shortcutsConfig.setDefaultShortcuts(findChildren()); + shortcutsConfig.setDefaultShortcuts(findChildren(), findChildren()); for (auto *action : findChildren()) action->setShortcuts(shortcutsConfig.getUserShortcuts(action)); + for (auto *shortcut : findChildren()) + shortcut->setKeys(shortcutsConfig.getUserShortcuts(shortcut)); } void MainWindow::initCustomUI() { diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 950b8d46..1c6b620e 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -86,7 +86,7 @@ void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTi void TilesetEditor::initUi() { ui->setupUi(this); - new QShortcut(QKeySequence("Ctrl+Shift+Z"), this, SLOT(on_actionRedo_triggered())); + ui->actionRedo->setShortcuts({ui->actionRedo->shortcut(), QKeySequence("Ctrl+Shift+Z")}); this->tileXFlip = ui->checkBox_xFlip->isChecked(); this->tileYFlip = ui->checkBox_yFlip->isChecked(); this->paletteId = ui->spinBox_paletteSelector->value();