Skip to content

Commit

Permalink
plugin system initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
zmoth committed Dec 19, 2022
1 parent e3ef06d commit bd98507
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ set(CPP_SOURCE_FILES
src/StyleCollection.cpp
src/UndoCommands.cpp
src/locateNode.cpp
src/PluginsManager.cpp
)

set(HPP_HEADER_FILES
Expand Down Expand Up @@ -136,6 +137,8 @@ set(HPP_HEADER_FILES
include/QtNodes/internal/Serializable.hpp
include/QtNodes/internal/Style.hpp
include/QtNodes/internal/StyleCollection.hpp
include/QtNodes/internal/PluginInterface.hpp
include/QtNodes/internal/PluginsManager.hpp
src/ConnectionPainter.hpp
src/DefaultHorizontalNodeGeometry.hpp
src/DefaultVerticalNodeGeometry.hpp
Expand Down
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ add_subdirectory(dynamic_ports)

add_subdirectory(lock_nodes_and_connections)

add_subdirectory(plugin_text)

add_subdirectory(plugins_load)

11 changes: 11 additions & 0 deletions examples/plugin_text/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
file(GLOB_RECURSE CPPS ./*.cpp)
file(GLOB_RECURSE HPPS ./*.hpp)

add_library(plugin_text SHARED ${CPPS} ${HPPS})

target_link_libraries(plugin_text QtNodes)

target_compile_definitions(plugin_text
PUBLIC
NODE_EDITOR_SHARED
)
18 changes: 18 additions & 0 deletions examples/plugin_text/PluginDefinition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "PluginDefinition.hpp"

#include "TextModel.hpp"

Plugin* Plugin::_this_plugin = nullptr;

Plugin::
Plugin()
{ _this_plugin = this; }

void
Plugin::
registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> & reg)
{
assert(reg);

reg->registerModel<TextModel>();
}
40 changes: 40 additions & 0 deletions examples/plugin_text/PluginDefinition.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <QObject>
#include <QtNodes/NodeDelegateModelRegistry>
#include <QtNodes/PluginInterface>

// This needs to be the same as the name of your project file ${PROJECT_NAME}
// 这里需要和您的工程文件名一致 ${PROJECT_NAME}
#if defined(plugin_text_EXPORTS)
#define DLL_EXPORT Q_DECL_EXPORT
#else
#define DLL_EXPORT Q_DECL_IMPORT
#endif

using QtNodes::NodeDelegateModelRegistry;
using QtNodes::PluginInterface;

#define PLUGIN_NAME "pluginText"

class DLL_EXPORT Plugin
: public QObject
, public QtNodes::PluginInterface
{
Q_OBJECT
Q_INTERFACES(QtNodes::PluginInterface)
Q_PLUGIN_METADATA(IID PLUGIN_NAME)

public:
Plugin();

QString
name() const override
{ return PLUGIN_NAME; };

void
registerDataModels(std::shared_ptr<QtNodes::NodeDelegateModelRegistry> & reg) override;

private:
static Plugin* _this_plugin;
};
28 changes: 28 additions & 0 deletions examples/plugin_text/TextData.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <QtNodes/NodeData>

using QtNodes::NodeData;
using QtNodes::NodeDataType;

/// The class can potentially incapsulate any user data which
/// need to be transferred within the Node Editor graph
class TextData : public NodeData
{
public:

TextData() {}

TextData(QString const &text)
: _text(text)
{}

NodeDataType type() const override
{ return NodeDataType {"text", "Text"}; }

QString text() const { return _text; }

private:

QString _text;
};
96 changes: 96 additions & 0 deletions examples/plugin_text/TextModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "TextModel.hpp"

#include <QtWidgets/QTextEdit>

TextModel::
TextModel()
{
//
}


unsigned int
TextModel::
nPorts(PortType portType) const
{
unsigned int result = 1;

switch (portType)
{
case PortType::In:
result = 1;
break;

case PortType::Out:
result = 1;

default:
break;
}

return result;
}


void
TextModel::
onTextEdited()
{
Q_EMIT dataUpdated(0);
}


NodeDataType
TextModel::
dataType(PortType, PortIndex) const
{
return TextData().type();
}


std::shared_ptr<NodeData>
TextModel::
outData(PortIndex const portIndex)
{
Q_UNUSED(portIndex);
return std::make_shared<TextData>(_textEdit->toPlainText());
}


QWidget *
TextModel::
embeddedWidget()
{
if (!_textEdit)
{
_textEdit = new QTextEdit();

connect(_textEdit, &QTextEdit::textChanged,
this, &TextModel::onTextEdited);

}

return _textEdit;
}


void
TextModel::
setInData(std::shared_ptr<NodeData> data, PortIndex const)
{
auto textData = std::dynamic_pointer_cast<TextData>(data);

QString inputText;

if (textData)
{
inputText = textData->text();
}
else
{
inputText = "";
}

_textEdit->setText(inputText);
}

69 changes: 69 additions & 0 deletions examples/plugin_text/TextModel.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#pragma once

#include <QtCore/QObject>

#include "TextData.hpp"

#include <QtNodes/NodeDelegateModel>

#include <iostream>

using QtNodes::PortType;
using QtNodes::PortIndex;
using QtNodes::NodeData;
using QtNodes::NodeDelegateModel;

class QTextEdit;

/// The model dictates the number of inputs and outputs for the Node.
/// In this example it has no logic.
class TextModel : public NodeDelegateModel
{
Q_OBJECT

public:
TextModel();

public:
QString
caption() const override
{ return QString("Text"); }

bool
captionVisible() const override { return true; }

static QString
Name()
{ return QString("TextModel"); }

QString
name() const override
{ return TextModel::Name(); }

public:
unsigned int
nPorts(PortType portType) const override;

NodeDataType
dataType(PortType portType, PortIndex portIndex) const override;

std::shared_ptr<NodeData>
outData(PortIndex const portIndex) override;

void
setInData(std::shared_ptr<NodeData>, PortIndex const) override;

QWidget *
embeddedWidget() override;

bool
resizable() const override { return true; }

private Q_SLOTS:

void
onTextEdited();

private:
QTextEdit * _textEdit = nullptr;
};
6 changes: 6 additions & 0 deletions examples/plugins_load/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
file(GLOB_RECURSE CPPS ./*.cpp)
file(GLOB_RECURSE HPPS ./*.hpp)

add_executable(plugins_load ${CPPS} ${HPPS})

target_link_libraries(plugins_load QtNodes)
Loading

0 comments on commit bd98507

Please sign in to comment.