hotkeys.cpp 3.2 KB

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