From fa2c106768da72dc6381e2bab59fd3aa4f826ab4 Mon Sep 17 00:00:00 2001 From: parker Date: Mon, 23 Jun 2025 19:30:53 +0100 Subject: [PATCH] fix: edge 1 frame lag --- src/gui/network/Network.cpp | 2 + src/gui/network/NodeEdgeGraphic.cpp | 2 +- src/gui/network/NodeGraphic.cpp | 74 ++++++++++++++++++++++------- src/gui/network/NodeGraphic.h | 8 +++- src/gui/network/SocketGraphic.cpp | 4 +- 5 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/gui/network/Network.cpp b/src/gui/network/Network.cpp index 3800bdc..8c606b6 100644 --- a/src/gui/network/Network.cpp +++ b/src/gui/network/Network.cpp @@ -75,6 +75,7 @@ void Network::resizeEvent(QResizeEvent *event) void Network::deleteEdge(QGraphicsItem* edge) { + std::cout << "----\ndeleting edge\n"; if(prevHoverItem_==edge) { prevHoverItem_=nullptr; @@ -85,6 +86,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; + std::cout << "finished deleting edge\n----\n"; } void Network::mousePressEvent(QMouseEvent *event) diff --git a/src/gui/network/NodeEdgeGraphic.cpp b/src/gui/network/NodeEdgeGraphic.cpp index a327ffc..915fc5f 100644 --- a/src/gui/network/NodeEdgeGraphic.cpp +++ b/src/gui/network/NodeEdgeGraphic.cpp @@ -86,7 +86,7 @@ void NodeEdgeGraphic::useDefaultColor() void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - std::cout << "painting\n"; + // std::cout << "painting\n"; pen_.setCapStyle(Qt::RoundCap); painter->setPen(pen_); painter->drawPath(path_); diff --git a/src/gui/network/NodeGraphic.cpp b/src/gui/network/NodeGraphic.cpp index 84c21f9..d3b9081 100644 --- a/src/gui/network/NodeGraphic.cpp +++ b/src/gui/network/NodeGraphic.cpp @@ -6,6 +6,7 @@ #include #include "gui/network/SocketGraphic.h" #include +#include NodeGraphic::NodeGraphic(QGraphicsItem *parent) : QGraphicsItem(parent) @@ -15,7 +16,7 @@ NodeGraphic::NodeGraphic(QGraphicsItem *parent) title_ = "hello world"; bodyRect_ = QRect(-10, -10, 10*maxTitleLen_, 20); - setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges); + setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); initSockets(); } @@ -23,11 +24,11 @@ NodeGraphic::NodeGraphic(QGraphicsItem *parent) void NodeGraphic::initSockets() { auto* socketInput = new SocketGraphic(SocketGraphic::SocketType::Input, this); - socketInput->setPos(bodyRect_.center().x(), bodyRect_.top()); + socketInput->setPos(getSocketPosition(0, SocketGraphic::SocketType::Input)); inputs_.push_back(socketInput); auto* socketOutput = new SocketGraphic(SocketGraphic::SocketType::Output, this); - socketOutput->setPos(bodyRect_.center().x(), bodyRect_.bottom()); + socketOutput->setPos(getSocketPosition(0, SocketGraphic::SocketType::Output)); outputs_.push_back(socketOutput); } @@ -96,19 +97,58 @@ void NodeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio } - -QVariant NodeGraphic::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) +void NodeGraphic::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if (change == ItemPositionChange && scene()) { - for(auto socket : inputs_) - { - socket->posChanged(value.toPointF()); - } - for(auto socket : outputs_) - { - socket->posChanged(value.toPointF()); - } - }; - - return QGraphicsItem::itemChange(change, value); + QGraphicsItem::mouseMoveEvent(event); + updatePositions(event->scenePos()); } + +// void NodeGraphic::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +// { +// updatePositions(event->scenePos()); +// QGraphicsItem::mouseReleaseEvent(event); +// } + +void NodeGraphic::updatePositions(QPointF pos) +{ + // NOTE: in the future I might store socket ids as internal member variables, + // but for now I'm just going based on order + for(int socketIndex=0; socketIndexposChanged(getSocketScenePosition(socketIndex, SocketGraphic::SocketType::Input)); + } + for(int socketIndex=0; socketIndexposChanged(getSocketScenePosition(socketIndex, SocketGraphic::SocketType::Output)); + } +} + +QPointF NodeGraphic::getSocketPosition(int socketIndex, SocketGraphic::SocketType socketType) +{ + float xPos, yPos; + xPos = bodyRect_.center().x(); + yPos = socketType == SocketGraphic::SocketType::Input ? bodyRect_.top() : bodyRect_.bottom(); + + return QPointF(xPos, yPos); +} +QPointF NodeGraphic::getSocketScenePosition(int socketIndex, SocketGraphic::SocketType socketType) +{ + return this->pos()+getSocketPosition(socketIndex, socketType); +} + + +// QVariant NodeGraphic::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) +// { +// if (change == ItemPositionChange && scene()) { +// for(auto socket : inputs_) +// { +// socket->posChanged(value.toPointF()); +// } +// for(auto socket : outputs_) +// { +// socket->posChanged(value.toPointF()); +// } +// }; + +// return QGraphicsItem::itemChange(change, value); +// } diff --git a/src/gui/network/NodeGraphic.h b/src/gui/network/NodeGraphic.h index 643060a..05b5bb6 100644 --- a/src/gui/network/NodeGraphic.h +++ b/src/gui/network/NodeGraphic.h @@ -14,6 +14,8 @@ public: SocketGraphic* getInput(int indx) const; SocketGraphic* getOutput(int indx) const; + QPointF getSocketPosition(int socketIndex, SocketGraphic::SocketType socketType); + QPointF getSocketScenePosition(int socketIndex, SocketGraphic::SocketType socketType); // void addEdge(NodeEdgeGraphic* edge); @@ -38,8 +40,12 @@ private: int socketSize_ = 1; int inputSocketCnt_=0; int outputSocketCnt_=0; + + void updatePositions(QPointF pos); protected: - QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override; + // QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override; + void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; + // void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; }; diff --git a/src/gui/network/SocketGraphic.cpp b/src/gui/network/SocketGraphic.cpp index 3a1b1e7..273a10f 100644 --- a/src/gui/network/SocketGraphic.cpp +++ b/src/gui/network/SocketGraphic.cpp @@ -53,11 +53,11 @@ void SocketGraphic::posChanged(QPointF pos) // edge->setPos(startSocket_->scenePos(), socket->scenePos()); if(type_==SocketType::Input) { - edge->setStartPos(this->scenePos()); + edge->setStartPos(pos); } else if(type_==SocketType::Output) { - edge->setEndPos(this->scenePos()); + edge->setEndPos(pos); } } }