feat: add node edges

This commit is contained in:
parker
2025-06-20 23:12:09 +01:00
parent cf876320d8
commit 20c6ad2b64
10 changed files with 211 additions and 28 deletions

View File

@@ -1,9 +1,87 @@
#include "gui/network/NodeGraphic.h"
#include <QTextDocument>
#include <iostream>
#include <qgraphicsitem.h>
#include <stdexcept>
#include <string>
#include "gui/network/SocketGraphic.h"
NodeGraphic::NodeGraphic(QGraphicsItem *parent)
: QGraphicsItem(parent)
{
title_ = new QGraphicsTextItem(this);
title_->setPlainText("hello world!");
setFlag(QGraphicsItem::ItemIsMovable);
maxTitleLen_ = 10;
socketSize_ = 3;
title_ = "hello world";
bodyRect_ = QRect(-10, -10, 10*maxTitleLen_, 20);
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
initSockets();
}
void NodeGraphic::initSockets()
{
auto* socketInput = new SocketGraphic(this);
socketInput->setPos(bodyRect_.center().x(), bodyRect_.top());
inputs_.push_back(socketInput);
auto* socketOutput = new SocketGraphic(this);
socketOutput->setPos(bodyRect_.center().x(), bodyRect_.bottom());
outputs_.push_back(socketOutput);
}
void NodeGraphic::addEdge(NodeEdgeGraphic* edge)
{
edges_.push_back(edge);
}
SocketGraphic* NodeGraphic::getInput(int indx) const
{
if(inputs_.size()==0)
{
throw std::out_of_range("Can't access inputs when node has no inputs: " + std::to_string(indx));
}
else if(indx>inputs_.size()-1)
{
throw std::out_of_range("Can't access input: " + std::to_string(indx));
}
return inputs_.at(indx);
}
SocketGraphic* NodeGraphic::getOutput(int indx) const
{
if(outputs_.size()==0)
{
throw std::out_of_range("Can't access outputs when node has no outputs: " + std::to_string(indx));
}
else if(indx>inputs_.size()-1)
{
throw std::out_of_range("Can't access input: " + std::to_string(indx));
}
return outputs_.at(indx);
}
QRectF NodeGraphic::boundingRect() const
{
QRectF boundRect = bodyRect_;
float padding = 10;
boundRect.adjust(-padding, -padding, padding, padding);
return boundRect;
}
void NodeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
QBrush whiteBrush = QBrush("white");
QPen greenPen = QPen(Qt::green);
greenPen.setWidth(6);
if(isSelected()) painter->setPen(QPen(QColor("#edd044")));
else painter->setPen(Qt::NoPen);
painter->setBrush(QBrush(QColor("#1b1b1b")));
painter->drawRoundedRect(bodyRect_, 5, 5);
painter->setPen(QPen(QColor("white")));
painter->drawText( bodyRect_, Qt::AlignCenter, title_.c_str());
}