diff --git a/src/gui/network/Network.cpp b/src/gui/network/Network.cpp index 7feee9b..362f6a5 100644 --- a/src/gui/network/Network.cpp +++ b/src/gui/network/Network.cpp @@ -42,11 +42,15 @@ Network::Network(QWidget* parent) node2->setPos(50, 50); scene_->addItem(node2); + NodeGraphic* node3 = new NodeGraphic(); + node3->setPos(50, 200); + scene_->addItem(node3); + NodeEdgeGraphic* edge1 = new NodeEdgeGraphic(node1->getOutput(0), node2->getInput(0)); scene_->addItem(edge1); - node1->addEdge(edge1); - node2->addEdge(edge1); + // node1->addEdge(edge1); + // node2->addEdge(edge1); @@ -75,11 +79,19 @@ void Network::socketClicked(SocketGraphic* socket, QMouseEvent *event) std::cout << "socket clicked\n"; if(!floatingEdge_) { + startSocket_=socket; std::cout << "creating floating edge\n"; floatingEdge_ = new FloatingEdgeGraphic(socket); scene_->addItem(floatingEdge_); floatingEdge_->setFloatPos(view_->mapToScene(event->pos())); } + // connect to opposite type + else if (socket->getIO()!=startSocket_->getIO()) + { + NodeEdgeGraphic* newEdge = new NodeEdgeGraphic(startSocket_, socket); + scene_->addItem(newEdge); + destroyFloatingEdge(); + } } void Network::destroyFloatingEdge() diff --git a/src/gui/network/Network.h b/src/gui/network/Network.h index b27ae9e..444b70f 100644 --- a/src/gui/network/Network.h +++ b/src/gui/network/Network.h @@ -17,7 +17,10 @@ private: QLayout* mainLayout_; NetworkGraphicsScene* scene_; NetworkGraphicsView* view_; + FloatingEdgeGraphic* floatingEdge_=nullptr; + SocketGraphic* startSocket_=nullptr; + void keyPressEvent(QKeyEvent *event) override; void destroyFloatingEdge(); diff --git a/src/gui/network/NodeGraphic.cpp b/src/gui/network/NodeGraphic.cpp index dc49017..635cb91 100644 --- a/src/gui/network/NodeGraphic.cpp +++ b/src/gui/network/NodeGraphic.cpp @@ -21,19 +21,30 @@ NodeGraphic::NodeGraphic(QGraphicsItem *parent) void NodeGraphic::initSockets() { - auto* socketInput = new SocketGraphic(this); + auto* socketInput = new SocketGraphic(SocketGraphic::SocketType::Input, this); socketInput->setPos(bodyRect_.center().x(), bodyRect_.top()); inputs_.push_back(socketInput); - auto* socketOutput = new SocketGraphic(this); + auto* socketOutput = new SocketGraphic(SocketGraphic::SocketType::Output, this); socketOutput->setPos(bodyRect_.center().x(), bodyRect_.bottom()); outputs_.push_back(socketOutput); } -void NodeGraphic::addEdge(NodeEdgeGraphic* edge) -{ - edges_.push_back(edge); -} +// void setInputEdge(NodeEdgeGraphic* edge, int indx) +// { + +// } + +// void setOutputEdge(NodeEdgeGraphic* edge, int indx) +// { + +// } + + +// void NodeGraphic::addEdge(NodeEdgeGraphic* edge) +// { +// edges_.push_back(edge); +// } SocketGraphic* NodeGraphic::getInput(int indx) const { diff --git a/src/gui/network/NodeGraphic.h b/src/gui/network/NodeGraphic.h index 03ed771..59acf6e 100644 --- a/src/gui/network/NodeGraphic.h +++ b/src/gui/network/NodeGraphic.h @@ -15,10 +15,13 @@ public: SocketGraphic* getInput(int indx) const; SocketGraphic* getOutput(int indx) const; - void addEdge(NodeEdgeGraphic* edge); + // void addEdge(NodeEdgeGraphic* edge); void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + // void setInputEdge(NodeEdgeGraphic* edge, int indx); + // void setOutputEdge(NodeEdgeGraphic* edge, int indx); + @@ -27,7 +30,7 @@ private: std::vector inputs_; std::vector outputs_; - std::vector edges_; + // std::vector edges_; std::string title_=""; int maxTitleLen_=10; diff --git a/src/gui/network/SocketGraphic.cpp b/src/gui/network/SocketGraphic.cpp index e356336..6d06f19 100644 --- a/src/gui/network/SocketGraphic.cpp +++ b/src/gui/network/SocketGraphic.cpp @@ -3,8 +3,8 @@ #include #include -SocketGraphic::SocketGraphic(QGraphicsItem *parent) -: QGraphicsItem(parent) +SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent) +: QGraphicsItem(parent), type_{type} { brushActive_ = QBrush("white"); brushInactive_ = QBrush("#9f9f9f"); @@ -15,10 +15,18 @@ SocketGraphic::SocketGraphic(QGraphicsItem *parent) QRectF SocketGraphic::boundingRect() const { float paddingScale = 10; - auto boundRect = QRect(-socketSize_/2.0f*paddingScale, -socketSize_/2.0f*paddingScale, socketSize_*paddingScale, socketSize_*paddingScale); + auto boundRect = QRect( + -socketSize_/2.0f*paddingScale, + type_==SocketType::Input ? -socketSize_/2.0f*paddingScale : 0, + socketSize_*paddingScale, + socketSize_/2.0f*paddingScale + ); return boundRect; } +SocketGraphic::SocketType SocketGraphic::getIO() { return type_; } + + void SocketGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->setPen(Qt::NoPen); diff --git a/src/gui/network/SocketGraphic.h b/src/gui/network/SocketGraphic.h index eb0dd9c..dc7d582 100644 --- a/src/gui/network/SocketGraphic.h +++ b/src/gui/network/SocketGraphic.h @@ -6,16 +6,24 @@ class SocketGraphic : public QGraphicsItem { public: - SocketGraphic(QGraphicsItem *parent = nullptr); QRectF boundingRect() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + enum class SocketType { + Input, + Output + }; + + SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent = nullptr); + SocketType getIO(); + private: int socketSize_ = 1; QBrush brushInactive_; QBrush brushActive_; bool hovered_=false; + SocketType type_; protected: void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override; void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;