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

Enable custom Node connections #386

Closed
wants to merge 8 commits into from
Closed
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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 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 @@ -168,6 +173,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 @@ -5,6 +5,7 @@
#include "ConnectionIdUtils.hpp"
#include "DefaultHorizontalNodeGeometry.hpp"
#include "DefaultNodePainter.hpp"
#include "ConnectionPainter.hpp"
#include "DefaultVerticalNodeGeometry.hpp"
#include "GraphicsView.hpp"
#include "NodeGraphicsObject.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
4 changes: 2 additions & 2 deletions src/ConnectionGraphicsObject.cpp
Original file line number Diff line number Diff line change
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)
{
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)
{
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)
{
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)
{
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
29 changes: 29 additions & 0 deletions src/DefaultConnectionPainter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <QtGui/QPainter>
#include <QtGui/QPainterPath>

#include "AbstractConnectionPainter.hpp"
#include "Definitions.hpp"

namespace QtNodes {

class ConnectionGeometry;
class ConnectionGraphicsObject;

class DefaultConnectionPainter : public AbstractConnectionPainter
{
public:
void paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const override;
QPainterPath getPainterStroke(ConnectionGraphicsObject const &cgo) const override;
private:
QPainterPath cubicPath(ConnectionGraphicsObject const &connection);
void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo);
void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo);
void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo);
#ifdef NODE_DEBUG_DRAWING
void debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo);
#endif
};

} // namespace QtNodes
Loading