feat: add node edges
This commit is contained in:
@@ -23,6 +23,8 @@ qt_add_executable(${AppExec}
|
|||||||
src/gui/network/NetworkGraphicsScene.cpp
|
src/gui/network/NetworkGraphicsScene.cpp
|
||||||
src/gui/network/Network.cpp
|
src/gui/network/Network.cpp
|
||||||
src/gui/network/NodeGraphic.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)
|
target_link_libraries(${AppExec} PRIVATE Qt6::Core Qt6::Widgets)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include "gui/network/Network.h"
|
#include "gui/network/Network.h"
|
||||||
#include "gui/network/NetworkGraphicsView.h"
|
#include "gui/network/NetworkGraphicsView.h"
|
||||||
|
#include "gui/network/NodeEdgeGraphic.h"
|
||||||
#include "gui/network/NetworkGraphicsScene.h"
|
#include "gui/network/NetworkGraphicsScene.h"
|
||||||
#include "gui/network/NodeGraphic.h"
|
#include "gui/network/NodeGraphic.h"
|
||||||
#include <qboxlayout.h>
|
#include <qboxlayout.h>
|
||||||
@@ -33,5 +34,15 @@ Network::Network(QWidget* parent)
|
|||||||
NodeGraphic* node1 = new NodeGraphic();
|
NodeGraphic* node1 = new NodeGraphic();
|
||||||
scene->addItem(node1);
|
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);
|
mainLayout_->addWidget(view);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ NetworkGraphicsScene::NetworkGraphicsScene()
|
|||||||
|
|
||||||
setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_);
|
setSceneRect(sceneWidth_/-2.0f, sceneHeight_/-2.0f, sceneWidth_, sceneHeight_);
|
||||||
|
|
||||||
setBackgroundBrush(QColor("#1b1b1b"));
|
setBackgroundBrush(QColor("#282828"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
|
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 left = floor(rect.left());
|
||||||
int right = ceil(rect.right());
|
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"));
|
QPen gridPen(QColor("#323232"));
|
||||||
|
|
||||||
painter->setPen(gridPen);
|
painter->setPen(gridPen);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ void NetworkGraphicsView::initUI()
|
|||||||
void NetworkGraphicsView::mousePressEvent(QMouseEvent *event)
|
void NetworkGraphicsView::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
if(
|
if(
|
||||||
event->button() & Qt::MiddleButton
|
event->button() == Qt::MiddleButton
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
panStartPos = event->pos();
|
panStartPos = event->pos();
|
||||||
@@ -60,8 +60,6 @@ void NetworkGraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
|||||||
{
|
{
|
||||||
QPointF pos = mouseEvent->pos();
|
QPointF pos = mouseEvent->pos();
|
||||||
QPointF delta = pos-panStartPos;
|
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;
|
float speed = 1.0f;
|
||||||
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
|
||||||
@@ -77,8 +75,6 @@ void NetworkGraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
|||||||
|
|
||||||
void NetworkGraphicsView::wheelEvent(QWheelEvent *event)
|
void NetworkGraphicsView::wheelEvent(QWheelEvent *event)
|
||||||
{
|
{
|
||||||
std::cout << "delta: " << event->angleDelta().x() << "\n";
|
|
||||||
std::cout << "delta: " << event->angleDelta().y() << "\n";
|
|
||||||
int delta = event->angleDelta().y();
|
int delta = event->angleDelta().y();
|
||||||
if(delta > 0)
|
if(delta > 0)
|
||||||
{
|
{
|
||||||
|
|||||||
27
src/gui/network/NodeEdgeGraphic.cpp
Normal file
27
src/gui/network/NodeEdgeGraphic.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "gui/network/NodeEdgeGraphic.h"
|
||||||
|
#include <QTextDocument>
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <qgraphicsitem.h>
|
||||||
|
|
||||||
|
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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
20
src/gui/network/NodeEdgeGraphic.h
Normal file
20
src/gui/network/NodeEdgeGraphic.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <QGraphicsItem>
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
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_;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,9 +1,87 @@
|
|||||||
#include "gui/network/NodeGraphic.h"
|
#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)
|
NodeGraphic::NodeGraphic(QGraphicsItem *parent)
|
||||||
|
: QGraphicsItem(parent)
|
||||||
{
|
{
|
||||||
title_ = new QGraphicsTextItem(this);
|
maxTitleLen_ = 10;
|
||||||
title_->setPlainText("hello world!");
|
socketSize_ = 3;
|
||||||
setFlag(QGraphicsItem::ItemIsMovable);
|
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());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,29 +1,39 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
#include "gui/network/NodeEdgeGraphic.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class NodeGraphic
|
class NodeGraphic
|
||||||
: public QGraphicsItem
|
: public QGraphicsItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NodeGraphic(QGraphicsItem *parent = nullptr);
|
NodeGraphic(QGraphicsItem *parent = nullptr);
|
||||||
QRectF boundingRect() const override
|
QRectF boundingRect() const override;
|
||||||
{
|
|
||||||
qreal penWidth = 6;
|
SocketGraphic* getInput(int indx) const;
|
||||||
return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
|
SocketGraphic* getOutput(int indx) const;
|
||||||
20 + penWidth, 20 + penWidth);
|
|
||||||
}
|
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:
|
private:
|
||||||
QGraphicsTextItem* title_;
|
void initSockets();
|
||||||
|
std::vector<SocketGraphic*> inputs_;
|
||||||
|
std::vector<SocketGraphic*> outputs_;
|
||||||
|
|
||||||
|
std::vector<NodeEdgeGraphic*> edges_;
|
||||||
|
|
||||||
|
std::string title_="";
|
||||||
|
int maxTitleLen_=10;
|
||||||
|
QRectF bodyRect_;
|
||||||
|
int socketSize_ = 1;
|
||||||
|
int inputSocketCnt_=0;
|
||||||
|
int outputSocketCnt_=0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
27
src/gui/network/SocketGraphic.cpp
Normal file
27
src/gui/network/SocketGraphic.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
#include <QTextDocument>
|
||||||
|
#include <iostream>
|
||||||
|
#include <qgraphicsitem.h>
|
||||||
|
|
||||||
|
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_);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
17
src/gui/network/SocketGraphic.h
Normal file
17
src/gui/network/SocketGraphic.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <QGraphicsItem>
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
Reference in New Issue
Block a user