Skip to content

Commit 3ec420f

Browse files
vhutterpaceholder
authored andcommitted
Support for multiple connections for a single input port
- added NodeDataModel::portInConnectionPolicy (similar to NodeDataModel::portOutConnectionPolicy) - added a new parameter to NodeDataModel::setInData representing the respective connection - modified NodeConnectionInteraction::disconnect to only erase one connection from the port rather than clear all connections
1 parent 6ef6166 commit 3ec420f

File tree

11 files changed

+114
-23
lines changed

11 files changed

+114
-23
lines changed

examples/example2/TextDisplayDataModel.hpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <nodes/NodeDataModel>
99

1010
#include <iostream>
11+
#include <vector>
1112

1213
using QtNodes::PortType;
1314
using QtNodes::PortIndex;
@@ -54,20 +55,45 @@ class TextDisplayDataModel : public NodeDataModel
5455
std::shared_ptr<NodeData>
5556
outData(PortIndex port) override;
5657

58+
ConnectionPolicy
59+
portInConnectionPolicy(PortIndex) const override
60+
{
61+
return ConnectionPolicy::Many;
62+
}
63+
5764
void
5865
setInData(std::shared_ptr<NodeData> data, int) override
66+
{
67+
}
68+
69+
void
70+
setInData(std::shared_ptr<NodeData> data, int, const QUuid& connectionId) override
5971
{
6072
auto textData = std::dynamic_pointer_cast<TextData>(data);
6173

74+
auto it = std::find_if(inputTexts.begin(), inputTexts.end(),
75+
[this, &connectionId](const auto& e)
76+
{
77+
return e.first == connectionId;
78+
});
6279
if (textData)
6380
{
64-
_label->setText(textData->text());
81+
if (it == inputTexts.end())
82+
inputTexts.emplace_back(connectionId, textData->text());
83+
else
84+
it->second = textData->text();
6585
}
6686
else
6787
{
68-
_label->clear();
88+
inputTexts.erase(it);
6989
}
7090

91+
QStringList textList;
92+
for (auto&& entry : inputTexts) textList.push_back(entry.second);
93+
94+
_label->setText(QStringLiteral("%1 inputs: %2")
95+
.arg(textList.size())
96+
.arg(textList.join(QStringLiteral(", "))));
7197
_label->adjustSize();
7298
}
7399

@@ -77,4 +103,5 @@ class TextDisplayDataModel : public NodeDataModel
77103
private:
78104

79105
QLabel * _label;
106+
std::vector<std::pair<QUuid, QString>> inputTexts;
80107
};

examples/example2/TextSourceDataModel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
TextSourceDataModel::
44
TextSourceDataModel()
5-
: _lineEdit(new QLineEdit("Default Text"))
5+
: _lineEdit(new QLineEdit("Default Text")),
6+
_textData(std::make_shared<TextData>())
67
{
78
connect(_lineEdit, &QLineEdit::textEdited,
89
this, &TextSourceDataModel::onTextEdited);
@@ -54,5 +55,6 @@ std::shared_ptr<NodeData>
5455
TextSourceDataModel::
5556
outData(PortIndex)
5657
{
57-
return std::make_shared<TextData>(_lineEdit->text());
58+
*_textData = TextData(_lineEdit->text());
59+
return _textData;
5860
}

examples/example2/TextSourceDataModel.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ private Q_SLOTS:
6969
private:
7070

7171
QLineEdit * _lineEdit;
72+
std::shared_ptr<TextData> _textData;
7273
};

