File indexing completed on 2024-11-24 03:56:27

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <vector>
0010 #include <unordered_map>
0011 
0012 #include <QKeySequence>
0013 #include <QAction>
0014 #include <QMenu>
0015 #include <QPointer>
0016 
0017 #include "app/utils/qstring_hash.hpp"
0018 #include "custom_settings_group.hpp"
0019 
0020 namespace app::settings {
0021 
0022 struct ShortcutAction
0023 {
0024     QIcon icon;
0025     QString label;
0026     QKeySequence shortcut;
0027     QKeySequence default_shortcut;
0028     bool overwritten = false;
0029     QPointer<QAction> action;
0030 };
0031 
0032 struct ShortcutGroup
0033 {
0034     QString label;
0035     std::vector<ShortcutAction*> actions;
0036 };
0037 
0038 
0039 class ShortcutSettings : public QObject, public CustomSettingsGroupBase
0040 {
0041     Q_OBJECT
0042 
0043 public:
0044     QString slug() const override { return "shortcuts"; }
0045     QString label() const override { return QObject::tr("Keyboard Shortcuts"); }
0046     QIcon icon() const override { return QIcon::fromTheme("input-keyboard"); }
0047     QWidget * make_widget(QWidget * parent) override;
0048 
0049     void load(QSettings & settings) override;
0050     void save(QSettings & settings) override;
0051 
0052     ShortcutGroup* add_group(const QString& label);
0053     void add_menu(QMenu* menu, const QString& prefix = {});
0054     ShortcutAction* action(const QString& slug);
0055     ShortcutAction* add_action(QAction* action, const QString& prefix = {});
0056 
0057     const QList<ShortcutGroup>& get_groups() const;
0058     const std::unordered_map<QString, ShortcutAction>& get_actions() const;
0059     const QKeySequence& get_shortcut(const QString& action_name) const;
0060 
0061     ShortcutGroup* find_group(const QString& label);
0062     void remove_action(ShortcutAction* action);
0063 
0064 Q_SIGNALS:
0065     void begin_actions_change();
0066     void end_actions_change();
0067 private:
0068     QList<ShortcutGroup> groups;
0069     std::unordered_map<QString, ShortcutAction> actions;
0070 };
0071 
0072 
0073 } // namespace app::settings