123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include "EditorDefs.h"
- #include "KeyboardCustomizationSettings.h"
- #include <AzToolsFramework/Editor/ActionManagerUtils.h>
- // Qt
- #include <QMenu>
- #include <QMenuBar>
- #include <QJsonObject>
- #include <QJsonDocument>
- #include <QFileDialog>
- #include <QMessageBox>
- #include <QSettings>
- QString RemoveAcceleratorAmpersands(const QString& original)
- {
- static const QRegularExpression expression(QStringLiteral("&(.)"));
- QString updated = original;
- updated.replace(expression, QStringLiteral("\\1"));
- return updated;
- }
- QVector<QAction*> GetAllActionsForMenu(const QMenu* menu)
- {
- QList<QAction*> menuActions = menu->actions();
- QVector<QAction*> actions;
- actions.reserve(menuActions.size());
- foreach(QAction * action, menuActions)
- {
- if (action->menu() != nullptr)
- {
- QVector<QAction*> subMenuActions = GetAllActionsForMenu(action->menu());
- actions.reserve(actions.size() + subMenuActions.size());
- actions += subMenuActions;
- }
- else if (!action->isSeparator())
- {
- actions.push_back(action);
- }
- }
- return actions;
- }
- void ProcessAllActions(const QWidget* parent, const std::function<bool(QAction*)>& processor)
- {
- QMenuBar* menuBar = parent->findChild<QMenuBar*>();
- if (!menuBar)
- {
- return;
- }
- QList<QAction*> menuBarActions = menuBar->actions();
- for(QAction* menuAction : menuBarActions)
- {
- processor(menuAction);
- auto actions = GetAllActionsForMenu(menuAction->menu());
- for(QAction* action : actions)
- {
- if (Q_UNLIKELY(processor(action)))
- {
- return;
- }
- }
- }
- }
- QString GetName(const QAction* action)
- {
- if (action->data().isValid())
- {
- return action->data().toString();
- }
- else
- {
- return action->objectName();
- }
- }
- QVector<KeyboardCustomizationSettings*> KeyboardCustomizationSettings::m_instances;
- KeyboardCustomizationSettings::KeyboardCustomizationSettings(const QString& group, QWidget* parent)
- : m_parent(parent)
- , m_group(group)
- , m_defaults(CreateSnapshot())
- , m_shortcutsEnabled(true)
- {
- m_instances.append(this);
- Load();
- }
- KeyboardCustomizationSettings::~KeyboardCustomizationSettings()
- {
- m_instances.remove(m_instances.indexOf(this));
- }
- void KeyboardCustomizationSettings::LoadDefaults()
- {
- LoadFromSnapshot(m_defaults);
- }
- void KeyboardCustomizationSettings::Load()
- {
- QSettings settings(QStringLiteral("O3DE"), QStringLiteral("O3DE"));
- settings.beginGroup(QStringLiteral("Keyboard Shortcuts"));
- settings.beginGroup(m_group);
- QStringList groups = settings.childGroups();
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- auto groupName = GetName(action);
- if (groups.contains(groupName))
- {
- settings.beginGroup(groupName);
- auto sequence = QKeySequence::listFromString(settings.value("shortcuts").toString());
- action->setShortcuts(sequence);
- settings.endGroup();
- }
- return false;
- });
- settings.endGroup(); // m_group
- settings.endGroup(); // Keyboard Shortcuts
- }
- void KeyboardCustomizationSettings::Load(const Snapshot& snapshot)
- {
- LoadFromSnapshot(snapshot);
- }
- void KeyboardCustomizationSettings::LoadFromSnapshot(const Snapshot& snapshot)
- {
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- auto entry = snapshot.constFind(action);
- if (entry != snapshot.constEnd())
- {
- const Shortcut& s = entry.value();
- action->setText(s.text);
- action->setShortcuts(s.keySequence);
- }
- return false;
- });
- }
- void KeyboardCustomizationSettings::Save()
- {
- QSettings settings(QStringLiteral("O3DE"), QStringLiteral("O3DE"));
- settings.beginGroup(QStringLiteral("Keyboard Shortcuts"));
- settings.beginGroup(m_group);
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- auto groupName = GetName(action);
- settings.beginGroup(groupName);
- settings.setValue("shortcuts", QKeySequence::listToString(action->shortcuts()));
- settings.endGroup();
- return false;
- });
- settings.endGroup(); // m_group
- settings.endGroup(); // Keyboard Shortcuts
- }
- KeyboardCustomizationSettings::Snapshot KeyboardCustomizationSettings::CreateSnapshot()
- {
- Snapshot result;
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- Shortcut s = { action->text(), action->shortcuts() };
- result[action] = s;
- return false;
- });
- return result;
- }
- void KeyboardCustomizationSettings::ExportToFile(QWidget* parent)
- {
- QString fileName = QFileDialog::getSaveFileName(parent, QObject::tr("Export Keyboard Shortcuts"), QStringLiteral("o3de.keys"), QObject::tr("Keyboard Settings (*.keys)"));
- if (fileName.isEmpty())
- {
- return;
- }
- QFile file(fileName);
- if (!file.open(QIODevice::WriteOnly))
- {
- QMessageBox::critical(parent, QObject::tr("Shortcut Export Error"), QObject::tr("Couldn't open \"%1\": %2").arg(fileName).arg(file.errorString()));
- return;
- }
- QJsonObject store;
- store.insert("version", "1.0");
- store.insert("Content-Type", "application/x-o3de-sdk-keyboard-settings+json");
- QJsonObject groups;
- for (auto instance = m_instances.constBegin(); instance != m_instances.constEnd(); instance++)
- {
- groups.insert((*instance)->m_group, QJsonValue((*instance)->ExportGroup()));
- }
- store.insert("groups", groups);
- QJsonDocument exported(store);
- if (-1 == file.write(exported.toJson()))
- {
- QMessageBox::critical(parent, QObject::tr("Shortcut Export Error"), QObject::tr("Couldn't write settings to \"%1\": %2").arg(fileName).arg(file.errorString()));
- return;
- }
- file.close();
- }
- QJsonObject KeyboardCustomizationSettings::ExportGroup()
- {
- QJsonObject group;
- group.insert("name", m_group);
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- QJsonObject entry;
- entry.insert("label", RemoveAcceleratorAmpersands(action->text()));
- entry.insert("shortcuts", QKeySequence::listToString(action->shortcuts()));
- group.insert(GetName(action), QJsonValue(entry));
- return false;
- });
- return group;
- }
- void KeyboardCustomizationSettings::ImportFromFile(QWidget* parent)
- {
- QString fileName = QFileDialog::getOpenFileName(parent, QObject::tr("Export Keyboard Shortcuts"), QString(), QObject::tr("Keyboard Settings (*.keys)"));
- if (fileName.isEmpty())
- {
- return;
- }
- QFile file(fileName);
- if (!file.open(QIODevice::ReadOnly))
- {
- QMessageBox::critical(parent, QObject::tr("Shortcut Import Error"), QObject::tr("Couldn't open \"%1\": %2").arg(fileName).arg(file.errorString()));
- return;
- }
- QByteArray rawData = file.readAll();
- QJsonDocument imported(QJsonDocument::fromJson(rawData));
- QJsonObject store = imported.object();
- if (store.value("Content-Type") != "application/x-o3de-sdk-keyboard-settings+json" || store.value("version") != "1.0")
- {
- QMessageBox::critical(parent, QObject::tr("Shortcut Import Error"), QObject::tr("\"%1\" doesn't appear to contain keyboard settings").arg(fileName));
- return;
- }
- QJsonObject groups = store.value("groups").toObject();
- if (QJsonObject() == groups)
- {
- QMessageBox::critical(parent, QObject::tr("Shortcut Import Error"), QObject::tr("\"%1\" contains no keyboard settings").arg(fileName));
- return;
- }
- for (auto instance = m_instances.begin(); instance != m_instances.end(); instance++)
- {
- QJsonValue rawGroup = groups.value((*instance)->m_group);
- if (!rawGroup.isUndefined())
- {
- QJsonObject group = rawGroup.toObject();
- if (QJsonObject() != group && group.value("name").toString() == (*instance)->m_group)
- {
- (*instance)->ImportGroup(group);
- }
- }
- }
- file.close();
- }
- void KeyboardCustomizationSettings::ImportGroup(const QJsonObject& group)
- {
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- auto position = group.constFind(GetName(action));
- if (position != group.constEnd())
- {
- QJsonObject entry = position.value().toObject();
- if (QJsonObject() != entry)
- {
- QString value = entry.value("shortcuts").toString();
- action->setShortcuts(QKeySequence::listFromString(value));
- }
- else
- {
- action->setShortcuts({});
- }
- }
- return false;
- });
- }
- QAction* KeyboardCustomizationSettings::FindActionForShortcut(QKeySequence shortcut) const
- {
- QAction* result {
- nullptr
- };
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- bool found { false };
- if (action->shortcuts().contains(shortcut))
- {
- result = action;
- found = true;
- }
- return found;
- });
- return result;
- }
- void KeyboardCustomizationSettings::ClearShortcutsAndAccelerators()
- {
- ProcessAllActions(m_parent, [&](QAction* action)
- {
- action->setText(RemoveAcceleratorAmpersands(action->text()));
- action->setShortcut({});
- return false;
- });
- }
- /** static */
- void KeyboardCustomizationSettings::EnableShortcutsGlobally(bool enable)
- {
- // This is part of the legacy shortcut manager, so bypass it if the new action manager is enabled.
- if (AzToolsFramework::IsNewActionManagerEnabled())
- {
- return;
- }
- for (auto it : m_instances)
- {
- it->EnableShortcuts(enable);
- }
- }
- void KeyboardCustomizationSettings::EnableShortcuts(bool enabled)
- {
- if (enabled != m_shortcutsEnabled)
- {
- m_shortcutsEnabled = enabled;
- if (enabled)
- {
- LoadFromSnapshot(m_lastEnabledShortcuts);
- CLogFile::WriteLine("Enable Accelerators");
- }
- else
- {
- m_lastEnabledShortcuts = CreateSnapshot();
- ClearShortcutsAndAccelerators();
- CLogFile::WriteLine("Disable Accelerators");
- }
- }
- }
- /** static */
- void KeyboardCustomizationSettings::LoadDefaultsGlobally()
- {
- for (auto it : m_instances)
- {
- it->LoadDefaults();
- }
- }
- /** static */
- void KeyboardCustomizationSettings::SaveGlobally()
- {
- for (auto it : m_instances)
- {
- it->Save();
- }
- }
|