hotkeys.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <QKeySequence>
  6. #include <QShortcut>
  7. #include <QTreeWidgetItem>
  8. #include <QtGlobal>
  9. #include "yuzu/hotkeys.h"
  10. #include "yuzu/ui_settings.h"
  11. struct Hotkey {
  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. UISettings::values.shortcuts.clear();
  22. for (auto group : hotkey_groups) {
  23. for (auto hotkey : group.second) {
  24. UISettings::values.shortcuts.emplace_back(
  25. UISettings::Shortcut(group.first + "/" + hotkey.first,
  26. UISettings::ContextualShortcut(hotkey.second.keyseq.toString(),
  27. hotkey.second.context)));
  28. }
  29. }
  30. }
  31. void LoadHotkeys() {
  32. // Make sure NOT to use a reference here because it would become invalid once we call
  33. // beginGroup()
  34. for (auto shortcut : UISettings::values.shortcuts) {
  35. QStringList cat = shortcut.first.split("/");
  36. Q_ASSERT(cat.size() >= 2);
  37. // RegisterHotkey assigns default keybindings, so use old values as default parameters
  38. Hotkey& hk = hotkey_groups[cat[0]][cat[1]];
  39. if (!shortcut.second.first.isEmpty()) {
  40. hk.keyseq = QKeySequence::fromString(shortcut.second.first);
  41. hk.context = (Qt::ShortcutContext)shortcut.second.second;
  42. }
  43. if (hk.shortcut)
  44. hk.shortcut->setKey(hk.keyseq);
  45. }
  46. }
  47. void RegisterHotkey(const QString& group, const QString& action, const QKeySequence& default_keyseq,
  48. Qt::ShortcutContext default_context) {
  49. if (hotkey_groups[group].find(action) == hotkey_groups[group].end()) {
  50. hotkey_groups[group][action].keyseq = default_keyseq;
  51. hotkey_groups[group][action].context = default_context;
  52. }
  53. }
  54. QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget) {
  55. Hotkey& hk = hotkey_groups[group][action];
  56. if (!hk.shortcut)
  57. hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
  58. return hk.shortcut;
  59. }
  60. GHotkeysDialog::GHotkeysDialog(QWidget* parent) : QWidget(parent) {
  61. ui.setupUi(this);
  62. for (auto group : hotkey_groups) {
  63. QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group.first));
  64. for (auto hotkey : group.second) {
  65. QStringList columns;
  66. columns << hotkey.first << hotkey.second.keyseq.toString();
  67. QTreeWidgetItem* item = new QTreeWidgetItem(columns);
  68. toplevel_item->addChild(item);
  69. }
  70. ui.treeWidget->addTopLevelItem(toplevel_item);
  71. }
  72. // TODO: Make context configurable as well (hiding the column for now)
  73. ui.treeWidget->setColumnCount(2);
  74. ui.treeWidget->resizeColumnToContents(0);
  75. ui.treeWidget->resizeColumnToContents(1);
  76. }