Skip to content

Commit e096d2e

Browse files
committed
Improve function signatures and names in DataFlowGraphModel
1 parent 3f3630e commit e096d2e

5 files changed

Lines changed: 98 additions & 55 deletions

File tree

include/QtNodes/internal/AbstractGraphModel.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ class NODE_EDITOR_PUBLIC AbstractGraphModel : public QObject
148148

149149
virtual
150150
bool
151-
setPortData(NodeId nodeId,
152-
PortType portType,
153-
PortIndex index,
154-
PortRole role) const = 0;
151+
setPortData(NodeId nodeId,
152+
PortType portType,
153+
PortIndex index,
154+
QVariant const& value,
155+
PortRole role = PortRole::Data
156+
) = 0;
155157

156158
virtual
157159
bool
@@ -221,9 +223,9 @@ class NODE_EDITOR_PUBLIC AbstractGraphModel : public QObject
221223
nodePositionUpdated(NodeId const nodeId);
222224

223225
void
224-
portDataSet(NodeId const nodeId,
225-
PortType const portType,
226-
PortIndex const portIndex);
226+
inPortDataWasSet(NodeId const nodeId,
227+
PortType const portType,
228+
PortIndex const portIndex);
227229

228230
/**
229231
* Signal emitted when model is about to remove port-related data.

include/QtNodes/internal/DataFlowGraphModel.hpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class NODE_EDITOR_PUBLIC DataFlowGraphModel : public AbstractGraphModel
3131
dataModelRegistry() { return _registry; }
3232

3333
public:
34-
3534
std::unordered_set<NodeId>
3635
allNodeIds() const override;
3736

@@ -77,10 +76,11 @@ class NODE_EDITOR_PUBLIC DataFlowGraphModel : public AbstractGraphModel
7776
PortRole role) const override;
7877

7978
bool
80-
setPortData(NodeId nodeId,
81-
PortType portType,
82-
PortIndex portIndex,
83-
PortRole role) const override;
79+
setPortData(NodeId nodeId,
80+
PortType portType,
81+
PortIndex portIndex,
82+
QVariant const& value,
83+
PortRole role = PortRole::Data) override;
8484

8585
bool
8686
deleteConnection(ConnectionId const connectionId) override;
@@ -106,6 +106,23 @@ class NODE_EDITOR_PUBLIC DataFlowGraphModel : public AbstractGraphModel
106106
void
107107
loadConnection(QJsonObject const & connJson) override;
108108

109+
/**
110+
* Fetches the NodeDelegateModel for the given `nodeId` and tries to cast the
111+
* stored pointer to the given type
112+
*/
113+
template<typename NodeDelegateModelType>
114+
NodeDelegateModelType*
115+
delegateModel(NodeId const nodeId)
116+
{
117+
auto it = _models.find(nodeId);
118+
if (it == _models.end())
119+
return nullptr;
120+
121+
auto model = dynamic_cast<NodeDelegateModelType*>(it->second.get());
122+
123+
return model;
124+
}
125+
109126
private:
110127
NodeId
111128
newNodeId() { return _nextNodeId++; }
@@ -124,12 +141,18 @@ class NODE_EDITOR_PUBLIC DataFlowGraphModel : public AbstractGraphModel
124141

125142
private Q_SLOTS:
126143
/**
127-
* Fuction is called by NodeDelegateModel when a node has new data to
128-
* propagate.
144+
* Fuction is called in three cases:
145+
*
146+
* - By underlying NodeDelegateModel when a node has new data to propagate.
147+
* @see DataFlowGraphModel::addNode
148+
* - When a new connection is created.
149+
* @see DataFlowGraphModel::addConnection
150+
* - When a node restored from JSON an needs to send data downstream.
151+
* @see DataFlowGraphModel::loadNode
129152
*/
130153
void
131-
onNodeDataUpdated(NodeId const nodeId,
132-
PortIndex const portIndex);
154+
onOutPortDataUpdated(NodeId const nodeId,
155+
PortIndex const portIndex);
133156

134157

135158
/// Function is called after detaching a connection.

include/QtNodes/internal/NodeData.hpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,13 @@ struct NODE_EDITOR_PUBLIC NodeDataType
1414
{
1515
QString id;
1616
QString name;
17-
18-
//friend bool operator<(QtNodes::NodeDataType const& d1,
19-
//QtNodes::NodeDataType const& d2)
20-
//{
21-
//return d1.id < d2.id;
22-
//}
23-
24-
//friend bool operator==(const QtNodes::NodeDataType& d1,
25-
//const QtNodes::NodeDataType& d2) noexcept
26-
//{
27-
//return d1.id == d2.id;
28-
//}
2917
};
3018

31-
/// Class represents data transferred between nodes.
32-
/// @param type is used for comparing the types
33-
/// The actual data is stored in subtypes
19+
/**
20+
* Class represents data transferred between nodes.
21+
* @param type is used for comparing the types
22+
* The actual data is stored in subtypes
23+
*/
3424
class NODE_EDITOR_PUBLIC NodeData
3525
{
3626
public:

src/DataFlowGraphModel.cpp

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace QtNodes
99

1010
DataFlowGraphModel::
1111
DataFlowGraphModel(std::shared_ptr<NodeDelegateModelRegistry> registry)
12-
: _registry(registry)
12+
: _registry(std::move(registry))
1313
, _nextNodeId{0}
1414
{}
1515

@@ -121,7 +121,7 @@ addNode(QString const nodeType)
121121

122122
connect(model.get(), &NodeDelegateModel::dataUpdated,
123123
[newId, this](PortIndex const portIndex)
124-
{ onNodeDataUpdated(newId, portIndex); });
124+
{ onOutPortDataUpdated(newId, portIndex); });
125125

