首先看看配置文件的改动:
{
"Extension": [
{
"ExtensionPoint": {
"pointName": "Logger",
"pointIcon": "./images/plane.png"
},
"ExtensionImpl": {
"pluginName": "JLogger",
"widgetName": "LoggerPanel"
}
},
{
"ExtensionPoint": {
"pointName": "Test",
"pointIcon": "./images/config.png"
},
"ExtensionImpl": {
"pluginName": "TestPlugin",
"widgetName": "Widget"
}
}
]
}
配置文件中,加入了 widgetName ,这样的话具体到了一个扩展点的实现是由具体的一个插件里面的widget!
同样很大的改动是扩展实现的逻辑是由 ActionManager 来实现,而不是由负责扩展点实现的插件来负担!
1 void ActionManager::slotTriggered(QString _aName)
2 {
3 auto _it = m_actionExtensionMap.begin();
4 for(;_it!= m_actionExtensionMap.end(); _it++)
5 {
6 if(_it->first.pointName == _aName)
7 {
8 break;
9 }
10 }
11
12 std::shared_ptr<QWidget> _widget;
13 if(_it == m_actionExtensionMap.end())
14 {
15 return;
16 }
17 _widget = _it->second.widget;
18
19 if(_widget.get())
20 {
21 if(m_mainWindow->centralWidget() != _widget.get())
22 {
23 JLogger::INFO(QString("[ %1 ] 窗体被加入到 centeralWidget!").arg(_widget->objectName()));
24 m_mainWindow->takeCentralWidget();
25 m_mainWindow->setCentralWidget(_widget.get());
26 }
27 }
28 }
ActionManager.h
1 #ifndef ACTIONMANAGER_H
2 #define ACTIONMANAGER_H
3
4 #include <QObject>
5 #include <QList>
6 #include <QToolBar>
7 #include <map>
8
9 #include "../JCore/jcore.h"
10 #include "jaction.h"
11 #include "IPluginInterface.h"
12 #include "jlogger.h"
13
14 class MainWindow;
15
16 // 扩展点结构体
17 struct ActionExtensionPoint
18 {
19 ActionExtensionPoint()
20 {
21 pointName = "";
22 pointIcon = "";
23 }
24 QString pointName;
25 QString pointIcon;
26 bool operator< (const ActionExtensionPoint &_b) const
27 {
28 return pointName < _b.pointName;
29 }
30 bool operator== (const ActionExtensionPoint &_b) const
31 {
32 return pointName == _b.pointName;
33 }
34 };
35
36 // 扩展实现结构体
37 struct ActionExtensionImpl
38 {
39 ActionExtensionImpl()
40 {
41 pluginName = "";
42 widgetName = "";
43 }
44 QString pluginName;
45 QString widgetName;
46
47 std::shared_ptr<IPluginInterface> plugin;
48 std::shared_ptr<QWidget> widget;
49 };
50
51 class ActionManager : public QObject
52 {
53 Q_OBJECT
54 public:
55 explicit ActionManager(QObject *parent = nullptr);
56
57 void initialize(MainWindow *_mainWindow, QToolBar *_mainToolBar);
58
59 private:
60 void parseExtension();
61 void constructActions();
62
63 private slots:
64 void slotTriggered(QString _aName);
65
66 private:
67
68 // 扩展点、扩展实现信息存放map
69 std::map<ActionExtensionPoint, ActionExtensionImpl> m_actionExtensionMap;
70
71 // 中央视图对应的 actions
72 QList<JAction*> m_centerViewActions;
73
74 MainWindow *m_mainWindow = nullptr;
75 };
76
77 #endif // ACTIONMANAGER_H
1 void TestPlugin::initWidgets()
2 {
3 m_widget = new Widget;
4 m_widget->setObjectName("Widget");
5 m_widgetList.append(std::shared_ptr<QWidget>(m_widget));
6