summaryrefslogtreecommitdiffstats
path: root/src/citra_qt/hotkeys.cpp
blob: 08be7ff74eee95036d42dc9c75cd5112f76b4d4a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <QKeySequence>
#include <QSettings>
#include "hotkeys.hxx"
#include <map>

struct Hotkey
{
    Hotkey() : shortcut(nullptr), context(Qt::WindowShortcut) {}

    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");

    for (auto group : hotkey_groups)
    {
        settings.beginGroup(group.first);
        for (auto hotkey : group.second)
        {
            settings.beginGroup(hotkey.first);
            settings.setValue(QString("KeySeq"), hotkey.second.keyseq.toString());
            settings.setValue(QString("Context"), hotkey.second.context);
            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();
    for (auto group : groups)
    {
        settings.beginGroup(group);

        QStringList hotkeys = settings.childGroups();
        for (auto hotkey : hotkeys)
        {
            settings.beginGroup(hotkey);

            // RegisterHotkey assigns default keybindings, so use old values as default parameters
            Hotkey& hk = hotkey_groups[group][hotkey];
            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)
        hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context);

    return hk.shortcut;
}


GHotkeysDialog::GHotkeysDialog(QWidget* parent): QDialog(parent)
{
    ui.setupUi(this);

    for (auto group : hotkey_groups)
    {
        QTreeWidgetItem* toplevel_item = new QTreeWidgetItem(QStringList(group.first));
        for (auto hotkey : group.second)
        {
            QStringList columns;
            columns << hotkey.first << hotkey.second.keyseq.toString();
            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);
}