diff options
Diffstat (limited to 'src/citra_qt/hotkeys.cpp')
| -rw-r--r-- | src/citra_qt/hotkeys.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/citra_qt/hotkeys.cpp b/src/citra_qt/hotkeys.cpp new file mode 100644 index 000000000..1aa1e8b96 --- /dev/null +++ b/src/citra_qt/hotkeys.cpp | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | #include <QKeySequence> | ||
| 2 | #include <QSettings> | ||
| 3 | #include "hotkeys.hxx" | ||
| 4 | #include <map> | ||
| 5 | |||
| 6 | struct Hotkey | ||
| 7 | { | ||
| 8 | Hotkey() : shortcut(NULL), context(Qt::WindowShortcut) {} | ||
| 9 | |||
| 10 | QKeySequence keyseq; | ||
| 11 | QShortcut* shortcut; | ||
| 12 | Qt::ShortcutContext context; | ||
| 13 | }; | ||
| 14 | |||
| 15 | typedef std::map<QString, Hotkey> HotkeyMap; | ||
| 16 | typedef std::map<QString, HotkeyMap> HotkeyGroupMap; | ||
| 17 | |||
| 18 | HotkeyGroupMap hotkey_groups; | ||
| 19 | |||
| 20 | void SaveHotkeys(QSettings& settings) | ||
| 21 | { | ||
| 22 | settings.beginGroup("Shortcuts"); | ||
| 23 | |||
| 24 | for (HotkeyGroupMap::iterator group = hotkey_groups.begin(); group != hotkey_groups.end(); ++group) | ||
| 25 | { | ||
| 26 | settings.beginGroup(group->first); | ||
| 27 | for (HotkeyMap::iterator hotkey = group->second.begin(); hotkey != group->second.end(); ++hotkey) | ||
| 28 | { | ||
| 29 | settings.beginGroup(hotkey->first); | ||
| 30 | settings.setValue(QString("KeySeq"), hotkey->second.keyseq.toString()); | ||
| 31 | settings.setValue(QString("Context"), hotkey->second.context); | ||
| 32 | settings.endGroup(); | ||
| 33 | } | ||
| 34 | settings.endGroup(); | ||
| 35 | } | ||
| 36 | settings.endGroup(); | ||
| 37 | } | ||
| 38 | |||
| 39 | void LoadHotkeys(QSettings& settings) | ||
| 40 | { | ||
| 41 | settings.beginGroup("Shortcuts"); | ||
| 42 | |||
| 43 | // Make sure NOT to use a reference here because it would become invalid once we call beginGroup() | ||
| 44 | QStringList groups = settings.childGroups(); | ||
| 45 | for (QList<QString>::iterator group = groups.begin(); group != groups.end(); ++group) | ||
| 46 | { | ||
| 47 | settings.beginGroup(*group); | ||
| 48 | |||
| 49 | QStringList hotkeys = settings.childGroups(); | ||
| 50 | for (QList<QString>::iterator hotkey = hotkeys.begin(); hotkey != hotkeys.end(); ++hotkey) | ||
| 51 | { | ||
| 52 | settings.beginGroup(*hotkey); | ||
| 53 | |||
| 54 | // RegisterHotkey assigns default keybindings, so use old values as default parameters | ||
| 55 | Hotkey& hk = hotkey_groups[*group][*hotkey]; | ||
| 56 | hk.keyseq = QKeySequence::fromString(settings.value("KeySeq", hk.keyseq.toString()).toString()); | ||
| 57 | hk.context = (Qt::ShortcutContext)settings.value("Context", hk.context).toInt(); | ||
| 58 | if (hk.shortcut) | ||
| 59 | hk.shortcut->setKey(hk.keyseq); | ||
| 60 | |||
| 61 | settings.endGroup(); | ||
| 62 | } | ||
| 63 | |||
| 64 | settings.endGroup(); | ||
| 65 | } | ||
| 66 | |||
| 67 | settings.endGroup(); | ||
| 68 | } | ||
| 69 | |||
| 70 | void RegisterHotkey(const QString& group, const QString& action, const QKeySequence& default_keyseq, Qt::ShortcutContext default_context) | ||
| 71 | { | ||
| 72 | if (hotkey_groups[group].find(action) == hotkey_groups[group].end()) | ||
| 73 | { | ||
| 74 | hotkey_groups[group][action].keyseq = default_keyseq; | ||
| 75 | hotkey_groups[group][action].context = default_context; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget) | ||
| 80 | { | ||
| 81 | Hotkey& hk = hotkey_groups[group][action]; | ||
| 82 | |||
| 83 | if (!hk.shortcut) | ||
| 84 | hk.shortcut = new QShortcut(hk.keyseq, widget, NULL, NULL, hk.context); | ||
| 85 | |||
| 86 | return hk.shortcut; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | GHotkeysDialog::GHotkeysDialog(QWidget* parent): QDialog(parent) | ||
| 91 | { | ||
| 92 | ui.setupUi(this); | ||
| 93 | |||
| 94 | for (HotkeyGroupMap::iterator group = hotkey_groups.begin(); group != hotkey_groups.end(); ++group) | ||
| 95 | { | ||
| 96 | QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group->first)); | ||
| 97 | for (HotkeyMap::iterator hotkey = group->second.begin(); hotkey != group->second.end(); ++hotkey) | ||
| 98 | { | ||
| 99 | QStringList columns; | ||
| 100 | columns << hotkey->first << hotkey->second.keyseq.toString(); | ||
| 101 | QTreeWidgetItem* item = new QTreeWidgetItem(columns); | ||
| 102 | toplevel_item->addChild(item); | ||
| 103 | } | ||
| 104 | ui.treeWidget->addTopLevelItem(toplevel_item); | ||
| 105 | } | ||
| 106 | // TODO: Make context configurable as well (hiding the column for now) | ||
| 107 | ui.treeWidget->setColumnCount(2); | ||
| 108 | |||
| 109 | ui.treeWidget->resizeColumnToContents(0); | ||
| 110 | ui.treeWidget->resizeColumnToContents(1); | ||
| 111 | } | ||