KeyboardShortcutManager.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <QKeySequence>
  11. #include <QAction>
  12. #include <AzCore/std/smart_ptr/unique_ptr.h>
  13. #include <AzCore/std/string/string.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <MCore/Source/StandardHeaders.h>
  16. #include "MysticQtConfig.h"
  17. #endif
  18. class QKeyEvent;
  19. class QSettings;
  20. namespace MysticQt
  21. {
  22. class MYSTICQT_API KeyboardShortcutManager
  23. : public QObject
  24. {
  25. public:
  26. struct Action
  27. {
  28. QAction* m_qaction;
  29. QKeySequence m_defaultKeySequence;
  30. bool m_local;
  31. Action(QAction* qaction, bool local)
  32. : m_qaction(qaction)
  33. , m_defaultKeySequence(qaction->shortcut())
  34. , m_local(local)
  35. {
  36. }
  37. };
  38. class Group
  39. {
  40. public:
  41. Group(AZStd::string_view groupName)
  42. : m_name(groupName)
  43. {
  44. }
  45. void AddAction(AZStd::unique_ptr<Action> action) { m_actions.emplace_back(AZStd::move(action)); }
  46. void RemoveAction(QAction* action, bool local);
  47. size_t GetNumActions() const { return m_actions.size(); }
  48. Action* GetAction(size_t index) { return m_actions[index].get(); }
  49. const AZStd::vector<AZStd::unique_ptr<Action>>& GetActions() const { return m_actions; }
  50. const AZStd::string& GetName() const { return m_name; }
  51. Action* FindActionByName(const QString& actionName, bool local) const;
  52. private:
  53. AZStd::string m_name;
  54. AZStd::vector<AZStd::unique_ptr<Action>> m_actions;
  55. };
  56. void RegisterKeyboardShortcut(QAction* qaction, AZStd::string_view groupName, bool local);
  57. void UnregisterKeyboardShortcut(QAction* qaction, AZStd::string_view groupName, bool local);
  58. Action* FindShortcut(QKeySequence keySequence, Group* group) const;
  59. Action* FindAction(const QString& actionName, AZStd::string_view groupName) const;
  60. Group* FindGroupForShortcut(Action* action) const;
  61. size_t GetNumGroups() const { return m_groups.size(); }
  62. Group* GetGroup(size_t index) const { return m_groups[index].get(); }
  63. void Save(QSettings* settings);
  64. void Load(QSettings* settings);
  65. private:
  66. AZStd::vector<AZStd::unique_ptr<Group>> m_groups;
  67. Group* FindGroupByName(AZStd::string_view groupName) const;
  68. };
  69. } // namespace MysticQt