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 #pragma once
0008 
0009 #include "service.hpp"
0010 
0011 namespace glaxnimate::plugin {
0012 
0013 
0014 class ActionService;
0015 
0016 class PluginActionRegistry : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     static PluginActionRegistry& instance()
0022     {
0023         static PluginActionRegistry instance;
0024         return instance;
0025     }
0026 
0027     QAction* make_qaction(ActionService* action);
0028 
0029     void add_action(ActionService* action);
0030     void remove_action(ActionService* action);
0031 
0032     const std::vector<ActionService*>& enabled() const;
0033 
0034 Q_SIGNALS:
0035     void action_added(ActionService* action, ActionService* sibling_before);
0036     void action_removed(ActionService*);
0037 
0038 private:
0039     std::vector<ActionService*>::iterator find(ActionService* as);
0040     static bool compare(ActionService* a, ActionService* b);
0041 
0042     PluginActionRegistry() = default;
0043     ~PluginActionRegistry() = default;
0044     std::vector<ActionService*> enabled_actions;
0045 };
0046 
0047 class ActionService : public PluginService
0048 {
0049     Q_OBJECT
0050 
0051 public:
0052     ServiceType type() const override { return ServiceType::Action; }
0053     QString name() const override { return label; }
0054     void enable() override { PluginActionRegistry::instance().add_action(this); }
0055     void disable() override
0056     {
0057         PluginActionRegistry::instance().remove_action(this);
0058         Q_EMIT disabled();
0059     }
0060     QIcon service_icon() const override;
0061 
0062     QString label;
0063     QString tooltip;
0064     QString icon;
0065     PluginScript script;
0066 
0067 public Q_SLOTS:
0068     void trigger() const;
0069 
0070 Q_SIGNALS:
0071     void disabled();
0072 };
0073 
0074 
0075 } // namespace glaxnimate::plugin