hotkeys.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <QKeySequence>
  5. #include <QShortcut>
  6. #include <QTreeWidgetItem>
  7. #include <QtGlobal>
  8. #include "yuzu/hotkeys.h"
  9. #include "yuzu/ui_settings.h"
  10. HotkeyRegistry::HotkeyRegistry() = default;
  11. HotkeyRegistry::~HotkeyRegistry() = default;
  12. void HotkeyRegistry::SaveHotkeys() {
  13. UISettings::values.shortcuts.clear();
  14. for (const auto& group : hotkey_groups) {
  15. for (const auto& hotkey : group.second) {
  16. UISettings::values.shortcuts.push_back(
  17. {hotkey.first, group.first,
  18. UISettings::ContextualShortcut(hotkey.second.keyseq.toString(),
  19. hotkey.second.context)});
  20. }
  21. }
  22. }
  23. void HotkeyRegistry::LoadHotkeys() {
  24. // Make sure NOT to use a reference here because it would become invalid once we call
  25. // beginGroup()
  26. for (auto shortcut : UISettings::values.shortcuts) {
  27. Hotkey& hk = hotkey_groups[shortcut.group][shortcut.name];
  28. if (!shortcut.shortcut.first.isEmpty()) {
  29. hk.keyseq = QKeySequence::fromString(shortcut.shortcut.first, QKeySequence::NativeText);
  30. hk.context = static_cast<Qt::ShortcutContext>(shortcut.shortcut.second);
  31. }
  32. if (hk.shortcut) {
  33. hk.shortcut->disconnect();
  34. hk.shortcut->setKey(hk.keyseq);
  35. }
  36. }
  37. }
  38. QShortcut* HotkeyRegistry::GetHotkey(const QString& group, const QString& action, QWidget* widget) {
  39. Hotkey& hk = hotkey_groups[group][action];
  40. if (!hk.shortcut)
  41. hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
  42. return hk.shortcut;
  43. }
  44. QKeySequence HotkeyRegistry::GetKeySequence(const QString& group, const QString& action) {
  45. return hotkey_groups[group][action].keyseq;
  46. }
  47. Qt::ShortcutContext HotkeyRegistry::GetShortcutContext(const QString& group,
  48. const QString& action) {
  49. return hotkey_groups[group][action].context;
  50. }