feat: add floating edge
This commit is contained in:
@@ -25,6 +25,7 @@ qt_add_executable(${AppExec}
|
|||||||
src/gui/network/NodeGraphic.cpp
|
src/gui/network/NodeGraphic.cpp
|
||||||
src/gui/network/SocketGraphic.cpp
|
src/gui/network/SocketGraphic.cpp
|
||||||
src/gui/network/NodeEdgeGraphic.cpp
|
src/gui/network/NodeEdgeGraphic.cpp
|
||||||
|
src/gui/network/FloatingEdgeGraphic.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(${AppExec} PRIVATE Qt6::Core Qt6::Widgets)
|
target_link_libraries(${AppExec} PRIVATE Qt6::Core Qt6::Widgets)
|
||||||
|
|||||||
2
build.sh
2
build.sh
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
cd build
|
cd build
|
||||||
cmake -G Ninja ..
|
cmake -DCMAKE_BUILD_TYPE=Debug -G Ninja ..
|
||||||
ninja
|
ninja
|
||||||
|
|||||||
29
src/gui/network/FloatingEdgeGraphic.cpp
Normal file
29
src/gui/network/FloatingEdgeGraphic.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "gui/network/FloatingEdgeGraphic.h"
|
||||||
|
#include <QTextDocument>
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <qgraphicsitem.h>
|
||||||
|
|
||||||
|
FloatingEdgeGraphic::FloatingEdgeGraphic(SocketGraphic* socket1, QGraphicsItem *parent)
|
||||||
|
: QGraphicsItem(parent), socket1_{socket1}
|
||||||
|
{
|
||||||
|
floatPos_ = socket1_->scenePos();
|
||||||
|
setZValue(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
QRectF FloatingEdgeGraphic::boundingRect() const
|
||||||
|
{
|
||||||
|
// TODO: fix
|
||||||
|
auto boundRect = QRect(10,10,10,10);
|
||||||
|
return boundRect;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FloatingEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||||
|
{
|
||||||
|
painter->setPen(QPen("white"));
|
||||||
|
painter->drawLine(socket1_->scenePos(),floatPos_);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void FloatingEdgeGraphic::setFloatPos(QPointF floatPos) { floatPos_ = floatPos; }
|
||||||
|
|
||||||
21
src/gui/network/FloatingEdgeGraphic.h
Normal file
21
src/gui/network/FloatingEdgeGraphic.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <QGraphicsItem>
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
|
||||||
|
#include <QPainter>
|
||||||
|
|
||||||
|
class FloatingEdgeGraphic
|
||||||
|
: public QGraphicsItem
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FloatingEdgeGraphic(SocketGraphic* socket1, QGraphicsItem *parent = nullptr);
|
||||||
|
QRectF boundingRect() const override;
|
||||||
|
|
||||||
|
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||||
|
void setFloatPos(QPointF floatPos);
|
||||||
|
|
||||||
|
private:
|
||||||
|
SocketGraphic* socket1_;
|
||||||
|
QPointF floatPos_;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -1,13 +1,16 @@
|
|||||||
#include "gui/network/Network.h"
|
#include "gui/network/Network.h"
|
||||||
#include "gui/network/NetworkGraphicsView.h"
|
|
||||||
#include "gui/network/NodeEdgeGraphic.h"
|
#include "gui/network/NodeEdgeGraphic.h"
|
||||||
|
#include "gui/network/NetworkGraphicsView.h"
|
||||||
#include "gui/network/NetworkGraphicsScene.h"
|
#include "gui/network/NetworkGraphicsScene.h"
|
||||||
#include "gui/network/NodeGraphic.h"
|
#include "gui/network/NodeGraphic.h"
|
||||||
|
#include "gui/network/FloatingEdgeGraphic.h"
|
||||||
#include <qboxlayout.h>
|
#include <qboxlayout.h>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
|
#include <QMouseEvent>
|
||||||
|
|
||||||
Network::Network(QWidget* parent)
|
Network::Network(QWidget* parent)
|
||||||
|
: QWidget(parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
mainLayout_ = new QVBoxLayout(parent);
|
mainLayout_ = new QVBoxLayout(parent);
|
||||||
@@ -16,33 +19,58 @@ Network::Network(QWidget* parent)
|
|||||||
this->setLayout(mainLayout_);
|
this->setLayout(mainLayout_);
|
||||||
|
|
||||||
|
|
||||||
NetworkGraphicsScene* scene = new NetworkGraphicsScene();
|
scene_ = new NetworkGraphicsScene();
|
||||||
NetworkGraphicsView* view = new NetworkGraphicsView(parent, scene);
|
view_ = new NetworkGraphicsView(this, this, scene_);
|
||||||
|
|
||||||
QPen greenPen = QPen(Qt::green);
|
QPen greenPen = QPen(Qt::green);
|
||||||
greenPen.setWidth(6);
|
greenPen.setWidth(6);
|
||||||
|
|
||||||
auto* rect1 = scene->addRect(50, 50, 100, 100, greenPen);
|
auto* rect1 = scene_->addRect(50, 50, 100, 100, greenPen);
|
||||||
rect1->setFlag(QGraphicsItem::ItemIsMovable);
|
rect1->setFlag(QGraphicsItem::ItemIsMovable);
|
||||||
|
|
||||||
auto* rect2 = scene->addRect(80, 120, 100, 100, greenPen);
|
auto* rect2 = scene_->addRect(80, 120, 100, 100, greenPen);
|
||||||
rect2->setFlag(QGraphicsItem::ItemIsMovable);
|
rect2->setFlag(QGraphicsItem::ItemIsMovable);
|
||||||
|
|
||||||
auto* rect3 = scene->addRect(80, -120, 100, 100, greenPen);
|
auto* rect3 = scene_->addRect(80, -120, 100, 100, greenPen);
|
||||||
rect3->setFlag(QGraphicsItem::ItemIsMovable);
|
rect3->setFlag(QGraphicsItem::ItemIsMovable);
|
||||||
|
|
||||||
NodeGraphic* node1 = new NodeGraphic();
|
NodeGraphic* node1 = new NodeGraphic();
|
||||||
scene->addItem(node1);
|
node1->setPos(-50, -50);
|
||||||
|
scene_->addItem(node1);
|
||||||
|
|
||||||
NodeGraphic* node2 = new NodeGraphic();
|
NodeGraphic* node2 = new NodeGraphic();
|
||||||
scene->addItem(node2);
|
node2->setPos(50, 50);
|
||||||
|
scene_->addItem(node2);
|
||||||
|
|
||||||
NodeEdgeGraphic* edge1 = new NodeEdgeGraphic(node1->getOutput(0), node2->getInput(0));
|
NodeEdgeGraphic* edge1 = new NodeEdgeGraphic(node1->getOutput(0), node2->getInput(0));
|
||||||
scene->addItem(edge1);
|
scene_->addItem(edge1);
|
||||||
|
|
||||||
node1->addEdge(edge1);
|
node1->addEdge(edge1);
|
||||||
node2->addEdge(edge1);
|
node2->addEdge(edge1);
|
||||||
|
|
||||||
|
|
||||||
mainLayout_->addWidget(view);
|
|
||||||
|
mainLayout_->addWidget(view_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::socketClicked(SocketGraphic* socket)
|
||||||
|
{
|
||||||
|
std::cout << "socket clicked\n";
|
||||||
|
if(!floatingEdge_)
|
||||||
|
{
|
||||||
|
std::cout << "creating floating edge\n";
|
||||||
|
floatingEdge_ = new FloatingEdgeGraphic(socket);
|
||||||
|
scene_->addItem(floatingEdge_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Network::mouseMoved(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
if(floatingEdge_)
|
||||||
|
{
|
||||||
|
floatingEdge_->setFloatPos(view_->mapToScene(event->pos()));
|
||||||
|
floatingEdge_->update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
#include "gui/network/NetworkGraphicsView.h"
|
||||||
|
#include "gui/network/NetworkGraphicsScene.h"
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
|
#include "gui/network/FloatingEdgeGraphic.h"
|
||||||
|
|
||||||
class Network
|
class Network
|
||||||
: public QWidget
|
: public QWidget
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Network(QWidget* parent = nullptr);
|
Network(QWidget* parent = nullptr);
|
||||||
|
void socketClicked(SocketGraphic* socket);
|
||||||
|
void mouseMoved(QMouseEvent *event);
|
||||||
private:
|
private:
|
||||||
QLayout* mainLayout_;
|
QLayout* mainLayout_;
|
||||||
|
NetworkGraphicsScene* scene_;
|
||||||
|
NetworkGraphicsView* view_;
|
||||||
|
FloatingEdgeGraphic* floatingEdge_=nullptr;
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
#include "gui/network/NetworkGraphicsView.h"
|
#include "gui/network/NetworkGraphicsView.h"
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
|
#include "gui/network/Network.h"
|
||||||
#include "gui/network/NetworkGraphicsScene.h"
|
#include "gui/network/NetworkGraphicsScene.h"
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QScrollBar>
|
#include <QScrollBar>
|
||||||
|
#include <qgraphicsitem.h>
|
||||||
|
#include <qobject.h>
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
NetworkGraphicsView::NetworkGraphicsView(QWidget *parent, QGraphicsScene* scene)
|
NetworkGraphicsView::NetworkGraphicsView(QWidget *parent, Network* network, QGraphicsScene* scene)
|
||||||
: QGraphicsView(parent)
|
: QGraphicsView(parent), scene_{scene}, network_{network}
|
||||||
{
|
{
|
||||||
setScene(scene);
|
setScene(scene_);
|
||||||
|
setMouseTracking(true);
|
||||||
|
|
||||||
initUI();
|
initUI();
|
||||||
|
|
||||||
@@ -31,6 +37,10 @@ void NetworkGraphicsView::initUI()
|
|||||||
|
|
||||||
void NetworkGraphicsView::mousePressEvent(QMouseEvent *event)
|
void NetworkGraphicsView::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
|
if( event->buttons() & Qt::LeftButton)
|
||||||
|
{
|
||||||
|
leftMousePress(event);
|
||||||
|
}
|
||||||
if(
|
if(
|
||||||
event->button() == Qt::MiddleButton
|
event->button() == Qt::MiddleButton
|
||||||
)
|
)
|
||||||
@@ -43,6 +53,19 @@ void NetworkGraphicsView::mousePressEvent(QMouseEvent *event)
|
|||||||
QGraphicsView::mousePressEvent(event);
|
QGraphicsView::mousePressEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void NetworkGraphicsView::leftMousePress(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
QGraphicsItem* itemClicked = itemAt(event->pos());
|
||||||
|
bool isSocket = itemClicked && typeid(*itemClicked)==typeid(SocketGraphic);
|
||||||
|
if(isSocket)
|
||||||
|
{
|
||||||
|
std::cout << "SOCKET!\n";
|
||||||
|
network_->socketClicked(static_cast<SocketGraphic*>(itemClicked));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// void NetworkView::mouseReleaseEvent(QMouseEvent *event)
|
// void NetworkView::mouseReleaseEvent(QMouseEvent *event)
|
||||||
// {
|
// {
|
||||||
// if(
|
// if(
|
||||||
@@ -53,26 +76,29 @@ void NetworkGraphicsView::mousePressEvent(QMouseEvent *event)
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
void NetworkGraphicsView::mouseMoveEvent(QMouseEvent *mouseEvent)
|
void NetworkGraphicsView::mouseMoveEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
|
network_->mouseMoved(event);
|
||||||
|
|
||||||
if( mouseEvent->buttons() & Qt::MiddleButton)
|
// pan view
|
||||||
|
if( event->buttons() & Qt::MiddleButton)
|
||||||
{
|
{
|
||||||
QPointF pos = mouseEvent->pos();
|
QPointF pos = event->pos();
|
||||||
QPointF delta = pos-panStartPos;
|
QPointF delta = pos-panStartPos;
|
||||||
|
|
||||||
float speed = 1.0f;
|
float speed = 1.0f;
|
||||||
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
|
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - delta.x());
|
||||||
verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());
|
verticalScrollBar()->setValue(verticalScrollBar()->value() - delta.y());
|
||||||
panStartPos = mouseEvent->pos();
|
panStartPos = event->pos();
|
||||||
mouseEvent->accept();
|
event->accept();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QGraphicsView::mouseMoveEvent(mouseEvent);
|
QGraphicsView::mouseMoveEvent(event);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void NetworkGraphicsView::wheelEvent(QWheelEvent *event)
|
void NetworkGraphicsView::wheelEvent(QWheelEvent *event)
|
||||||
{
|
{
|
||||||
int delta = event->angleDelta().y();
|
int delta = event->angleDelta().y();
|
||||||
|
|||||||
@@ -1,20 +1,27 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <qgraphicsitem.h>
|
||||||
#include <qwidget.h>
|
#include <qwidget.h>
|
||||||
#include <QGraphicsView>
|
#include <QGraphicsView>
|
||||||
#include <QGraphicsScene>
|
#include <QGraphicsScene>
|
||||||
|
|
||||||
|
class Network;
|
||||||
|
|
||||||
class NetworkGraphicsView
|
class NetworkGraphicsView
|
||||||
: public QGraphicsView
|
: public QGraphicsView
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NetworkGraphicsView(QWidget *parent = nullptr, QGraphicsScene* scene = nullptr);
|
NetworkGraphicsView(QWidget *parent = nullptr, Network* network=nullptr, QGraphicsScene* scene = nullptr);
|
||||||
private:
|
private:
|
||||||
QPointF panStartPos;
|
QPointF panStartPos;
|
||||||
void initUI();
|
void initUI();
|
||||||
|
QGraphicsScene* scene_;
|
||||||
|
Network* network_;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void mouseMoveEvent(QMouseEvent *mouseEvent) override;
|
void mouseMoveEvent(QMouseEvent *mouseEvent) override;
|
||||||
void mousePressEvent(QMouseEvent *event) override;
|
void mousePressEvent(QMouseEvent *event) override;
|
||||||
|
void leftMousePress(QMouseEvent *event);
|
||||||
|
QGraphicsItem* getItemAtClick(QMouseEvent *event);
|
||||||
void wheelEvent(QWheelEvent *event) override;
|
void wheelEvent(QWheelEvent *event) override;
|
||||||
// void mouseReleaseEvent(QMouseEvent *event) override;
|
// void mouseReleaseEvent(QMouseEvent *event) override;
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ QRectF NodeEdgeGraphic::boundingRect() const
|
|||||||
void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||||
{
|
{
|
||||||
painter->setPen(QPen("white"));
|
painter->setPen(QPen("white"));
|
||||||
std::cout << "drawing " << socket1_->scenePos().x() << " " << socket1_->scenePos().y() << "\n";
|
|
||||||
painter->drawLine(socket1_->scenePos(),socket2_->scenePos());
|
painter->drawLine(socket1_->scenePos(),socket2_->scenePos());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user