Convert QShortcut's to Shortcut's and add them to config

This commit is contained in:
BigBahss 2020-11-03 05:58:10 -05:00
parent ed2f8bf180
commit eabd2d6618
4 changed files with 116 additions and 52 deletions

View file

@ -195,21 +195,29 @@ private:
extern ProjectConfig projectConfig;
class QAction;
class Shortcut;
class ShortcutsConfig : public KeyValueConfigBase
{
public:
ShortcutsConfig() :
shortcuts(QMultiMap<QString, QKeySequence>()),
userShortcuts(QMultiMap<QString, QKeySequence>()),
defaultShortcuts(QMultiMap<QString, QKeySequence>())
{
reset();
}
virtual void reset() override { shortcuts.clear(); }
void setDefaultShortcuts(const QList<QAction *> &actions);
{ }
virtual void reset() override { userShortcuts.clear(); }
void setDefaultShortcuts(
const QList<QAction *> &actions = QList<QAction *>(),
const QList<Shortcut *> &shortcuts = QList<Shortcut *>());
QList<QKeySequence> getDefaultShortcuts(QAction *action) const;
void setUserShortcuts(const QList<QAction *> &actions);
QList<QKeySequence> getDefaultShortcuts(Shortcut *shortcut) const;
void setUserShortcuts(
const QList<QAction *> &actions = QList<QAction *>(),
const QList<Shortcut *> &shortcuts = QList<Shortcut *>());
QList<QKeySequence> getUserShortcuts(QAction *action) const;
QList<QKeySequence> getUserShortcuts(Shortcut *shortcut) const;
protected:
virtual QString getConfigFilepath() override;
@ -219,10 +227,10 @@ protected:
virtual void setUnreadKeys() override {};
private:
QMultiMap<QString, QKeySequence> shortcuts;
QMultiMap<QString, QKeySequence> userShortcuts;
QMultiMap<QString, QKeySequence> defaultShortcuts;
QString getKey(QObject *object) const;
QString cfgKey(QObject *object) const;
};
extern ShortcutsConfig shortcutsConfig;

View file

@ -1,5 +1,6 @@
#include "config.h"
#include "log.h"
#include "shortcut.h"
#include <QDir>
#include <QFile>
#include <QFormLayout>
@ -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<QString, QString> ShortcutsConfig::getKeyValueMap() {
QMap<QString, QString> 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<QAction *> &actions) {
void ShortcutsConfig::setDefaultShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &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<QKeySequence> ShortcutsConfig::getDefaultShortcuts(QAction *action) const {
return defaultShortcuts.values(getKey(action));
return defaultShortcuts.values(cfgKey(action));
}
void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions) {
shortcuts.clear();
QList<QKeySequence> ShortcutsConfig::getDefaultShortcuts(Shortcut *shortcut) const {
return defaultShortcuts.values(cfgKey(shortcut));
}
void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &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<QKeySequence> 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<QKeySequence> 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();
}

View file

@ -14,11 +14,11 @@
#include "draggablepixmapitem.h"
#include "editcommands.h"
#include "flowlayout.h"
#include "shortcut.h"
#include <QFileDialog>
#include <QDirIterator>
#include <QStandardItemModel>
#include <QShortcut>
#include <QSpinBox>
#include <QTextEdit>
#include <QSpacerItem>
@ -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<QAction *>());
shortcutsConfig.setDefaultShortcuts(findChildren<QAction *>(), findChildren<Shortcut *>());
for (auto *action : findChildren<QAction *>())
action->setShortcuts(shortcutsConfig.getUserShortcuts(action));
for (auto *shortcut : findChildren<Shortcut *>())
shortcut->setKeys(shortcutsConfig.getUserShortcuts(shortcut));
}
void MainWindow::initCustomUI() {

View file

@ -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();