126126
_models[newId] = std::move(model);
127127

@@ -190,8 +190,8 @@ addConnection(ConnectionId const connectionId)
190190

191191
Q_EMIT connectionCreated(connectionId);
192192

193-
onNodeDataUpdated(getNodeId(PortType::Out, connectionId),
194-
getPortIndex(PortType::Out, connectionId));
193+
onOutPortDataUpdated(getNodeId(PortType::Out, connectionId),
194+
getPortIndex(PortType::Out, connectionId));
195195
}
196196

197197

@@ -394,15 +394,35 @@ portData(NodeId nodeId,
394394

395395
bool
396396
DataFlowGraphModel::
397-
setPortData(NodeId nodeId,
398-
PortType portType,
399-
PortIndex portIndex,
400-
PortRole role) const
397+
setPortData(NodeId nodeId,
398+
PortType portType,
399+
PortIndex portIndex,
400+
QVariant const& value,
401+
PortRole role)
401402
{
402403
Q_UNUSED(nodeId);
403-
Q_UNUSED(portType);
404-
Q_UNUSED(portIndex);
405-
Q_UNUSED(role);
404+
405+
406+
QVariant result;
407+
408+
auto it = _models.find(nodeId);
409+
if (it == _models.end())
410+
return false;
411+
412+
auto& model = it->second;
413+
414+
switch (role)
415+
{
416+
case PortRole::Data:
417+
if (portType == PortType::In)
418+
model->setInData(value.value<std::shared_ptr<NodeData>>(),
419+
portIndex);
420+
break;
421+
422+
default:
423+
break;
424+
}
425+
406426

407427
return false;
408428
}
@@ -558,14 +578,12 @@ loadNode(QJsonObject const & nodeJson)
558578
{
559579
connect(model.get(), &NodeDelegateModel::dataUpdated,
560580
[restoredNodeId, this](PortIndex const portIndex)
561-
{ onNodeDataUpdated(restoredNodeId, portIndex); });
581+
{ onOutPortDataUpdated(restoredNodeId, portIndex); });
562582

563583
_models[restoredNodeId] = std::move(model);
564584

565585
Q_EMIT nodeCreated(restoredNodeId);
566586

567-
//
568-
569587
QJsonObject posJson = nodeJson["position"].toObject();
570588
QPointF const pos(posJson["x"].toDouble(),
571589
posJson["y"].toDouble());
@@ -574,7 +592,6 @@ loadNode(QJsonObject const & nodeJson)
574592
NodeRole::Position,
575593
pos);
576594

577-
578595
_models[restoredNodeId]->load(internalDataJson);
579596
}
580597
}
@@ -633,20 +650,27 @@ loadConnection(QJsonObject const & connJson)
633650

634651
void
635652
DataFlowGraphModel::
636-
onNodeDataUpdated(NodeId const nodeId,
637-
PortIndex const portIndex)
653+
onOutPortDataUpdated(NodeId const nodeId,
654+
PortIndex const portIndex)
638655
{
639656
std::unordered_set<ConnectionId> const& connected =
640657
connections(nodeId, PortType::Out, portIndex);
641658

642-
// We coudl also pull the data through the model::portData
643-
auto const outPortData = _models[nodeId]->outData(portIndex);
659+
QVariant const portDataToPropagate =
660+
portData(nodeId, PortType::Out, portIndex, PortRole::Data);
644661

645662
for (auto const& cn : connected)
646663
{
647-
_models[cn.inNodeId]->setInData(outPortData, cn.inPortIndex);
664+
// When restoring a model from file, not all models are loaded simultaneously.
665+
if (_models.find(cn.inNodeId) == _models.end())
666+
continue;
648667

649-
Q_EMIT portDataSet(cn.inNodeId, PortType::In, cn.inPortIndex);
668+
setPortData(cn.inNodeId, PortType::In,
669+
cn.inPortIndex, portDataToPropagate,
670+
PortRole::Data);
671+
672+
// Maybe this call should be on the receiving side.
673+
Q_EMIT inPortDataWasSet(cn.inNodeId, PortType::In, cn.inPortIndex);
650674
}
651675
}
652676

@@ -659,9 +683,13 @@ propagateEmptyDataTo(NodeId const nodeId,
659683
{
660684
auto const emptyData = std::shared_ptr<NodeData>();
661685

662-
_models[nodeId]->setInData(emptyData, portIndex);
686+
// When restoring a model from file, not all models are loaded simultaneously.
687+
if (_models.find(nodeId) != _models.end())
688+
{
689+
_models[nodeId]->setInData(emptyData, portIndex);
663690

664-
Q_EMIT portDataSet(nodeId, PortType::In, portIndex);
691+
Q_EMIT inPortDataWasSet(nodeId, PortType::In, portIndex);
692+
}
665693
}
666694

667695

src/DataFlowGraphicsScene.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ DataFlowGraphicsScene(DataFlowGraphModel& graphModel,
3737
: BasicGraphicsScene(graphModel, parent)
3838
, _graphModel(graphModel)
3939
{
40-
connect(&_graphModel, &AbstractGraphModel::portDataSet,
40+
connect(&_graphModel, &AbstractGraphModel::inPortDataWasSet,
4141
this, &DataFlowGraphicsScene::onPortDataSet);
4242
}
4343

0 commit comments

Comments
 (0)