diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f1d01b..8782d5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ qt_add_executable(${AppExec} src/gui/network/NetworkGraphicsScene.cpp src/gui/network/Network.cpp src/gui/network/NodeGraphic.cpp + src/gui/network/SocketGraphic.cpp + src/gui/network/NodeEdgeGraphic.cpp ) target_link_libraries(${AppExec} PRIVATE Qt6::Core Qt6::Widgets) diff --git a/src/gui/network/Network.cpp b/src/gui/network/Network.cpp index 01bd1ae..7717ef9 100644 --- a/src/gui/network/Network.cpp +++ b/src/gui/network/Network.cpp @@ -1,5 +1,6 @@ #include "gui/network/Network.h" #include "gui/network/NetworkGraphicsView.h" +#include "gui/network/NodeEdgeGraphic.h" #include "gui/network/NetworkGraphicsScene.h" #include "gui/network/NodeGraphic.h" #include @@ -33,5 +34,15 @@ Network::Network(QWidget* parent) NodeGraphic* node1 = new NodeGraphic(); scene->addItem(node1); + NodeGraphic* node2 = new NodeGraphic(); + scene->addItem(node2); + + NodeEdgeGraphic* edge1 = new NodeEdgeGraphic(node1->getOutput(0), node2->getInput(0)); + scene->addItem(edge1); + + node1->addEdge(edge1); + node2->addEdge(edge1); + + mainLayout_->addWidget(view); } diff --git a/src/gui/network/NetworkGraphicsScene.cpp b/src/gui/network/NetworkGraphicsScene.cpp index d66f9ba..22c92fd 100644 --- a/src/gui/network/NetworkGraphicsScene.cpp +++ b/src/gui/network/NetworkGraphicsScene.cpp @@ -13,7 +13,7 @@ NetworkGraphicsScene::NetworkGraphicsScene() setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_); - setBackgroundBrush(QColor("#1b1b1b")); + setBackgroundBrush(QColor("#282828")); } void NetworkGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) @@ -26,11 +26,6 @@ void NetworkGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect) int left = floor(rect.left()); int right = ceil(rect.right()); - std::cout << "top: " << top << "\n"; - std::cout << "bottom: " << bottom << "\n"; - std::cout << "left: " << left << "\n"; - std::cout << "right: " << right << "\n"; - QPen gridPen(QColor("#323232")); painter->setPen(gridPen); diff --git a/src/gui/network/NetworkGraphicsView.cpp b/src/gui/network/NetworkGraphicsView.cpp index b2e11bc..6ff879a 100644 --- a/src/gui/network/NetworkGraphicsView.cpp +++ b/src/gui/network/NetworkGraphicsView.cpp @@ -32,7 +32,7 @@ void NetworkGraphicsView::initUI() void NetworkGraphicsView::mousePressEvent(QMouseEvent *event) { if( - event->button() & Qt::MiddleButton + event->button() == Qt::MiddleButton ) { panStartPos = event->pos(); @@ -60,8 +60,6 @@ void NetworkGraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent) { QPointF pos = mouseEvent->pos(); QPointF delta = pos-panStartPos; - std::cout << "pos: " << mouseEvent->pos().x() << " " << mouseEvent->pos().y() << "\n"; - std::cout << "delta: " << delta.x() << " " << delta.y() << "\n"; float speed = 1.0f; horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x()); @@ -77,8 +75,6 @@ void NetworkGraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent) void NetworkGraphicsView::wheelEvent(QWheelEvent *event) { - std::cout << "delta: " << event->angleDelta().x() << "\n"; - std::cout << "delta: " << event->angleDelta().y() << "\n"; int delta = event->angleDelta().y(); if(delta > 0) { diff --git a/src/gui/network/NodeEdgeGraphic.cpp b/src/gui/network/NodeEdgeGraphic.cpp new file mode 100644 index 0000000..3d6fefe --- /dev/null +++ b/src/gui/network/NodeEdgeGraphic.cpp @@ -0,0 +1,27 @@ +#include "gui/network/NodeEdgeGraphic.h" +#include +#include "gui/network/SocketGraphic.h" +#include +#include + +NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent) +: QGraphicsItem(parent), socket1_{socket1}, socket2_{socket2} +{ + setZValue(-1); +} + +QRectF NodeEdgeGraphic::boundingRect() const +{ + auto boundRect = QRect(10,10,10,10); + return boundRect; +} + +void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + painter->setPen(QPen("white")); + std::cout << "drawing " << socket1_->scenePos().x() << " " << socket1_->scenePos().y() << "\n"; + painter->drawLine(socket1_->scenePos(),socket2_->scenePos()); + +} + + diff --git a/src/gui/network/NodeEdgeGraphic.h b/src/gui/network/NodeEdgeGraphic.h new file mode 100644 index 0000000..43ab7dc --- /dev/null +++ b/src/gui/network/NodeEdgeGraphic.h @@ -0,0 +1,20 @@ +#pragma once +#include +#include "gui/network/SocketGraphic.h" + +#include + +class NodeEdgeGraphic +: public QGraphicsItem +{ +public: + NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent = nullptr); + QRectF boundingRect() const override; + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + +private: + SocketGraphic* socket1_; + SocketGraphic* socket2_; +}; + diff --git a/src/gui/network/NodeGraphic.cpp b/src/gui/network/NodeGraphic.cpp index 97ea34e..dc49017 100644 --- a/src/gui/network/NodeGraphic.cpp +++ b/src/gui/network/NodeGraphic.cpp @@ -1,9 +1,87 @@ #include "gui/network/NodeGraphic.h" +#include +#include +#include +#include +#include +#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()); + +} + + diff --git a/src/gui/network/NodeGraphic.h b/src/gui/network/NodeGraphic.h index cde7fe2..03ed771 100644 --- a/src/gui/network/NodeGraphic.h +++ b/src/gui/network/NodeGraphic.h @@ -1,29 +1,39 @@ #pragma once #include #include +#include "gui/network/SocketGraphic.h" +#include "gui/network/NodeEdgeGraphic.h" +#include class NodeGraphic : public QGraphicsItem { public: NodeGraphic(QGraphicsItem *parent = nullptr); - QRectF boundingRect() const override - { - qreal penWidth = 6; - return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, - 20 + penWidth, 20 + penWidth); - } + QRectF boundingRect() const override; + + SocketGraphic* getInput(int indx) const; + SocketGraphic* getOutput(int indx) const; + + void addEdge(NodeEdgeGraphic* edge); + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override - { - QPen greenPen = QPen(Qt::green); - greenPen.setWidth(6); - painter->setPen(greenPen); - painter->drawRoundedRect(-10, -10, 20, 20, 5, 5); - } private: - QGraphicsTextItem* title_; + void initSockets(); + std::vector inputs_; + std::vector outputs_; + + std::vector edges_; + + std::string title_=""; + int maxTitleLen_=10; + QRectF bodyRect_; + int socketSize_ = 1; + int inputSocketCnt_=0; + int outputSocketCnt_=0; }; diff --git a/src/gui/network/SocketGraphic.cpp b/src/gui/network/SocketGraphic.cpp new file mode 100644 index 0000000..edc1d79 --- /dev/null +++ b/src/gui/network/SocketGraphic.cpp @@ -0,0 +1,27 @@ +#include "gui/network/SocketGraphic.h" +#include +#include +#include + +SocketGraphic::SocketGraphic(QGraphicsItem *parent) +: QGraphicsItem(parent) +{ + socketSize_ = 3; +} + +QRectF SocketGraphic::boundingRect() const +{ + auto boundRect = QRect(-socketSize_/2, -socketSize_/2, socketSize_, socketSize_); + return boundRect; +} + +void SocketGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + QBrush whiteBrush = QBrush("white"); + painter->setPen(Qt::NoPen); + painter->setBrush(whiteBrush); + painter->drawEllipse(QPoint(0,0), socketSize_, socketSize_); + +} + + diff --git a/src/gui/network/SocketGraphic.h b/src/gui/network/SocketGraphic.h new file mode 100644 index 0000000..8a09759 --- /dev/null +++ b/src/gui/network/SocketGraphic.h @@ -0,0 +1,17 @@ +#pragma once +#include +#include + +class SocketGraphic +: public QGraphicsItem +{ +public: + SocketGraphic(QGraphicsItem *parent = nullptr); + QRectF boundingRect() const override; + + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; + +private: + int socketSize_ = 1; +}; +