Skip to content

Commit

Permalink
(#6) In port now supports ConnectionPolicy::Many
Browse files Browse the repository at this point in the history
  • Loading branch information
Daguerreo committed Dec 18, 2020
1 parent b272e4e commit dd68f0f
Show file tree
Hide file tree
Showing 25 changed files with 608 additions and 170 deletions.
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)
127 changes: 127 additions & 0 deletions examples/calculator/AdditionModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,130 @@ 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"); }

bool
portCaptionVisible(PortType portType, PortIndex portIndex) const override
{
Q_UNUSED(portType); Q_UNUSED(portIndex);
return true;
}

QString
portCaption(PortType portType, PortIndex portIndex) const override
{
Q_UNUSED(portIndex);

if(portType == PortType::In && _input.size() > 0)
{
return QString("Decimals (%1)").arg(_input.size());
}

return QStringLiteral("Decimal");
}

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

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

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

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

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

_input.clear();
_input.reserve(data.size());
for (auto& node : data)
{
std::shared_ptr<DecimalData> decimalData = std::dynamic_pointer_cast<DecimalData>(node);
_input.push_back(decimalData);
}

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

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

if(counter > 0)
{
_result = std::make_shared<DecimalData>(result);
}
else
{
_result.reset();
}

Q_EMIT 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 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");
};
5 changes: 3 additions & 2 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>("I/O//Displays");

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

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

Expand Down Expand Up @@ -94,8 +95,8 @@ main(int argc, char *argv[])
QWidget mainWidget;

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

QVBoxLayout *l = new QVBoxLayout(&mainWidget);

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)
86 changes: 86 additions & 0 deletions examples/state_machine/StateNodeModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "StateNodeModel.hpp"

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/StateNodeModel.hpp
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; }
};
84 changes: 84 additions & 0 deletions examples/state_machine/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "StateNodeModel.hpp"

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

#include <QtWidgets/QApplication>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/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();
}
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
Loading

0 comments on commit dd68f0f

Please sign in to comment.