hotkeys.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include <QtGlobal>
  6. #include <QKeySequence>
  7. #include <QShortcut>
  8. #include "citra_qt/hotkeys.h"
  9. #include "citra_qt/ui_settings.h"
  10. struct Hotkey
  11. {
  12. Hotkey() : shortcut(nullptr), context(Qt::WindowShortcut) {}
  13. QKeySequence keyseq;
  14. QShortcut* shortcut;
  15. Qt::ShortcutContext context;
  16. };
  17. typedef std::map<QString, Hotkey> HotkeyMap;
  18. typedef std::map<QString, HotkeyMap> HotkeyGroupMap;
  19. HotkeyGroupMap hotkey_groups;
  20. void SaveHotkeys()
  21. {
  22. UISettings::values.shortcuts.clear();
  23. for (auto group : hotkey_groups)
  24. {
  25. for (auto hotkey : group.second)
  26. {
  27. UISettings::values.shortcuts.emplace_back(
  28. UISettings::Shortcut(group.first + "/" + hotkey.first,
  29. UISettings::ContextualShortcut(hotkey.second.keyseq.toString(),
  30. hotkey.second.context)));
  31. }
  32. }
  33. }
  34. void LoadHotkeys()
  35. {
  36. // Make sure NOT to use a reference here because it would become invalid once we call beginGroup()
  37. for (auto shortcut : UISettings::values.shortcuts)
  38. {
  39. QStringList cat = shortcut.first.split("/");
  40. Q_ASSERT(cat.size() >= 2);
  41. // RegisterHotkey assigns default keybindings, so use old values as default parameters
  42. Hotkey& hk = hotkey_groups[cat[0]][cat[1]];
  43. if (!shortcut.second.first.isEmpty())
  44. {
  45. hk.keyseq = QKeySequence::fromString(shortcut.second.first);
  46. hk.context = (Qt::ShortcutContext)shortcut.second.second;
  47. }
  48. if (hk.shortcut)
  49. hk.shortcut->setKey(hk.keyseq);
  50. }
  51. }
  52. void RegisterHotkey(const QString& group, const QString& action, const QKeySequence& default_keyseq, Qt::ShortcutContext default_context)
  53. {
  54. if (hotkey_groups[group].find(action) == hotkey_groups[group].end())
  55. {
  56. hotkey_groups[group][action].keyseq = default_keyseq;
  57. hotkey_groups[group][action].context = default_context;
  58. }
  59. }
  60. QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget)
  61. {
  62. Hotkey& hk = hotkey_groups[group][action];
  63. if (!hk.shortcut)
  64. hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
  65. return hk.shortcut;
  66. }
  67. GHotkeysDialog::GHotkeysDialog(QWidget* parent): QWidget(parent)
  68. {
  69. ui.setupUi(this);
  70. for (auto group : hotkey_groups)
  71. {
  72. QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group.first));
  73. for (auto hotkey : group.second)
  74. {
  75. QStringList columns;
  76. columns << hotkey.first << hotkey.second.keyseq.toString();
  77. QTreeWidgetItem* item = new QTreeWidgetItem(columns);
  78. toplevel_item->addChild(item);
  79. }
  80. ui.treeWidget->addTopLevelItem(toplevel_item);
  81. }
  82. // TODO: Make context configurable as well (hiding the column for now)
  83. ui.treeWidget->setColumnCount(2);
  84. ui.treeWidget->resizeColumnToContents(0);
  85. ui.treeWidget->resizeColumnToContents(1);
  86. }