Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many in connections #213

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.py
*.pyc
_build
CMakeLists.txt.user
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ add_subdirectory(external)
find_package(Qt5 5.3 COMPONENTS
Core
Widgets
Gui
OpenGL)
Gui)

qt5_add_resources(RESOURCES ./resources/resources.qrc)

Expand Down Expand Up @@ -92,7 +91,6 @@ target_link_libraries(nodes
Qt5::Core
Qt5::Widgets
Qt5::Gui
Qt5::OpenGL
)

target_compile_definitions(nodes
Expand Down Expand Up @@ -154,6 +152,7 @@ if(BUILD_TESTING)
add_subdirectory(test)
endif()

if (NOT ${CMAKE_DISABLE_INSTALL_TARGETS})
###############
# Installation
##
Expand Down Expand Up @@ -187,3 +186,4 @@ install(FILES
${CMAKE_CURRENT_BINARY_DIR}/NodeEditorConfig.cmake
DESTINATION ${INSTALL_CONFIGDIR}
)
endif()
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ add_subdirectory(calculator)
add_subdirectory(images)

add_subdirectory(styles)

add_subdirectory(state_machine)
95 changes: 95 additions & 0 deletions examples/calculator/AdditionModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,98 @@ class AdditionModel : public MathOperationDataModel
Q_EMIT dataUpdated(outPortIndex);
}
};

class MultiAdditionModel : public NodeDataModel
{
public:
QString
caption() const override
{
return QStringLiteral("Multi Addition");
}

QString
name() const override
{
return QStringLiteral("Multi Addition");
}

unsigned int
nPorts(PortType portType) const override
{
return 1;
}

NodeDataType
dataType(PortType portType,
PortIndex portIndex) const
{
return DecimalData().type();
}

std::shared_ptr<NodeData>
outData(PortIndex port) override
{
return _result;
}

void setInData(std::shared_ptr<NodeData> data, PortIndex portIndex) override
{
}

void setInData(std::vector<std::shared_ptr<NodeData>> data, PortIndex portIndex) override
{
_input.clear();
_input.reserve(data.size());
for (auto& node : data)
{
std::shared_ptr<DecimalData> decimalData = std::dynamic_pointer_cast<DecimalData>(node);
if (decimalData != nullptr)
_input.push_back(decimalData);
}

if (_input.empty())
{
modelValidationState = NodeValidationState::Warning;
}
else
{
modelValidationState = NodeValidationState::Valid;
}

double result = 0.0f;
for (auto& node : _input)
{
std::shared_ptr<DecimalData> locked = node.lock();
result += locked->number();
}

_result = std::make_shared<DecimalData>(result);
dataUpdated(0);
}

QWidget *
embeddedWidget() override { return nullptr; }

NodeValidationState
validationState() const override
{
return modelValidationState;
}

QString
validationMessage() const override
{
return modelValidationError;
}

ConnectionPolicy portInConnectionPolicy(PortIndex) const override { return QtNodes::NodeDataModel::ConnectionPolicy::Many; }

private:
std::vector<std::weak_ptr<DecimalData>> _input;

std::shared_ptr<DecimalData> _result;

NodeValidationState modelValidationState = NodeValidationState::Warning;
QString modelValidationError = QString("Missing or incorrect inputs");
};
1 change: 1 addition & 0 deletions examples/calculator/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ registerDataModels()
ret->registerModel<NumberDisplayDataModel>("Displays");

ret->registerModel<AdditionModel>("Operators");
ret->registerModel<MultiAdditionModel>("Operators");

ret->registerModel<SubtractionModel>("Operators");

