Skip to content

Commit

Permalink
Add AbstractConnectionPainter to enable custom connections
Browse files Browse the repository at this point in the history
  • Loading branch information
dine-j authored and paceholder committed Aug 16, 2024
1 parent 4ac0857 commit de5fd0a
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 89 deletions.
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,21 @@ set(CPP_SOURCE_FILES
src/AbstractNodeGeometry.cpp
src/BasicGraphicsScene.cpp
src/ConnectionGraphicsObject.cpp
src/ConnectionPainter.cpp
src/ConnectionState.cpp
src/ConnectionStyle.cpp
src/DataFlowGraphModel.cpp
src/DataFlowGraphicsScene.cpp
src/DefaultConnectionPainter.cpp
src/DefaultHorizontalNodeGeometry.cpp
src/DefaultNodePainter.cpp
src/DefaultVerticalNodeGeometry.cpp
src/Definitions.cpp
src/GraphicsView.cpp
src/GraphicsViewStyle.cpp
src/NodeDelegateModelRegistry.cpp
src/NodeConnectionInteraction.cpp
src/NodeDelegateModel.cpp
src/NodeDelegateModelRegistry.cpp
src/NodeGraphicsObject.cpp
src/DefaultNodePainter.cpp
src/NodeState.cpp
src/NodeStyle.cpp
src/StyleCollection.cpp
Expand All @@ -98,6 +98,7 @@ set(CPP_SOURCE_FILES
)

set(HPP_HEADER_FILES
include/QtNodes/internal/AbstractConnectionPainter.hpp
include/QtNodes/internal/AbstractGraphModel.hpp
include/QtNodes/internal/AbstractNodeGeometry.hpp
include/QtNodes/internal/AbstractNodePainter.hpp
Expand All @@ -110,7 +111,6 @@ set(HPP_HEADER_FILES
include/QtNodes/internal/ConnectionStyle.hpp
include/QtNodes/internal/DataFlowGraphicsScene.hpp
include/QtNodes/internal/DataFlowGraphModel.hpp
include/QtNodes/internal/DefaultNodePainter.hpp
include/QtNodes/internal/Definitions.hpp
include/QtNodes/internal/Export.hpp
include/QtNodes/internal/GraphicsView.hpp
Expand All @@ -128,8 +128,9 @@ set(HPP_HEADER_FILES
include/QtNodes/internal/Serializable.hpp
include/QtNodes/internal/Style.hpp
include/QtNodes/internal/StyleCollection.hpp
src/ConnectionPainter.hpp
src/DefaultConnectionPainter.hpp
src/DefaultHorizontalNodeGeometry.hpp
src/DefaultNodePainter.hpp
src/DefaultVerticalNodeGeometry.hpp
src/NodeConnectionInteraction.hpp
src/UndoCommands.hpp
Expand Down
1 change: 1 addition & 0 deletions include/QtNodes/AbstractConnectionPainter
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "internal/AbstractConnectionPainter.hpp"
26 changes: 26 additions & 0 deletions include/QtNodes/internal/AbstractConnectionPainter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <QPainter>

#include "Export.hpp"

class QPainter;

namespace QtNodes {

class ConnectionGraphicsObject;

/// Class enables custom painting for connections.
class NODE_EDITOR_PUBLIC AbstractConnectionPainter
{
public:
virtual ~AbstractConnectionPainter() = default;

/**
* Reimplement this function in order to have a custom connection painting.
*/
virtual void paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const = 0;

virtual QPainterPath getPainterStroke(ConnectionGraphicsObject const &cgo) const = 0;
};
} // namespace QtNodes
7 changes: 7 additions & 0 deletions include/QtNodes/internal/BasicGraphicsScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class QUndoStack;

namespace QtNodes {

class AbstractConnectionPainter;
class AbstractGraphModel;
class AbstractNodePainter;
class ConnectionGraphicsObject;
Expand Down Expand Up @@ -49,8 +50,12 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene

AbstractNodePainter &nodePainter();

AbstractConnectionPainter &connectionPainter();

void setNodePainter(std::unique_ptr<AbstractNodePainter> newPainter);

void setConnectionPainter(std::unique_ptr<AbstractConnectionPainter> newPainter);

QUndoStack &undoStack();

public:
Expand Down Expand Up @@ -170,6 +175,8 @@ public Q_SLOTS:

std::unique_ptr<AbstractNodePainter> _nodePainter;

std::unique_ptr<AbstractConnectionPainter> _connectionPainter;

bool _nodeDrag;

QUndoStack *_undoStack;
Expand Down
12 changes: 12 additions & 0 deletions src/BasicGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "AbstractNodeGeometry.hpp"
#include "ConnectionGraphicsObject.hpp"
#include "ConnectionIdUtils.hpp"
#include "DefaultConnectionPainter.hpp"
#include "DefaultHorizontalNodeGeometry.hpp"
#include "DefaultNodePainter.hpp"
#include "DefaultVerticalNodeGeometry.hpp"
Expand Down Expand Up @@ -36,6 +37,7 @@ BasicGraphicsScene::BasicGraphicsScene(AbstractGraphModel &graphModel, QObject *
, _graphModel(graphModel)
, _nodeGeometry(std::make_unique<DefaultHorizontalNodeGeometry>(_graphModel))
, _nodePainter(std::make_unique<DefaultNodePainter>())
, _connectionPainter(std::make_unique<DefaultConnectionPainter>())
, _nodeDrag(false)
, _undoStack(new QUndoStack(this))
, _orientation(Qt::Horizontal)
Expand Down Expand Up @@ -101,11 +103,21 @@ AbstractNodePainter &BasicGraphicsScene::nodePainter()
return *_nodePainter;
}

AbstractConnectionPainter &BasicGraphicsScene::connectionPainter()
{
return *_connectionPainter;
}

void BasicGraphicsScene::setNodePainter(std::unique_ptr<AbstractNodePainter> newPainter)
{
_nodePainter = std::move(newPainter);
}

void BasicGraphicsScene::setConnectionPainter(std::unique_ptr<AbstractConnectionPainter> newPainter)
{
_connectionPainter = std::move(newPainter);
}

QUndoStack &BasicGraphicsScene::undoStack()
{
return *_undoStack;
Expand Down
6 changes: 3 additions & 3 deletions src/ConnectionGraphicsObject.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "ConnectionGraphicsObject.hpp"

#include "AbstractConnectionPainter.hpp"
#include "AbstractGraphModel.hpp"
#include "AbstractNodeGeometry.hpp"
#include "BasicGraphicsScene.hpp"
#include "ConnectionIdUtils.hpp"
#include "ConnectionPainter.hpp"
#include "ConnectionState.hpp"
#include "ConnectionStyle.hpp"
#include "NodeConnectionInteraction.hpp"
Expand Down Expand Up @@ -128,7 +128,7 @@ QPainterPath ConnectionGraphicsObject::shape() const
//return path;

#else
return ConnectionPainter::getPainterStroke(*this);
return nodeScene()->connectionPainter().getPainterStroke(*this);
#endif
}

Expand Down Expand Up @@ -198,7 +198,7 @@ void ConnectionGraphicsObject::paint(QPainter *painter,

painter->setClipRect(option->exposedRect);

ConnectionPainter::paint(painter, *this);
nodeScene()->connectionPainter().paint(painter, *this);
}

void ConnectionGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent *event)
Expand Down
21 changes: 0 additions & 21 deletions src/ConnectionPainter.hpp

This file was deleted.

119 changes: 59 additions & 60 deletions src/ConnectionPainter.cpp → src/DefaultConnectionPainter.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ConnectionPainter.hpp"
#include "DefaultConnectionPainter.hpp"

#include <QtGui/QIcon>

Expand All @@ -11,7 +11,7 @@

namespace QtNodes {

static QPainterPath cubicPath(ConnectionGraphicsObject const &connection)
QPainterPath DefaultConnectionPainter::cubicPath(ConnectionGraphicsObject const &connection) const
{
QPointF const &in = connection.endPoint(PortType::In);
QPointF const &out = connection.endPoint(PortType::Out);
Expand All @@ -26,67 +26,15 @@ static QPainterPath cubicPath(ConnectionGraphicsObject const &connection)
return cubic;
}

QPainterPath ConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection)
{
auto cubic = cubicPath(connection);

QPointF const &out = connection.endPoint(PortType::Out);
QPainterPath result(out);

unsigned segments = 20;

for (auto i = 0ul; i < segments; ++i) {
double ratio = double(i + 1) / segments;
result.lineTo(cubic.pointAtPercent(ratio));
}

QPainterPathStroker stroker;
stroker.setWidth(10.0);

return stroker.createStroke(result);
}

#ifdef NODE_DEBUG_DRAWING
static void debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
Q_UNUSED(painter);

{
QPointF const &in = cgo.endPoint(PortType::In);
QPointF const &out = cgo.endPoint(PortType::Out);

auto const points = cgo.pointsC1C2();

painter->setPen(Qt::red);
painter->setBrush(Qt::red);

painter->drawLine(QLineF(out, points.first));
painter->drawLine(QLineF(points.first, points.second));
painter->drawLine(QLineF(points.second, in));
painter->drawEllipse(points.first, 3, 3);
painter->drawEllipse(points.second, 3, 3);

painter->setBrush(Qt::NoBrush);
painter->drawPath(cubicPath(cgo));
}

{
painter->setPen(Qt::yellow);
painter->drawRect(cgo.boundingRect());
}
}

#endif

static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo)
void DefaultConnectionPainter::drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const
{
ConnectionState const &state = cgo.connectionState();

if (state.requiresPort()) {
auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle();

QPen pen;
pen.setWidth(connectionStyle.constructionLineWidth());
pen.setWidth(static_cast<int>(connectionStyle.constructionLineWidth()));
pen.setColor(connectionStyle.constructionColor());
pen.setStyle(Qt::DashLine);

Expand All @@ -100,7 +48,7 @@ static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cg
}
}

static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo)
void DefaultConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo) const
{
bool const hovered = cgo.connectionState().hovered();
bool const selected = cgo.isSelected();
Expand All @@ -112,7 +60,7 @@ static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject co
double const lineWidth = connectionStyle.lineWidth();

QPen pen;
pen.setWidth(2 * lineWidth);
pen.setWidth(static_cast<int>(2 * lineWidth));
pen.setColor(selected ? connectionStyle.selectedHaloColor()
: connectionStyle.hoveredColor());

Expand All @@ -125,7 +73,7 @@ static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject co
}
}

