File indexing completed on 2024-12-15 04:01:20

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 "action.hpp"
0008 #include "plugin.hpp"
0009 
0010 #include "app/settings/widget_builder.hpp"
0011 
0012 using namespace glaxnimate;
0013 
0014 const std::vector<plugin::ActionService *> & plugin::PluginActionRegistry::enabled() const
0015 {
0016     return enabled_actions;
0017 }
0018 
0019 
0020 QAction * plugin::PluginActionRegistry::make_qaction ( plugin::ActionService* action )
0021 {
0022     QAction* act = new QAction;
0023     act->setIcon(action->plugin()->make_icon(action->icon));
0024     if ( action->label.isEmpty() )
0025         act->setText(action->plugin()->data().name);
0026     else
0027         act->setText(action->label);
0028     act->setToolTip(action->tooltip);
0029     connect(act, &QAction::triggered, action, &ActionService::trigger);
0030     connect(action, &ActionService::disabled, act, &QAction::deleteLater);
0031     act->setData(QVariant::fromValue(action));
0032     act->setObjectName("action_plugin_" + action->plugin()->data().name.toLower() + "_" + action->label.toLower());
0033     return act;
0034 }
0035 
0036 void plugin::PluginActionRegistry::add_action ( plugin::ActionService* action )
0037 {
0038     auto it = find(action);
0039     if ( it != enabled_actions.end() && *it == action )
0040         return;
0041 
0042     ActionService* sibling_before = nullptr;
0043     if ( it != enabled_actions.end() )
0044         sibling_before = *it;
0045     enabled_actions.insert(it, action);
0046     Q_EMIT action_added(action, sibling_before);
0047 }
0048 
0049 void plugin::PluginActionRegistry::remove_action ( plugin::ActionService* action )
0050 {
0051     auto it = find(action);
0052     if ( it == enabled_actions.end() || *it != action )
0053         return;
0054 
0055     enabled_actions.erase(it);
0056     Q_EMIT action_removed(action);
0057 }
0058 
0059 bool plugin::PluginActionRegistry::compare(plugin::ActionService* a, plugin::ActionService* b)
0060 {
0061     if ( a->plugin()->data().id == b->plugin()->data().id )
0062     {
0063         if ( a->label == b->label )
0064             return a < b;
0065         return a->label < b->label;
0066     }
0067     return a->plugin()->data().id < b->plugin()->data().id;
0068 }
0069 
0070 std::vector<plugin::ActionService *>::iterator plugin::PluginActionRegistry::find(plugin::ActionService* as)
0071 {
0072     auto it = std::lower_bound(enabled_actions.begin(), enabled_actions.end(), as, &PluginActionRegistry::compare);
0073     return it;
0074 }
0075 
0076 QIcon plugin::ActionService::service_icon() const
0077 {
0078     return plugin()->make_icon(icon);
0079 }
0080 
0081 
0082 void plugin::ActionService::trigger() const
0083 {
0084     QVariantMap settings_value;
0085     if ( !script.settings.empty() )
0086     {
0087         if ( !app::settings::WidgetBuilder().show_dialog(
0088             script.settings, settings_value, plugin()->data().name
0089         ) )
0090             return;
0091     }
0092 
0093     plugin()->run_script(script, {
0094         PluginRegistry::instance().global_parameter("window"),
0095         PluginRegistry::instance().global_parameter("document"),
0096         settings_value
0097     });
0098 }
0099