From bb6786f24eda12377c0a111a3c8c9d9bdeaa400a Mon Sep 17 00:00:00 2001 From: BigBahss Date: Thu, 12 Nov 2020 23:48:03 -0500 Subject: [PATCH] Add extra shortcuts to main window and sub-editors --- include/config.h | 5 +---- include/ui/tileseteditor.h | 1 + src/config.cpp | 26 ++++++-------------------- src/mainwindow.cpp | 33 +++++++++++++++++++++++++++------ src/ui/regionmapeditor.cpp | 17 ++++++++++++----- src/ui/shortcutseditor.cpp | 6 +++--- src/ui/tileseteditor.cpp | 25 +++++++++++++++++++------ 7 files changed, 69 insertions(+), 44 deletions(-) diff --git a/include/config.h b/include/config.h index 6d7753ac..4b0db13c 100644 --- a/include/config.h +++ b/include/config.h @@ -207,16 +207,14 @@ public: virtual void reset() override { user_shortcuts.clear(); } + // Call this before applying user shortcuts so that the user can restore defaults. void setDefaultShortcuts(const QObjectList &objects); - void setDefaultShortcuts(const QMultiMap &objects_keySequences); QList defaultShortcuts(const QObject *object) const; void setUserShortcuts(const QObjectList &objects); void setUserShortcuts(const QMultiMap &objects_keySequences); QList userShortcuts(const QObject *object) const; - static bool objectNameIsValid(const QObject *object); - protected: virtual QString getConfigFilepath() override; virtual void parseConfigKeyValue(QString key, QString value) override; @@ -243,7 +241,6 @@ private: const QList &keySequences); }; -// Call setDefaultShortcuts() prior to applying user shortcuts. extern ShortcutsConfig shortcutsConfig; #endif // CONFIG_H diff --git a/include/ui/tileseteditor.h b/include/ui/tileseteditor.h index 6cf7350c..1af92f35 100644 --- a/include/ui/tileseteditor.h +++ b/include/ui/tileseteditor.h @@ -108,6 +108,7 @@ private: void initSelectedTileItem(); void initMetatileLayersItem(); void initShortcuts(); + void initExtraShortcuts(); void restoreWindowState(); void initMetatileHistory(); void setTilesets(QString primaryTilesetLabel, QString secondaryTilesetLabel); diff --git a/src/config.cpp b/src/config.cpp index a50f3c3f..ae562334 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -730,11 +730,10 @@ QMap ShortcutsConfig::getKeyValueMap() { QMap map; for (auto cfg_key : user_shortcuts.uniqueKeys()) { auto keySequences = user_shortcuts.values(cfg_key); - QStringList values; + QStringList keySequenceStrings; for (auto keySequence : keySequences) - values.append(keySequence.toString()); - QString value = values.join(' '); - map.insert(cfg_key, value); + keySequenceStrings.append(keySequence.toString()); + map.insert(cfg_key, keySequenceStrings.join(' ')); } return map; } @@ -744,12 +743,6 @@ void ShortcutsConfig::setDefaultShortcuts(const QObjectList &objects) { save(); } -void ShortcutsConfig::setDefaultShortcuts(const QMultiMap &objects_keySequences) { - for (auto *object : objects_keySequences.uniqueKeys()) - storeShortcuts(StoreType::Default, cfgKey(object), objects_keySequences.values(object)); - save(); -} - QList ShortcutsConfig::defaultShortcuts(const QObject *object) const { return default_shortcuts.values(cfgKey(object)); } @@ -761,7 +754,8 @@ void ShortcutsConfig::setUserShortcuts(const QObjectList &objects) { void ShortcutsConfig::setUserShortcuts(const QMultiMap &objects_keySequences) { for (auto *object : objects_keySequences.uniqueKeys()) - storeShortcuts(StoreType::User, cfgKey(object), objects_keySequences.values(object)); + if (!object->objectName().isEmpty() && !object->objectName().startsWith("_q_")) + storeShortcuts(StoreType::User, cfgKey(object), objects_keySequences.values(object)); save(); } @@ -771,7 +765,7 @@ QList ShortcutsConfig::userShortcuts(const QObject *object) const void ShortcutsConfig::storeShortcutsFromList(StoreType storeType, const QObjectList &objects) { for (const auto *object : objects) - if (objectNameIsValid(object)) + if (!object->objectName().isEmpty() && !object->objectName().startsWith("_q_")) storeShortcuts(storeType, cfgKey(object), currentShortcuts(object)); } @@ -802,11 +796,6 @@ void ShortcutsConfig::storeShortcuts( } } -bool ShortcutsConfig::objectNameIsValid(const QObject *object) { - // Qt internal action names start with "_q_" so we filter those out. - return !object->objectName().isEmpty() && !object->objectName().startsWith("_q_"); -} - /* Creates a config key from the object's name prepended with the parent * window's object name, and converts camelCase to snake_case. */ QString ShortcutsConfig::cfgKey(const QObject *object) const { @@ -830,9 +819,6 @@ QList ShortcutsConfig::currentShortcuts(const QObject *object) con if (object->inherits("QAction")) { const auto *action = qobject_cast(object); return action->shortcuts(); - } else if (object->inherits("QAbstractButton")) { - const auto *button = qobject_cast(object); - return { button->shortcut() }; } else if (object->inherits("Shortcut")) { const auto *shortcut = qobject_cast(object); return shortcut->keys(); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 489365aa..dcc90c9c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -106,6 +106,8 @@ void MainWindow::initShortcuts() { } void MainWindow::initExtraShortcuts() { + ui->actionZoom_In->setShortcuts({ui->actionZoom_In->shortcut(), QKeySequence("Ctrl+=")}); + auto *shortcutReset_Zoom = new Shortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale())); shortcutReset_Zoom->setObjectName("shortcutZoom_Reset"); shortcutReset_Zoom->setWhatsThis("Zoom Reset"); @@ -123,27 +125,46 @@ void MainWindow::initExtraShortcuts() { shortcutDelete_Object->setObjectName("shortcutDelete_Object"); shortcutDelete_Object->setWhatsThis("Delete Selected Event(s)"); - ui->actionZoom_In->setShortcuts({ui->actionZoom_In->shortcut(), QKeySequence("Ctrl+=")}); + auto *shortcutToggle_Border = new Shortcut(QKeySequence(), ui->checkBox_ToggleBorder, SLOT(toggle())); + shortcutToggle_Border->setObjectName("shortcutToggle_Border"); + shortcutToggle_Border->setWhatsThis("Toggle Border"); + + auto *shortcutToggle_Smart_Paths = new Shortcut(QKeySequence(), ui->checkBox_smartPaths, SLOT(toggle())); + shortcutToggle_Smart_Paths->setObjectName("shortcutToggle_Smart_Paths"); + shortcutToggle_Smart_Paths->setWhatsThis("Toggle Smart Paths"); + + auto *shortcutExpand_All = new Shortcut(QKeySequence(), this, SLOT(on_toolButton_ExpandAll_clicked())); + shortcutExpand_All->setObjectName("shortcutExpand_All"); + shortcutExpand_All->setWhatsThis("Map List: Expand all folders"); + + auto *shortcutCollapse_All = new Shortcut(QKeySequence(), this, SLOT(on_toolButton_CollapseAll_clicked())); + shortcutCollapse_All->setObjectName("shortcutCollapse_All"); + shortcutCollapse_All->setWhatsThis("Map List: Collapse all folders"); + + auto *shortcutNew_Event = new Shortcut(QKeySequence(), this, SLOT(on_toolButton_Open_Scripts_clicked())); + shortcutNew_Event->setObjectName("shortcut_Open_Scripts"); + shortcutNew_Event->setWhatsThis("Open Map Scripts"); } QObjectList MainWindow::shortcutableObjects() const { QObjectList shortcutable_objects; + for (auto *action : findChildren()) - if (ShortcutsConfig::objectNameIsValid(action)) + if (!action->objectName().isEmpty()) shortcutable_objects.append(qobject_cast(action)); for (auto *shortcut : findChildren()) - if (ShortcutsConfig::objectNameIsValid(shortcut)) + if (!shortcut->objectName().isEmpty()) shortcutable_objects.append(qobject_cast(shortcut)); - + return shortcutable_objects; } void MainWindow::applyUserShortcuts() { for (auto *action : findChildren()) - if (ShortcutsConfig::objectNameIsValid(action)) + if (!action->objectName().isEmpty()) action->setShortcuts(shortcutsConfig.userShortcuts(action)); for (auto *shortcut : findChildren()) - if (ShortcutsConfig::objectNameIsValid(shortcut)) + if (!shortcut->objectName().isEmpty()) shortcut->setKeys(shortcutsConfig.userShortcuts(shortcut)); } diff --git a/src/ui/regionmapeditor.cpp b/src/ui/regionmapeditor.cpp index b025b90e..a116aa5a 100644 --- a/src/ui/regionmapeditor.cpp +++ b/src/ui/regionmapeditor.cpp @@ -25,6 +25,7 @@ RegionMapEditor::RegionMapEditor(QWidget *parent, Project *project_) : this->project = project_; this->region_map = new RegionMap; this->ui->action_RegionMap_Resize->setVisible(false); + this->initShortcuts(); this->restoreWindowState(); } @@ -94,6 +95,11 @@ bool RegionMapEditor::loadCityMaps() { } void RegionMapEditor::initShortcuts() { + auto *shortcut_RM_Options_delete = new Shortcut( + {QKeySequence("Del"), QKeySequence("Backspace")}, this, SLOT(on_pushButton_RM_Options_delete_clicked())); + shortcut_RM_Options_delete->setObjectName("shortcut_RM_Options_delete"); + shortcut_RM_Options_delete->setWhatsThis("Map Layout: Delete Square"); + shortcutsConfig.load(); shortcutsConfig.setDefaultShortcuts(shortcutableObjects()); applyUserShortcuts(); @@ -101,22 +107,23 @@ void RegionMapEditor::initShortcuts() { QObjectList RegionMapEditor::shortcutableObjects() const { QObjectList shortcutable_objects; + for (auto *action : findChildren()) - if (ShortcutsConfig::objectNameIsValid(action)) + if (!action->objectName().isEmpty()) shortcutable_objects.append(qobject_cast(action)); for (auto *shortcut : findChildren()) - if (ShortcutsConfig::objectNameIsValid(shortcut)) + if (!shortcut->objectName().isEmpty()) shortcutable_objects.append(qobject_cast(shortcut)); - + return shortcutable_objects; } void RegionMapEditor::applyUserShortcuts() { for (auto *action : findChildren()) - if (ShortcutsConfig::objectNameIsValid(action)) + if (!action->objectName().isEmpty()) action->setShortcuts(shortcutsConfig.userShortcuts(action)); for (auto *shortcut : findChildren()) - if (ShortcutsConfig::objectNameIsValid(shortcut)) + if (!shortcut->objectName().isEmpty()) shortcut->setKeys(shortcutsConfig.userShortcuts(shortcut)); } diff --git a/src/ui/shortcutseditor.cpp b/src/ui/shortcutseditor.cpp index 0e0843f0..0f9e77ac 100644 --- a/src/ui/shortcutseditor.cpp +++ b/src/ui/shortcutseditor.cpp @@ -21,8 +21,8 @@ ShortcutsEditor::ShortcutsEditor(QWidget *parent) : ui->setupUi(this); setAttribute(Qt::WA_DeleteOnClose); main_container = ui->scrollAreaWidgetContents_Shortcuts; - auto *formLayout = new QVBoxLayout(main_container); - formLayout->setSpacing(12); + auto *main_layout = new QVBoxLayout(main_container); + main_layout->setSpacing(12); connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &ShortcutsEditor::dialogButtonClicked); } @@ -69,7 +69,7 @@ void ShortcutsEditor::resetShortcuts() { void ShortcutsEditor::parseObjectList(const QObjectList &objectList) { for (auto *object : objectList) { const auto label = getLabel(object); - if (!label.isEmpty() && ShortcutsConfig::objectNameIsValid(object)) + if (!label.isEmpty() && !object->objectName().isEmpty() && !object->objectName().startsWith("_q_")) labels_objects.insert(label, object); } } diff --git a/src/ui/tileseteditor.cpp b/src/ui/tileseteditor.cpp index 5e0b9eed..a63b6680 100644 --- a/src/ui/tileseteditor.cpp +++ b/src/ui/tileseteditor.cpp @@ -211,31 +211,44 @@ void TilesetEditor::initSelectedTileItem() { } void TilesetEditor::initShortcuts() { - ui->actionRedo->setShortcuts({ui->actionRedo->shortcut(), QKeySequence("Ctrl+Shift+Z")}); + initExtraShortcuts(); shortcutsConfig.load(); shortcutsConfig.setDefaultShortcuts(shortcutableObjects()); applyUserShortcuts(); } +void TilesetEditor::initExtraShortcuts() { + ui->actionRedo->setShortcuts({ui->actionRedo->shortcut(), QKeySequence("Ctrl+Shift+Z")}); + + auto *shortcut_xFlip = new Shortcut(QKeySequence(), ui->checkBox_xFlip, SLOT(toggle())); + shortcut_xFlip->setObjectName("shortcut_xFlip"); + shortcut_xFlip->setWhatsThis("X Flip"); + + auto *shortcut_yFlip = new Shortcut(QKeySequence(), ui->checkBox_yFlip, SLOT(toggle())); + shortcut_yFlip->setObjectName("shortcut_yFlip"); + shortcut_yFlip->setWhatsThis("Y Flip"); +} + QObjectList TilesetEditor::shortcutableObjects() const { QObjectList shortcutable_objects; + for (auto *action : findChildren()) - if (ShortcutsConfig::objectNameIsValid(action)) + if (!action->objectName().isEmpty()) shortcutable_objects.append(qobject_cast(action)); for (auto *shortcut : findChildren()) - if (ShortcutsConfig::objectNameIsValid(shortcut)) + if (!shortcut->objectName().isEmpty()) shortcutable_objects.append(qobject_cast(shortcut)); - + return shortcutable_objects; } void TilesetEditor::applyUserShortcuts() { for (auto *action : findChildren()) - if (ShortcutsConfig::objectNameIsValid(action)) + if (!action->objectName().isEmpty()) action->setShortcuts(shortcutsConfig.userShortcuts(action)); for (auto *shortcut : findChildren()) - if (ShortcutsConfig::objectNameIsValid(shortcut)) + if (!shortcut->objectName().isEmpty()) shortcut->setKeys(shortcutsConfig.userShortcuts(shortcut)); }