@@ -9,7 +9,7 @@ namespace QtNodes
99
1010DataFlowGraphModel::
1111DataFlowGraphModel (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
395395bool
396396DataFlowGraphModel::
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
634651void
635652DataFlowGraphModel::
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
0 commit comments