summaryrefslogtreecommitdiff
path: root/src/config.hpp
blob: 81da2dc12f0142dbcff2deb4e7d0ed543dc99713 (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
#ifndef GETSUYOMI_CONFIG_HPP
#define GETSUYOMI_CONFIG_HPP

#include <QtWidgets>
#include <fud_status.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);

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;

  private:
    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{};

    ShortcutMap shortcuts() const;
};

class ShortcutCollector : public QWidget {
  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 slots:
    void removeShortcut(int row, int);

  private:
    ActionType m_action;
    Shortcuts& m_shortcuts;
    QKeySequenceEdit* m_shortcutEditor{nullptr};
    QGridLayout* m_layout{nullptr};
    ShortcutList m_actionList{};
    QTableWidget* m_shortcutTable{nullptr};
};

class Settings : public QDialog {
  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