refactor: abstract highlight edge

This commit is contained in:
parker
2025-06-21 23:02:00 +01:00
parent 00adbd08e5
commit cc1976ff6d
2 changed files with 27 additions and 10 deletions

View File

@@ -8,6 +8,7 @@
#include <QPushButton> #include <QPushButton>
#include <QGraphicsItem> #include <QGraphicsItem>
#include <QMouseEvent> #include <QMouseEvent>
#include <qgraphicsitem.h>
#include <qnamespace.h> #include <qnamespace.h>
Network::Network(QWidget* parent) Network::Network(QWidget* parent)
@@ -140,9 +141,7 @@ void Network::mouseMoved(QMouseEvent *event)
// set node edge color // set node edge color
if(ctrlMod && isType<NodeEdgeGraphic>(hoverItem)) if(ctrlMod && isType<NodeEdgeGraphic>(hoverItem))
{ {
static_cast<NodeEdgeGraphic*>(hoverItem)->setColor(QColor("red")); highlightEdge(hoverItem, true);
hoverItem->update();
prevHoverItem_=hoverItem;
} }
// reset node edge color // reset node edge color
if( if(
@@ -150,8 +149,7 @@ void Network::mouseMoved(QMouseEvent *event)
isType<NodeEdgeGraphic>(prevHoverItem) isType<NodeEdgeGraphic>(prevHoverItem)
) )
{ {
static_cast<NodeEdgeGraphic*>(prevHoverItem)->useDefaultColor(); highlightEdge(prevHoverItem, false);
prevHoverItem->update();
} }
} }
@@ -169,14 +167,13 @@ void Network::keyPressEvent(QKeyEvent *event)
QGraphicsItem* hoverItem = view_->itemAt(widgetPos); QGraphicsItem* hoverItem = view_->itemAt(widgetPos);
// edge detection
if( if(
event->key() == Qt::Key_Control && event->key() == Qt::Key_Control &&
isType<NodeEdgeGraphic>(hoverItem) isType<NodeEdgeGraphic>(hoverItem)
) )
{ {
static_cast<NodeEdgeGraphic*>(hoverItem)->setColor(QColor("red")); highlightEdge(hoverItem, true);
hoverItem->update();
prevHoverItem_=hoverItem;
} }
if(event->key() == Qt::Key_Escape) if(event->key() == Qt::Key_Escape)
@@ -185,19 +182,36 @@ void Network::keyPressEvent(QKeyEvent *event)
} }
} }
void Network::highlightEdge(QGraphicsItem* edge, bool state)
{
if(state)
{
static_cast<NodeEdgeGraphic*>(edge)->setColor(QColor("red"));
edge->update();
prevHoverItem_=edge;
}
else
{
static_cast<NodeEdgeGraphic*>(edge)->useDefaultColor();
edge->update();
}
}
void Network::keyReleaseEvent(QKeyEvent *event) void Network::keyReleaseEvent(QKeyEvent *event)
{ {
// modifiers // modifiers
Qt::KeyboardModifiers mods = event->modifiers(); Qt::KeyboardModifiers mods = event->modifiers();
bool ctrlMod = mods & Qt::ControlModifier; bool ctrlMod = mods & Qt::ControlModifier;
// edge detection
if( if(
prevHoverItem_ && prevHoverItem_ &&
event->key() == Qt::Key_Control && event->key() == Qt::Key_Control &&
isType<NodeEdgeGraphic>(prevHoverItem_) isType<NodeEdgeGraphic>(prevHoverItem_)
) )
{ {
static_cast<NodeEdgeGraphic*>(prevHoverItem_)->useDefaultColor(); highlightEdge(prevHoverItem_, false);
prevHoverItem_->update();
} }
} }

View File

@@ -3,6 +3,7 @@
#include <qgraphicsitem.h> #include <qgraphicsitem.h>
#include <typeinfo> #include <typeinfo>
#include "gui/network/NetworkGraphicsView.h" #include "gui/network/NetworkGraphicsView.h"
#include "gui/network/NodeEdgeGraphic.h"
#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"
@@ -29,6 +30,8 @@ private:
void keyReleaseEvent(QKeyEvent *event) override; void keyReleaseEvent(QKeyEvent *event) override;
void destroyFloatingEdge(); void destroyFloatingEdge();
void highlightEdge(QGraphicsItem* edge, bool state);
template<typename T> template<typename T>
bool isType(QGraphicsItem* item) bool isType(QGraphicsItem* item)
{ {