refactor: adjust
This commit is contained in:
@@ -4,6 +4,7 @@
|
|||||||
#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 "gui/network/FloatingEdgeGraphic.h"
|
||||||
|
#include "gui/network/SocketGraphic.h"
|
||||||
#include <qboxlayout.h>
|
#include <qboxlayout.h>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
@@ -49,6 +50,10 @@ Network::Network(QWidget* parent)
|
|||||||
node3->setPos(50, 200);
|
node3->setPos(50, 200);
|
||||||
scene_->addItem(node3);
|
scene_->addItem(node3);
|
||||||
|
|
||||||
|
NodeGraphic* node4 = new NodeGraphic();
|
||||||
|
node4->setPos(50, -200);
|
||||||
|
scene_->addItem(node4);
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@@ -73,20 +78,30 @@ void Network::leftMousePress(QMouseEvent *event)
|
|||||||
{
|
{
|
||||||
Qt::KeyboardModifiers mods = event->modifiers();
|
Qt::KeyboardModifiers mods = event->modifiers();
|
||||||
|
|
||||||
QGraphicsItem* itemClicked = view_->itemAt(event->pos());
|
// QGraphicsItem* itemClicked = view_->itemAt(event->pos());
|
||||||
if(isType<SocketGraphic>(itemClicked))
|
QList<QGraphicsItem*> clickedItems = view_->items(event->pos());
|
||||||
|
QGraphicsItem* clickedSocket = itemOfType<SocketGraphic>(clickedItems);
|
||||||
|
QGraphicsItem* clickedEdge = itemOfType<NodeEdgeGraphic>(clickedItems);
|
||||||
|
|
||||||
|
|
||||||
|
// delete edges
|
||||||
|
if(mods & Qt::ControlModifier && clickedEdge)
|
||||||
{
|
{
|
||||||
socketClicked(static_cast<SocketGraphic*>(itemClicked), event);
|
scene_->removeItem(clickedEdge);
|
||||||
|
if(prevHoverItem_==clickedEdge)
|
||||||
|
{
|
||||||
|
prevHoverItem_=nullptr;
|
||||||
|
}
|
||||||
|
delete clickedEdge;
|
||||||
|
}
|
||||||
|
else if(clickedSocket)
|
||||||
|
{
|
||||||
|
socketClicked(static_cast<SocketGraphic*>(clickedSocket), event);
|
||||||
}
|
}
|
||||||
else if(floatingEdge_)
|
else if(floatingEdge_)
|
||||||
{
|
{
|
||||||
destroyFloatingEdge();
|
destroyFloatingEdge();
|
||||||
}
|
}
|
||||||
// delete edges
|
|
||||||
else if(mods & Qt::ControlModifier && isType<NodeEdgeGraphic>(itemClicked))
|
|
||||||
{
|
|
||||||
scene_->removeItem(itemClicked);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +111,7 @@ void Network::leftMousePress(QMouseEvent *event)
|
|||||||
void Network::socketClicked(SocketGraphic* socket, QMouseEvent *event)
|
void Network::socketClicked(SocketGraphic* socket, QMouseEvent *event)
|
||||||
{
|
{
|
||||||
std::cout << "socket clicked\n";
|
std::cout << "socket clicked\n";
|
||||||
|
// if first click
|
||||||
if(!floatingEdge_)
|
if(!floatingEdge_)
|
||||||
{
|
{
|
||||||
startSocket_=socket;
|
startSocket_=socket;
|
||||||
@@ -104,6 +120,7 @@ void Network::socketClicked(SocketGraphic* socket, QMouseEvent *event)
|
|||||||
scene_->addItem(floatingEdge_);
|
scene_->addItem(floatingEdge_);
|
||||||
floatingEdge_->setFloatPos(view_->mapToScene(event->pos()));
|
floatingEdge_->setFloatPos(view_->mapToScene(event->pos()));
|
||||||
}
|
}
|
||||||
|
// second click
|
||||||
// connect to opposite type
|
// connect to opposite type
|
||||||
else if (socket->getIO()!=startSocket_->getIO())
|
else if (socket->getIO()!=startSocket_->getIO())
|
||||||
{
|
{
|
||||||
@@ -142,19 +159,23 @@ void Network::mouseMoved(QMouseEvent *event)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QGraphicsItem* hoverItem = view_->itemAt(event->pos());
|
QList<QGraphicsItem*> hoverItems = view_->items(event->pos());
|
||||||
|
// QGraphicsItem* hoverItem = view_->itemAt(event->pos());
|
||||||
|
QGraphicsItem* hoverEdge = itemOfType<NodeEdgeGraphic>(hoverItems);
|
||||||
|
|
||||||
// set node edge color
|
// set node edge color
|
||||||
if(ctrlMod && isType<NodeEdgeGraphic>(hoverItem))
|
if(ctrlMod && hoverEdge)
|
||||||
{
|
{
|
||||||
highlightEdge(hoverItem, true);
|
std::cout << "highlighting\n";
|
||||||
|
highlightEdge(hoverEdge, true);
|
||||||
}
|
}
|
||||||
// reset node edge color
|
// reset node edge color
|
||||||
if(
|
if(
|
||||||
(!ctrlMod || hoverItem!=prevHoverItem) &&
|
prevHoverItem &&
|
||||||
isType<NodeEdgeGraphic>(prevHoverItem)
|
(!ctrlMod || hoverEdge!=prevHoverItem)
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
std::cout << "unhighlighting\n";
|
||||||
highlightEdge(prevHoverItem, false);
|
highlightEdge(prevHoverItem, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,15 +192,19 @@ void Network::keyPressEvent(QKeyEvent *event)
|
|||||||
QPoint globalPos = QCursor::pos();
|
QPoint globalPos = QCursor::pos();
|
||||||
QPoint widgetPos = mapFromGlobal(globalPos);
|
QPoint widgetPos = mapFromGlobal(globalPos);
|
||||||
|
|
||||||
QGraphicsItem* hoverItem = view_->itemAt(widgetPos);
|
QList<QGraphicsItem*> hoverItems = view_->items(widgetPos);
|
||||||
|
|
||||||
// edge detection
|
// edge detection
|
||||||
if(
|
if(
|
||||||
event->key() == Qt::Key_Control &&
|
event->key() == Qt::Key_Control
|
||||||
isType<NodeEdgeGraphic>(hoverItem)
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
highlightEdge(hoverItem, true);
|
QGraphicsItem* hoverItem = itemOfType<NodeEdgeGraphic>(hoverItems);
|
||||||
|
if(hoverItem!=nullptr)
|
||||||
|
{
|
||||||
|
highlightEdge(hoverItem, true);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event->key() == Qt::Key_Escape)
|
if(event->key() == Qt::Key_Escape)
|
||||||
@@ -193,13 +218,11 @@ void Network::highlightEdge(QGraphicsItem* edge, bool state)
|
|||||||
if(state)
|
if(state)
|
||||||
{
|
{
|
||||||
static_cast<NodeEdgeGraphic*>(edge)->setColor(QColor("red"));
|
static_cast<NodeEdgeGraphic*>(edge)->setColor(QColor("red"));
|
||||||
edge->update();
|
|
||||||
prevHoverItem_=edge;
|
prevHoverItem_=edge;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
static_cast<NodeEdgeGraphic*>(edge)->useDefaultColor();
|
static_cast<NodeEdgeGraphic*>(edge)->useDefaultColor();
|
||||||
edge->update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "gui/network/NetworkGraphicsScene.h"
|
#include "gui/network/NetworkGraphicsScene.h"
|
||||||
#include "gui/network/SocketGraphic.h"
|
#include "gui/network/SocketGraphic.h"
|
||||||
#include "gui/network/FloatingEdgeGraphic.h"
|
#include "gui/network/FloatingEdgeGraphic.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class Network
|
class Network
|
||||||
: public QWidget
|
: public QWidget
|
||||||
@@ -25,6 +26,7 @@ private:
|
|||||||
SocketGraphic* startSocket_=nullptr;
|
SocketGraphic* startSocket_=nullptr;
|
||||||
|
|
||||||
QGraphicsItem* prevHoverItem_=nullptr;
|
QGraphicsItem* prevHoverItem_=nullptr;
|
||||||
|
QList<QGraphicsItem*> prevHoverItems_;
|
||||||
|
|
||||||
void keyPressEvent(QKeyEvent *event) override;
|
void keyPressEvent(QKeyEvent *event) override;
|
||||||
void keyReleaseEvent(QKeyEvent *event) override;
|
void keyReleaseEvent(QKeyEvent *event) override;
|
||||||
@@ -38,6 +40,21 @@ private:
|
|||||||
return item && typeid(*item)==typeid(T);
|
return item && typeid(*item)==typeid(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
QGraphicsItem* itemOfType(QList<QGraphicsItem*> items)
|
||||||
|
{
|
||||||
|
// std::cout << "count: " << items.size() << "\n";
|
||||||
|
for(QGraphicsItem* item : items)
|
||||||
|
{
|
||||||
|
std::cout << "item: " << typeid(*item).name() << "\n";
|
||||||
|
if(item && typeid(*item)==typeid(T))
|
||||||
|
{
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void resizeEvent(QResizeEvent *event) override;
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,20 +5,34 @@
|
|||||||
#include <qgraphicsitem.h>
|
#include <qgraphicsitem.h>
|
||||||
|
|
||||||
NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent)
|
NodeEdgeGraphic::NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent)
|
||||||
: QGraphicsItem(parent), socket1_{socket1}, socket2_{socket2}, defaultColor_{QColor("white")}
|
: QGraphicsItem(parent), socket1_{socket1}, socket2_{socket2}, defaultColor_{QColor("white")}, pen_{QPen(QColor("white"))}
|
||||||
{
|
{
|
||||||
setZValue(-1);
|
setZValue(-1);
|
||||||
color_=defaultColor_;
|
color_=defaultColor_;
|
||||||
|
|
||||||
|
socket1_->addEdge(this);
|
||||||
|
socket2_->addEdge(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
NodeEdgeGraphic::~NodeEdgeGraphic()
|
||||||
|
{
|
||||||
|
std::cout << "edge destructor\n";
|
||||||
|
socket1_->removeEdge(this);
|
||||||
|
socket2_->removeEdge(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QRectF NodeEdgeGraphic::boundingRect() const
|
QRectF NodeEdgeGraphic::boundingRect() const
|
||||||
{
|
{
|
||||||
|
// std::cout << "bounds set" << socket1_->scenePos().x() << " " << socket1_->scenePos().y() << " " << socket2_->scenePos().x() << " " << socket2_->scenePos().y() << "\n";
|
||||||
auto boundRect = QRectF(socket1_->scenePos(), socket2_->scenePos());
|
auto boundRect = QRectF(socket1_->scenePos(), socket2_->scenePos());
|
||||||
return boundRect;
|
return boundRect;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPainterPath NodeEdgeGraphic::shape() const{
|
QPainterPath NodeEdgeGraphic::shape() const{
|
||||||
|
// FIX: shape not changing with node position
|
||||||
QPainterPath path;
|
QPainterPath path;
|
||||||
|
// std::cout << "setting shape to: " << socket1_->scenePos().x() << " " << socket2_->scenePos().x() <<"\n";
|
||||||
path.moveTo(socket1_->scenePos());
|
path.moveTo(socket1_->scenePos());
|
||||||
path.lineTo(socket2_->scenePos());
|
path.lineTo(socket2_->scenePos());
|
||||||
|
|
||||||
@@ -29,7 +43,10 @@ QPainterPath NodeEdgeGraphic::shape() const{
|
|||||||
|
|
||||||
void NodeEdgeGraphic::setColor(QColor color)
|
void NodeEdgeGraphic::setColor(QColor color)
|
||||||
{
|
{
|
||||||
|
std::cout << "color set to: " << color.name().toStdString() << "\n";
|
||||||
color_ = color;
|
color_ = color;
|
||||||
|
pen_.setColor(color_);
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeEdgeGraphic::useDefaultColor()
|
void NodeEdgeGraphic::useDefaultColor()
|
||||||
@@ -39,9 +56,9 @@ void NodeEdgeGraphic::useDefaultColor()
|
|||||||
|
|
||||||
void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||||
{
|
{
|
||||||
auto pen = QPen(color_);
|
std::cout << "painting\n";
|
||||||
pen.setCapStyle(Qt::RoundCap);
|
pen_.setCapStyle(Qt::RoundCap);
|
||||||
painter->setPen(pen);
|
painter->setPen(pen_);
|
||||||
painter->drawLine(socket1_->scenePos(),socket2_->scenePos());
|
painter->drawLine(socket1_->scenePos(),socket2_->scenePos());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class NodeEdgeGraphic
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent = nullptr);
|
NodeEdgeGraphic(SocketGraphic* socket1, SocketGraphic* socket2, QGraphicsItem *parent = nullptr);
|
||||||
|
~NodeEdgeGraphic();
|
||||||
QRectF boundingRect() const override;
|
QRectF boundingRect() const override;
|
||||||
|
|
||||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||||
@@ -21,5 +22,6 @@ private:
|
|||||||
SocketGraphic* socket2_;
|
SocketGraphic* socket2_;
|
||||||
QColor color_;
|
QColor color_;
|
||||||
QColor defaultColor_;
|
QColor defaultColor_;
|
||||||
|
QPen pen_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ NodeGraphic::NodeGraphic(QGraphicsItem *parent)
|
|||||||
title_ = "hello world";
|
title_ = "hello world";
|
||||||
bodyRect_ = QRect(-10, -10, 10*maxTitleLen_, 20);
|
bodyRect_ = QRect(-10, -10, 10*maxTitleLen_, 20);
|
||||||
|
|
||||||
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
|
setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemSendsGeometryChanges);
|
||||||
|
|
||||||
initSockets();
|
initSockets();
|
||||||
}
|
}
|
||||||
@@ -96,3 +96,18 @@ void NodeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *optio
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QVariant NodeGraphic::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
||||||
|
{
|
||||||
|
if (change == ItemPositionChange && scene()) {
|
||||||
|
for(auto socket : inputs_)
|
||||||
|
{
|
||||||
|
socket->posChanged(value.toPointF());
|
||||||
|
}
|
||||||
|
for(auto socket : outputs_)
|
||||||
|
{
|
||||||
|
socket->posChanged(value.toPointF());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return QGraphicsItem::itemChange(change, value);
|
||||||
|
}
|
||||||
|
|||||||
@@ -38,5 +38,8 @@ private:
|
|||||||
int socketSize_ = 1;
|
int socketSize_ = 1;
|
||||||
int inputSocketCnt_=0;
|
int inputSocketCnt_=0;
|
||||||
int outputSocketCnt_=0;
|
int outputSocketCnt_=0;
|
||||||
|
protected:
|
||||||
|
QVariant itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <qgraphicsitem.h>
|
#include <qgraphicsitem.h>
|
||||||
|
#include "gui/network/NodeEdgeGraphic.h"
|
||||||
|
|
||||||
SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent)
|
SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent)
|
||||||
: QGraphicsItem(parent), type_{type}
|
: QGraphicsItem(parent), type_{type}
|
||||||
@@ -12,9 +13,39 @@ SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *pare
|
|||||||
setAcceptHoverEvents(true);
|
setAcceptHoverEvents(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SocketGraphic::addEdge(NodeEdgeGraphic* edge)
|
||||||
|
{
|
||||||
|
std::cout << "adding edge\n";
|
||||||
|
edges_.insert(edge);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SocketGraphic::removeEdge(NodeEdgeGraphic* edge)
|
||||||
|
{
|
||||||
|
edges_.erase(edge);
|
||||||
|
std::cout << "removing edge\n";
|
||||||
|
// auto it = find(edges_.begin(), edges_.end(), edge);
|
||||||
|
// if(it!=edges_.end())
|
||||||
|
// {
|
||||||
|
// edges_.erase(it);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void SocketGraphic::posChanged(QPointF pos)
|
||||||
|
{
|
||||||
|
std::cout << "socket pos changed\n";
|
||||||
|
//
|
||||||
|
// for(auto* edge : edges_)
|
||||||
|
// {
|
||||||
|
// edge->prepareGeometryChange();
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
QRectF SocketGraphic::boundingRect() const
|
QRectF SocketGraphic::boundingRect() const
|
||||||
{
|
{
|
||||||
float paddingScale = 10;
|
float paddingScale = 20;
|
||||||
auto boundRect = QRect(
|
auto boundRect = QRect(
|
||||||
-socketSize_/2.0f*paddingScale,
|
-socketSize_/2.0f*paddingScale,
|
||||||
type_==SocketType::Input ? -socketSize_/2.0f*paddingScale : 0,
|
type_==SocketType::Input ? -socketSize_/2.0f*paddingScale : 0,
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
|
||||||
|
class NodeEdgeGraphic;
|
||||||
|
|
||||||
class SocketGraphic
|
class SocketGraphic
|
||||||
: public QGraphicsItem
|
: public QGraphicsItem
|
||||||
@@ -17,6 +21,9 @@ public:
|
|||||||
|
|
||||||
SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent = nullptr);
|
SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *parent = nullptr);
|
||||||
SocketType getIO();
|
SocketType getIO();
|
||||||
|
void addEdge(NodeEdgeGraphic* edge);
|
||||||
|
void removeEdge(NodeEdgeGraphic* edge);
|
||||||
|
void posChanged(QPointF pos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int socketSize_ = 1;
|
int socketSize_ = 1;
|
||||||
@@ -24,6 +31,7 @@ private:
|
|||||||
QBrush brushActive_;
|
QBrush brushActive_;
|
||||||
bool hovered_=false;
|
bool hovered_=false;
|
||||||
SocketType type_;
|
SocketType type_;
|
||||||
|
std::unordered_set<NodeEdgeGraphic*> edges_;
|
||||||
protected:
|
protected:
|
||||||
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
|
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
|
||||||
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
|
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;
|
||||||
|
|||||||
Reference in New Issue
Block a user