include/nodes/internal/DataModelRegistry.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,6 @@
1919
namespace QtNodes
2020
{
2121

22-
inline
23-
bool
24-
operator<(QtNodes::NodeDataType const & d1,
25-
QtNodes::NodeDataType const & d2)
26-
{
27-
return d1.id < d2.id;
28-
}
29-
30-
3122
/// Class uses map for storing models (name, model)
3223
class NODE_EDITOR_PUBLIC DataModelRegistry
3324
{
@@ -40,7 +31,8 @@ class NODE_EDITOR_PUBLIC DataModelRegistry
4031
using RegisteredModelsCategoryMap = std::unordered_map<QString, QString>;
4132
using CategoriesSet = std::set<QString>;
4233

43-
using RegisteredTypeConvertersMap = std::map<TypeConverterId, TypeConverter>;
34+
using RegisteredTypeConvertersMap = std::unordered_map<
35+
TypeConverterId, TypeConverter, TypeConverterIdHash>;
4436

4537
DataModelRegistry() = default;
4638
~DataModelRegistry() = default;

include/nodes/internal/Node.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public Q_SLOTS: // data propagation
9090
/// Propagates incoming data to the underlying model.
9191
void
9292
propagateData(std::shared_ptr<NodeData> nodeData,
93-
PortIndex inPortIndex) const;
93+
PortIndex inPortIndex,
94+
const QUuid& connectionId) const;
9495

9596
/// Fetches data from model's OUT #index port
9697
/// and propagates it to the connection

include/nodes/internal/NodeData.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ struct NodeDataType
1111
{
1212
QString id;
1313
QString name;
14+
15+
friend bool operator<(QtNodes::NodeDataType const& d1,
16+
QtNodes::NodeDataType const& d2)
17+
{
18+
return d1.id < d2.id;
19+
}
20+
21+
friend bool operator==(const QtNodes::NodeDataType& d1,
22+
const QtNodes::NodeDataType& d2) noexcept
23+
{
24+
return d1.id == d2.id;
25+
}
1426
};
1527

1628
/// Class represents data transferred between nodes.

include/nodes/internal/NodeDataModel.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ class NODE_EDITOR_PUBLIC NodeDataModel
8787
return ConnectionPolicy::Many;
8888
}
8989

90+
virtual
91+
ConnectionPolicy
92+
portInConnectionPolicy(PortIndex) const
93+
{
94+
return ConnectionPolicy::One;
95+
}
96+
9097
NodeStyle const&
9198
nodeStyle() const;
9299

@@ -101,6 +108,17 @@ class NODE_EDITOR_PUBLIC NodeDataModel
101108
setInData(std::shared_ptr<NodeData> nodeData,
102109
PortIndex port) = 0;
103110

111+
// Use this if portInConnectionPolicy returns ConnectionPolicy::Many
112+
virtual
113+
void
114+
setInData(std::shared_ptr<NodeData> nodeData,
115+
PortIndex port,
116+
const QUuid& connectionId)
117+
{
118+
Q_UNUSED(connectionId);
119+
setInData(nodeData, port);
120+
}
121+
104122
virtual
105123
std::shared_ptr<NodeData>
106124
outData(PortIndex port) = 0;

include/nodes/internal/TypeConverter.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ using TypeConverter =
1818
using TypeConverterId =
1919
std::pair<NodeDataType, NodeDataType>;
2020

21+
struct TypeConverterIdHash
22+
{
23+
std::size_t operator()(const QtNodes::TypeConverterId& converter) const noexcept
24+
{
25+
return qHash(converter.first.id)
26+
^ qHash(converter.second.id);
27+
}
28+
};
29+
2130
}

src/Connection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ propagateData(std::shared_ptr<NodeData> nodeData) const
435435
nodeData = _converter(nodeData);
436436
}
437437

438-
_inNode->propagateData(nodeData, _inPortIndex);
438+
_inNode->propagateData(nodeData, _inPortIndex, id());
439439
}
440440
}
441441

src/Node.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ nodeDataModel() const
185185
void
186186
Node::
187187
propagateData(std::shared_ptr<NodeData> nodeData,
188-
PortIndex inPortIndex) const
188+
PortIndex inPortIndex,
189+
const QUuid& connectionId) const
189190
{
190-
_nodeDataModel->setInData(std::move(nodeData), inPortIndex);
191+
_nodeDataModel->setInData(std::move(nodeData), inPortIndex, connectionId);
191192

192193
//Recalculate the nodes visuals. A data change can result in the node taking more space than before, so this forces a recalculate+repaint on the affected node
193194
_nodeGraphicsObject->setGeometryChanged();

0 commit comments

Comments
 (0)