#include "QtConnectionGraphicsObject.hpp"#include <QtWidgets/QGraphicsSceneMouseEvent>#include <QtWidgets/QGraphicsDropShadowEffect>#include <QtWidgets/QGraphicsBlurEffect>#include <QtWidgets/QStyleOptionGraphicsItem>#include <QtWidgets/QGraphicsView>#include "QtFlowScene.hpp"#include "QtNode.hpp"#include "QtConnection.hpp"#include "QtNodeGraphicsObject.hpp"#include "QtInteraction.hpp"namespace Qt{staticQPainterPathcubicPath(ConnectionGeometry const& geom){QPointF const& source = geom.source();QPointF const& sink = geom.sink();auto c1c2 = geom.pointsC1C2();// cubic splineQPainterPath cubic(source);cubic.cubicTo(c1c2.first, c1c2.second, sink);return cubic;}QPainterPathQtConnectionPainter::getPainterStroke(ConnectionGeometry const& geom){auto cubic = cubicPath(geom);QPointF const& source = geom.source();QPainterPath result(source);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_DRAWINGstaticvoiddebugDrawing(QPainter * painter,QtConnection const & connection){Q_UNUSED(painter);Q_UNUSED(connection);ConnectionGeometry const& geom =connection.connectionGeometry();{QPointF const& source = geom.source();QPointF const& sink = geom.sink();auto points = geom.pointsC1C2();painter->setPen(Qt::red);painter->setBrush(Qt::red);painter->drawLine(QLineF(source, points.first));painter->drawLine(QLineF(points.first, points.second));painter->drawLine(QLineF(points.second, sink));painter->drawEllipse(points.first, 3, 3);painter->drawEllipse(points.second, 3, 3);painter->setBrush(Qt::NoBrush);painter->drawPath(cubicPath(geom));}{painter->setPen(Qt::yellow);painter->drawRect(geom.boundingRect());}}#endifstaticvoiddrawSketchLine(QPainter * painter,QtConnection const & connection){using Qt::ConnectionState;ConnectionState const& state =connection.connectionState();if (state.requiresPort()){auto const & connectionStyle =Qt::StyleCollection::connectionStyle();QPen p;p.setWidth(connectionStyle.constructionLineWidth());p.setColor(connectionStyle.constructionColor());p.setStyle(Qt::DashLine);painter->setPen(p);painter->setBrush(Qt::NoBrush);using Qt::ConnectionGeometry;ConnectionGeometry const& geom = connection.connectionGeometry();auto cubic = cubicPath(geom);// cubic splinepainter->drawPath(cubic);}}staticvoiddrawHoveredOrSelected(QPainter * painter,QtConnection const & connection){using Qt::ConnectionGeometry;ConnectionGeometry const& geom = connection.connectionGeometry();bool const hovered = geom.hovered();auto const& graphicsObject =connection.getConnectionGraphicsObject();bool const selected = graphicsObject.isSelected();// drawn as a fat backgroundif (hovered || selected){QPen p;auto const &connectionStyle =Qt::StyleCollection::connectionStyle();double const lineWidth = connectionStyle.lineWidth();p.setWidth(2 * lineWidth);p.setColor(selected ?connectionStyle.selectedHaloColor() :connectionStyle.hoveredColor());painter->setPen(p);painter->setBrush(Qt::NoBrush);// cubic splineauto cubic = cubicPath(geom);painter->drawPath(cubic);}}staticvoiddrawNormalLine(QPainter * painter,QtConnection const & connection){using Qt::ConnectionState;ConnectionState const& state =connection.connectionState();if (state.requiresPort())return;// colorsauto const &connectionStyle =Qt::StyleCollection::connectionStyle();QColor normalColorOut = connectionStyle.normalColor();QColor normalColorIn = connectionStyle.normalColor();QColor selectedColor = connectionStyle.selectedColor();bool gradientColor = false;if (connectionStyle.useDataDefinedColors()){using Qt::PortType;auto dataTypeOut = connection.dataType(PortType::Out);auto dataTypeIn = connection.dataType(PortType::In);gradientColor = (dataTypeOut.id != dataTypeIn.id);normalColorOut = connectionStyle.normalColor(dataTypeOut.id);normalColorIn = connectionStyle.normalColor(dataTypeIn.id);selectedColor = normalColorOut.darker(200);}// geometryConnectionGeometry const& geom = connection.connectionGeometry();double const lineWidth = connectionStyle.lineWidth();// draw normal lineQPen p;p.setWidth(lineWidth);auto const& graphicsObject = connection.getConnectionGraphicsObject();bool const selected = graphicsObject.isSelected();auto cubic = cubicPath(geom);if (gradientColor){painter->setBrush(Qt::NoBrush);QColor c = normalColorOut;if (selected)c = c.darker(200);p.setColor(c);painter->setPen(p);unsigned int const segments = 60;for (unsigned int i = 0ul; i < segments; ++i){double ratioPrev = double(i) / segments;double ratio = double(i + 1) / segments;if (i == segments / 2){QColor c = normalColorIn;if (selected)c = c.darker(200);p.setColor(c);painter->setPen(p);}painter->drawLine(cubic.pointAtPercent(ratioPrev),cubic.pointAtPercent(ratio));}{QIcon icon(":convert.png");QPixmap pixmap = icon.pixmap(QSize(22, 22));painter->drawPixmap(cubic.pointAtPercent(0.50) - QPoint(pixmap.width() / 2,pixmap.height() / 2),pixmap);}}else{p.setColor(normalColorOut);if (selected){p.setColor(selectedColor);}painter->setPen(p);painter->setBrush(Qt::NoBrush);painter->drawPath(cubic);}}voidQtConnectionPainter::paint(QPainter* painter,QtConnection const &connection){drawHoveredOrSelected(painter, connection);drawSketchLine(painter, connection);drawNormalLine(painter, connection);#ifdef NODE_DEBUG_DRAWINGdebugDrawing(painter, connection);#endif// draw end pointsConnectionGeometry const& geom = connection.connectionGeometry();QPointF const & source = geom.source();QPointF const & sink = geom.sink();auto const & connectionStyle =Qt::StyleCollection::connectionStyle();double const pointDiameter = connectionStyle.pointDiameter();painter->setPen(connectionStyle.constructionColor());painter->setBrush(connectionStyle.constructionColor());double const pointRadius = pointDiameter / 4.0;painter->drawEllipse(source, pointRadius, pointRadius);painter->drawEllipse(sink, pointRadius, pointRadius);}QtConnectionGraphicsObject::QtConnectionGraphicsObject(QtFlowScene &scene,QtConnection &connection): _scene(scene), _connection(connection){_scene.addItem(this);setFlag(QGraphicsItem::ItemIsMovable, true);setFlag(QGraphicsItem::ItemIsFocusable, true);setFlag(QGraphicsItem::ItemIsSelectable, true);setAcceptHoverEvents(true);// addGraphicsEffect();setZValue(-1.0);}QtConnectionGraphicsObject::~QtConnectionGraphicsObject(){_scene.removeItem(this);}Qt::QtConnection&QtConnectionGraphicsObject::connection(){return _connection;}QRectFQtConnectionGraphicsObject::boundingRect() const{return _connection.connectionGeometry().boundingRect();}QPainterPathQtConnectionGraphicsObject::shape() const{#ifdef DEBUG_DRAWING//QPainterPath path;//path.addRect(boundingRect());//return path;#elseauto const &geom =_connection.connectionGeometry();return QtConnectionPainter::getPainterStroke(geom);#endif}voidQtConnectionGraphicsObject::setGeometryChanged(){prepareGeometryChange();}voidQtConnectionGraphicsObject::move(){for (PortType portType : { PortType::In, PortType::Out }){if (auto node = _connection.getNode(portType)){auto const &nodeGraphics = node->nodeGraphicsObject();auto const &nodeGeom = node->nodeGeometry();QPointF scenePos =nodeGeom.portScenePosition(_connection.getPortIndex(portType),portType,nodeGraphics.sceneTransform());QTransform sceneTransform = this->sceneTransform();QPointF connectionPos = sceneTransform.inverted().map(scenePos);_connection.connectionGeometry().setEndPoint(portType,connectionPos);_connection.getConnectionGraphicsObject().setGeometryChanged();_connection.getConnectionGraphicsObject().update();}}}void QtConnectionGraphicsObject::lock(bool locked){setFlag(QGraphicsItem::ItemIsMovable, !locked);setFlag(QGraphicsItem::ItemIsFocusable, !locked);setFlag(QGraphicsItem::ItemIsSelectable, !locked);}voidQtConnectionGraphicsObject::paint(QPainter* painter,QStyleOptionGraphicsItem const* option,QWidget*){painter->setClipRect(option->exposedRect);QtConnectionPainter::paint(painter,_connection);}voidQtConnectionGraphicsObject::mousePressEvent(QGraphicsSceneMouseEvent* event){QGraphicsItem::mousePressEvent(event);//event->ignore();}voidQtConnectionGraphicsObject::mouseMoveEvent(QGraphicsSceneMouseEvent* event){prepareGeometryChange();auto view = static_cast<QGraphicsView*>(event->widget());auto node = locateNodeAt(event->scenePos(),_scene,view->transform());auto &state = _connection.connectionState();state.interactWithNode(node);if (node){node->reactToPossibleConnection(state.requiredPort(),_connection.dataType(oppositePort(state.requiredPort())),event->scenePos());}//-------------------QPointF offset = event->pos() - event->lastPos();auto requiredPort = _connection.requiredPort();if (requiredPort != PortType::None){_connection.connectionGeometry().moveEndPoint(requiredPort, offset);}//-------------------update();event->accept();}voidQtConnectionGraphicsObject::mouseReleaseEvent(QGraphicsSceneMouseEvent* event){ungrabMouse();event->accept();auto node = locateNodeAt(event->scenePos(), _scene,_scene.views()[0]->transform());QtInteraction interaction(*node, _connection, _scene);if (node && interaction.tryConnect()){node->resetReactionToConnection();}else if (event->button() == Qt::LeftButton){auto nodeOut = _connection.getNode(PortType::Out);auto portOut = _connection.getPortIndex(PortType::Out);auto nodeIn = _connection.getNode(PortType::In);auto portIn = _connection.getPortIndex(PortType::In);if (nodeOut != nullptr && nodeIn == nullptr && portOut != INVALID_PORT){_scene.outPortConextMenu(*nodeOut, portOut, event->screenPos());}}if (_connection.connectionState().requiresPort()){_scene.deleteConnection(_connection);}}voidQtConnectionGraphicsObject::hoverEnterEvent(QGraphicsSceneHoverEvent* event){_connection.connectionGeometry().setHovered(true);update();_scene.connectionHovered(connection(), event->screenPos());event->accept();}voidQtConnectionGraphicsObject::hoverLeaveEvent(QGraphicsSceneHoverEvent* event){_connection.connectionGeometry().setHovered(false);update();_scene.connectionHoverLeft(connection());event->accept();}voidQtConnectionGraphicsObject::addGraphicsEffect(){auto effect = new QGraphicsBlurEffect;effect->setBlurRadius(5);setGraphicsEffect(effect);//auto effect = new QGraphicsDropShadowEffect;//auto effect = new ConnectionBlurEffect(this);//effect->setOffset(4, 4);//effect->setColor(QColor(Qt::gray).darker(800));}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。