From b81dc7a8892c93c011656e6064c57619b44cf7ae Mon Sep 17 00:00:00 2001 From: parker Date: Tue, 19 Aug 2025 21:25:20 +0100 Subject: [PATCH] fix: connection switching --- src/Engine/Operator/GeometryConnection.cpp | 5 ++++ src/Engine/Operator/GeometryConnection.h | 7 +++++ src/Engine/Operator/GeometryOperator.cpp | 24 ++++++++++------- src/Engine/Operator/GeometryOperator.h | 3 ++- src/Gui/Network/NodeEdgeGraphic.cpp | 14 +++++++--- src/Gui/Network/NodeEdgeGraphic.h | 2 +- src/Gui/Network/SocketGraphic.cpp | 31 +++++++++++----------- 7 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/Engine/Operator/GeometryConnection.cpp b/src/Engine/Operator/GeometryConnection.cpp index 5d3e097..e4121e2 100644 --- a/src/Engine/Operator/GeometryConnection.cpp +++ b/src/Engine/Operator/GeometryConnection.cpp @@ -1,5 +1,6 @@ #include "Engine/Operator/GeometryConnection.h" #include "Engine/Network/NetworkManager.h" +#include enzo::nt::GeometryConnection::GeometryConnection(enzo::nt::OpId inputOpId, unsigned int inputIndex, enzo::nt::OpId outputOpId, unsigned int outputIndex) :inputOperatorId_{inputOpId}, inputIndex_{inputIndex}, outputOperatorId_{outputOpId}, outputIndex_{outputIndex} @@ -14,7 +15,11 @@ unsigned int enzo::nt::GeometryConnection::getOutputIndex() const {return output void enzo::nt::GeometryConnection::remove() { NetworkManager& nm = nt::nm(); + std::cout << "removing connection " << this; nm.getGeoOperator(inputOperatorId_).removeOutputConnection(this); nm.getGeoOperator(outputOperatorId_).removeInputConnection(inputIndex_); + removed(); } + + diff --git a/src/Engine/Operator/GeometryConnection.h b/src/Engine/Operator/GeometryConnection.h index 6dbc14f..17eef0e 100644 --- a/src/Engine/Operator/GeometryConnection.h +++ b/src/Engine/Operator/GeometryConnection.h @@ -1,6 +1,8 @@ #pragma once #include "Engine/Types.h" +#include + namespace enzo::nt { class GeometryOperator; @@ -16,7 +18,12 @@ public: enzo::nt::OpId getOutputOpId() const; unsigned int getInputIndex() const; unsigned int getOutputIndex() const; + friend std::ostream& operator<<(std::ostream& os, const GeometryConnection& p) + { + return os << p.inputOperatorId_ << ":" << p.inputIndex_ << " -> " << p.outputOperatorId_ << ":" << p.outputIndex_ << "\n"; + } + boost::signals2::signal removed; void remove(); // bool isValid(); private: diff --git a/src/Engine/Operator/GeometryOperator.cpp b/src/Engine/Operator/GeometryOperator.cpp index 99d6e81..b6327c4 100644 --- a/src/Engine/Operator/GeometryOperator.cpp +++ b/src/Engine/Operator/GeometryOperator.cpp @@ -1,4 +1,5 @@ #include "Engine/Operator/GeometryOperator.h" +#include #include #include "Engine/Network/NetworkManager.h" #include @@ -78,22 +79,24 @@ geo::Geometry& enzo::nt::GeometryOperator::getOutputGeo(unsigned outputIndex) co void nt::GeometryOperator::addInputConnection(std::shared_ptr newConnection) { // delete previous input - for(auto it=inputConnections_.begin(); it!=inputConnections_.end();) + std::shared_ptr previousConection = nullptr; + IC(); + for(auto it=inputConnections_.begin(); it!=inputConnections_.end(); ++it) { if((*it)->getOutputIndex()==newConnection->getOutputIndex()) { - inputConnections_.erase(it); - } - else - { - ++it; + previousConection = *it; + break; } } + if(previousConection) + { + previousConection->remove(); + } // add new newConnection inputConnections_.push_back(newConnection); - IC(); dirtyNode(); } @@ -113,11 +116,12 @@ void nt::GeometryOperator::removeInputConnection(unsigned int inputIndex) { inputConnections_.erase(it); dirtyNode(); - std::cout << "removing input connection\n"; + std::cerr << "removing input connection\n"; return; } } std::cerr << "Couldn't remove input connection: " << inputIndex << "\n"; + IC(inputIndex, opId_, inputConnections_.size()); } void nt::GeometryOperator::removeOutputConnection(const nt::GeometryConnection* connectionPtr) @@ -177,13 +181,13 @@ std::string nt::GeometryOperator::getTypeName() } -std::optional nt::GeometryOperator::getInputConnection(size_t index) const +std::optional> nt::GeometryOperator::getInputConnection(size_t index) const { for(auto it=inputConnections_.begin(); it!=inputConnections_.end();) { if((*it)->getOutputIndex()==index) { - return std::optional(**it); + return std::cref(**it); } } return std::nullopt; diff --git a/src/Engine/Operator/GeometryOperator.h b/src/Engine/Operator/GeometryOperator.h index fd00116..51091e3 100644 --- a/src/Engine/Operator/GeometryOperator.h +++ b/src/Engine/Operator/GeometryOperator.h @@ -4,6 +4,7 @@ #include "Engine/Operator/GeometryOpDef.h" #include "Engine/Parameter/Parameter.h" #include "Engine/Types.h" +#include #include #include @@ -30,7 +31,7 @@ public: std::vector> getInputConnections() const; std::vector> getOutputConnections() const; - std::optional getInputConnection(size_t index) const; + std::optional> getInputConnection(size_t index) const; std::vector> getParameters(); std::weak_ptr getParameter(std::string parameterName); diff --git a/src/Gui/Network/NodeEdgeGraphic.cpp b/src/Gui/Network/NodeEdgeGraphic.cpp index 1ecd41f..ba8ee5b 100644 --- a/src/Gui/Network/NodeEdgeGraphic.cpp +++ b/src/Gui/Network/NodeEdgeGraphic.cpp @@ -21,6 +21,11 @@ NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, deleteHighlightPen_.setWidth(2); updateDeleteHighlightPen(); + // remove self when connection is removed + if(auto connectionShared = connection_.lock()) + { + connectionShared->removed.connect([this](){ this->remove(false); }); + } socket1_->addEdge(this); socket2_->addEdge(this); @@ -149,7 +154,7 @@ void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *o } -void NodeEdgeGraphic::remove() +void NodeEdgeGraphic::remove(bool full) { // TODO: possible memory leak // these probably aren't necessary but i'm trying to fix a bug @@ -161,8 +166,11 @@ void NodeEdgeGraphic::remove() socket1_->removeEdge(this); socket2_->removeEdge(this); - if(auto connectionShared = connection_.lock()) + if(full) { - connectionShared->remove(); + if(auto connectionShared = connection_.lock()) + { + connectionShared->remove(); + } } } diff --git a/src/Gui/Network/NodeEdgeGraphic.h b/src/Gui/Network/NodeEdgeGraphic.h index 67984fb..3bdeefe 100644 --- a/src/Gui/Network/NodeEdgeGraphic.h +++ b/src/Gui/Network/NodeEdgeGraphic.h @@ -22,7 +22,7 @@ public: void setPos(QPointF pos1, QPointF pos2); void setStartPos(QPointF pos); void setEndPos(QPointF pos); - void remove(); + void remove(bool full=true); void setDeleteHighlight(bool state); QPen deleteHighlightPen_; QPen defaultPen_; diff --git a/src/Gui/Network/SocketGraphic.cpp b/src/Gui/Network/SocketGraphic.cpp index 65a035b..91d0a2b 100644 --- a/src/Gui/Network/SocketGraphic.cpp +++ b/src/Gui/Network/SocketGraphic.cpp @@ -22,29 +22,28 @@ unsigned int SocketGraphic::getIndex() const void SocketGraphic::addEdge(NodeEdgeGraphic* edge) { - std::cout << "adding edge\n"; - if(getIO()==enzo::nt::SocketIOType::Input && edges_.size()>0) - { - std::unordered_set edgesCopy = edges_; - for(NodeEdgeGraphic* edge : edgesCopy) - { - if(!edge) - { - continue; - } - edge->remove(); - } - edges_.clear(); - } + // std::cout << "adding edge\n"; + // const bool isInput = getIO()==enzo::nt::SocketIOType::Input; + // if(isInput && edges_.size()>0) + // { + // std::unordered_set edgesCopy = edges_; + // for(NodeEdgeGraphic* edge : edgesCopy) + // { + // if(!edge) + // { + // continue; + // } + // edge->remove(); + // } + // edges_.clear(); + // } edges_.insert(edge); } void SocketGraphic::removeEdge(NodeEdgeGraphic* edge) { std::cout << "removing edge\n"; - std::cout << "before size: " << edges_.size() << "\n"; edges_.erase(edge); - std::cout << "after size: " << edges_.size() << "\n"; // auto it = find(edges_.begin(), edges_.end(), edge); // if(it!=edges_.end()) // {