MCPcopy Create free account
hub / github.com/BehaviorTree/Groot / substituteNode

Method substituteNode

bt_editor/graphic_container.cpp:388–448  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

386}
387
388QtNodes::Node* GraphicContainer::substituteNode(Node *old_node, const QString& new_node_ID)
389{
390 const QSignalBlocker blocker(this);
391 QPointF prev_pos = _scene->getNodePosition( *old_node );
392 double prev_width = old_node->nodeGeometry().width();
393
394 auto& new_node = scene()->createNodeAtPos( new_node_ID, new_node_ID, prev_pos);
395
396 auto bt_old_node = dynamic_cast<BehaviorTreeDataModel*>( old_node->nodeDataModel());
397 auto bt_new_node = dynamic_cast<BehaviorTreeDataModel*>( new_node.nodeDataModel());
398
399 if( bt_old_node && bt_new_node)
400 {
401 // if the old one contains an edited instance name, use it in new_node
402 if( bt_old_node->instanceName() != bt_old_node->registrationName() &&
403 bt_new_node->model().type != NodeType::SUBTREE )
404 {
405 bt_new_node->setInstanceName( bt_old_node->instanceName() );
406 }
407 // if the old one contains an editedport remapping, use it in new_node
408 for(const auto& old_it: bt_old_node->getCurrentPortMapping() )
409 {
410 auto new_mapping = bt_new_node->getCurrentPortMapping();
411 if( old_it.second.isEmpty() == false && new_mapping.count( old_it.first ) )
412 {
413 bt_new_node->setPortMapping( old_it.first, old_it.second );
414 }
415 }
416 }
417
418 QPointF new_pos = prev_pos;
419 double new_width = new_node.nodeGeometry().width();
420
421 new_pos.setX( prev_pos.x() - (new_width - prev_width)*0.5 );
422 _scene->setNodePosition(new_node, new_pos);
423
424 if( old_node->nodeDataModel()->nPorts( PortType::In ) == 1 &&
425 new_node.nodeDataModel()->nPorts( PortType::In ) == 1 )
426 {
427 auto conn_in = old_node->nodeState().connections(PortType::In, 0);
428 for(auto it: conn_in)
429 {
430 auto child_node = it.second->getNode(PortType::Out);
431 _scene->createConnection( new_node, 0, *child_node, 0 );
432 }
433 }
434
435 if( old_node->nodeDataModel()->nPorts( PortType::Out ) == 1 &&
436 new_node.nodeDataModel()->nPorts( PortType::Out ) == 1 )
437 {
438 auto conn_in = old_node->nodeState().connections(PortType::Out, 0);
439 for(auto it: conn_in)
440 {
441 auto child_node = it.second->getNode(PortType::In);
442 _scene->createConnection( *child_node, 0, new_node, 0 );
443 }
444 }
445 _scene->removeNode(*old_node);

Callers 1

onTreeNodeEditedMethod · 0.80

Calls 4

getCurrentPortMappingMethod · 0.80
setPortMappingMethod · 0.80
setInstanceNameMethod · 0.45
nPortsMethod · 0.45

Tested by

no test coverage detected