static void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo)
void DefaultConnectionPainter::drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo) const
{
ConnectionState const &state = cgo.connectionState();

Expand Down Expand Up @@ -227,7 +175,7 @@ static void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cg
}
}

void ConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject const &cgo)
void DefaultConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const
{
drawHoveredOrSelected(painter, cgo);

Expand All @@ -251,4 +199,55 @@ void ConnectionPainter::paint(QPainter *painter, ConnectionGraphicsObject const
painter->drawEllipse(cgo.in(), pointRadius, pointRadius);
}

QPainterPath DefaultConnectionPainter::getPainterStroke(ConnectionGraphicsObject const &connection) const
{
auto cubic = cubicPath(connection);

QPointF const &out = connection.endPoint(PortType::Out);
QPainterPath result(out);

unsigned segments = 20;

for (auto i = 0ul; i < segments; ++i) {
double ratio = double(i + 1) / segments;
result.lineTo(cubic.pointAtPercent(ratio));
}

QPainterPathStroker stroker;
stroker.setWidth(10.0);

return stroker.createStroke(result);
}

#ifdef NODE_DEBUG_DRAWING
void DefaultConnectionPainter::debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
Q_UNUSED(painter);

{
QPointF const &in = cgo.endPoint(PortType::In);
QPointF const &out = cgo.endPoint(PortType::Out);

auto const points = cgo.pointsC1C2();

painter->setPen(Qt::red);
painter->setBrush(Qt::red);

painter->drawLine(QLineF(out, points.first));
painter->drawLine(QLineF(points.first, points.second));
painter->drawLine(QLineF(points.second, in));
painter->drawEllipse(points.first, 3, 3);
painter->drawEllipse(points.second, 3, 3);

painter->setBrush(Qt::NoBrush);
painter->drawPath(cubicPath(cgo));
}

{
painter->setPen(Qt::yellow);
painter->drawRect(cgo.boundingRect());
}
}
#endif

} // namespace QtNodes
Loading

0 comments on commit de5fd0a

Please sign in to comment.