aboutsummaryrefslogtreecommitdiff
path: root/src/yuzu/hotkeys.h
blob: f38e6c0028908e48302481815a871d922f681dc3 (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
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <map>
#include "ui_hotkeys.h"

class QDialog;
class QKeySequence;
class QSettings;
class QShortcut;

class HotkeyRegistry final {
public:
    friend class GHotkeysDialog;

    explicit HotkeyRegistry();
    ~HotkeyRegistry();

    /**
     * Loads hotkeys from the settings file.
     *
     * @note Yet unregistered hotkeys which are present in the settings will automatically be
     *       registered.
     */
    void LoadHotkeys();

    /**
     * Saves all registered hotkeys to the settings file.
     *
     * @note Each hotkey group will be stored a settings group; For each hotkey inside that group, a
     *       settings group will be created to store the key sequence and the hotkey context.
     */
    void SaveHotkeys();

    /**
     * Returns a QShortcut object whose activated() signal can be connected to other QObjects'
     * slots.
     *
     * @param group  General group this hotkey belongs to (e.g. "Main Window", "Debugger").
     * @param action Name of the action (e.g. "Start Emulation", "Load Image").
     * @param widget Parent widget of the returned QShortcut.
     * @warning If multiple QWidgets' call this function for the same action, the returned QShortcut
     *          will be the same. Thus, you shouldn't rely on the caller really being the
     *          QShortcut's parent.
     */
    QShortcut* GetHotkey(const QString& group, const QString& action, QWidget* widget);

    /**
     * Register a hotkey.
     *
     * @param group General group this hotkey belongs to (e.g. "Main Window", "Debugger")
     * @param action Name of the action (e.g. "Start Emulation", "Load Image")
     * @param default_keyseq Default key sequence to assign if the hotkey wasn't present in the
     *                       settings file before
     * @param default_context Default context to assign if the hotkey wasn't present in the settings
     *                        file before
     * @warning Both the group and action strings will be displayed in the hotkey settings dialog
     */
    void RegisterHotkey(const QString& group, const QString& action,
                        const QKeySequence& default_keyseq = {},
                        Qt::ShortcutContext default_context = Qt::WindowShortcut);

private:
    struct Hotkey {
        QKeySequence keyseq;
        QShortcut* shortcut = nullptr;
        Qt::ShortcutContext context = Qt::WindowShortcut;
    };

    using HotkeyMap = std::map<QString, Hotkey>;
    using HotkeyGroupMap = std::map<QString, HotkeyMap>;

    HotkeyGroupMap hotkey_groups;
};

class GHotkeysDialog : public QWidget {
    Q_OBJECT

public:
    explicit GHotkeysDialog(QWidget* parent = nullptr);

    void Populate(const HotkeyRegistry& registry);

private:
    Ui::hotkeys ui;
};