Extract functions in ShortcutsConfig

This commit is contained in:
BigBahss 2020-11-05 06:32:31 -05:00
parent eabd2d6618
commit 43d3257d89
3 changed files with 96 additions and 66 deletions

View file

@ -207,15 +207,15 @@ public:
virtual void reset() override { userShortcuts.clear(); } virtual void reset() override { userShortcuts.clear(); }
void setDefaultShortcuts( void setDefaultShortcuts(const QList<QAction *> &actions);
const QList<QAction *> &actions = QList<QAction *>(), void setDefaultShortcuts(const QList<Shortcut *> &shortcuts);
const QList<Shortcut *> &shortcuts = QList<Shortcut *>()); void setDefaultShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &shortcuts);
QList<QKeySequence> getDefaultShortcuts(QAction *action) const; QList<QKeySequence> getDefaultShortcuts(QAction *action) const;
QList<QKeySequence> getDefaultShortcuts(Shortcut *shortcut) const; QList<QKeySequence> getDefaultShortcuts(Shortcut *shortcut) const;
void setUserShortcuts( void setUserShortcuts(const QList<QAction *> &actions);
const QList<QAction *> &actions = QList<QAction *>(), void setUserShortcuts(const QList<Shortcut *> &shortcuts);
const QList<Shortcut *> &shortcuts = QList<Shortcut *>()); void setUserShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &shortcuts);
QList<QKeySequence> getUserShortcuts(QAction *action) const; QList<QKeySequence> getUserShortcuts(QAction *action) const;
QList<QKeySequence> getUserShortcuts(Shortcut *shortcut) const; QList<QKeySequence> getUserShortcuts(Shortcut *shortcut) const;
@ -230,6 +230,21 @@ private:
QMultiMap<QString, QKeySequence> userShortcuts; QMultiMap<QString, QKeySequence> userShortcuts;
QMultiMap<QString, QKeySequence> defaultShortcuts; QMultiMap<QString, QKeySequence> defaultShortcuts;
enum StoreType {
User,
Default
};
void storeShortcuts(
StoreType storeType,
const QList<QAction *> &actions);
void storeShortcuts(
StoreType storeType,
const QList<Shortcut *> &shortcuts);
void storeShortcut(
StoreType storeType,
const QString &cfgKey,
const QList<QKeySequence> &keySequences);
QString cfgKey(QObject *object) const; QString cfgKey(QObject *object) const;
}; };

View file

@ -738,40 +738,22 @@ QMap<QString, QString> ShortcutsConfig::getKeyValueMap() {
return map; return map;
} }
// Call this before applying user shortcuts to be able to restore default shortcuts.
void ShortcutsConfig::setDefaultShortcuts(const QList<QAction *> &actions) {
storeShortcuts(StoreType::Default, actions);
save();
}
// Call this before applying user shortcuts to be able to restore default shortcuts.
void ShortcutsConfig::setDefaultShortcuts(const QList<Shortcut *> &shortcuts) {
storeShortcuts(StoreType::Default, shortcuts);
save();
}
// 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, const QList<Shortcut *> &shortcuts) { void ShortcutsConfig::setDefaultShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &shortcuts) {
defaultShortcuts.clear(); storeShortcuts(StoreType::Default, actions);
storeShortcuts(StoreType::Default, shortcuts);
for (auto *action : actions) {
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)
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();
} }
@ -783,31 +765,19 @@ QList<QKeySequence> ShortcutsConfig::getDefaultShortcuts(Shortcut *shortcut) con
return defaultShortcuts.values(cfgKey(shortcut)); return defaultShortcuts.values(cfgKey(shortcut));
} }
void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions) {
storeShortcuts(StoreType::User, actions);
save();
}
void ShortcutsConfig::setUserShortcuts(const QList<Shortcut *> &shortcuts) {
storeShortcuts(StoreType::User, shortcuts);
save();
}
void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &shortcuts) { void ShortcutsConfig::setUserShortcuts(const QList<QAction *> &actions, const QList<Shortcut *> &shortcuts) {
userShortcuts.clear(); storeShortcuts(StoreType::User, actions);
storeShortcuts(StoreType::User, shortcuts);
for (auto *action : actions) {
if (action->text().isEmpty() || action->objectName().isEmpty())
continue;
const QString cfg_key = cfgKey(action);
if (action->shortcuts().isEmpty())
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(); save();
} }
@ -819,13 +789,58 @@ QList<QKeySequence> ShortcutsConfig::getUserShortcuts(Shortcut *shortcut) const
return userShortcuts.values(cfgKey(shortcut)); return userShortcuts.values(cfgKey(shortcut));
} }
// Converts a camelCase object name to snake_case. void ShortcutsConfig::storeShortcuts(StoreType storeType, const QList<QAction *> &actions) {
for (auto *action : actions)
if (!action->text().isEmpty() && !action->objectName().isEmpty())
storeShortcut(storeType, cfgKey(action), action->shortcuts());
}
void ShortcutsConfig::storeShortcuts(StoreType storeType, const QList<Shortcut *> &shortcuts) {
for (auto *shortcut : shortcuts)
if (!shortcut->whatsThis().isEmpty() && !shortcut->objectName().isEmpty())
storeShortcut(storeType, cfgKey(shortcut), shortcut->keys());
}
void ShortcutsConfig::storeShortcut(
StoreType storeType,
const QString &cfgKey,
const QList<QKeySequence> &keySequences)
{
bool storeUser = (storeType == User) || !userShortcuts.contains(cfgKey);
if (storeType == Default)
defaultShortcuts.remove(cfgKey);
if (storeUser)
userShortcuts.remove(cfgKey);
if (keySequences.isEmpty()) {
if (storeType == Default)
defaultShortcuts.insert(cfgKey, QKeySequence());
if (storeUser)
userShortcuts.insert(cfgKey, QKeySequence());
} else {
for (auto keySequence : keySequences) {
if (storeType == Default)
defaultShortcuts.insert(cfgKey, keySequence);
if (storeUser)
userShortcuts.insert(cfgKey, keySequence);
}
}
}
/* 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(QObject *object) const { QString ShortcutsConfig::cfgKey(QObject *object) const {
QString cfg_key = object->objectName(); auto cfg_key = QString();
auto *parentWidget = static_cast<QWidget *>(object->parent());
if (parentWidget)
cfg_key = parentWidget->window()->objectName() + '_';
cfg_key += object->objectName();
QRegularExpression re("[A-Z]"); QRegularExpression re("[A-Z]");
int i = cfg_key.indexOf(re); int i = cfg_key.indexOf(re);
while (i != -1) { while (i != -1) {
if (cfg_key.at(i - 1) != '_') if (i != 0 && cfg_key.at(i - 1) != '_')
cfg_key.insert(i++, '_'); cfg_key.insert(i++, '_');
i = cfg_key.indexOf(re, i + 1); i = cfg_key.indexOf(re, i + 1);
} }

View file

@ -128,7 +128,7 @@ int Shortcut::id() const {
} }
QList<int> Shortcut::ids() const { QList<int> Shortcut::ids() const {
QList<int> id_list({id()}); QList<int> id_list;
for (auto *sc : sc_vec) for (auto *sc : sc_vec)
id_list.append(sc->id()); id_list.append(sc->id());
return id_list; return id_list;