绘制弧并覆盖boundingRect(),shape()

时间:2018-05-09 17:41:52

标签: c++ qt5 qgraphicsitem qpainter qpolygon

我有一个class Edge : public QGraphicsItem,它实现了从一个节点到另一个节点的绘制箭头(下面的屏幕) 现在我需要添加自己绘制箭头的能力(弧线) 我无法绘制圆弧,覆盖boundingRect()shape() 下面的代码我绘制箭头或弧形。这里的完整项目 - > github

Edge::Edge(Node *sourceNode, Node *destNode)
    : id(_idStatic++), arrowSize(15)
{
    setFlag(QGraphicsItem::ItemIsSelectable);
    source = sourceNode;
    dest = destNode;
    source->addEdge(this);
    if(source != dest)
        dest->addEdge(this);
    adjust();
}

QPolygonF Edge::nPolygonMath() const {
    QPolygonF nPolygon;
    if (source != dest) {
        QLineF line = QLineF(sourcePoint.x(), sourcePoint.y(), destPoint.x(), destPoint.y());
        qreal radAngle = line.angle() * M_PI / 180;
        qreal selectionOffset = 3;
        qreal dx = selectionOffset * sin(radAngle);
        qreal dy = selectionOffset * cos(radAngle);
        QPointF offset1 = QPointF(dx, dy);
        QPointF offset2 = QPointF(-dx, -dy);
        nPolygon << line.p1() + offset1
                 << line.p1() + offset2
                 << line.p2() + offset2
                 << line.p2() + offset1;
    } else {
        nPolygon << mapFromItem(source, -Node::Radius, -Node::Radius)
                 << mapFromItem(source, Node::Radius, -Node::Radius)
                 << mapFromItem(source, Node::Radius, Node::Radius)
                 << mapFromItem(source, -Node::Radius, Node::Radius);
    }
    return nPolygon;
}

QRectF Edge::boundingRect() const
{
    if (!source || !dest)
        return QRectF();

    return nPolygonMath().boundingRect();

}

QPainterPath Edge::shape() const{
    QPainterPath ret;
    ret.addPolygon(nPolygonMath());
    return ret;
}

