From 2ea7fa0e038a442377dce2860d7d5e41b926897c Mon Sep 17 00:00:00 2001 From: maozihua Date: Thu, 29 Aug 2024 18:25:55 +0800 Subject: [PATCH 1/3] add arrow config --- examples/styles/main.cpp | 4 +++- include/QtNodes/internal/ConnectionStyle.hpp | 4 ++++ src/ConnectionStyle.cpp | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/examples/styles/main.cpp b/examples/styles/main.cpp index 528780583..0551dc659 100644 --- a/examples/styles/main.cpp +++ b/examples/styles/main.cpp @@ -77,7 +77,9 @@ static void setStyle() "ConstructionLineWidth": 2.0, "PointDiameter": 10.0, - "UseDataDefinedColors": false + "UseDataDefinedColors": false, + "InArrow" : true, + "OutArrow" : false } } )"); diff --git a/include/QtNodes/internal/ConnectionStyle.hpp b/include/QtNodes/internal/ConnectionStyle.hpp index b718bcfae..11dd6f6f3 100644 --- a/include/QtNodes/internal/ConnectionStyle.hpp +++ b/include/QtNodes/internal/ConnectionStyle.hpp @@ -37,6 +37,8 @@ class NODE_EDITOR_PUBLIC ConnectionStyle : public Style float pointDiameter() const; bool useDataDefinedColors() const; + bool inArrow() const; + bool outArrow() const; private: QColor ConstructionColor; @@ -44,6 +46,8 @@ class NODE_EDITOR_PUBLIC ConnectionStyle : public Style QColor SelectedColor; QColor SelectedHaloColor; QColor HoveredColor; + bool InArrow; + bool OutArrow; float LineWidth; float ConstructionLineWidth; diff --git a/src/ConnectionStyle.cpp b/src/ConnectionStyle.cpp index 812658989..c80c9f559 100644 --- a/src/ConnectionStyle.cpp +++ b/src/ConnectionStyle.cpp @@ -120,6 +120,8 @@ void ConnectionStyle::loadJson(QJsonObject const &json) CONNECTION_STYLE_READ_FLOAT(obj, PointDiameter); CONNECTION_STYLE_READ_BOOL(obj, UseDataDefinedColors); + CONNECTION_STYLE_READ_BOOL(obj, InArrow); + CONNECTION_STYLE_READ_BOOL(obj, OutArrow); } QJsonObject ConnectionStyle::toJson() const @@ -137,6 +139,8 @@ QJsonObject ConnectionStyle::toJson() const CONNECTION_STYLE_WRITE_FLOAT(obj, PointDiameter); CONNECTION_STYLE_WRITE_BOOL(obj, UseDataDefinedColors); + CONNECTION_STYLE_WRITE_BOOL(obj, InArrow); + CONNECTION_STYLE_WRITE_BOOL(obj, OutArrow); QJsonObject root; root["ConnectionStyle"] = obj; @@ -203,3 +207,13 @@ bool ConnectionStyle::useDataDefinedColors() const { return UseDataDefinedColors; } + + +bool ConnectionStyle::inArrow() const +{ + return InArrow; +} +bool ConnectionStyle::outArrow() const +{ + return OutArrow; +} From c31a585a0cebd508b5f8c7d4e9d3296ef328460a Mon Sep 17 00:00:00 2001 From: maozihua Date: Thu, 29 Aug 2024 18:52:22 +0800 Subject: [PATCH 2/3] draw Arrow --- src/DefaultConnectionPainter.cpp | 62 +++++++++++++++++++++++--------- src/DefaultConnectionPainter.hpp | 9 ++--- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/DefaultConnectionPainter.cpp b/src/DefaultConnectionPainter.cpp index 4a95bc909..b6115e8ad 100644 --- a/src/DefaultConnectionPainter.cpp +++ b/src/DefaultConnectionPainter.cpp @@ -26,7 +26,7 @@ QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const return cubic; } -void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const +void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo, QPainterPath const &cubic) const { ConnectionState const &state = cgo.connectionState(); @@ -40,15 +40,12 @@ void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraph painter->setPen(pen); painter->setBrush(Qt::NoBrush); - - auto cubic = cubicPath(cgo); - // cubic spline painter->drawPath(cubic); } } -void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo) const +void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo, QPainterPath const &cubic) const { bool const hovered = cgo.connectionState().hovered(); bool const selected = cgo.isSelected(); @@ -68,12 +65,11 @@ void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, Connecti painter->setBrush(Qt::NoBrush); // cubic spline - auto const cubic = cubicPath(cgo); painter->drawPath(cubic); } } -void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const +void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo, QPainterPath const &cubic) const { ConnectionState const &state = cgo.connectionState(); @@ -126,7 +122,6 @@ void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraph bool const selected = cgo.isSelected(); - auto cubic = cubicPath(cgo); if (useGradientColor) { painter->setBrush(Qt::NoBrush); @@ -177,14 +172,15 @@ void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraph void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const { - drawHoveredOrSelected(painter, cgo); + auto cubic = cubicPath(cgo); + drawHoveredOrSelected(painter, cgo,cubic); - drawSketchLine(painter, cgo); + drawSketchLine(painter, cgo,cubic); - drawNormalLine(painter, cgo); + drawNormalLine(painter, cgo,cubic); #ifdef NODE_DEBUG_DRAWING - debugDrawing(painter, cgo); + debugDrawing(painter, cgo,cubic); #endif // draw end points @@ -195,8 +191,18 @@ void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject painter->setPen(connectionStyle.constructionColor()); painter->setBrush(connectionStyle.constructionColor()); double const pointRadius = pointDiameter / 2.0; - painter->drawEllipse(cgo.out(), pointRadius, pointRadius); - painter->drawEllipse(cgo.in(), pointRadius, pointRadius); + if(connectionStyle.outArrow()) { + auto out = createArrowPoly(cubic,pointRadius,pointDiameter * 1.5,false); + painter->drawPolygon(out); + } else { + painter->drawEllipse(cgo.out(), pointRadius, pointRadius); + } + if(connectionStyle.inArrow()) { + auto in = createArrowPoly(cubic,pointRadius,pointDiameter * 1.5,true); + painter->drawPolygon(in); + } else { + painter->drawEllipse(cgo.in(), pointRadius, pointRadius); + } } QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection) const @@ -220,7 +226,7 @@ QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject } #ifdef NODE_DEBUG_DRAWING -void DefaultConnectionPainter::debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo) +void DefaultConnectionPainter::debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo,QPainterPath const & cubic) { Q_UNUSED(painter); @@ -240,7 +246,7 @@ void DefaultConnectionPainter::debugDrawing(QPainter *painter, ConnectionGraphic painter->drawEllipse(points.second, 3, 3); painter->setBrush(Qt::NoBrush); - painter->drawPath(cubicPath(cgo)); + painter->drawPath(debugDrawing); } { @@ -250,4 +256,28 @@ void DefaultConnectionPainter::debugDrawing(QPainter *painter, ConnectionGraphic } #endif +QPolygonF DefaultConnectionPainter::createArrowPoly(const QPainterPath& p, double mRadius,double arrowSize,bool drawIn) { + float arrowStartPercentage; + float arrowEndPercentage; + + if (drawIn) { + arrowStartPercentage = p.percentAtLength(p.length() - mRadius - arrowSize); + arrowEndPercentage = p.percentAtLength(p.length() - mRadius); + } + else { + arrowStartPercentage = p.percentAtLength(mRadius + arrowSize); + arrowEndPercentage = p.percentAtLength(mRadius); + } + QPointF headStartP = p.pointAtPercent(arrowStartPercentage); + QPointF headEndP = p.pointAtPercent(arrowEndPercentage); + QLineF arrowMiddleLine(headStartP, headEndP); + QPointF normHead(arrowMiddleLine.dy(), -arrowMiddleLine.dx()); + QPointF arrowP1 = headStartP + normHead * 0.4; + QPointF arrowP2 = headStartP - normHead * 0.4; + + QPolygonF arrowHeadEnd; + arrowHeadEnd << headEndP << arrowP1 << arrowP2 /*<< headEndP*/; + return arrowHeadEnd; +} + } // namespace QtNodes diff --git a/src/DefaultConnectionPainter.hpp b/src/DefaultConnectionPainter.hpp index b2c44d102..0bc0bd007 100644 --- a/src/DefaultConnectionPainter.hpp +++ b/src/DefaultConnectionPainter.hpp @@ -16,13 +16,14 @@ class DefaultConnectionPainter : public AbstractConnectionPainter public: void paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const override; QPainterPath getPainterStroke(ConnectionGraphicsObject const &cgo) const override; + static QPolygonF createArrowPoly(const QPainterPath& p, double mRadius,double arrowSize,bool drawIn = true); private: QPainterPath cubicPath(ConnectionGraphicsObject const &connection) const; - void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const; - void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo) const; - void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const; + void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo,QPainterPath const & cubic) const; + void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo,QPainterPath const & cubic) const; + void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo,QPainterPath const & cubic) const; #ifdef NODE_DEBUG_DRAWING - void debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo) const; + void debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo,QPainterPath const & cubic) const; #endif }; From 27c2064f0834cb15cf46ffb233c7530d971a30b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A1=E4=B8=96=E7=99=BD=E7=8E=89?= Date: Mon, 28 Oct 2024 18:05:22 +0800 Subject: [PATCH 3/3] Update NodeGraphicsObject.cpp fix _proxyWidget maybe null --- src/NodeGraphicsObject.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/NodeGraphicsObject.cpp b/src/NodeGraphicsObject.cpp index f76942ea7..ac3c4f0ab 100644 --- a/src/NodeGraphicsObject.cpp +++ b/src/NodeGraphicsObject.cpp @@ -78,7 +78,9 @@ BasicGraphicsScene *NodeGraphicsObject::nodeScene() const void NodeGraphicsObject::updateQWidgetEmbedPos() { - _proxyWidget->setPos(nodeScene()->nodeGeometry().widgetPosition(_nodeId)); + if(_proxyWidget) { + _proxyWidget->setPos(nodeScene()->nodeGeometry().widgetPosition(_nodeId)); + } } void NodeGraphicsObject::embedQWidget()