KeyboardCustomizationSettings.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "KeyboardCustomizationSettings.h"
  10. #include <AzToolsFramework/Editor/ActionManagerUtils.h>
  11. // Qt
  12. #include <QMenu>
  13. #include <QMenuBar>
  14. #include <QJsonObject>
  15. #include <QJsonDocument>
  16. #include <QFileDialog>
  17. #include <QMessageBox>
  18. #include <QSettings>
  19. QString RemoveAcceleratorAmpersands(const QString& original)
  20. {
  21. static const QRegularExpression expression(QStringLiteral("&(.)"));
  22. QString updated = original;
  23. updated.replace(expression, QStringLiteral("\\1"));
  24. return updated;
  25. }
  26. QVector<QAction*> GetAllActionsForMenu(const QMenu* menu)
  27. {
  28. QList<QAction*> menuActions = menu->actions();
  29. QVector<QAction*> actions;
  30. actions.reserve(menuActions.size());
  31. foreach(QAction * action, menuActions)
  32. {
  33. if (action->menu() != nullptr)
  34. {
  35. QVector<QAction*> subMenuActions = GetAllActionsForMenu(action->menu());
  36. actions.reserve(actions.size() + subMenuActions.size());
  37. actions += subMenuActions;
  38. }
  39. else if (!action->isSeparator())
  40. {
  41. actions.push_back(action);
  42. }
  43. }
  44. return actions;
  45. }
  46. void ProcessAllActions(const QWidget* parent, const std::function<bool(QAction*)>& processor)
  47. {
  48. QMenuBar* menuBar = parent->findChild<QMenuBar*>();
  49. if (!menuBar)
  50. {
  51. return;
  52. }
  53. QList<QAction*> menuBarActions = menuBar->actions();
  54. for(QAction* menuAction : menuBarActions)
  55. {
  56. processor(menuAction);
  57. auto actions = GetAllActionsForMenu(menuAction->menu());
  58. for(QAction* action : actions)
  59. {
  60. if (Q_UNLIKELY(processor(action)))
  61. {
  62. return;
  63. }
  64. }
  65. }
  66. }
  67. QString GetName(const QAction* action)
  68. {
  69. if (action->data().isValid())
  70. {
  71. return action->data().toString();
  72. }
  73. else
  74. {
  75. return action->objectName();
  76. }
  77. }
  78. QVector<KeyboardCustomizationSettings*> KeyboardCustomizationSettings::m_instances;
  79. KeyboardCustomizationSettings::KeyboardCustomizationSettings(const QString& group, QWidget* parent)
  80. : m_parent(parent)
  81. , m_group(group)
  82. , m_defaults(CreateSnapshot())
  83. , m_shortcutsEnabled(true)
  84. {
  85. m_instances.append(this);
  86. Load();
  87. }
  88. KeyboardCustomizationSettings::~KeyboardCustomizationSettings()
  89. {
  90. m_instances.remove(m_instances.indexOf(this));
  91. }
  92. void KeyboardCustomizationSettings::LoadDefaults()
  93. {
  94. LoadFromSnapshot(m_defaults);
  95. }
  96. void KeyboardCustomizationSettings::Load()
  97. {
  98. QSettings settings(QStringLiteral("O3DE"), QStringLiteral("O3DE"));
  99. settings.beginGroup(QStringLiteral("Keyboard Shortcuts"));
  100. settings.beginGroup(m_group);
  101. QStringList groups = settings.childGroups();
  102. ProcessAllActions(m_parent, [&](QAction* action)
  103. {
  104. auto groupName = GetName(action);
  105. if (groups.contains(groupName))
  106. {
  107. settings.beginGroup(groupName);
  108. auto sequence = QKeySequence::listFromString(settings.value("shortcuts").toString());
  109. action->setShortcuts(sequence);
  110. settings.endGroup();
  111. }
  112. return false;
  113. });
  114. settings.endGroup(); // m_group
  115. settings.endGroup(); // Keyboard Shortcuts
  116. }
  117. void KeyboardCustomizationSettings::Load(const Snapshot& snapshot)
  118. {
  119. LoadFromSnapshot(snapshot);
  120. }
  121. void KeyboardCustomizationSettings::LoadFromSnapshot(const Snapshot& snapshot)
  122. {
  123. ProcessAllActions(m_parent, [&](QAction* action)
  124. {
  125. auto entry = snapshot.constFind(action);
  126. if (entry != snapshot.constEnd())
  127. {
  128. const Shortcut& s = entry.value();
  129. action->setText(s.text);
  130. action->setShortcuts(s.keySequence);
  131. }
  132. return false;
  133. });
  134. }
  135. void KeyboardCustomizationSettings::Save()
  136. {
  137. QSettings settings(QStringLiteral("O3DE"), QStringLiteral("O3DE"));
  138. settings.beginGroup(QStringLiteral("Keyboard Shortcuts"));
  139. settings.beginGroup(m_group);
  140. ProcessAllActions(m_parent, [&](QAction* action)
  141. {
  142. auto groupName = GetName(action);
  143. settings.beginGroup(groupName);
  144. settings.setValue("shortcuts", QKeySequence::listToString(action->shortcuts()));
  145. settings.endGroup();
  146. return false;
  147. });
  148. settings.endGroup(); // m_group
  149. settings.endGroup(); // Keyboard Shortcuts
  150. }
  151. KeyboardCustomizationSettings::Snapshot KeyboardCustomizationSettings::CreateSnapshot()
  152. {
  153. Snapshot result;
  154. ProcessAllActions(m_parent, [&](QAction* action)
  155. {
  156. Shortcut s = { action->text(), action->shortcuts() };
  157. result[action] = s;
  158. return false;
  159. });
  160. return result;
  161. }
  162. void KeyboardCustomizationSettings::ExportToFile(QWidget* parent)
  163. {
  164. QString fileName = QFileDialog::getSaveFileName(parent, QObject::tr("Export Keyboard Shortcuts"), QStringLiteral("o3de.keys"), QObject::tr("Keyboard Settings (*.keys)"));
  165. if (fileName.isEmpty())
  166. {
  167. return;
  168. }
  169. QFile file(fileName);
  170. if (!file.open(QIODevice::WriteOnly))
  171. {
  172. QMessageBox::critical(parent, QObject::tr("Shortcut Export Error"), QObject::tr("Couldn't open \"%1\": %2").arg(fileName).arg(file.errorString()));
  173. return;
  174. }
  175. QJsonObject store;
  176. store.insert("version", "1.0");
  177. store.insert("Content-Type", "application/x-o3de-sdk-keyboard-settings+json");
  178. QJsonObject groups;
  179. for (auto instance = m_instances.constBegin(); instance != m_instances.constEnd(); instance++)
  180. {
  181. groups.insert((*instance)->m_group, QJsonValue((*instance)->ExportGroup()));
  182. }
  183. store.insert("groups", groups);
  184. QJsonDocument exported(store);
  185. if (-1 == file.write(exported.toJson()))
  186. {
  187. QMessageBox::critical(parent, QObject::tr("Shortcut Export Error"), QObject::tr("Couldn't write settings to \"%1\": %2").arg(fileName).arg(file.errorString()));
  188. return;
  189. }
  190. file.close();
  191. }
  192. QJsonObject KeyboardCustomizationSettings::ExportGroup()
  193. {
  194. QJsonObject group;
  195. group.insert("name", m_group);
  196. ProcessAllActions(m_parent, [&](QAction* action)
  197. {
  198. QJsonObject entry;
  199. entry.insert("label", RemoveAcceleratorAmpersands(action->text()));
  200. entry.insert("shortcuts", QKeySequence::listToString(action->shortcuts()));
  201. group.insert(GetName(action), QJsonValue(entry));
  202. return false;
  203. });
  204. return group;
  205. }
  206. void KeyboardCustomizationSettings::ImportFromFile(QWidget* parent)
  207. {
  208. QString fileName = QFileDialog::getOpenFileName(parent, QObject::tr("Export Keyboard Shortcuts"), QString(), QObject::tr("Keyboard Settings (*.keys)"));
  209. if (fileName.isEmpty())
  210. {
  211. return;
  212. }
  213. QFile file(fileName);
  214. if (!file.open(QIODevice::ReadOnly))
  215. {
  216. QMessageBox::critical(parent, QObject::tr("Shortcut Import Error"), QObject::tr("Couldn't open \"%1\": %2").arg(fileName).arg(file.errorString()));
  217. return;
  218. }
  219. QByteArray rawData = file.readAll();
  220. QJsonDocument imported(QJsonDocument::fromJson(rawData));
  221. QJsonObject store = imported.object();
  222. if (store.value("Content-Type") != "application/x-o3de-sdk-keyboard-settings+json" || store.value("version") != "1.0")
  223. {
  224. QMessageBox::critical(parent, QObject::tr("Shortcut Import Error"), QObject::tr("\"%1\" doesn't appear to contain keyboard settings").arg(fileName));
  225. return;
  226. }
  227. QJsonObject groups = store.value("groups").toObject();
  228. if (QJsonObject() == groups)
  229. {
  230. QMessageBox::critical(parent, QObject::tr("Shortcut Import Error"), QObject::tr("\"%1\" contains no keyboard settings").arg(fileName));
  231. return;
  232. }
  233. for (auto instance = m_instances.begin(); instance != m_instances.end(); instance++)
  234. {
  235. QJsonValue rawGroup = groups.value((*instance)->m_group);
  236. if (!rawGroup.isUndefined())
  237. {
  238. QJsonObject group = rawGroup.toObject();
  239. if (QJsonObject() != group && group.value("name").toString() == (*instance)->m_group)
  240. {
  241. (*instance)->ImportGroup(group);
  242. }
  243. }
  244. }
  245. file.close();
  246. }
  247. void KeyboardCustomizationSettings::ImportGroup(const QJsonObject& group)
  248. {
  249. ProcessAllActions(m_parent, [&](QAction* action)
  250. {
  251. auto position = group.constFind(GetName(action));
  252. if (position != group.constEnd())
  253. {
  254. QJsonObject entry = position.value().toObject();
  255. if (QJsonObject() != entry)
  256. {
  257. QString value = entry.value("shortcuts").toString();
  258. action->setShortcuts(QKeySequence::listFromString(value));
  259. }
  260. else
  261. {
  262. action->setShortcuts({});
  263. }
  264. }
  265. return false;
  266. });
  267. }
  268. QAction* KeyboardCustomizationSettings::FindActionForShortcut(QKeySequence shortcut) const
  269. {
  270. QAction* result {
  271. nullptr
  272. };
  273. ProcessAllActions(m_parent, [&](QAction* action)
  274. {
  275. bool found { false };
  276. if (action->shortcuts().contains(shortcut))
  277. {
  278. result = action;
  279. found = true;
  280. }
  281. return found;
  282. });
  283. return result;
  284. }
  285. void KeyboardCustomizationSettings::ClearShortcutsAndAccelerators()
  286. {
  287. ProcessAllActions(m_parent, [&](QAction* action)
  288. {
  289. action->setText(RemoveAcceleratorAmpersands(action->text()));
  290. action->setShortcut({});
  291. return false;
  292. });
  293. }
  294. /** static */
  295. void KeyboardCustomizationSettings::EnableShortcutsGlobally(bool enable)
  296. {
  297. // This is part of the legacy shortcut manager, so bypass it if the new action manager is enabled.
  298. if (AzToolsFramework::IsNewActionManagerEnabled())
  299. {
  300. return;
  301. }
  302. for (auto it : m_instances)
  303. {
  304. it->EnableShortcuts(enable);
  305. }
  306. }
  307. void KeyboardCustomizationSettings::EnableShortcuts(bool enabled)
  308. {
  309. if (enabled != m_shortcutsEnabled)
  310. {
  311. m_shortcutsEnabled = enabled;
  312. if (enabled)
  313. {
  314. LoadFromSnapshot(m_lastEnabledShortcuts);
  315. CLogFile::WriteLine("Enable Accelerators");
  316. }
  317. else
  318. {
  319. m_lastEnabledShortcuts = CreateSnapshot();
  320. ClearShortcutsAndAccelerators();
  321. CLogFile::WriteLine("Disable Accelerators");
  322. }
  323. }
  324. }
  325. /** static */
  326. void KeyboardCustomizationSettings::LoadDefaultsGlobally()
  327. {
  328. for (auto it : m_instances)
  329. {
  330. it->LoadDefaults();
  331. }
  332. }
  333. /** static */
  334. void KeyboardCustomizationSettings::SaveGlobally()
  335. {
  336. for (auto it : m_instances)
  337. {
  338. it->Save();
  339. }
  340. }