void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    if (!source || !dest)
        return;

    painter->setPen(QPen((option->state & QStyle::State_Selected ? Qt::cyan: Qt::black), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    if (source != dest) {
        QLineF line(sourcePoint, destPoint);
        if (qFuzzyCompare(line.length(), qreal(0.)))
            return;

        // Draw the line itself
        painter->drawLine(line);

        // Draw the arrows
        double angle = std::atan2(-line.dy(), line.dx());

        QPointF destArrowP1 = destPoint + QPointF(sin(angle - M_PI / 1.8) * qMin(arrowSize, line.length()),
                                                  cos(angle - M_PI / 1.8) * qMin(arrowSize, line.length()));
        QPointF destArrowP2 = destPoint + QPointF(sin(angle - M_PI + M_PI / 1.8) * qMin(arrowSize, line.length()),
                                                  cos(angle - M_PI + M_PI / 1.8) * qMin(arrowSize, line.length()));

        painter->setBrush((option->state & QStyle::State_Selected ? Qt::cyan: Qt::black));
        painter->drawPolygon(QPolygonF() << line.p2() << destArrowP1 << destArrowP2);
    } else {
        painter->drawArc(mapFromItem(source, Node::Radius, 0).x(),
                         mapFromItem(source, Node::Radius, 0).y(),
                         2 * Node::Radius, 2 * Node::Radius, 16 * -90, 16 * 180);

    }
}

Screen

1 个答案:

答案 0 :(得分:1)

boundingRect()所需的矩形必须具有圆圈中心的底部,我还删除了nPolygonMath,而我使用了形状来返回QPainterPath,并且这用于boundingRect()

<强> edge.h

#ifndef EDGE_H
#define EDGE_H

#include <QGraphicsItem>

class Node;

class Edge : public QGraphicsItem
{
public:
    Edge(Node *sourceNode, Node *destNode);
    virtual ~Edge();
    const uint id;
    Node *sourceNode() const;
    Node *destNode() const;

    void adjust();

    enum { Type = UserType + 2 };
    int type() const override { return Type; }

protected:
    QRectF boundingRect() const override;
    QPainterPath shape() const override;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
private:
    Node *source, *dest;
    QPointF sourcePoint;
    QPointF destPoint;
    qreal arrowSize;
    static uint _idStatic;
};

#endif // EDGE_H

<强> edge.cpp

#include "edge.h"
#include "node.h"

#include <qmath.h>
#include <QPainter>
#include <QStyleOption>


uint Edge::_idStatic = 0;

Edge::Edge(Node *sourceNode, Node *destNode)
    : id(_idStatic++), arrowSize(15)
{
    setFlag(QGraphicsItem::ItemIsSelectable);
    source = sourceNode;
    dest = destNode;
    source->addEdge(this);
    if(source != dest)
        dest->addEdge(this);
    adjust();
}

Edge::~Edge()
{
    source->removeEdge(this);
    if (source != dest)
        dest->removeEdge(this);
}

Node *Edge::sourceNode() const
{
    return source;
}

Node *Edge::destNode() const
{
    return dest;
}

void Edge::adjust()
{
    if (!source || !dest)
        return;

    if(source != dest) {
        QLineF line(mapFromItem(source, 0, 0), mapFromItem(dest, 0, 0));
        qreal length = line.length();

        prepareGeometryChange();
        if (length > qreal(2 * Node::Radius)) {
            QPointF edgeOffset((line.dx() * Node::Radius) / length, (line.dy() * Node::Radius) / length);
            sourcePoint = line.p1() + edgeOffset;
            destPoint = line.p2() - edgeOffset;
        } else {
            sourcePoint = destPoint = line.p1();
        }
    } else {
        sourcePoint = mapFromItem(source, 0, Node::Radius);
        destPoint = mapFromItem(source, Node::Radius, 0);
        prepareGeometryChange();
    }
}

QPainterPath Edge::shape() const {
    QPainterPath path;
    if (source != dest) {
        QLineF line = QLineF(sourcePoint.x(), sourcePoint.y(), destPoint.x(), destPoint.y());
        qreal radAngle = line.angle() * M_PI / 180;
        qreal selectionOffset = 3;
        qreal dx = selectionOffset * sin(radAngle);
        qreal dy = selectionOffset * cos(radAngle);
        QPointF offset1 = QPointF(dx, dy);
        QPointF offset2 = QPointF(-dx, -dy);
        path.moveTo(line.p1() + offset1);
        path.lineTo(line.p1() + offset2);
        path.lineTo( line.p2() + offset2);
        path.lineTo( line.p2() + offset1);
    } else {
        QRectF r= mapRectFromItem(source, source->boundingRect());
        r.moveCenter(r.topRight());
        path.addRect(r);
    }
    return path;
}

QRectF Edge::boundingRect() const
{
    if (!source || !dest)
        return QRectF();

    return shape().boundingRect();

}

void Edge::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    if (!source || !dest)
        return;

    double angle;
    QPointF peak, destArrowP1, destArrowP2;
    painter->setPen(QPen((option->state & QStyle::State_Selected ? Qt::cyan: Qt::black), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
    if (source != dest) {
        QLineF line(sourcePoint, destPoint);
        if (qFuzzyCompare(line.length(), qreal(0.)))
            return;

        // Draw the line itself
        painter->drawLine(line);

        // Draw the arrows
        angle = std::atan2(-line.dy(), line.dx());
        peak = line.p2();
        destArrowP1 = destPoint + QPointF(sin(angle - M_PI / 1.8) * qMin(arrowSize, line.length()),
                                                          cos(angle - M_PI / 1.8) * qMin(arrowSize, line.length()));
        destArrowP2 = destPoint + QPointF(sin(angle - M_PI + M_PI / 1.8) * qMin(arrowSize, line.length()),
                                                          cos(angle - M_PI + M_PI / 1.8) * qMin(arrowSize, line.length()));

    } else {
        painter->drawArc(boundingRect().toRect(),  16 * -90, 16 * 270);
        angle = 1.06*M_PI;
        destArrowP1 = destPoint + QPointF(sin(angle - M_PI / 1.8) * arrowSize,
                                                  cos(angle - M_PI / 1.8) * arrowSize);
        destArrowP2 = destPoint + QPointF(sin(angle - M_PI + M_PI / 1.8)* arrowSize,
                                                  cos(angle - M_PI + M_PI / 1.8) * arrowSize);
        painter->setBrush((option->state & QStyle::State_Selected ? Qt::cyan: Qt::black));
        peak = QPointF(boundingRect().center().x(), boundingRect().bottom());
    }

    painter->setBrush((option->state & QStyle::State_Selected ? Qt::cyan: Qt::black));
    painter->drawPolygon(QPolygonF() << peak << destArrowP1 << destArrowP2);

}

enter image description here

完整示例可在以下link中找到。