From efc21cd62851736b2dc9f606c29148d5ea1991eb Mon Sep 17 00:00:00 2001 From: parker Date: Sat, 16 Aug 2025 01:34:13 +0100 Subject: [PATCH] fix: functional node deletion --- src/Engine/Operator/GeometryConnection.cpp | 8 +++++ src/Engine/Operator/GeometryConnection.h | 3 ++ src/Engine/Operator/GeometryOperator.cpp | 38 +++++++++++++++++++++- src/Engine/Operator/GeometryOperator.h | 8 +++-- src/Gui/Network/Network.cpp | 11 +++---- src/Gui/Network/NodeEdgeGraphic.cpp | 13 +++++--- src/Gui/Network/NodeEdgeGraphic.h | 8 +++-- src/Gui/Network/SocketGraphic.cpp | 2 +- 8 files changed, 75 insertions(+), 16 deletions(-) diff --git a/src/Engine/Operator/GeometryConnection.cpp b/src/Engine/Operator/GeometryConnection.cpp index 55c0170..5d3e097 100644 --- a/src/Engine/Operator/GeometryConnection.cpp +++ b/src/Engine/Operator/GeometryConnection.cpp @@ -1,4 +1,5 @@ #include "Engine/Operator/GeometryConnection.h" +#include "Engine/Network/NetworkManager.h" 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} @@ -10,3 +11,10 @@ enzo::nt::OpId enzo::nt::GeometryConnection::getOutputOpId() const {return outpu unsigned int enzo::nt::GeometryConnection::getInputIndex() const {return inputIndex_; } unsigned int enzo::nt::GeometryConnection::getOutputIndex() const {return outputIndex_; } +void enzo::nt::GeometryConnection::remove() +{ + NetworkManager& nm = nt::nm(); + nm.getGeoOperator(inputOperatorId_).removeOutputConnection(this); + nm.getGeoOperator(outputOperatorId_).removeInputConnection(inputIndex_); +} + diff --git a/src/Engine/Operator/GeometryConnection.h b/src/Engine/Operator/GeometryConnection.h index dcfa922..6dbc14f 100644 --- a/src/Engine/Operator/GeometryConnection.h +++ b/src/Engine/Operator/GeometryConnection.h @@ -16,6 +16,9 @@ public: enzo::nt::OpId getOutputOpId() const; unsigned int getInputIndex() const; unsigned int getOutputIndex() const; + + void remove(); + // bool isValid(); private: enzo::nt::OpId inputOperatorId_; enzo::nt::OpId outputOperatorId_; diff --git a/src/Engine/Operator/GeometryOperator.cpp b/src/Engine/Operator/GeometryOperator.cpp index a8cf344..99d6e81 100644 --- a/src/Engine/Operator/GeometryOperator.cpp +++ b/src/Engine/Operator/GeometryOperator.cpp @@ -6,11 +6,12 @@ #include "Engine/Parameter/Parameter.h" #include "Engine/Parameter/Template.h" #include +#include #include "icecream.hpp" using namespace enzo; -void enzo::nt::connectOperators(enzo::nt::OpId inputOpId, unsigned int inputIndex, enzo::nt::OpId outputOpId, unsigned int outputIndex) +std::weak_ptr enzo::nt::connectOperators(enzo::nt::OpId inputOpId, unsigned int inputIndex, enzo::nt::OpId outputOpId, unsigned int outputIndex) { auto& nm = nt::nm(); @@ -24,6 +25,8 @@ void enzo::nt::connectOperators(enzo::nt::OpId inputOpId, unsigned int inputInde // set input on the lower operator outputOp.addInputConnection(newConnection); + + return newConnection; } nt::GeometryOperator::GeometryOperator(enzo::nt::OpId opId, op::OpInfo opInfo) @@ -102,6 +105,39 @@ void nt::GeometryOperator::addOutputConnection(std::shared_ptrgetInputIndex() == inputIndex) + { + inputConnections_.erase(it); + dirtyNode(); + std::cout << "removing input connection\n"; + return; + } + } + std::cerr << "Couldn't remove input connection: " << inputIndex << "\n"; +} + +void nt::GeometryOperator::removeOutputConnection(const nt::GeometryConnection* connectionPtr) +{ + for(auto it=outputConnections_.begin(); it!=outputConnections_.end(); ++it) + { + const nt::GeometryConnection* otherConnectionPtr = (*it).get(); + if(connectionPtr == otherConnectionPtr) + { + outputConnections_.erase(it); + dirtyNode(); + std::cout << "removing output connection\n"; + return; + } + } + std::cerr << "Couldn't remove output connection\n"; + +} + + std::weak_ptr nt::GeometryOperator::getParameter(std::string parameterName) { for(auto parm : parameters_) diff --git a/src/Engine/Operator/GeometryOperator.h b/src/Engine/Operator/GeometryOperator.h index 9dddca0..fd00116 100644 --- a/src/Engine/Operator/GeometryOperator.h +++ b/src/Engine/Operator/GeometryOperator.h @@ -8,7 +8,7 @@ #include namespace enzo::nt { -void connectOperators(enzo::nt::OpId inputOpId, unsigned int inputIndex, enzo::nt::OpId outputOpId, unsigned int outputIndex); +std::weak_ptr connectOperators(enzo::nt::OpId inputOpId, unsigned int inputIndex, enzo::nt::OpId outputOpId, unsigned int outputIndex); class GeometryOperator { @@ -20,10 +20,14 @@ public: GeometryOperator& operator=(const GeometryOperator&) = delete; void cookOp(op::Context context); - geo::Geometry& getOutputGeo(unsigned outputIndex) const; + geo::Geometry& getOutputGeo(unsigned int outputIndex) const; void addInputConnection(std::shared_ptr connection); void addOutputConnection(std::shared_ptr connection); + + void removeInputConnection(unsigned int inputIndex); + void removeOutputConnection(const nt::GeometryConnection* connection); + std::vector> getInputConnections() const; std::vector> getOutputConnections() const; std::optional getInputConnection(size_t index) const; diff --git a/src/Gui/Network/Network.cpp b/src/Gui/Network/Network.cpp index b0892e5..0ade0aa 100644 --- a/src/Gui/Network/Network.cpp +++ b/src/Gui/Network/Network.cpp @@ -67,7 +67,7 @@ void Network::deleteEdge(QGraphicsItem* edge) // NOTE: deleting edge kept giving me segmentation faults // I coundn't figure it out so I'm just leaving it for now // delete edge; - static_cast(edge)->cleanUp(); + static_cast(edge)->remove(); std::cout << "finished deleting edge\n----\n"; } @@ -158,13 +158,12 @@ void Network::socketClicked(SocketGraphic* socket, QMouseEvent *event) std::cout << "CONNECTING opid: " << inputNodeSocket->getOpId() << " -> " << outputNodeSocket->getOpId() << "\n"; - nt::connectOperators(inputNodeSocket->getOpId(), inputNodeSocket->getIndex(), outputNodeSocket->getOpId(), outputNodeSocket->getIndex()); + std::weak_ptr newConnection = nt::connectOperators(inputNodeSocket->getOpId(), inputNodeSocket->getIndex(), outputNodeSocket->getOpId(), outputNodeSocket->getIndex()); + NodeEdgeGraphic* newEdgeGraphic = new NodeEdgeGraphic(outputNodeSocket, inputNodeSocket, newConnection); - NodeEdgeGraphic* newEdge = new NodeEdgeGraphic(outputNodeSocket, inputNodeSocket); - - newEdge->setPos(outputNodeSocket->scenePos(), inputNodeSocket->scenePos()); - scene_->addItem(newEdge); + newEdgeGraphic->setPos(outputNodeSocket->scenePos(), inputNodeSocket->scenePos()); + scene_->addItem(newEdgeGraphic); destroyFloatingEdge(); } } diff --git a/src/Gui/Network/NodeEdgeGraphic.cpp b/src/Gui/Network/NodeEdgeGraphic.cpp index 716e389..1ecd41f 100644 --- a/src/Gui/Network/NodeEdgeGraphic.cpp +++ b/src/Gui/Network/NodeEdgeGraphic.cpp @@ -7,8 +7,8 @@ #include #include "icecream.hpp" -NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent) -: QGraphicsItem(parent), socket1_{socket1}, socket2_{socket2}, defaultColor_{QColor("white")} +NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, std::weak_ptr connection, QGraphicsItem *parent) +: QGraphicsItem(parent), socket1_{socket1}, socket2_{socket2}, defaultColor_{QColor("white")}, connection_{connection} { setAcceptHoverEvents(true); @@ -39,7 +39,7 @@ void NodeEdgeGraphic::updateDeleteHighlightPen() NodeEdgeGraphic::~NodeEdgeGraphic() { std::cout << "edge destructor\n"; - cleanUp(); + remove(); std::cout << "destructor finished\n"; } @@ -149,7 +149,7 @@ void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *o } -void NodeEdgeGraphic::cleanUp() +void NodeEdgeGraphic::remove() { // TODO: possible memory leak // these probably aren't necessary but i'm trying to fix a bug @@ -160,4 +160,9 @@ void NodeEdgeGraphic::cleanUp() scene()->removeItem(this); socket1_->removeEdge(this); socket2_->removeEdge(this); + + if(auto connectionShared = connection_.lock()) + { + connectionShared->remove(); + } } diff --git a/src/Gui/Network/NodeEdgeGraphic.h b/src/Gui/Network/NodeEdgeGraphic.h index c5cc6e0..67984fb 100644 --- a/src/Gui/Network/NodeEdgeGraphic.h +++ b/src/Gui/Network/NodeEdgeGraphic.h @@ -1,14 +1,16 @@ #pragma once #include #include "Gui/Network/SocketGraphic.h" +#include "Engine/Operator/GeometryConnection.h" #include +#include class NodeEdgeGraphic : public QGraphicsItem { public: - NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent = nullptr); + NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, std::weak_ptr connection, QGraphicsItem *parent = nullptr); ~NodeEdgeGraphic(); QRectF boundingRect() const override; @@ -20,7 +22,7 @@ public: void setPos(QPointF pos1, QPointF pos2); void setStartPos(QPointF pos); void setEndPos(QPointF pos); - void cleanUp(); + void remove(); void setDeleteHighlight(bool state); QPen deleteHighlightPen_; QPen defaultPen_; @@ -41,6 +43,8 @@ private: qreal padding_=40; QPointF hoverPos_; + std::weak_ptr connection_; + void updatePath(); protected: void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override; diff --git a/src/Gui/Network/SocketGraphic.cpp b/src/Gui/Network/SocketGraphic.cpp index b947f0d..65a035b 100644 --- a/src/Gui/Network/SocketGraphic.cpp +++ b/src/Gui/Network/SocketGraphic.cpp @@ -32,7 +32,7 @@ void SocketGraphic::addEdge(NodeEdgeGraphic* edge) { continue; } - edge->cleanUp(); + edge->remove(); } edges_.clear(); }