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 #include "keyboard_shortcuts.hpp"
0008 #include "app/widgets/keyboard_settings_widget.hpp"
0009 
0010 void app::settings::ShortcutSettings::load(QSettings & settings)
0011 {
0012     for ( const auto& key : settings.childKeys() )
0013     {
0014         auto& action = actions[key];
0015         action.overwritten = true;
0016         action.shortcut = QKeySequence(settings.value(key).toString(), QKeySequence::PortableText);
0017     }
0018 }
0019 
0020 void app::settings::ShortcutSettings::save(QSettings& settings)
0021 {
0022     for ( const auto& p : actions )
0023     {
0024         if ( p.second.overwritten )
0025              settings.setValue(p.first, p.second.shortcut.toString(QKeySequence::PortableText));
0026         else
0027             settings.remove(p.first);
0028     }
0029 }
0030 
0031 void app::settings::ShortcutSettings::add_menu(QMenu* menu, const QString& prefix)
0032 {
0033     auto group = add_group(menu->menuAction()->iconText());
0034     for ( QAction* act : menu->actions() )
0035     {
0036         if ( !act->isSeparator() && !act->menu() && !act->objectName().isEmpty() )
0037             group->actions.push_back(
0038                 add_action(act, prefix)
0039             );
0040     }
0041 
0042     QObject::connect(menu->menuAction(), &QAction::changed, menu, [menu, group]{
0043         group->label = menu->menuAction()->iconText();
0044     });
0045 }
0046 
0047 app::settings::ShortcutGroup * app::settings::ShortcutSettings::add_group(const QString& label)
0048 {
0049     groups.push_back(ShortcutGroup{label, {}});
0050     return &groups.back();
0051 }
0052 
0053 app::settings::ShortcutAction * app::settings::ShortcutSettings::action(const QString& slug)
0054 {
0055     return &actions[slug];
0056 }
0057 
0058 app::settings::ShortcutAction * app::settings::ShortcutSettings::add_action(QAction* qaction, const QString& prefix)
0059 {
0060     Q_EMIT begin_actions_change();
0061     auto sca = action(prefix + qaction->objectName());
0062 
0063     sca->icon = qaction->icon();
0064     sca->label = qaction->iconText();
0065     sca->default_shortcut = qaction->shortcut();
0066     if ( sca->overwritten )
0067         qaction->setShortcut(sca->shortcut);
0068     else
0069         sca->shortcut = qaction->shortcut();
0070     sca->action = qaction;
0071 
0072     QObject::connect(qaction, &QAction::changed, qaction, [qaction, sca]{
0073         sca->icon = qaction->icon();
0074         sca->label = qaction->iconText();
0075     });
0076 
0077     Q_EMIT end_actions_change();
0078     return sca;
0079 }
0080 
0081 const QList<app::settings::ShortcutGroup> & app::settings::ShortcutSettings::get_groups() const
0082 {
0083     return groups;
0084 }
0085 
0086 QWidget * app::settings::ShortcutSettings::make_widget(QWidget* parent)
0087 {
0088     return new KeyboardSettingsWidget(this, parent);
0089 }
0090 
0091 const std::unordered_map<QString, app::settings::ShortcutAction> & app::settings::ShortcutSettings::get_actions() const
0092 {
0093     return actions;
0094 }
0095 
0096 const QKeySequence & app::settings::ShortcutSettings::get_shortcut(const QString& action_name) const
0097 {
0098     return actions.at(action_name).shortcut;
0099 }
0100 
0101 app::settings::ShortcutGroup* app::settings::ShortcutSettings::find_group(const QString& label)
0102 {
0103     for ( app::settings::ShortcutGroup& group : groups )
0104         if ( group.label == label )
0105             return &group;
0106     return {};
0107 }
0108 
0109 void app::settings::ShortcutSettings::remove_action(ShortcutAction* action)
0110 {
0111     Q_EMIT begin_actions_change();
0112     QString name;
0113     name = action->action->objectName();
0114     for ( app::settings::ShortcutGroup& group : groups )
0115     {
0116         std::vector<ShortcutAction*>::iterator it = std::find(group.actions.begin(), group.actions.end(), action);
0117         if(it != group.actions.end())
0118         {
0119            group.actions.erase(it);
0120            break;
0121         }
0122     }
0123     actions.erase(name);
0124     Q_EMIT end_actions_change();
0125 }