Expand Down
5 changes: 5 additions & 0 deletions examples/state_machine/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file(GLOB_RECURSE CPPS ./*.cpp )

add_executable(state_machine ${CPPS})

target_link_libraries(state_machine nodes)
81 changes: 81 additions & 0 deletions examples/state_machine/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "state_node_model.h"

#include <nodes/FlowScene>
#include <nodes/FlowView>
#include <nodes/ConnectionStyle>

#include <QApplication>
#include <QMenuBar>
#include <QBoxLayout>

using QtNodes::FlowScene;
using QtNodes::FlowView;
using QtNodes::DataModelRegistry;
using QtNodes::ConnectionStyle;

std::shared_ptr<DataModelRegistry> registerDataModels()
{
std::shared_ptr<DataModelRegistry> registry(new DataModelRegistry());

registry->registerModel<StateNodeModel>();

return registry;
}


static
void
setStyle()
{
ConnectionStyle::setConnectionStyle(
R"(
{
"ConnectionStyle": {
"ConstructionColor": "gray",
"NormalColor": "black",
"SelectedColor": "gray",
"SelectedHaloColor": "deepskyblue",
"HoveredColor": "deepskyblue",

"LineWidth": 3.0,
"ConstructionLineWidth": 2.0,
"PointDiameter": 10.0,

"UseDataDefinedColors": true
}
}
)");
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

setStyle();

QWidget mainWidget;

auto menuBar = new QMenuBar();
auto saveAction = menuBar->addAction("Save..");
auto loadAction = menuBar->addAction("Load..");

QVBoxLayout *l = new QVBoxLayout(&mainWidget);

l->addWidget(menuBar);
auto scene = new FlowScene(registerDataModels(), &mainWidget);
l->addWidget(new FlowView(scene));
l->setContentsMargins(0, 0, 0, 0);
l->setSpacing(0);

QObject::connect(saveAction, &QAction::triggered,
scene, &FlowScene::save);

QObject::connect(loadAction, &QAction::triggered,
scene, &FlowScene::load);

mainWidget.setWindowTitle("Simplest state editor");
mainWidget.resize(800, 600);
mainWidget.showNormal();

return app.exec();
}
57 changes: 57 additions & 0 deletions examples/state_machine/state_node_model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "state_node_model.h"

QString StateNodeModel::caption() const
{
return QStringLiteral("State");
}

QString StateNodeModel::name() const
{
return QStringLiteral("state_node");
}

unsigned int StateNodeModel::nPorts(PortType portType) const
{
std::ignore = portType;
return 1;
}

NodeDataType StateNodeModel::dataType(PortType portType, PortIndex portIndex) const
{
std::ignore = portType;
std::ignore = portIndex;
return getTranstitionType();
}

NodeDataType StateNodeModel::getTranstitionType()
{
return NodeDataType{"transition_port", ""};
}

QtNodes::NodeDataModel::ConnectionPolicy StateNodeModel::portOutConnectionPolicy(PortIndex) const
{
return ConnectionPolicy::Many;
}

QtNodes::NodeDataModel::ConnectionPolicy StateNodeModel::portInConnectionPolicy(PortIndex) const
{
return ConnectionPolicy::Many;
}

void StateNodeModel::setInData(std::shared_ptr<NodeData> nodeData, PortIndex port)
{
std::ignore = nodeData;
std::ignore = port;
}

void StateNodeModel::setInData(std::vector<std::shared_ptr<NodeData>> nodeData, PortIndex port)
{
std::ignore = nodeData;
std::ignore = port;
}

std::shared_ptr<NodeData> StateNodeModel::outData(PortIndex port)
{
std::ignore = port;
return nullptr;
}
28 changes: 28 additions & 0 deletions examples/state_machine/state_node_model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <nodes/NodeDataModel>

using QtNodes::NodeData;
using QtNodes::PortIndex;
using QtNodes::PortType;
using QtNodes::NodeDataType;

class StateNodeModel : public QtNodes::NodeDataModel
{
public:
static NodeDataType getTranstitionType();

QString caption() const override;
QString name() const override;

unsigned int nPorts(PortType portType) const override;
NodeDataType dataType(PortType portType, PortIndex portIndex) const override;
ConnectionPolicy portOutConnectionPolicy(PortIndex) const override;
ConnectionPolicy portInConnectionPolicy(PortIndex) const override;

void setInData(std::shared_ptr<NodeData> nodeData, PortIndex port) override;
void setInData(std::vector<std::shared_ptr<NodeData>> nodeData, PortIndex port) override;
std::shared_ptr<NodeData> outData(PortIndex port) override;

QWidget* embeddedWidget() override { return nullptr; }
};
4 changes: 3 additions & 1 deletion include/nodes/internal/Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ class NODE_EDITOR_PUBLIC Connection
NodeDataType
dataType(PortType portType) const;

TypeConverter getTypeConverter() const;

void
setTypeConverter(TypeConverter converter);

Expand All @@ -124,7 +126,7 @@ class NODE_EDITOR_PUBLIC Connection
public: // data propagation

void
propagateData(std::shared_ptr<NodeData> nodeData) const;
propagateData() const;

void
propagateEmptyData() const;
Expand Down
3 changes: 2 additions & 1 deletion include/nodes/internal/ConnectionGraphicsObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QtCore/QUuid>

#include <QtWidgets/QGraphicsObject>
#include "Export.hpp"

class QGraphicsSceneMouseEvent;

Expand All @@ -15,7 +16,7 @@ class ConnectionGeometry;
class Node;

/// Graphic Object for connection. Adds itself to scene
class ConnectionGraphicsObject
class NODE_EDITOR_PUBLIC ConnectionGraphicsObject
: public QGraphicsObject
{
Q_OBJECT
Expand Down
8 changes: 4 additions & 4 deletions include/nodes/internal/FlowView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ class NODE_EDITOR_PUBLIC FlowView

public Q_SLOTS:

void scaleUp();
virtual void scaleUp();

void scaleDown();
virtual void scaleDown();

void deleteSelectedNodes();
virtual void deleteSelectedNodes();

protected:

Expand All @@ -55,7 +55,7 @@ public Q_SLOTS:

protected:

FlowScene * scene();
FlowScene * scene();

private:

Expand Down
3 changes: 1 addition & 2 deletions include/nodes/internal/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public Q_SLOTS: // data propagation

/// Propagates incoming data to the underlying model.
void
propagateData(std::shared_ptr<NodeData> nodeData,
PortIndex inPortIndex) const;
propagateData(PortIndex inPortIndex) const;

/// Fetches data from model's OUT #index port
/// and propagates it to the connection
Expand Down
Loading