feat: edge hover detection
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <QPushButton>
|
||||
#include <QGraphicsItem>
|
||||
#include <QMouseEvent>
|
||||
#include <qnamespace.h>
|
||||
|
||||
Network::Network(QWidget* parent)
|
||||
: QWidget(parent)
|
||||
@@ -118,17 +119,88 @@ void Network::destroyFloatingEdge()
|
||||
|
||||
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_)
|
||||
{
|
||||
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)
|
||||
{
|
||||
// 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)
|
||||
{
|
||||
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;
|
||||
SocketGraphic* startSocket_=nullptr;
|
||||
|
||||
QGraphicsItem* prevHoverItem_=nullptr;
|
||||
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void keyReleaseEvent(QKeyEvent *event) override;
|
||||
void destroyFloatingEdge();
|
||||
|
||||
protected:
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
#include <qgraphicsitem.h>
|
||||
|
||||
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);
|
||||
color_=defaultColor_;
|
||||
}
|
||||
|
||||
QRectF NodeEdgeGraphic::boundingRect() const
|
||||
@@ -16,9 +17,29 @@ QRectF NodeEdgeGraphic::boundingRect() const
|
||||
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)
|
||||
{
|
||||
auto pen = QPen("white");
|
||||
auto pen = QPen(color_);
|
||||
pen.setCapStyle(Qt::RoundCap);
|
||||
painter->setPen(pen);
|
||||
painter->drawLine(socket1_->scenePos(),socket2_->scenePos());
|
||||
|
||||
@@ -12,9 +12,14 @@ public:
|
||||
QRectF boundingRect() const override;
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
|
||||
QPainterPath shape() const override;
|
||||
void setColor(QColor color);
|
||||
void useDefaultColor();
|
||||
|
||||
private:
|
||||
SocketGraphic* socket1_;
|
||||
SocketGraphic* socket2_;
|
||||
QColor color_;
|
||||
QColor defaultColor_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user