hotkeys.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <map>
  6. #include "ui_hotkeys.h"
  7. class QDialog;
  8. class QKeySequence;
  9. class QSettings;
  10. class QShortcut;
  11. class HotkeyRegistry final {
  12. public:
  13. friend class GHotkeysDialog;
  14. explicit HotkeyRegistry();
  15. ~HotkeyRegistry();
  16. /**
  17. * Loads hotkeys from the settings file.
  18. *
  19. * @note Yet unregistered hotkeys which are present in the settings will automatically be
  20. * registered.
  21. */
  22. void LoadHotkeys();
  23. /**
  24. * Saves all registered hotkeys to the settings file.
  25. *
  26. * @note Each hotkey group will be stored a settings group; For each hotkey inside that group, a
  27. * settings group will be created to store the key sequence and the hotkey context.
  28. */
  29. void SaveHotkeys();
  30. /**
  31. * Returns a QShortcut object whose activated() signal can be connected to other QObjects'
  32. * slots.
  33. *
  34. * @param group General group this hotkey belongs to (e.g. "Main Window", "Debugger").
  35. * @param action Name of the action (e.g. "Start Emulation", "Load Image").
  36. * @param widget Parent widget of the returned QShortcut.
  37. * @warning If multiple QWidgets' call this function for the same action, the returned QShortcut
  38. * will be the same. Thus, you shouldn't rely on the caller really being the
  39. * QShortcut's parent.
  40. */
  41. QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget);
  42. /**
  43. * Register a hotkey.
  44. *
  45. * @param group General group this hotkey belongs to (e.g. "Main Window", "Debugger")
  46. * @param action Name of the action (e.g. "Start Emulation", "Load Image")
  47. * @param default_keyseq Default key sequence to assign if the hotkey wasn't present in the
  48. * settings file before
  49. * @param default_context Default context to assign if the hotkey wasn't present in the settings
  50. * file before
  51. * @warning Both the group and action strings will be displayed in the hotkey settings dialog
  52. */
  53. void RegisterHotkey(const QString& group, const QString& action,
  54. const QKeySequence& default_keyseq = {},
  55. Qt::ShortcutContext default_context = Qt::WindowShortcut);
  56. private:
  57. struct Hotkey {
  58. QKeySequence keyseq;
  59. QShortcut* shortcut = nullptr;
  60. Qt::ShortcutContext context = Qt::WindowShortcut;
  61. };
  62. using HotkeyMap = std::map<QString, Hotkey>;
  63. using HotkeyGroupMap = std::map<QString, HotkeyMap>;
  64. HotkeyGroupMap hotkey_groups;
  65. };
  66. class GHotkeysDialog : public QWidget {
  67. Q_OBJECT
  68. public:
  69. explicit GHotkeysDialog(QWidget* parent = nullptr);
  70. void Populate(const HotkeyRegistry& registry);
  71. private:
  72. Ui::hotkeys ui;
  73. };