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 committed Apr 17, 2023
1 parent 5465ddc commit 0b2ec15
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 13 deletions.
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"
27 changes: 27 additions & 0 deletions include/QtNodes/internal/AbstractConnectionPainter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#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<ConnectionPainter>())
, _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
16 changes: 8 additions & 8 deletions src/ConnectionPainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace QtNodes {

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

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

Expand Down Expand Up @@ -78,15 +78,15 @@ static void debugDrawing(QPainter *painter, ConnectionGraphicsObject const &cgo)

#endif

static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo)
void ConnectionPainter::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 +100,7 @@ static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cg
}
}

static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo)
void ConnectionPainter::drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo)
{
bool const hovered = cgo.connectionState().hovered();
bool const selected = cgo.isSelected();
Expand All @@ -112,7 +112,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 +125,7 @@ static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject co
}
}

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

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

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

Expand Down
12 changes: 9 additions & 3 deletions src/ConnectionPainter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
#include <QtGui/QPainter>
#include <QtGui/QPainterPath>

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

namespace QtNodes {

class ConnectionGeometry;
class ConnectionGraphicsObject;

class ConnectionPainter
class ConnectionPainter : public AbstractConnectionPainter
{
public:
static void paint(QPainter *painter, ConnectionGraphicsObject const &cgo);
void paint(QPainter *painter, ConnectionGraphicsObject const &cgo) const override;
QPainterPath getPainterStroke(ConnectionGraphicsObject const &cgo) const override;

static QPainterPath getPainterStroke(ConnectionGraphicsObject const &cgo);
private:
static QPainterPath cubicPath(ConnectionGraphicsObject const &connection);
static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cgo);
static void drawHoveredOrSelected(QPainter *painter, ConnectionGraphicsObject const &cgo);
static void drawNormalLine(QPainter *painter, ConnectionGraphicsObject const &cgo);
};

} // namespace QtNodes

0 comments on commit 0b2ec15

Please sign in to comment.