2015-01-04 23:06:57 +05:30
|
|
|
// Copyright 2014 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2014-04-01 07:56:50 +05:30
|
|
|
#include <QKeySequence>
|
|
|
|
#include <QSettings>
|
2015-01-04 05:21:14 +05:30
|
|
|
#include "hotkeys.h"
|
2014-04-01 07:56:50 +05:30
|
|
|
#include <map>
|
|
|
|
|
|
|
|
struct Hotkey
|
|
|
|
{
|
2014-12-04 00:27:57 +05:30
|
|
|
Hotkey() : shortcut(nullptr), context(Qt::WindowShortcut) {}
|
2014-04-01 07:56:50 +05:30
|
|
|
|
|
|
|
QKeySequence keyseq;
|
|
|
|
QShortcut* shortcut;
|
|
|
|
Qt::ShortcutContext context;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<QString, Hotkey> HotkeyMap;
|
|
|
|
typedef std::map<QString, HotkeyMap> HotkeyGroupMap;
|
|
|
|
|
|
|
|
HotkeyGroupMap hotkey_groups;
|
|
|
|
|
|
|
|
void SaveHotkeys(QSettings& settings)
|
|
|
|
{
|
|
|
|
settings.beginGroup("Shortcuts");
|
|
|
|
|
2014-08-12 06:14:59 +05:30
|
|
|
for (auto group : hotkey_groups)
|
2014-04-01 07:56:50 +05:30
|
|
|
{
|
2014-08-12 06:14:59 +05:30
|
|
|
settings.beginGroup(group.first);
|
|
|
|
for (auto hotkey : group.second)
|
2014-04-01 07:56:50 +05:30
|
|
|
{
|
2014-08-12 06:14:59 +05:30
|
|
|
settings.beginGroup(hotkey.first);
|
|
|
|
settings.setValue(QString("KeySeq"), hotkey.second.keyseq.toString());
|
|
|
|
settings.setValue(QString("Context"), hotkey.second.context);
|
2014-04-01 07:56:50 +05:30
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoadHotkeys(QSettings& settings)
|
|
|
|
{
|
|
|
|
settings.beginGroup("Shortcuts");
|
|
|
|
|
|
|
|
// Make sure NOT to use a reference here because it would become invalid once we call beginGroup()
|
|
|
|
QStringList groups = settings.childGroups();
|
2014-08-12 06:14:59 +05:30
|
|
|
for (auto group : groups)
|
2014-04-01 07:56:50 +05:30
|
|
|
{
|
2014-08-12 06:14:59 +05:30
|
|
|
settings.beginGroup(group);
|
2014-04-01 07:56:50 +05:30
|
|
|
|
|
|
|
QStringList hotkeys = settings.childGroups();
|
2014-08-12 06:14:59 +05:30
|
|
|
for (auto hotkey : hotkeys)
|
2014-04-01 07:56:50 +05:30
|
|
|
{
|
2014-08-12 06:14:59 +05:30
|
|
|
settings.beginGroup(hotkey);
|
2014-04-01 07:56:50 +05:30
|
|
|
|
|
|
|
// RegisterHotkey assigns default keybindings, so use old values as default parameters
|
2014-08-12 06:14:59 +05:30
|
|
|
Hotkey& hk = hotkey_groups[group][hotkey];
|
2014-04-01 07:56:50 +05:30
|
|
|
hk.keyseq = QKeySequence::fromString(settings.value("KeySeq", hk.keyseq.toString()).toString());
|
|
|
|
hk.context = (Qt::ShortcutContext)settings.value("Context", hk.context).toInt();
|
|
|
|
if (hk.shortcut)
|
|
|
|
hk.shortcut->setKey(hk.keyseq);
|
|
|
|
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
settings.endGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
hotkey_groups[group][action].keyseq = default_keyseq;
|
|
|
|
hotkey_groups[group][action].context = default_context;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget)
|
|
|
|
{
|
|
|
|
Hotkey& hk = hotkey_groups[group][action];
|
|
|
|
|
|
|
|
if (!hk.shortcut)
|
2014-12-04 00:27:57 +05:30
|
|
|
hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);
|
2014-04-01 07:56:50 +05:30
|
|
|
|
|
|
|
return hk.shortcut;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GHotkeysDialog::GHotkeysDialog(QWidget* parent): QDialog(parent)
|
|
|
|
{
|
|
|
|
ui.setupUi(this);
|
|
|
|
|
2014-08-12 06:14:59 +05:30
|
|
|
for (auto group : hotkey_groups)
|
2014-04-01 07:56:50 +05:30
|
|
|
{
|
2014-08-12 06:14:59 +05:30
|
|
|
QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group.first));
|
|
|
|
for (auto hotkey : group.second)
|
2014-04-01 07:56:50 +05:30
|
|
|
{
|
|
|
|
QStringList columns;
|
2014-08-12 06:14:59 +05:30
|
|
|
columns << hotkey.first << hotkey.second.keyseq.toString();
|
2014-04-01 07:56:50 +05:30
|
|
|
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);
|
|
|
|
}
|