JFrame(增加 DockManager 的扩展实现并重构扩展实现和JBus)

增加 DockWidget 的配置文件

 

 重构 JExtensionManageInterface 并实现 DocManager

 1 #ifndef JEXTENSIONMANAGEINTERFACE_H
 2 #define JEXTENSIONMANAGEINTERFACE_H
 3 
 4 #include <QObject>
 5 #include <map>
 6 #include "jextensionglobal.h"
 7 
 8 class JExtensionManageInterface : public QObject
 9 {
10     Q_OBJECT
11 public:
12     explicit JExtensionManageInterface(QObject *parent = nullptr);
13 
14 protected:
15     void readExtension(QString _extensionType);
16     virtual void implExtension() = 0;
17 
18 protected:
19     // 扩展点、扩展实现信息存放map
20     std::map<ActionExtensionPoint, ActionExtensionImpl> m_extensionMap;
21 
22 private:
23     void _parseExtension();
24 };
25 
26 #endif // JEXTENSIONMANAGEINTERFACE_H
 1 #ifndef DOCKMANAGER_H
 2 #define DOCKMANAGER_H
 3 
 4 #include <QObject>
 5 #include "jextensionmanageinterface.h"
 6 
 7 class MainWindow;
 8 class DockManager : public JExtensionManageInterface
 9 {
10     Q_OBJECT
11 public:
12     explicit DockManager(QObject *parent = nullptr);
13     void initialize(MainWindow *_mainWindow);
14 
15 protected:
16     virtual void implExtension() override;
17 
18 public slots:
19 
20 private:
21 
22     MainWindow *m_mainWindow = nullptr;
23 };
24 
25 #endif // DOCKMANAGER_H

重构 JBus

 1 #ifndef JBUS_H
 2 #define JBUS_H
 3 
 4 #if defined(JBUS_LIBRARY)
 5 #  define JBUSSHARED_EXPORT Q_DECL_EXPORT
 6 #else
 7 #  define JBUSSHARED_EXPORT Q_DECL_IMPORT
 8 #endif
 9 #include "jbusservice.h"
10 
11 namespace JBus
12 {
13     void JBUSSHARED_EXPORT onResponse(std::shared_ptr<JEvent> event, void *_user);
14 
15     void JBUSSHARED_EXPORT Regist(void *_user, std::string _type);
16     void JBUSSHARED_EXPORT UnRegist(void *_user, std::string _type);
17     void JBUSSHARED_EXPORT Publish(std::shared_ptr<JEvent> _event);
18 
19     namespace JBusTopic
20     {
21         static std::string LOG_INFO = "LOG_INFO";
22     }
23 }
24 
25 #endif // JBUS_H
 1 #include "jbus.h"
 2 #include "IPluginInterface.h"
 3 
 4 void JBus::onResponse(std::shared_ptr<JEvent> event, void *_user)
 5 {
 6     static_cast<IPluginInterface *>(_user)->onDelMsg(event->getType(), event->getData());
 7 }
 8 
 9 void JBus::Regist(void *_user, std::string _type)
10 {
11     JBusService::getInstance()->Regist(JBus::onResponse, _user, _type);
12 }
13 
14 void JBus::Publish(std::shared_ptr<JEvent> _event)
15 {
16     JBusService::getInstance()->Publish(_event);
17 }
18 
19 void JBus::UnRegist(void *_user, std::string _type)
20 {
21     JBusService::getInstance()->UnRegist(JBus::onResponse, _user, _type);
22 }

经过改造的 JBus 使用统一的接口 JBus::onResponse 作为回调,这里运用多态,消息到来的时候会转发给具体的插件的具体的 onDelMsg!

 1 class QWidget;
 2 class IPluginInterface : public QObject
 3 {
 4     Q_OBJECT
 5 public:
 6     IPluginInterface(QObject *parent = nullptr)
 7         : QObject(parent){}
 8     virtual ~IPluginInterface(){}
 9 
10     // 插件初始化
11     virtual void initialize() = 0;
12 
13     // 获取插件的名字
14     virtual QString getPluginNmae() = 0;
15 
16     // 插件退出时候调用的函数
17     virtual int exit() = 0;
18 
19     virtual void onDelMsg(const std::string _eventType, const QVariant & _var) {};
20 
21     // 初始化插件的 widgets
22     virtual void initWidgets() {}
23 
24     QList<std::shared_ptr<QWidget>> m_widgetList;
25 };

如此,注册消息并处理消息变得非常简洁和优雅!

 

 

 

 

posted @ 2022-04-17 21:09  SSSnail  阅读(80)  评论(0)    收藏  举报