Skip to content

Commit 8fc3348

Browse files
committed
plugin system initialisation
1 parent 0d6c318 commit 8fc3348

15 files changed

Lines changed: 557 additions & 0 deletions

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ set(CPP_SOURCE_FILES
103103
src/StyleCollection.cpp
104104
src/UndoCommands.cpp
105105
src/locateNode.cpp
106+
src/PluginsManager.cpp
106107
)
107108

108109
set(HPP_HEADER_FILES
@@ -136,6 +137,8 @@ set(HPP_HEADER_FILES
136137
include/QtNodes/internal/Serializable.hpp
137138
include/QtNodes/internal/Style.hpp
138139
include/QtNodes/internal/StyleCollection.hpp
140+
include/QtNodes/internal/PluginInterface.hpp
141+
include/QtNodes/internal/PluginsManager.hpp
139142
src/ConnectionPainter.hpp
140143
src/DefaultHorizontalNodeGeometry.hpp
141144
src/DefaultVerticalNodeGeometry.hpp

examples/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ add_subdirectory(dynamic_ports)
1616

1717
add_subdirectory(lock_nodes_and_connections)
1818

19+
add_subdirectory(plugin_text)
20+
21+
add_subdirectory(plugins_load)
22+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
file(GLOB_RECURSE CPPS ./*.cpp)
2+
file(GLOB_RECURSE HPPS ./*.hpp)
3+
4+
add_library(plugin_text SHARED ${CPPS} ${HPPS})
5+
6+
target_link_libraries(plugin_text QtNodes)
7+
8+
target_compile_definitions(plugin_text
9+
PUBLIC
10+
NODE_EDITOR_SHARED
11+
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "PluginDefinition.hpp"
2+
3+
#include "TextSourceDataModel.hpp"
4+
5+
Plugin *Plugin::_this_plugin = nullptr;
6+
7+
Plugin::Plugin() { _this_plugin = this; }
8+
9+
void Plugin::registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg)
10+
{
11+
assert(reg);
12+
13+
reg->registerModel<TextSourceDataModel>();
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
3+
#include <QObject>
4+
#include <QtNodes/NodeDelegateModelRegistry>
5+
#include <QtNodes/PluginInterface>
6+
7+
#if defined(plugin_text_EXPORTS) // 这里需要和您的工程文件名一致${PROJECT_NAME}
8+
#define DLL_EXPORT Q_DECL_EXPORT
9+
#else
10+
#define DLL_EXPORT Q_DECL_IMPORT
11+
#endif
12+
13+
using QtNodes::NodeDelegateModelRegistry;
14+
using QtNodes::PluginInterface;
15+
16+
/* 插件名称,一个插件可以存在多个节点 */
17+
#define PLUGIN_NAME "plugin_text"
18+
19+
class DLL_EXPORT Plugin
20+
: public QObject
21+
, public QtNodes::PluginInterface
22+
{
23+
Q_OBJECT
24+
Q_INTERFACES(QtNodes::PluginInterface)
25+
Q_PLUGIN_METADATA(IID PLUGIN_NAME)
26+
27+
public:
28+
Plugin();
29+
30+
QString name() const override { return PLUGIN_NAME; };
31+
32+
void registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg) override;
33+
34+
private:
35+
static Plugin *_this_plugin;
36+
};

examples/plugin_text/TextData.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <QtNodes/NodeData>
4+
5+
using QtNodes::NodeData;
6+
using QtNodes::NodeDataType;
7+
8+
/// The class can potentially incapsulate any user data which
9+
/// need to be transferred within the Node Editor graph
10+
class TextData : public NodeData
11+
{
12+
public:
13+
14+
TextData() {}
15+
16+
TextData(QString const &text)
17+
: _text(text)
18+
{}
19+
20+
NodeDataType type() const override
21+
{ return NodeDataType {"text", "Text"}; }
22+
23+
QString text() const { return _text; }
24+
25+
private:
26+
27+
QString _text;
28+
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "TextSourceDataModel.hpp"
2+
3+
#include <QtWidgets/QLineEdit>
4+
5+
TextSourceDataModel::
6+
TextSourceDataModel()
7+
: _lineEdit{nullptr}
8+
{
9+
//
10+
}
11+
12+
13+
unsigned int
14+
TextSourceDataModel::
15+
nPorts(PortType portType) const
16+
{
17+
unsigned int result = 1;
18+
19+
switch (portType)
20+
{
21+
case PortType::In:
22+
result = 0;
23+
break;
24+
25+
case PortType::Out:
26+
result = 1;
27+
28+
default:
29+
break;
30+
}
31+
32+
return result;
33+
}
34+
35+
36+
void
37+
TextSourceDataModel::
38+
onTextEdited(QString const & string)
39+
{
40+
Q_UNUSED(string);
41+
42+
Q_EMIT dataUpdated(0);
43+
}
44+
45+
46+
NodeDataType
47+
TextSourceDataModel::
48+
dataType(PortType, PortIndex) const
49+
{
50+
return TextData().type();
51+
}
52+
53+
54+
std::shared_ptr<NodeData>
55+
TextSourceDataModel::
56+
outData(PortIndex const portIndex)
57+
{
58+
Q_UNUSED(portIndex);
59+
return std::make_shared<TextData>(_lineEdit->text());
60+
}
61+
62+
63+
QWidget *
64+
TextSourceDataModel::
65+
embeddedWidget()
66+
{
67+
if (!_lineEdit)
68+
{
69+
_lineEdit = new QLineEdit("Default Text"),
70+
71+
connect(_lineEdit, &QLineEdit::textEdited,
72+
this, &TextSourceDataModel::onTextEdited);
73+
74+
}
75+
76+
return _lineEdit;
77+
}
78+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
3+
#include <QtCore/QObject>
4+
5+
#include "TextData.hpp"
6+
7+
#include <QtNodes/NodeDelegateModel>
8+
9+
#include <iostream>
10+
11+
using QtNodes::PortType;
12+
using QtNodes::PortIndex;
13+
using QtNodes::NodeData;
14+
using QtNodes::NodeDelegateModel;
15+
16+
class QLineEdit;
17+
18+
/// The model dictates the number of inputs and outputs for the Node.
19+
/// In this example it has no logic.
20+
class TextSourceDataModel : public NodeDelegateModel
21+
{
22+
Q_OBJECT
23+
24+
public:
25+
TextSourceDataModel();
26+
27+
public:
28+
QString
29+
caption() const override
30+
{ return QString("Text Source"); }
31+
32+
bool
33+
captionVisible() const override { return false; }
34+
35+
static QString
36+
Name()
37+
{ return QString("TextSourceDataModel"); }
38+
39+
QString
40+
name() const override
41+
{ return TextSourceDataModel::Name(); }
42+
43+
public:
44+
unsigned int
45+
nPorts(PortType portType) const override;
46+
47+
NodeDataType
48+
dataType(PortType portType, PortIndex portIndex) const override;
49+
50+
std::shared_ptr<NodeData>
51+
outData(PortIndex const portIndex) override;
52+
53+
void
54+
setInData(std::shared_ptr<NodeData>, PortIndex const) override { }
55+
56+
QWidget *
57+
embeddedWidget() override;
58+
59+
private Q_SLOTS:
60+
61+
void
62+
onTextEdited(QString const & string);
63+
64+
private:
65+
QLineEdit * _lineEdit;
66+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
file(GLOB_RECURSE CPPS ./*.cpp)
2+
file(GLOB_RECURSE HPPS ./*.hpp)
3+
4+
add_executable(plugins_load ${CPPS} ${HPPS})
5+
6+
target_link_libraries(plugins_load QtNodes)

examples/plugins_load/main.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <QApplication>
2+
#include <QMenuBar>
3+
#include <QVBoxLayout>
4+
#include <QtNodes/DataFlowGraphModel>
5+
#include <QtNodes/DataFlowGraphicsScene>
6+
#include <QtNodes/GraphicsView>
7+
#include <QtNodes/NodeDelegateModelRegistry>
8+
#include <QtNodes/PluginsManager>
9+
10+
using QtNodes::DataFlowGraphicsScene;
11+
using QtNodes::DataFlowGraphModel;
12+
using QtNodes::GraphicsView;
13+
using QtNodes::NodeDelegateModelRegistry;
14+
using QtNodes::PluginsManager;
15+
16+
int main(int argc, char *argv[])
17+
{
18+
qSetMessagePattern(
19+
"[%{time yyyyMMdd h:mm:ss.zzz}] [%{time process}] "
20+
"[%{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-"
21+
"fatal}F%{endif}]: %{message}\t| (%{function}) [%{file}:%{line}]");
22+
23+
QApplication app(argc, argv);
24+
25+
PluginsManager *pluginsManager = PluginsManager::instance();
26+
std::shared_ptr<NodeDelegateModelRegistry> registry = pluginsManager->registry();
27+
pluginsManager->loadPlugins(R"(./nodes)");
28+
for (auto plugin : pluginsManager->pluginList())
29+
{
30+
plugin->registerDataModels(registry);
31+
}
32+
33+
QWidget mainWidget;
34+
35+
auto menuBar = new QMenuBar();
36+
QMenu *menu = menuBar->addMenu("File");
37+
auto saveAction = menu->addAction("Save Scene");
38+
auto loadAction = menu->addAction("Load Scene");
39+
40+
QVBoxLayout *l = new QVBoxLayout(&mainWidget);
41+
42+
DataFlowGraphModel dataFlowGraphModel(registry);
43+
44+
l->addWidget(menuBar);
45+
auto scene = new DataFlowGraphicsScene(dataFlowGraphModel, &mainWidget);
46+
l->addWidget(new GraphicsView(scene));
47+
l->setContentsMargins(0, 0, 0, 0);
48+
l->setSpacing(0);
49+
50+
QObject::connect(saveAction, &QAction::triggered, scene, &DataFlowGraphicsScene::save);
51+
QObject::connect(loadAction, &QAction::triggered, scene, &DataFlowGraphicsScene::load);
52+
53+
mainWidget.setWindowTitle("Data Flow: Plugins Load");
54+
mainWidget.resize(800, 600);
55+
mainWidget.show();
56+
57+
return app.exec();
58+
}

0 commit comments

Comments
 (0)