Skip to content

Commit bd98507

Browse files
committed
plugin system initialisation
1 parent e3ef06d commit bd98507

File tree

15 files changed

+659
-0
lines changed

15 files changed

+659
-0
lines changed

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "PluginDefinition.hpp"
2+
3+
#include "TextModel.hpp"
4+
5+
Plugin* Plugin::_this_plugin = nullptr;
6+
7+
Plugin::
8+
Plugin()
9+
{ _this_plugin = this; }
10+
11+
void
12+
Plugin::
13+
registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> & reg)
14+
{
15+
assert(reg);
16+
17+
reg->registerModel<TextModel>();
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#include <QObject>
4+
#include <QtNodes/NodeDelegateModelRegistry>
5+
#include <QtNodes/PluginInterface>
6+
7+
// This needs to be the same as the name of your project file ${PROJECT_NAME}
8+
// 这里需要和您的工程文件名一致 ${PROJECT_NAME}
9+
#if defined(plugin_text_EXPORTS)
10+
#define DLL_EXPORT Q_DECL_EXPORT
11+
#else
12+
#define DLL_EXPORT Q_DECL_IMPORT
13+
#endif
14+
15+
using QtNodes::NodeDelegateModelRegistry;
16+
using QtNodes::PluginInterface;
17+
18+
#define PLUGIN_NAME "pluginText"
19+
20+
class DLL_EXPORT Plugin
21+
: public QObject
22+
, public QtNodes::PluginInterface
23+
{
24+
Q_OBJECT
25+
Q_INTERFACES(QtNodes::PluginInterface)
26+
Q_PLUGIN_METADATA(IID PLUGIN_NAME)
27+
28+
public:
29+
Plugin();
30+
31+
QString
32+
name() const override
33+
{ return PLUGIN_NAME; };
34+
35+
void
36+
registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> & reg) override;
37+
38+
private:
39+
static Plugin* _this_plugin;
40+
};

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+
};

examples/plugin_text/TextModel.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include "TextModel.hpp"
2+
3+
#include <QtWidgets/QTextEdit>
4+
5+
TextModel::
6+
TextModel()
7+
{
8+
//
9+
}
10+
11+
12+
unsigned int
13+
TextModel::
14+
nPorts(PortType portType) const
15+
{
16+
unsigned int result = 1;
17+
18+
switch (portType)
19+
{
20+
case PortType::In:
21+
result = 1;
22+
break;
23+
24+
case PortType::Out:
25+
result = 1;
26+
27+
default:
28+
break;
29+
}
30+
31+
return result;
32+
}
33+
34+
35+
void
36+
TextModel::
37+
onTextEdited()
38+
{
39+
Q_EMIT dataUpdated(0);
40+
}
41+
42+
43+
NodeDataType
44+
TextModel::
45+
dataType(PortType, PortIndex) const
46+
{
47+
return TextData().type();
48+
}
49+
50+
51+
std::shared_ptr<NodeData>
52+
TextModel::
53+
outData(PortIndex const portIndex)
54+
{
55+
Q_UNUSED(portIndex);
56+
return std::make_shared<TextData>(_textEdit->toPlainText());
57+
}
58+
59+
60+
QWidget *
61+
TextModel::
62+
embeddedWidget()
63+
{
64+
if (!_textEdit)
65+
{
66+
_textEdit = new QTextEdit();
67+
68+
connect(_textEdit, &QTextEdit::textChanged,
69+
this, &TextModel::onTextEdited);
70+
71+
}
72+
73+
return _textEdit;
74+
}
75+
76+
77+
void
78+
TextModel::
79+
setInData(std::shared_ptr<NodeData> data, PortIndex const)
80+
{
81+
auto textData = std::dynamic_pointer_cast<TextData>(data);
82+
83+
QString inputText;
84+
85+
if (textData)
86+
{
87+
inputText = textData->text();
88+
}
89+
else
90+
{
91+
inputText = "";
92+
}
93+
94+
_textEdit->setText(inputText);
95+
}
96+

examples/plugin_text/TextModel.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 QTextEdit;
17+
18+
/// The model dictates the number of inputs and outputs for the Node.
19+
/// In this example it has no logic.
20+
class TextModel : public NodeDelegateModel
21+
{
22+
Q_OBJECT
23+
24+
public:
25+
TextModel();
26+
27+
public:
28+
QString
29+
caption() const override
30+
{ return QString("Text"); }
31+
32+
bool
33+
captionVisible() const override { return true; }
34+
35+
static QString
36+
Name()
37+
{ return QString("TextModel"); }
38+
39+
QString
40+
name() const override
41+
{ return TextModel::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+
bool
60+
resizable() const override { return true; }
61+
62+
private Q_SLOTS:
63+
64+
void
65+
onTextEdited();
66+
67+
private:
68+
QTextEdit * _textEdit = nullptr;
69+
};
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)

0 commit comments

Comments
 (0)