summaryrefslogtreecommitdiff
path: root/src/config.hpp
blob: 5937a9ba2eea6b3e166302522b17823cb90a75e7 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#ifndef GETSUYOMI_CONFIG_HPP
#define GETSUYOMI_CONFIG_HPP

#include <QtWidgets>
#include <fud_status.hpp>
#include <fud_result.hpp>
#include <map>
#include <optional>
#include <qkeysequence.h>
#include <qlist.h>
#include <set>
#include <string>

namespace getsuyomi {

enum class ActionType
{
    OpenFile,
    OpenDirectory,
    Quit,
    Configure,
    // Help,
    // About
    // GotoFirst,
    Next,
    Back,
    // GotoLast,
    SinglePage,
    DualPage,
    MangaPage
};

constexpr const char* actionTypeToString(ActionType action)
{
    switch (action) {
    case ActionType::OpenFile:
        return "OpenFile";
    case ActionType::OpenDirectory:
        return "OpenDirectory";
    case ActionType::Quit:
        return "Quit";
    case ActionType::Configure:
        return "Configure";
        // case ActionType::Help:
        return "Help";
        // case ActionType::About:
        // return "About";
        // case ActionType::GotoFirst:
        // return "GotoFirst";
    case ActionType::Next:
        return "Next";
    case ActionType::Back:
        return "Back";
        // case ActionType::GotoLast:
        // return "GotoLast";
    case ActionType::SinglePage:
        return "SinglePage";
    case ActionType::DualPage:
        return "DualPage";
    case ActionType::MangaPage:
        return "MangaPage";
    default:
        return "Unknown";
    }
}

using ShortcutList = QList<QKeySequence>;
using ShortcutSet = std::set<QKeySequence>;
using ShortcutMap = std::map<ActionType, ShortcutSet>;
using ShortcutRevMap = std::map<QKeySequence, ActionType>;

ShortcutSet shortcutSetFromList(const ShortcutList& shortcutList);
ShortcutList shortcutListFromSet(const ShortcutSet& shortcutList);

class Shortcuts {
  public:
    Shortcuts(ShortcutMap&& shortcutMap);

    [[nodiscard]] bool contains(QKeySequence keySequence) const;
    [[nodiscard]] bool contains(ActionType action) const;

    std::vector<ActionType> actions() const;

    std::optional<ActionType> action(QKeySequence keySequence) const;

    std::optional<ShortcutList> shortcuts(ActionType action) const;

    fud::FudStatus bind(ActionType action, QKeySequence keySequence);
    fud::FudStatus remove(QKeySequence keySequence);
    fud::FudStatus clear(ActionType action);

    [[nodiscard]] bool valid() const;

    const ShortcutMap& shortcutMap() const;

    static ShortcutMap fromUserConfig(const std::filesystem::path& configFileName);

  private:
    Shortcuts() = default;

    static Shortcuts fromLuaConfig(const std::filesystem::path& configFileName);

    bool m_valid{false};

    ShortcutMap m_actionToShortcuts{};
    ShortcutSet m_shortcuts{};
    ShortcutRevMap m_shortcutToAction{};
};

struct GetsuyomiConfig {
    std::string home{};
    std::string dataHome{};
    std::string configHome{};
    std::string stateHome{};

    ShortcutList openFileShortcuts{};
    ShortcutList openDirectoryShortcuts{};
    ShortcutList quitShortcuts{};

    ShortcutList settingsShortcuts{};

    ShortcutList nextShortcuts{};
    ShortcutList backShortcuts{};

    ShortcutList singlePageShortcuts{};
    ShortcutList dualPageShortcuts{};
    ShortcutList mangaPageShortcuts{};

    ShortcutMap shortcuts() const;
};

class ShortcutDisplay : public QWidget {
    Q_OBJECT
public:
    ShortcutDisplay(QWidget* parent, QKeySequence shortcut);

signals:
    void removeClicked(QKeySequence shortcut);
private:
    QKeySequence m_binding;
private slots:
    void removeOnClicked();
};

constexpr auto foo = sizeof(QKeySequence);

class ShortcutCollector : public QWidget {
    Q_OBJECT
  public:
    ShortcutCollector(QWidget* parent, ActionType action, Shortcuts& shortcuts);
    ~ShortcutCollector() = default;
    ShortcutCollector(const ShortcutCollector&) = delete;
    ShortcutCollector(ShortcutCollector&&) = delete;
    ShortcutCollector& operator=(const ShortcutCollector&) = delete;
    ShortcutCollector& operator=(ShortcutCollector&&) = delete;

private:
    void createBinding(QKeySequence binding);

  private slots:
    void checkBinding();
    void addBinding();
    void removeBinding(QKeySequence shortcut);

  private:
    ActionType m_action;
    Shortcuts& m_shortcuts;
    std::map<QKeySequence, ShortcutDisplay*> m_bindings;
    QKeySequenceEdit* m_shortcutEditor{nullptr};
    QPushButton* m_acceptButton{nullptr};
    QVBoxLayout* m_layout{nullptr};
};

class Settings : public QDialog {
    Q_OBJECT
  public:
    Settings(QWidget* parent, Shortcuts&& shortcuts);
    Settings(const Settings&) = delete;
    Settings(Settings&& rhs) = delete;
    ~Settings() = default;
    Settings& operator=(const Settings&) = delete;
    Settings& operator=(Settings&& rhs) = delete;

    [[nodiscard]] bool valid() const;

    const Shortcuts& shortcuts() const;

  private:
    Shortcuts m_shortcuts;
};

} // namespace getsuyomi

#endif