feat: connect nodes
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -17,7 +17,10 @@ private:
|
||||
QLayout* mainLayout_;
|
||||
NetworkGraphicsScene* scene_;
|
||||
NetworkGraphicsView* view_;
|
||||
|
||||
FloatingEdgeGraphic* floatingEdge_=nullptr;
|
||||
SocketGraphic* startSocket_=nullptr;
|
||||
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void destroyFloatingEdge();
|
||||
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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<SocketGraphic*> inputs_;
|
||||
std::vector<SocketGraphic*> outputs_;
|
||||
|
||||
std::vector<NodeEdgeGraphic*> edges_;
|
||||
// std::vector<NodeEdgeGraphic*> edges_;
|
||||
|
||||
std::string title_="";
|
||||
int maxTitleLen_=10;
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
#include <iostream>
|
||||
#include <qgraphicsitem.h>
|
||||
|
||||
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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user