feat: fix socket hover

This commit is contained in:
parker
2025-07-15 01:04:07 +01:00
parent 1253785f2b
commit e3f66ea81a
4 changed files with 29 additions and 6 deletions

View File

@@ -190,6 +190,16 @@ void Network::mouseMoved(QMouseEvent *event)
QList<QGraphicsItem*> hoverItems = view_->items(event->pos()); QList<QGraphicsItem*> hoverItems = view_->items(event->pos());
// handle previous items
for(QGraphicsItem* item : prevHoverItems_)
{
if(isType<SocketGraphic>(item))
{
static_cast<SocketGraphic*>(item)->setHover(false);
}
}
prevHoverItems_.clear();
if(state_==State::MOVING_NODE) if(state_==State::MOVING_NODE)
{ {
moveNodes(view_->mapToScene(event->pos())+nodeMoveDelta_); moveNodes(view_->mapToScene(event->pos())+nodeMoveDelta_);
@@ -241,7 +251,11 @@ void Network::mouseMoved(QMouseEvent *event)
std::cout << "unhighlighting\n"; std::cout << "unhighlighting\n";
highlightEdge(prevHoverItem, false); highlightEdge(prevHoverItem, false);
} }
if(auto hoverSocket = closestItemOfType<SocketGraphic>(hoverItems, view_->mapToScene(event->pos())))
{
static_cast<SocketGraphic*>(hoverSocket)->setHover(true);
prevHoverItems_.insert(hoverSocket);
}
} }
void Network::moveNodes(QPointF pos) void Network::moveNodes(QPointF pos)
@@ -331,14 +345,10 @@ void Network::highlightEdge(QGraphicsItem* edge, bool state)
{ {
static_cast<NodeEdgeGraphic*>(edge)->setDeleteHighlight(true); static_cast<NodeEdgeGraphic*>(edge)->setDeleteHighlight(true);
prevHoverItem_=edge; prevHoverItem_=edge;
// NOTE: sloppy fix for color not updating
view_->update();
} }
else else
{ {
static_cast<NodeEdgeGraphic*>(edge)->setDeleteHighlight(false); static_cast<NodeEdgeGraphic*>(edge)->setDeleteHighlight(false);
// NOTE: sloppy fix for color not updating
view_->update();
} }
} }

View File

@@ -43,6 +43,7 @@ private:
SocketGraphic* startSocket_=nullptr; SocketGraphic* startSocket_=nullptr;
QGraphicsItem* prevHoverItem_=nullptr; QGraphicsItem* prevHoverItem_=nullptr;
std::unordered_set<QGraphicsItem*> prevHoverItems_;
// nodes currently being moved // nodes currently being moved
std::vector<QGraphicsItem*> moveNodeBuffer; std::vector<QGraphicsItem*> moveNodeBuffer;
QPointF nodeMoveDelta_; QPointF nodeMoveDelta_;

View File

@@ -10,7 +10,6 @@ SocketGraphic::SocketGraphic(enzo::nt::SocketIOType type, enzo::nt::OpId opId, u
brushActive_ = QBrush("white"); brushActive_ = QBrush("white");
brushInactive_ = QBrush("#9f9f9f"); brushInactive_ = QBrush("#9f9f9f");
socketSize_ = 3; socketSize_ = 3;
setAcceptHoverEvents(true);
initBoundingBox(); initBoundingBox();
} }
@@ -102,6 +101,17 @@ void SocketGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt
} }
void SocketGraphic::setHover(bool state)
{
bool prevState = hovered_;
hovered_ = state;
if(state!=prevState)
{
update();
}
}
void SocketGraphic::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void SocketGraphic::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{ {
hovered_ = true; hovered_ = true;

View File

@@ -25,6 +25,7 @@ public:
enzo::nt::OpId getOpId() const; enzo::nt::OpId getOpId() const;
unsigned int getIndex() const; unsigned int getIndex() const;
void setHover(bool state);
private: private:
int socketSize_ = 1; int socketSize_ = 1;
@@ -37,6 +38,7 @@ private:
qreal paddingScale_=20; qreal paddingScale_=20;
QRectF boundRect_; QRectF boundRect_;
enzo::nt::OpId opId_; enzo::nt::OpId opId_;
bool isHover_ = false;
void initBoundingBox(); void initBoundingBox();
protected: protected: