summaryrefslogtreecommitdiff
path: root/src/settings.cpp
blob: abd53c11e22d62962360e8370d89690adf81a26b (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
159
160
161
162
163
164
165
166
167
#include "settings.hpp"

namespace getsuyomi {

using fud::FudStatus;

Settings::Settings(QWidget* parent, Shortcuts&& shortcuts) : QDialog{parent}, m_shortcuts{std::move(shortcuts)}
{
    auto* layout = new QVBoxLayout();
    setWindowTitle("getsuyomi settings");

    if (!m_shortcuts.valid()) {
        return;
    }

    auto containerLayout = new QHBoxLayout();
    auto* columnLayout = new QVBoxLayout();
    size_t counter{0};
    constexpr size_t maxEntriesPerColumn{4};

    for (const auto& action : m_shortcuts.actions()) {
        auto* collector = new ShortcutCollector(this, action, m_shortcuts);
        columnLayout->addWidget(collector);
        counter++;
        if (counter % (maxEntriesPerColumn + 1) == 0) {
            containerLayout->addLayout(columnLayout);
            columnLayout = new QVBoxLayout();
        }
    }

    if (columnLayout->count() > 0) {
        containerLayout->addLayout(columnLayout);
    }

    layout->addLayout(containerLayout);

    auto* dialogButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(dialogButtonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(dialogButtonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    layout->addWidget(dialogButtonBox);

    setLayout(layout);
}

bool Settings::valid() const
{
    return m_shortcuts.valid();
}

const Shortcuts& Settings::shortcuts() const
{
    return m_shortcuts;
}

ShortcutCollector::ShortcutCollector(QWidget* parent, ActionType action, Shortcuts& shortcuts) :
    QWidget(parent), m_action{action}, m_shortcuts{shortcuts}, m_bindings{}
{
    m_layout = new QVBoxLayout();
    auto headerLayout = new QHBoxLayout();
    auto* name = new QLabel(actionTypeToString(m_action));
    headerLayout->addWidget(name);
    m_shortcutEditor = new QKeySequenceEdit(this);
    m_shortcutEditor->setMaximumSequenceLength(1);
    connect(m_shortcutEditor, &QKeySequenceEdit::editingFinished, this, &ShortcutCollector::checkBinding);
    headerLayout->addWidget(m_shortcutEditor);

    m_acceptButton = new QPushButton("Accept", this);
    m_acceptButton->setEnabled(false);
    connect(m_acceptButton, &QPushButton::clicked, this, &ShortcutCollector::addBinding);
    headerLayout->addWidget(m_acceptButton);
    headerLayout->addStretch();

    m_layout->addLayout(headerLayout);

    auto shortcutOptions = m_shortcuts.shortcuts(m_action);
    if (shortcutOptions != std::nullopt) {
        for (const auto& binding : *shortcutOptions) {
            createBinding(binding);
        }
    } else {
        qWarning("No shortcuts found for %s", actionTypeToString(action));
    }

    setLayout(m_layout);
}

void ShortcutCollector::createBinding(QKeySequence binding)
{
    auto displayItem = new ShortcutDisplay(this, binding);
    m_bindings[binding] = displayItem;
    m_layout->addWidget(displayItem);
    connect(displayItem, &ShortcutDisplay::removeClicked, this, &ShortcutCollector::removeBinding);
}

void ShortcutCollector::checkBinding()
{
    auto keySequence = m_shortcutEditor->keySequence();
    if (keySequence == QKeySequence::UnknownKey) {
        m_acceptButton->setEnabled(false);
        return;
    }

    m_acceptButton->setEnabled(not m_shortcuts.contains(keySequence));
}

void ShortcutCollector::addBinding()
{
    auto keySequence = m_shortcutEditor->keySequence();
    if (keySequence == QKeySequence::UnknownKey) {
        qWarning("Invalid state - can't accept unknown key");
        return;
    } else if (m_shortcuts.contains(keySequence)) {
        qWarning("Shortcut %s already bound", qPrintable(keySequence.toString()));
        return;
    }
    auto result = m_shortcuts.bind(m_action, keySequence);
    if (result != FudStatus::Success) {
        qCritical("Error binding %s to action %s", qPrintable(keySequence.toString()), actionTypeToString(m_action));
        return;
    }
    if (m_bindings.contains(keySequence)) {
        qWarning(
            "binding %s to action %s already exists",
            qPrintable(keySequence.toString()),
            actionTypeToString(m_action));
        m_layout->removeWidget(m_bindings[keySequence]);
        delete m_bindings[keySequence];
    }

    createBinding(keySequence);
    m_shortcutEditor->clear();
}

void ShortcutCollector::removeBinding(QKeySequence binding)
{
    auto result = m_shortcuts.remove(binding);
    if (result == FudStatus::NotFound) {
        qWarning("binding %s not found", qPrintable(binding.toString()));
    } else if (result != FudStatus::Success) {
        qWarning("error removing binding %s: %s", qPrintable(binding.toString()), FudStatusToString(result));
    }
    auto bindingHandle = m_bindings.extract(binding);
    if (bindingHandle) {
        m_layout->removeWidget(bindingHandle.mapped());
        delete bindingHandle.mapped();
    } else {
        qWarning("Could not remove widget!");
    }
    checkBinding();
}

ShortcutDisplay::ShortcutDisplay(QWidget* parent, QKeySequence shortcut) : QWidget{parent}, m_binding{shortcut}
{
    auto layout = new QHBoxLayout();
    layout->addWidget(new QLabel(shortcut.toString()));
    auto* deleteButton = new QPushButton("Delete", this);
    connect(deleteButton, &QPushButton::clicked, this, &ShortcutDisplay::removeOnClicked);
    layout->addWidget(deleteButton);
    setLayout(layout);
}

void ShortcutDisplay::removeOnClicked()
{
    emit removeClicked(m_binding);
}

}