Convert QShortcut's to Shortcut's and add them to config
This commit is contained in:
parent
ed2f8bf180
commit
eabd2d6618
4 changed files with 116 additions and 52 deletions
|
@ -195,21 +195,29 @@ private:
|
||||||
extern ProjectConfig projectConfig;
|
extern ProjectConfig projectConfig;
|
||||||
|
|
||||||
class QAction;
|
class QAction;
|
||||||
|
class Shortcut;
|
||||||
|
|
||||||
class ShortcutsConfig : public KeyValueConfigBase
|
class ShortcutsConfig : public KeyValueConfigBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ShortcutsConfig() :
|
ShortcutsConfig() :
|
||||||
shortcuts(QMultiMap<QString, QKeySequence>()),
|
userShortcuts(QMultiMap<QString, QKeySequence>()),
|
||||||
defaultShortcuts(QMultiMap<QString, QKeySequence>())
|
defaultShortcuts(QMultiMap<QString, QKeySequence>())
|
||||||
{
|
{ }
|
||||||
reset();
|
|
||||||
}
|
virtual void reset() override { userShortcuts.clear(); }
|
||||||
virtual void reset() override { shortcuts.clear(); }
|
|
||||||
void setDefaultShortcuts(const QList<QAction *> &actions);
|
void setDefaultShortcuts(
|
||||||
|
const QList<QAction *> &actions = QList<QAction *>(),
|
||||||
|
const QList<Shortcut *> &shortcuts = QList<Shortcut *>());
|
||||||
QList<QKeySequence> getDefaultShortcuts(QAction *action) const;
|
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(QAction *action) const;
|
||||||
|
QList<QKeySequence> getUserShortcuts(Shortcut *shortcut) const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual QString getConfigFilepath() override;
|
virtual QString getConfigFilepath() override;
|
||||||
|
@ -219,10 +227,10 @@ protected:
|
||||||
virtual void setUnreadKeys() override {};
|
virtual void setUnreadKeys() override {};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QMultiMap<QString, QKeySequence> shortcuts;
|
QMultiMap<QString, QKeySequence> userShortcuts;
|
||||||
QMultiMap<QString, QKeySequence> defaultShortcuts;
|
QMultiMap<QString, QKeySequence> defaultShortcuts;
|
||||||
|
|
||||||
QString getKey(QObject *object) const;
|
QString cfgKey(QObject *object) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern ShortcutsConfig shortcutsConfig;
|
extern ShortcutsConfig shortcutsConfig;
|
||||||
|
|
114
src/config.cpp
114
src/config.cpp
|
@ -1,5 +1,6 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "shortcut.h"
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QFormLayout>
|
#include <QFormLayout>
|
||||||
|
@ -719,69 +720,114 @@ QString ShortcutsConfig::getConfigFilepath() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShortcutsConfig::parseConfigKeyValue(QString key, QString value) {
|
void ShortcutsConfig::parseConfigKeyValue(QString key, QString value) {
|
||||||
QStringList keySeqs = value.split(' ');
|
QStringList keySequences = value.split(' ');
|
||||||
if (keySeqs.isEmpty())
|
for (auto keySequence : keySequences)
|
||||||
shortcuts.insert(key, value);
|
userShortcuts.insert(key, keySequence);
|
||||||
else for (auto keySeq : keySeqs)
|
|
||||||
shortcuts.insert(key, keySeq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QMap<QString, QString> ShortcutsConfig::getKeyValueMap() {
|
QMap<QString, QString> ShortcutsConfig::getKeyValueMap() {
|
||||||
QMap<QString, QString> map;
|
QMap<QString, QString> map;
|
||||||
for (auto key : shortcuts.uniqueKeys()) {
|
for (auto cfg_key : userShortcuts.uniqueKeys()) {
|
||||||
auto keySeqs = shortcuts.values(key);
|
auto keySequences = userShortcuts.values(cfg_key);
|
||||||
QStringList values;
|
QStringList values;
|
||||||
for (auto keySeq : keySeqs)
|
for (auto keySequence : keySequences)
|
||||||
values.append(keySeq.toString());
|
values.append(keySequence.toString());
|
||||||
QString value = values.join(' ');
|
QString value = values.join(' ');
|
||||||
map.insert(key, value);
|
map.insert(cfg_key, value);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call this before applying user shortcuts to be able to restore default shortcuts.
|
// 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();
|
defaultShortcuts.clear();
|
||||||
|
|
||||||
for (auto *action : actions) {
|
for (auto *action : actions) {
|
||||||
const QString key = getKey(action);
|
if (action->text().isEmpty() || action->objectName().isEmpty())
|
||||||
bool addToUserShortcuts = !shortcuts.contains(key);
|
continue;
|
||||||
for (auto shortcut : action->shortcuts()) {
|
const QString cfg_key = cfgKey(action);
|
||||||
defaultShortcuts.insert(key, shortcut);
|
bool addToUserShortcuts = !userShortcuts.contains(cfg_key);
|
||||||
|
if (action->shortcuts().isEmpty()) {
|
||||||
|
defaultShortcuts.insert(cfg_key, QKeySequence());
|
||||||
if (addToUserShortcuts)
|
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();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QKeySequence> ShortcutsConfig::getDefaultShortcuts(QAction *action) const {
|
QList<QKeySequence> ShortcutsConfig::getDefaultShortcuts(QAction *action) const {
|
||||||
return defaultShortcuts.values(getKey(action));
|
return defaultShortcuts.values(cfgKey(action));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions) {
|
QList<QKeySequence> ShortcutsConfig::getDefaultShortcuts(Shortcut *shortcut) const {
|
||||||
shortcuts.clear();
|
return defaultShortcuts.values(cfgKey(shortcut));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &shortcuts) {
|
||||||
|
userShortcuts.clear();
|
||||||
|
|
||||||
for (auto *action : actions) {
|
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())
|
if (action->shortcuts().isEmpty())
|
||||||
shortcuts.insert(key, QKeySequence());
|
userShortcuts.insert(cfg_key, QKeySequence());
|
||||||
else for (auto shortcut : action->shortcuts())
|
else
|
||||||
shortcuts.insert(key, shortcut);
|
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();
|
save();
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QKeySequence> ShortcutsConfig::getUserShortcuts(QAction *action) const {
|
QList<QKeySequence> ShortcutsConfig::getUserShortcuts(QAction *action) const {
|
||||||
return shortcuts.values(getKey(action));
|
return userShortcuts.values(cfgKey(action));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ShortcutsConfig::getKey(QObject *object) const {
|
QList<QKeySequence> ShortcutsConfig::getUserShortcuts(Shortcut *shortcut) const {
|
||||||
QString key = object->objectName();
|
return userShortcuts.values(cfgKey(shortcut));
|
||||||
QRegularExpression re("[A-Z]");
|
}
|
||||||
int i = key.indexOf(re);
|
|
||||||
while (i != -1) {
|
// Converts a camelCase object name to snake_case.
|
||||||
if (key.at(i - 1) != '_')
|
QString ShortcutsConfig::cfgKey(QObject *object) const {
|
||||||
key.insert(i++, '_');
|
QString cfg_key = object->objectName();
|
||||||
i = key.indexOf(re, i + 1);
|
QRegularExpression re("[A-Z]");
|
||||||
}
|
int i = cfg_key.indexOf(re);
|
||||||
return key.toLower();
|
while (i != -1) {
|
||||||
|
if (cfg_key.at(i - 1) != '_')
|
||||||
|
cfg_key.insert(i++, '_');
|
||||||
|
i = cfg_key.indexOf(re, i + 1);
|
||||||
|
}
|
||||||
|
return cfg_key.toLower();
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,11 +14,11 @@
|
||||||
#include "draggablepixmapitem.h"
|
#include "draggablepixmapitem.h"
|
||||||
#include "editcommands.h"
|
#include "editcommands.h"
|
||||||
#include "flowlayout.h"
|
#include "flowlayout.h"
|
||||||
|
#include "shortcut.h"
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
#include <QShortcut>
|
|
||||||
#include <QSpinBox>
|
#include <QSpinBox>
|
||||||
#include <QTextEdit>
|
#include <QTextEdit>
|
||||||
#include <QSpacerItem>
|
#include <QSpacerItem>
|
||||||
|
@ -99,19 +99,29 @@ void MainWindow::initWindow() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::initExtraShortcuts() {
|
void MainWindow::initExtraShortcuts() {
|
||||||
new QShortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale()));
|
auto *shortcutReset_Zoom = new Shortcut(QKeySequence("Ctrl+0"), this, SLOT(resetMapViewScale()));
|
||||||
new QShortcut(QKeySequence("Ctrl+G"), ui->checkBox_ToggleGrid, SLOT(toggle()));
|
shortcutReset_Zoom->setObjectName("shortcutReset_Zoom");
|
||||||
new QShortcut(QKeySequence("Ctrl+D"), this, SLOT(duplicate()));
|
|
||||||
new QShortcut(QKeySequence::Delete, this, SLOT(on_toolButton_deleteObject_clicked()));
|
auto *shortcutToggle_Grid = new Shortcut(QKeySequence("Ctrl+G"), ui->checkBox_ToggleGrid, SLOT(toggle()));
|
||||||
new QShortcut(QKeySequence("Backspace"), this, SLOT(on_toolButton_deleteObject_clicked()));
|
shortcutToggle_Grid->setObjectName("shortcutToggle_Grid");
|
||||||
ui->actionZoom_In->setShortcuts({QKeySequence("Ctrl++"), QKeySequence("Ctrl+=")});
|
|
||||||
|
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() {
|
void MainWindow::initUserShortcuts() {
|
||||||
shortcutsConfig.load();
|
shortcutsConfig.load();
|
||||||
shortcutsConfig.setDefaultShortcuts(findChildren<QAction *>());
|
shortcutsConfig.setDefaultShortcuts(findChildren<QAction *>(), findChildren<Shortcut *>());
|
||||||
for (auto *action : findChildren<QAction *>())
|
for (auto *action : findChildren<QAction *>())
|
||||||
action->setShortcuts(shortcutsConfig.getUserShortcuts(action));
|
action->setShortcuts(shortcutsConfig.getUserShortcuts(action));
|
||||||
|
for (auto *shortcut : findChildren<Shortcut *>())
|
||||||
|
shortcut->setKeys(shortcutsConfig.getUserShortcuts(shortcut));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::initCustomUI() {
|
void MainWindow::initCustomUI() {
|
||||||
|
|
|
@ -86,7 +86,7 @@ void TilesetEditor::setTilesets(QString primaryTilesetLabel, QString secondaryTi
|
||||||
|
|
||||||
void TilesetEditor::initUi() {
|
void TilesetEditor::initUi() {
|
||||||
ui->setupUi(this);
|
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->tileXFlip = ui->checkBox_xFlip->isChecked();
|
||||||
this->tileYFlip = ui->checkBox_yFlip->isChecked();
|
this->tileYFlip = ui->checkBox_yFlip->isChecked();
|
||||||
this->paletteId = ui->spinBox_paletteSelector->value();
|
this->paletteId = ui->spinBox_paletteSelector->value();
|
||||||
|
|
Loading…
Reference in a new issue