Chatterino
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HotkeyController.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include "common/Singleton.hpp"
6 
7 #include <boost/optional.hpp>
8 #include <pajlada/signals/signal.hpp>
9 #include <pajlada/signals/signalholder.hpp>
10 
11 #include <optional>
12 #include <set>
13 
14 class QShortcut;
15 
16 namespace chatterino {
17 
18 class Hotkey;
19 
20 class HotkeyModel;
21 
22 class HotkeyController final : public Singleton
23 {
24 public:
25  using HotkeyFunction = std::function<QString(std::vector<QString>)>;
26  using HotkeyMap = std::map<QString, HotkeyFunction>;
27 
29  HotkeyModel *createModel(QObject *parent);
30 
31  std::vector<QShortcut *> shortcutsForCategory(HotkeyCategory category,
32  HotkeyMap actionMap,
33  QWidget *parent);
34 
35  void save() override;
36  std::shared_ptr<Hotkey> getHotkeyByName(QString name);
45  QKeySequence getDisplaySequence(
46  HotkeyCategory category, const QString &action,
47  const std::optional<std::vector<QString>> &arguments = {}) const;
48 
54  int replaceHotkey(QString oldName, std::shared_ptr<Hotkey> newHotkey);
55  boost::optional<HotkeyCategory> hotkeyCategoryFromName(
56  QString categoryName);
57 
66  [[nodiscard]] bool isDuplicate(std::shared_ptr<Hotkey> hotkey,
67  QString ignoreNamed);
68 
74  [[nodiscard]] QString categoryDisplayName(HotkeyCategory category) const;
75 
81  [[nodiscard]] QString categoryName(HotkeyCategory category) const;
82 
86  [[nodiscard]] const std::map<HotkeyCategory, HotkeyCategoryData>
87  &categories() const;
88 
89  pajlada::Signals::NoArgSignal onItemsUpdated;
90 
91 private:
95  void loadHotkeys();
96 
103  void saveHotkeys();
104 
110  void addDefaults(std::set<QString> &addedHotkeys);
111 
115  void resetToDefaults();
116 
120  void tryAddDefault(std::set<QString> &addedHotkeys, HotkeyCategory category,
121  QKeySequence keySequence, QString action,
122  std::vector<QString> args, QString name);
123 
127  static void showHotkeyError(const std::shared_ptr<Hotkey> &hotkey,
128  QString warning);
137  std::shared_ptr<Hotkey> findLike(
138  HotkeyCategory category, const QString &action,
139  const std::optional<std::vector<QString>> &arguments = {}) const;
140 
141  friend class KeyboardSettingsPage;
142 
144  pajlada::Signals::SignalHolder signalHolder_;
145 
146  const std::map<HotkeyCategory, HotkeyCategoryData> hotkeyCategories_ = {
147  {HotkeyCategory::PopupWindow, {"popupWindow", "Popup Windows"}},
148  {HotkeyCategory::Split, {"split", "Split"}},
149  {HotkeyCategory::SplitInput, {"splitInput", "Split input box"}},
150  {HotkeyCategory::Window, {"window", "Window"}},
151  };
152 };
153 
154 } // namespace chatterino
pajlada::Signals::NoArgSignal onItemsUpdated
Definition: HotkeyController.hpp:89
Definition: SignalVector.hpp:21
int replaceHotkey(QString oldName, std::shared_ptr< Hotkey > newHotkey)
removes the hotkey with the oldName and inserts newHotkey at the end
Definition: HotkeyController.cpp:113
void save() override
Definition: HotkeyController.cpp:96
Definition: Singleton.hpp:10
HotkeyController()
Definition: HotkeyController.cpp:23
Definition: HotkeyModel.hpp:13
QString categoryName(HotkeyCategory category) const
Returns the name of the given hotkey category.
Definition: HotkeyController.cpp:177
Definition: Application.cpp:48
QKeySequence getDisplaySequence(HotkeyCategory category, const QString &action, const std::optional< std::vector< QString >> &arguments={}) const
returns a QKeySequence that perfoms the actions requested. Accepted if and only if the category match...
Definition: HotkeyController.cpp:551
QString categoryDisplayName(HotkeyCategory category) const
Returns the display name of the given hotkey category.
Definition: HotkeyController.cpp:163
std::map< QString, HotkeyFunction > HotkeyMap
Definition: HotkeyController.hpp:26
std::shared_ptr< Hotkey > getHotkeyByName(QString name)
Definition: HotkeyController.cpp:101
Definition: KeyboardSettingsPage.hpp:10
Definition: HotkeyController.hpp:22
HotkeyModel * createModel(QObject *parent)
Definition: HotkeyController.cpp:34
QString name
Definition: Credentials.cpp:94
bool isDuplicate(std::shared_ptr< Hotkey > hotkey, QString ignoreNamed)
checks if the hotkey is duplicate
Definition: HotkeyController.cpp:143
std::function< QString(std::vector< QString >)> HotkeyFunction
Definition: HotkeyController.hpp:25
const std::map< HotkeyCategory, HotkeyCategoryData > & categories() const
Definition: HotkeyController.cpp:192
std::vector< QShortcut * > shortcutsForCategory(HotkeyCategory category, HotkeyMap actionMap, QWidget *parent)
Definition: HotkeyController.cpp:41
boost::optional< HotkeyCategory > hotkeyCategoryFromName(QString categoryName)
Definition: HotkeyController.cpp:129
HotkeyCategory
Definition: HotkeyCategory.hpp:10