hotkeys.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <QtGlobal>
  8. #include "citra_qt/hotkeys.h"
  9. #include "citra_qt/ui_settings.h"
  10. struct Hotkey {
  11. Hotkey() : shortcut(nullptr), context(Qt::WindowShortcut) {}
  12. QKeySequence keyseq;
  13. QShortcut* shortcut;
  14. Qt::ShortcutContext context;
  15. };
  16. typedef std::map<QString, Hotkey> HotkeyMap;
  17. typedef std::map<QString, HotkeyMap> HotkeyGroupMap;
  18. HotkeyGroupMap hotkey_groups;
  19. void SaveHotkeys() {
  20. UISettings::values.shortcuts.clear();
  21. for (auto group : hotkey_groups) {
  22. for (auto hotkey : group.second) {
  23. UISettings::values.shortcuts.emplace_back(
  24. UISettings::Shortcut(group.first + "/" + hotkey.first,
  25. UISettings::ContextualShortcut(hotkey.second.keyseq.toString(),
  26. hotkey.second.context)));
  27. }
  28. }
  29. }
  30. void LoadHotkeys() {
  31. // Make sure NOT to use a reference here because it would become invalid once we call
  32. // beginGroup()
  33. for (auto shortcut : UISettings::values.shortcuts) {
  34. QStringList cat = shortcut.first.split("/");
  35. Q_ASSERT(cat.size() >= 2);
  36. // RegisterHotkey assigns default keybindings, so use old values as default parameters
  37. Hotkey& hk = hotkey_groups[cat[0]][cat[1]];
  38. if (!shortcut.second.first.isEmpty()) {
  39. hk.keyseq = QKeySequence::fromString(shortcut.second.first);
  40. hk.context = (Qt::ShortcutContext)shortcut.second.second;
  41. }
  42. if (hk.shortcut)
  43. hk.shortcut->setKey(hk.keyseq);
  44. }
  45. }
  46. void RegisterHotkey(const QString& group, const QString& action, const QKeySequence& default_keyseq,
  47. Qt::ShortcutContext default_context) {
  48. if (hotkey_groups[group].find(action) == hotkey_groups[group].end()) {
  49. hotkey_groups[group][action].keyseq = default_keyseq;
  50. hotkey_groups[group][action].context = default_context;
  51. }
  52. }
  53. QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget) {
  54. Hotkey& hk = hotkey_groups[group][action];
  55. if (!hk.shortcut)
  56. hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
  57. return hk.shortcut;
  58. }
  59. GHotkeysDialog::GHotkeysDialog(QWidget* parent) : QWidget(parent) {
  60. ui.setupUi(this);
  61. for (auto group : hotkey_groups) {
  62. QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group.first));
  63. for (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. }