Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Connection Line Arrow #438

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/styles/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ static void setStyle()
"ConstructionLineWidth": 2.0,
"PointDiameter": 10.0,

"UseDataDefinedColors": false
"UseDataDefinedColors": false,
"InArrow" : true,
"OutArrow" : false
}
}
)");
Expand Down
4 changes: 4 additions & 0 deletions include/QtNodes/internal/ConnectionStyle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ class NODE_EDITOR_PUBLIC ConnectionStyle : public Style
float pointDiameter() const;

bool useDataDefinedColors() const;
bool inArrow() const;
bool outArrow() const;

private:
QColor ConstructionColor;
QColor NormalColor;
QColor SelectedColor;
QColor SelectedHaloColor;
QColor HoveredColor;
bool InArrow;
bool OutArrow;

float LineWidth;
float ConstructionLineWidth;
Expand Down
14 changes: 14 additions & 0 deletions src/ConnectionStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -203,3 +207,13 @@ bool ConnectionStyle::useDataDefinedColors() const
{
return UseDataDefinedColors;
}


bool ConnectionStyle::inArrow() const
{
return InArrow;
}
bool ConnectionStyle::outArrow() const
{
return OutArrow;
}
62 changes: 46 additions & 16 deletions src/DefaultConnectionPainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -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();

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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);
}

{
Expand All @@ -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
9 changes: 5 additions & 4 deletions src/DefaultConnectionPainter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
4 changes: 3 additions & 1 deletion src/NodeGraphicsObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down