|
| 1 | +#include "CustomNodePainter.hpp" |
| 2 | + |
| 3 | +#include <QtNodes/AbstractGraphModel> |
| 4 | +#include <QtNodes/BasicGraphicsScene> |
| 5 | +#include <QtNodes/StyleCollection> |
| 6 | +#include <QtNodes/internal/NodeGraphicsObject.hpp> |
| 7 | + |
| 8 | +#include <QPainterPath> |
| 9 | + |
| 10 | +using QtNodes::AbstractGraphModel; |
| 11 | +using QtNodes::AbstractNodeGeometry; |
| 12 | +using QtNodes::BasicGraphicsScene; |
| 13 | +using QtNodes::NodeRole; |
| 14 | +using QtNodes::PortType; |
| 15 | +using QtNodes::StyleCollection; |
| 16 | + |
| 17 | +void CustomNodePainter::paint(QPainter *painter, NodeGraphicsObject &ngo) const |
| 18 | +{ |
| 19 | + painter->setRenderHint(QPainter::Antialiasing); |
| 20 | + |
| 21 | + drawBackground(painter, ngo); |
| 22 | + drawCaption(painter, ngo); |
| 23 | + drawPorts(painter, ngo); |
| 24 | +} |
| 25 | + |
| 26 | +void CustomNodePainter::drawBackground(QPainter *painter, NodeGraphicsObject &ngo) const |
| 27 | +{ |
| 28 | + AbstractNodeGeometry const &geometry = ngo.nodeScene()->nodeGeometry(); |
| 29 | + AbstractGraphModel const &model = ngo.graphModel(); |
| 30 | + QtNodes::NodeId const nodeId = ngo.nodeId(); |
| 31 | + |
| 32 | + QRectF const boundingRect = geometry.boundingRect(nodeId); |
| 33 | + |
| 34 | + // Custom rounded rectangle with gradient |
| 35 | + double const radius = 15.0; |
| 36 | + |
| 37 | + QPainterPath path; |
| 38 | + path.addRoundedRect(boundingRect, radius, radius); |
| 39 | + |
| 40 | + // Create a custom gradient - blue to purple |
| 41 | + QLinearGradient gradient(boundingRect.topLeft(), boundingRect.bottomRight()); |
| 42 | + |
| 43 | + if (ngo.isSelected()) { |
| 44 | + gradient.setColorAt(0.0, QColor(100, 149, 237)); // Cornflower blue |
| 45 | + gradient.setColorAt(1.0, QColor(186, 85, 211)); // Medium orchid |
| 46 | + } else { |
| 47 | + gradient.setColorAt(0.0, QColor(70, 130, 180)); // Steel blue |
| 48 | + gradient.setColorAt(1.0, QColor(138, 43, 226)); // Blue violet |
| 49 | + } |
| 50 | + |
| 51 | + painter->fillPath(path, gradient); |
| 52 | + |
| 53 | + // Draw border |
| 54 | + QPen pen(ngo.isSelected() ? QColor(255, 215, 0) : QColor(255, 255, 255), 2.0); |
| 55 | + painter->setPen(pen); |
| 56 | + painter->drawPath(path); |
| 57 | +} |
| 58 | + |
| 59 | +void CustomNodePainter::drawCaption(QPainter *painter, NodeGraphicsObject &ngo) const |
| 60 | +{ |
| 61 | + AbstractNodeGeometry const &geometry = ngo.nodeScene()->nodeGeometry(); |
| 62 | + AbstractGraphModel const &model = ngo.graphModel(); |
| 63 | + QtNodes::NodeId const nodeId = ngo.nodeId(); |
| 64 | + |
| 65 | + QString const caption = model.nodeData(nodeId, NodeRole::Caption).toString(); |
| 66 | + QRectF const boundingRect = geometry.boundingRect(nodeId); |
| 67 | + |
| 68 | + // Draw caption centered at top |
| 69 | + QFont font = painter->font(); |
| 70 | + font.setBold(true); |
| 71 | + font.setPointSize(12); |
| 72 | + painter->setFont(font); |
| 73 | + |
| 74 | + painter->setPen(Qt::white); |
| 75 | + |
| 76 | + QRectF captionRect = boundingRect; |
| 77 | + captionRect.setHeight(30); |
| 78 | + |
| 79 | + painter->drawText(captionRect, Qt::AlignCenter, caption); |
| 80 | +} |
| 81 | + |
| 82 | +void CustomNodePainter::drawPorts(QPainter *painter, NodeGraphicsObject &ngo) const |
| 83 | +{ |
| 84 | + AbstractNodeGeometry const &geometry = ngo.nodeScene()->nodeGeometry(); |
| 85 | + AbstractGraphModel const &model = ngo.graphModel(); |
| 86 | + QtNodes::NodeId const nodeId = ngo.nodeId(); |
| 87 | + |
| 88 | + // Draw input ports (left side) - green circles |
| 89 | + unsigned int const inPortCount = model.nodeData<unsigned int>(nodeId, NodeRole::InPortCount); |
| 90 | + for (unsigned int i = 0; i < inPortCount; ++i) { |
| 91 | + QPointF pos = geometry.portPosition(nodeId, PortType::In, i); |
| 92 | + painter->setBrush(QColor(50, 205, 50)); // Lime green |
| 93 | + painter->setPen(QPen(Qt::white, 1.5)); |
| 94 | + painter->drawEllipse(pos, 6.0, 6.0); |
| 95 | + } |
| 96 | + |
| 97 | + // Draw output ports (right side) - orange circles |
| 98 | + unsigned int const outPortCount = model.nodeData<unsigned int>(nodeId, NodeRole::OutPortCount); |
| 99 | + for (unsigned int i = 0; i < outPortCount; ++i) { |
| 100 | + QPointF pos = geometry.portPosition(nodeId, PortType::Out, i); |
| 101 | + painter->setBrush(QColor(255, 165, 0)); // Orange |
| 102 | + painter->setPen(QPen(Qt::white, 1.5)); |
| 103 | + painter->drawEllipse(pos, 6.0, 6.0); |
| 104 | + } |
| 105 | +} |
0 commit comments