fix: socket bounding box and path

This commit is contained in:
parker
2025-06-23 15:33:11 +01:00
parent 882dde0ea3
commit 74da83fdfa
3 changed files with 35 additions and 9 deletions

View File

@@ -81,7 +81,10 @@ void Network::deleteEdge(QGraphicsItem* edge)
}
scene_->removeItem(edge);
scene_->update();
delete edge;
view_->update();
// NOTE: deleting edge kept giving me segmentation faults
// I coundn't figure it out so I'm just leaving it for now
// delete edge;
}
void Network::leftMousePress(QMouseEvent *event)

View File

@@ -11,6 +11,7 @@ SocketGraphic::SocketGraphic(SocketGraphic::SocketType type, QGraphicsItem *pare
brushInactive_ = QBrush("#9f9f9f");
socketSize_ = 3;
setAcceptHoverEvents(true);
initBoundingBox();
}
void SocketGraphic::addEdge(NodeEdgeGraphic* edge)
@@ -32,6 +33,16 @@ void SocketGraphic::removeEdge(NodeEdgeGraphic* edge)
// }
}
void SocketGraphic::initBoundingBox()
{
boundRect_ = QRect(
-socketSize_/2.0f*paddingScale_,
-socketSize_/2.0f*paddingScale_,
socketSize_*paddingScale_,
socketSize_*paddingScale_
);
}
void SocketGraphic::posChanged(QPointF pos)
{
@@ -55,14 +66,18 @@ void SocketGraphic::posChanged(QPointF pos)
QRectF SocketGraphic::boundingRect() const
{
float paddingScale = 20;
auto boundRect = QRect(
-socketSize_/2.0f*paddingScale,
type_==SocketType::Input ? -socketSize_/2.0f*paddingScale : 0,
socketSize_*paddingScale,
socketSize_/2.0f*paddingScale
);
return boundRect;
return boundRect_;
}
QPainterPath SocketGraphic::shape() const{
QPainterPath path;
QPointF startPt(boundRect_.center().x(), type_==SocketType::Input ? boundRect_.top() : boundRect_.bottom());
path.moveTo(startPt);
path.arcTo(boundRect_, 0, type_==SocketType::Input ? 180 : -180);
path.lineTo(boundRect_.right(), boundRect_.center().y());
path.closeSubpath();
return path;
}
SocketGraphic::SocketType SocketGraphic::getIO() { return type_; }
@@ -74,6 +89,9 @@ void SocketGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
painter->setBrush(hovered_ ? brushActive_ : brushInactive_);
painter->drawEllipse(QPoint(0,0), socketSize_, socketSize_);
// painter->drawRect(boundRect_);
}
void SocketGraphic::hoverEnterEvent(QGraphicsSceneHoverEvent *event)

View File

@@ -24,6 +24,7 @@ public:
void addEdge(NodeEdgeGraphic* edge);
void removeEdge(NodeEdgeGraphic* edge);
void posChanged(QPointF pos);
QPainterPath shape() const override;
private:
int socketSize_ = 1;
@@ -32,6 +33,10 @@ private:
bool hovered_=false;
SocketType type_;
std::unordered_set<NodeEdgeGraphic*> edges_;
qreal paddingScale_=20;
QRectF boundRect_;
void initBoundingBox();
protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event) override;
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event) override;