feat: edge hover detection
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QGraphicsItem>
|
#include <QGraphicsItem>
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
|
#include <qnamespace.h>
|
||||||
|
|
||||||
Network::Network(QWidget* parent)
|
Network::Network(QWidget* parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
@@ -118,17 +119,88 @@ void Network::destroyFloatingEdge()
|
|||||||
|
|
||||||
void Network::mouseMoved(QMouseEvent *event)
|
void Network::mouseMoved(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
|
// cache and reset prev hover item
|
||||||
|
QGraphicsItem* prevHoverItem=prevHoverItem_;
|
||||||
|
prevHoverItem_=nullptr;
|
||||||
|
|
||||||
|
// modifiers
|
||||||
|
Qt::KeyboardModifiers mods = event->modifiers();
|
||||||
|
bool ctrlMod = mods & Qt::ControlModifier;
|
||||||
|
|
||||||
if(floatingEdge_)
|
if(floatingEdge_)
|
||||||
{
|
{
|
||||||
floatingEdge_->setFloatPos(view_->mapToScene(event->pos()));
|
floatingEdge_->setFloatPos(view_->mapToScene(event->pos()));
|
||||||
|
event->accept();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QGraphicsItem* hoverItem = view_->itemAt(event->pos());
|
||||||
|
bool isEdge = hoverItem && typeid(*hoverItem)==typeid(NodeEdgeGraphic);
|
||||||
|
|
||||||
|
// set
|
||||||
|
if(ctrlMod && isEdge)
|
||||||
|
{
|
||||||
|
std::cout << "EDGE\n";
|
||||||
|
static_cast<NodeEdgeGraphic*>(hoverItem)->setColor(QColor("red"));
|
||||||
|
hoverItem->update();
|
||||||
|
prevHoverItem_=hoverItem;
|
||||||
|
}
|
||||||
|
// reset node edge color
|
||||||
|
if(
|
||||||
|
prevHoverItem &&
|
||||||
|
(!ctrlMod || hoverItem!=prevHoverItem) &&
|
||||||
|
typeid(*prevHoverItem)==typeid(NodeEdgeGraphic)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
std::cout << " reset\n";
|
||||||
|
static_cast<NodeEdgeGraphic*>(prevHoverItem)->useDefaultColor();
|
||||||
|
prevHoverItem->update();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Network::keyPressEvent(QKeyEvent *event)
|
void Network::keyPressEvent(QKeyEvent *event)
|
||||||
{
|
{
|
||||||
|
// modifiers
|
||||||
|
Qt::KeyboardModifiers mods = event->modifiers();
|
||||||
|
bool ctrlMod = mods & Qt::ControlModifier;
|
||||||
|
|
||||||
|
// get pos
|
||||||
|
QPoint globalPos = QCursor::pos();
|
||||||
|
QPoint widgetPos = mapFromGlobal(globalPos);
|
||||||
|
|
||||||
|
QGraphicsItem* hoverItem = view_->itemAt(widgetPos);
|
||||||
|
|
||||||
|
if(
|
||||||
|
event->key() == Qt::Key_Control &&
|
||||||
|
hoverItem && typeid(*hoverItem)==typeid(NodeEdgeGraphic)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
static_cast<NodeEdgeGraphic*>(hoverItem)->setColor(QColor("red"));
|
||||||
|
hoverItem->update();
|
||||||
|
prevHoverItem_=hoverItem;
|
||||||
|
}
|
||||||
|
|
||||||
if(event->key() == Qt::Key_Escape)
|
if(event->key() == Qt::Key_Escape)
|
||||||
{
|
{
|
||||||
destroyFloatingEdge();
|
destroyFloatingEdge();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Network::keyReleaseEvent(QKeyEvent *event)
|
||||||
|
{
|
||||||
|
// modifiers
|
||||||
|
Qt::KeyboardModifiers mods = event->modifiers();
|
||||||
|
bool ctrlMod = mods & Qt::ControlModifier;
|
||||||
|
|
||||||
|
if(
|
||||||
|
prevHoverItem_ &&
|
||||||
|
event->key() == Qt::Key_Control &&
|
||||||
|
typeid(*prevHoverItem_)==typeid(NodeEdgeGraphic)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
static_cast<NodeEdgeGraphic*>(prevHoverItem_)->useDefaultColor();
|
||||||
|
prevHoverItem_->update();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ private:
|
|||||||
FloatingEdgeGraphic* floatingEdge_=nullptr;
|
FloatingEdgeGraphic* floatingEdge_=nullptr;
|
||||||
SocketGraphic* startSocket_=nullptr;
|
SocketGraphic* startSocket_=nullptr;
|
||||||
|
|
||||||
|
QGraphicsItem* prevHoverItem_=nullptr;
|
||||||
|
|
||||||
void keyPressEvent(QKeyEvent *event) override;
|
void keyPressEvent(QKeyEvent *event) override;
|
||||||
|
void keyReleaseEvent(QKeyEvent *event) override;
|
||||||
void destroyFloatingEdge();
|
void destroyFloatingEdge();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -5,9 +5,10 @@
|
|||||||
#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}
|
: QGraphicsItem(parent), socket1_{socket1}, socket2_{socket2}, defaultColor_{QColor("white")}
|
||||||
{
|
{
|
||||||
setZValue(-1);
|
setZValue(-1);
|
||||||
|
color_=defaultColor_;
|
||||||
}
|
}
|
||||||
|
|
||||||
QRectF NodeEdgeGraphic::boundingRect() const
|
QRectF NodeEdgeGraphic::boundingRect() const
|
||||||
@@ -16,9 +17,29 @@ QRectF NodeEdgeGraphic::boundingRect() const
|
|||||||
return boundRect;
|
return boundRect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QPainterPath NodeEdgeGraphic::shape() const{
|
||||||
|
QPainterPath path;
|
||||||
|
path.moveTo(socket1_->scenePos());
|
||||||
|
path.lineTo(socket2_->scenePos());
|
||||||
|
|
||||||
|
QPainterPathStroker stroker;
|
||||||
|
stroker.setWidth(10);
|
||||||
|
return stroker.createStroke(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeEdgeGraphic::setColor(QColor color)
|
||||||
|
{
|
||||||
|
color_ = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NodeEdgeGraphic::useDefaultColor()
|
||||||
|
{
|
||||||
|
setColor(defaultColor_);
|
||||||
|
}
|
||||||
|
|
||||||
void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
void NodeEdgeGraphic::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
||||||
{
|
{
|
||||||
auto pen = QPen("white");
|
auto pen = QPen(color_);
|
||||||
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());
|
||||||
|
|||||||
@@ -12,9 +12,14 @@ public:
|
|||||||
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;
|
||||||
|
QPainterPath shape() const override;
|
||||||
|
void setColor(QColor color);
|
||||||
|
void useDefaultColor();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SocketGraphic* socket1_;
|
SocketGraphic* socket1_;
|
||||||
SocketGraphic* socket2_;
|
SocketGraphic* socket2_;
|
||||||
|
QColor color_;
|
||||||
|
QColor defaultColor_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user