Skip to content

Commit 15260c5

Browse files
committed
plugin
1 parent 5465ddc commit 15260c5

17 files changed

+754
-3
lines changed

CMakeLists.txt

+20-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,23 @@ option(QT_NODES_FORCE_TEST_COLOR "Force colorized unit test output" OFF)
3232
enable_testing()
3333

3434
if(QT_NODES_DEVELOPER_DEFAULTS)
35-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/bin")
36-
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
37-
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/lib")
35+
# Set Output Path
36+
include(GNUInstallDirs)
37+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
38+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
39+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
40+
41+
if(NOT DEFINED CMAKE_CONFIGURATION_TYPES)
42+
set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE})
43+
endif()
44+
45+
foreach(OUTPUT_TYPES ${CMAKE_CONFIGURATION_TYPES})
46+
string(TOUPPER ${OUTPUT_TYPES} OUTPUT_CONFIG)
47+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/${OUTPUT_TYPES})
48+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${OUTPUT_TYPES})
49+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${OUTPUT_TYPES})
50+
message(STATUS "CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} : ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG}}")
51+
endforeach(OUTPUT_TYPES CMAKE_CONFIGURATION_TYPES)
3852
endif()
3953

4054
if(BUILD_DEBUG_POSTFIX_D)
@@ -89,6 +103,7 @@ set(CPP_SOURCE_FILES
89103
src/StyleCollection.cpp
90104
src/UndoCommands.cpp
91105
src/locateNode.cpp
106+
src/PluginsManager.cpp
92107
)
93108

94109
set(HPP_HEADER_FILES
@@ -122,6 +137,8 @@ set(HPP_HEADER_FILES
122137
include/QtNodes/internal/Serializable.hpp
123138
include/QtNodes/internal/Style.hpp
124139
include/QtNodes/internal/StyleCollection.hpp
140+
include/QtNodes/internal/PluginInterface.hpp
141+
include/QtNodes/internal/PluginsManager.hpp
125142
src/ConnectionPainter.hpp
126143
src/DefaultHorizontalNodeGeometry.hpp
127144
src/DefaultVerticalNodeGeometry.hpp

examples/CMakeLists.txt

+4
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+

examples/plugin_text/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
file(GLOB_RECURSE CPPS ./*.cpp)
2+
file(GLOB_RECURSE HPPS ./*.hpp)
3+
4+
foreach(OUTPUT_TYPES ${CMAKE_CONFIGURATION_TYPES})
5+
string(TOUPPER ${OUTPUT_TYPES} OUTPUT_CONFIG)
6+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUT_CONFIG}}/plugins)
7+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUT_CONFIG}}/plugins)
8+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG}}/plugins)
9+
# message(STATUS "CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG} : ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUT_CONFIG}}")
10+
endforeach(OUTPUT_TYPES CMAKE_CONFIGURATION_TYPES)
11+
12+
add_library(plugin_text SHARED ${CPPS} ${HPPS})
13+
14+
target_link_libraries(plugin_text QtNodes)
15+
16+
target_compile_definitions(plugin_text PUBLIC NODE_EDITOR_SHARED)
17+
18+
set_target_properties(plugin_text PROPERTIES SUFFIX ".node")
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "PluginDefinition.hpp"
2+
3+
#include "TextModel.hpp"
4+
5+
Plugin *Plugin::_this_plugin = nullptr;
6+
7+
Plugin::Plugin()
8+
{
9+
_this_plugin = this;
10+
}
11+
12+
Plugin::~Plugin()
13+
{
14+
// TODO: Unregister all models here
15+
}
16+
17+
void Plugin::registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg)
18+
{
19+
assert(reg);
20+
21+
reg->registerModel<TextModel>();
22+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#ifdef plugin_text_EXPORTS
9+
#define DLL_EXPORT Q_DECL_EXPORT
10+
#else
11+
#define DLL_EXPORT Q_DECL_IMPORT
12+
#endif
13+
14+
#define PLUGIN_NAME "Text"
15+
16+
class DLL_EXPORT Plugin
17+
: public QObject
18+
, public QtNodes::PluginInterface
19+
{
20+
Q_OBJECT
21+
Q_INTERFACES(QtNodes::PluginInterface)
22+
Q_PLUGIN_METADATA(IID PLUGIN_NAME)
23+
24+
public:
25+
Plugin();
26+
~Plugin();
27+
28+
QString name() const override { return PLUGIN_NAME; };
29+
30+
void registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> &reg) override;
31+
32+
private:
33+
static Plugin *_this_plugin;
34+
};

examples/plugin_text/TextData.hpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
TextData() {}
14+
15+
TextData(QString const &text)
16+
: _text(text)
17+
{}
18+
19+
NodeDataType type() const override { return NodeDataType{"text", "Text"}; }
20+
21+
QString text() const { return _text; }
22+
23+
private:
24+
QString _text;
25+
};

examples/plugin_text/TextModel.cpp

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

examples/plugin_text/TextModel.hpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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::NodeData;
12+
using QtNodes::NodeDelegateModel;
13+
using QtNodes::PortIndex;
14+
using QtNodes::PortType;
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 caption() const override { return QString("Text"); }
29+
30+
bool captionVisible() const override { return true; }
31+
32+
static QString Name() { return QString("Text"); }
33+
34+
QString name() const override { return TextModel::Name(); }
35+
36+
public:
37+
unsigned int nPorts(PortType portType) const override;
38+
39+
NodeDataType dataType(PortType portType, PortIndex portIndex) const override;
40+
41+
std::shared_ptr<NodeData> outData(PortIndex const portIndex) override;
42+
43+
void setInData(std::shared_ptr<NodeData>, PortIndex const) override;
44+
45+
QWidget *embeddedWidget() override;
46+
47+
bool resizable() const override { return true; }
48+
49+
private Q_SLOTS:
50+
51+
void onTextEdited();
52+
53+
private:
54+
QTextEdit *_textEdit = nullptr;
55+
};

examples/plugins_load/CMakeLists.txt

+6
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)