2015-01-04 09:36:57 -08:00
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-06-21 06:58:59 -07:00
|
|
|
#include <map>
|
|
|
|
|
2014-03-31 19:26:50 -07:00
|
|
|
#include <QKeySequence>
|
2015-06-21 06:58:59 -07:00
|
|
|
#include <QShortcut>
|
2016-09-17 17:38:01 -07:00
|
|
|
#include <QtGlobal>
|
2015-06-21 06:58:59 -07:00
|
|
|
|
2015-09-10 21:23:00 -07:00
|
|
|
#include "citra_qt/hotkeys.h"
|
2016-01-24 12:23:55 -08:00
|
|
|
#include "citra_qt/ui_settings.h"
|
2014-03-31 19:26:50 -07:00
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
struct Hotkey {
|
2016-09-18 18:01:46 -07:00
|
|
|
Hotkey() : shortcut(nullptr), context(Qt::WindowShortcut) {}
|
2014-03-31 19:26:50 -07:00
|
|
|
|
|
|
|
QKeySequence keyseq;
|
|
|
|
QShortcut* shortcut;
|
|
|
|
Qt::ShortcutContext context;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<QString, Hotkey> HotkeyMap;
|
|
|
|
typedef std::map<QString, HotkeyMap> HotkeyGroupMap;
|
|
|
|
|
|
|
|
HotkeyGroupMap hotkey_groups;
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
void SaveHotkeys() {
|
2016-01-24 12:23:55 -08:00
|
|
|
UISettings::values.shortcuts.clear();
|
2016-09-17 17:38:01 -07:00
|
|
|
for (auto group : hotkey_groups) {
|
|
|
|
for (auto hotkey : group.second) {
|
2016-01-24 12:54:04 -08:00
|
|
|
UISettings::values.shortcuts.emplace_back(
|
2016-09-17 17:38:01 -07:00
|
|
|
UISettings::Shortcut(group.first + "/" + hotkey.first,
|
|
|
|
UISettings::ContextualShortcut(hotkey.second.keyseq.toString(),
|
|
|
|
hotkey.second.context)));
|
2014-03-31 19:26:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
void LoadHotkeys() {
|
|
|
|
// Make sure NOT to use a reference here because it would become invalid once we call
|
|
|
|
// beginGroup()
|
|
|
|
for (auto shortcut : UISettings::values.shortcuts) {
|
2016-01-24 12:23:55 -08:00
|
|
|
QStringList cat = shortcut.first.split("/");
|
|
|
|
Q_ASSERT(cat.size() >= 2);
|
2014-03-31 19:26:50 -07:00
|
|
|
|
2016-01-24 12:23:55 -08:00
|
|
|
// RegisterHotkey assigns default keybindings, so use old values as default parameters
|
|
|
|
Hotkey& hk = hotkey_groups[cat[0]][cat[1]];
|
2016-09-17 17:38:01 -07:00
|
|
|
if (!shortcut.second.first.isEmpty()) {
|
2016-01-24 12:23:55 -08:00
|
|
|
hk.keyseq = QKeySequence::fromString(shortcut.second.first);
|
|
|
|
hk.context = (Qt::ShortcutContext)shortcut.second.second;
|
2014-03-31 19:26:50 -07:00
|
|
|
}
|
2016-01-24 12:23:55 -08:00
|
|
|
if (hk.shortcut)
|
|
|
|
hk.shortcut->setKey(hk.keyseq);
|
2014-03-31 19:26:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
void RegisterHotkey(const QString& group, const QString& action, const QKeySequence& default_keyseq,
|
|
|
|
Qt::ShortcutContext default_context) {
|
|
|
|
if (hotkey_groups[group].find(action) == hotkey_groups[group].end()) {
|
2014-03-31 19:26:50 -07:00
|
|
|
hotkey_groups[group][action].keyseq = default_keyseq;
|
|
|
|
hotkey_groups[group][action].context = default_context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget) {
|
2014-03-31 19:26:50 -07:00
|
|
|
Hotkey& hk = hotkey_groups[group][action];
|
|
|
|
|
|
|
|
if (!hk.shortcut)
|
2014-12-03 10:57:57 -08:00
|
|
|
hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
|
2014-03-31 19:26:50 -07:00
|
|
|
|
|
|
|
return hk.shortcut;
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
GHotkeysDialog::GHotkeysDialog(QWidget* parent) : QWidget(parent) {
|
2014-03-31 19:26:50 -07:00
|
|
|
ui.setupUi(this);
|
|
|
|
|
2016-09-17 17:38:01 -07:00
|
|
|
for (auto group : hotkey_groups) {
|
2014-08-11 17:44:59 -07:00
|
|
|
QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group.first));
|
2016-09-17 17:38:01 -07:00
|
|
|
for (auto hotkey : group.second) {
|
2014-03-31 19:26:50 -07:00
|
|
|
QStringList columns;
|
2014-08-11 17:44:59 -07:00
|
|
|
columns << hotkey.first << hotkey.second.keyseq.toString();
|
2014-03-31 19:26:50 -07:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(columns);
|
|
|
|
toplevel_item->addChild(item);
|
|
|
|
}
|
|
|
|
ui.treeWidget->addTopLevelItem(toplevel_item);
|
|
|
|
}
|
|
|
|
// TODO: Make context configurable as well (hiding the column for now)
|
|
|
|
ui.treeWidget->setColumnCount(2);
|
|
|
|
|
|
|
|
ui.treeWidget->resizeColumnToContents(0);
|
|
|
|
ui.treeWidget->resizeColumnToContents(1);
|
|
|
|
}
|