diff --git a/CMakeLists.txt b/CMakeLists.txt index 6227e341..0c56a8d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/QtNodes/AbstractConnectionPainter b/include/QtNodes/AbstractConnectionPainter new file mode 100644 index 00000000..9e143404 --- /dev/null +++ b/include/QtNodes/AbstractConnectionPainter @@ -0,0 +1 @@ +#include "internal/AbstractConnectionPainter.hpp" diff --git a/include/QtNodes/internal/AbstractConnectionPainter.hpp b/include/QtNodes/internal/AbstractConnectionPainter.hpp new file mode 100644 index 00000000..a25fb622 --- /dev/null +++ b/include/QtNodes/internal/AbstractConnectionPainter.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include + +#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 diff --git a/include/QtNodes/internal/BasicGraphicsScene.hpp b/include/QtNodes/internal/BasicGraphicsScene.hpp index a36e2372..01356887 100644 --- a/include/QtNodes/internal/BasicGraphicsScene.hpp +++ b/include/QtNodes/internal/BasicGraphicsScene.hpp @@ -21,6 +21,7 @@ class QUndoStack; namespace QtNodes { +class AbstractConnectionPainter; class AbstractGraphModel; class AbstractNodePainter; class ConnectionGraphicsObject; @@ -49,8 +50,12 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene AbstractNodePainter &nodePainter(); + AbstractConnectionPainter &connectionPainter(); + void setNodePainter(std::unique_ptr newPainter); + void setConnectionPainter(std::unique_ptr newPainter); + QUndoStack &undoStack(); public: @@ -168,6 +173,8 @@ public Q_SLOTS: std::unique_ptr _nodePainter; + std::unique_ptr _connectionPainter; + bool _nodeDrag; QUndoStack *_undoStack; diff --git a/src/BasicGraphicsScene.cpp b/src/BasicGraphicsScene.cpp index 050bc632..0dab989b 100644 --- a/src/BasicGraphicsScene.cpp +++ b/src/BasicGraphicsScene.cpp @@ -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" @@ -36,6 +37,7 @@ BasicGraphicsScene::BasicGraphicsScene(AbstractGraphModel &graphModel, QObject * , _graphModel(graphModel) , _nodeGeometry(std::make_unique(_graphModel)) , _nodePainter(std::make_unique()) + , _connectionPainter(std::make_unique()) , _nodeDrag(false) , _undoStack(new QUndoStack(this)) , _orientation(Qt::Horizontal) @@ -101,11 +103,21 @@ AbstractNodePainter &BasicGraphicsScene::nodePainter() return *_nodePainter; } +AbstractConnectionPainter &BasicGraphicsScene::connectionPainter() +{ + return *_connectionPainter; +} + void BasicGraphicsScene::setNodePainter(std::unique_ptr newPainter) { _nodePainter = std::move(newPainter); } +void BasicGraphicsScene::setConnectionPainter(std::unique_ptr newPainter) +{ + _connectionPainter = std::move(newPainter); +} + QUndoStack &BasicGraphicsScene::undoStack() { return *_undoStack; diff --git a/src/ConnectionGraphicsObject.cpp b/src/ConnectionGraphicsObject.cpp index 6a871c0e..c7459628 100644 --- a/src/ConnectionGraphicsObject.cpp +++ b/src/ConnectionGraphicsObject.cpp @@ -128,7 +128,7 @@ QPainterPath ConnectionGraphicsObject::shape() const //return path; #else - return ConnectionPainter::getPainterStroke(*this); + return nodeScene()->connectionPainter().getPainterStroke(*this); #endif } @@ -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) diff --git a/src/ConnectionPainter.cpp b/src/ConnectionPainter.cpp index 97002ef7..d9b0276e 100644 --- a/src/ConnectionPainter.cpp +++ b/src/ConnectionPainter.cpp @@ -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); @@ -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); @@ -78,7 +78,7 @@ 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(); @@ -86,7 +86,7 @@ static void drawSketchLine(QPainter *painter, ConnectionGraphicsObject const &cg auto const &connectionStyle = QtNodes::StyleCollection::connectionStyle(); QPen pen; - pen.setWidth(connectionStyle.constructionLineWidth()); + pen.setWidth(static_cast(connectionStyle.constructionLineWidth())); pen.setColor(connectionStyle.constructionColor()); pen.setStyle(Qt::DashLine); @@ -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(); @@ -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(2 * lineWidth)); pen.setColor(selected ? connectionStyle.selectedHaloColor() : connectionStyle.hoveredColor()); @@ -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(); @@ -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); diff --git a/src/ConnectionPainter.hpp b/src/ConnectionPainter.hpp index 8db24d8e..4589c228 100644 --- a/src/ConnectionPainter.hpp +++ b/src/ConnectionPainter.hpp @@ -3,6 +3,7 @@ #include #include +#include "AbstractConnectionPainter.hpp" #include "Definitions.hpp" namespace QtNodes { @@ -10,12 +11,17 @@ 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