summaryrefslogtreecommitdiffstats
path: root/src/ui/ShortcutRegistry.cpp
blob: 5c32b8cb6c5b7f079a90e04e629a18381a9c9570 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ShortcutRegistry.h"

ShortcutRegistry *ShortcutRegistry::s_instance = nullptr;

EditableShortcut::EditableShortcut(QObject *parent)
  : QObject{parent}
{
    ShortcutRegistry::instance()->registerShortcut(this);
}

EditableShortcut::EditableShortcut(const QString &name, const QString &description, QObject *parent)
  : QObject{parent}
  , m_name{name}
  , m_description{description}
{
    ShortcutRegistry::instance()->registerShortcut(this);
}

const QStringList
EditableShortcut::shortcuts() const
{
    QStringList dest;
    dest.resize(m_shortcuts.size());
    std::transform(m_shortcuts.begin(), m_shortcuts.end(), dest.begin(), [](const auto &shortcut) {
        return shortcut.toString();
    });
    return dest;
}

void
EditableShortcut::setName(const QString &name)
{
    if (name == m_name)
        return;
    m_name = name;
    emit nameChanged();
}

void
EditableShortcut::setDescription(const QString &description)
{
    if (description == m_description)
        return;
    m_description = description;
    emit descriptionChanged();
}

void
EditableShortcut::setShortcut(const QString &shortcut)
{
    setShortcuts({shortcut});
}

void
EditableShortcut::setShortcuts(const QStringList &shortcuts)
{
    QList<QKeySequence> temp;
    temp.resize(shortcuts.size());
    std::transform(shortcuts.begin(), shortcuts.end(), temp.begin(), [](const auto &shortcut) {
        return QKeySequence(shortcut);
    });

    if (temp == m_shortcuts)
        return;
    m_shortcuts = temp;
    emit shortcutsChanged();
}

ShortcutRegistry::ShortcutRegistry(QObject *parent)
  : QAbstractListModel{parent}
{
    if (s_instance)
        m_shortcuts = s_instance->m_shortcuts;

    s_instance = this;
}

ShortcutRegistry *
ShortcutRegistry::instance()
{
    return s_instance;
}

ShortcutRegistry *
ShortcutRegistry::create(QQmlEngine *qmlEngine, QJSEngine *)
{
    // The instance has to exist before it is used. We cannot replace it.
    Q_ASSERT(s_instance);

    // The engine has to have the same thread affinity as the singleton.
    Q_ASSERT(qmlEngine->thread() == s_instance->thread());

    // There can only be one engine accessing the singleton.
    static QJSEngine *s_engine = nullptr;
    if (s_engine)
        Q_ASSERT(qmlEngine == s_engine);
    else
        s_engine = qmlEngine;

    QJSEngine::setObjectOwnership(s_instance, QJSEngine::CppOwnership);
    return s_instance;
}

QHash<int, QByteArray>
ShortcutRegistry::roleNames() const
{
    return {
      {Roles::Name, "name"}, {Roles::Description, "description"}, {Roles::Shortcut, "shortcut"}};
}

QVariant
ShortcutRegistry::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_shortcuts.size() || index.row() < 0)
        return {};

    switch (role) {
    case Roles::Name:
        return m_shortcuts[index.row()]->name();
    case Roles::Description:
        return m_shortcuts[index.row()]->description();
    case Roles::Shortcut:
        return m_shortcuts[index.row()]->shortcut();
    default:
        return {};
    }
}

void
ShortcutRegistry::changeShortcut(const QString &name, const QString &newShortcut)
{
    for (int i = 0; i < m_shortcuts.size(); ++i) {
        if (m_shortcuts[i]->name() == name) {
            qDebug() << "new:" << newShortcut;
            m_shortcuts[i]->setShortcut(newShortcut);
            emit dataChanged(index(i), index(i), {Roles::Shortcut});
            return;
        }
    }
}

QString
ShortcutRegistry::keycodeToChar(int keycode) const
{
    return QString((char)keycode);
}

void
ShortcutRegistry::registerShortcut(EditableShortcut *action)
{
    beginInsertRows({}, m_shortcuts.size(), m_shortcuts.size());
    m_shortcuts.push_back(action);
    endInsertRows();
}