使用捕捉功能在网格中绘制线条

时间:2014-12-01 07:03:09

标签: c++ qt

我想在网格中绘制一条线并为其实现捕捉功能。我绘制了网格和线条,但没有实现快照功能。我如何在其中实现该功能。我画了如下网格:

void CadGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)
{
    if (isGridVisible)
    {
       const int gridSize = 50;
        const int realLeft = static_cast<int>(std::floor(rect.left()));
        const int realRight = static_cast<int>(std::ceil(rect.right()));
        const int realTop = static_cast<int>(std::floor(rect.top()));
        const int realBottom = static_cast<int>(std::ceil(rect.bottom()));

        // Draw grid.
        const int firstLeftGridLine = realLeft - (realLeft % gridSize);
        const int firstTopGridLine = realTop - (realTop % gridSize);
        QVarLengthArray<QLine, 100> lines;

        for (qreal x = firstLeftGridLine; x <= realRight; x += gridSize)
            lines.append(QLine(x, realTop, x, realBottom));
        for (qreal y = firstTopGridLine; y <= realBottom; y += gridSize)
            lines.append(QLine(realLeft, y, realRight, y));

        painter->setPen(QPen(QColor(220, 220, 220), 0.0));
        painter->drawLines(lines.data(), lines.size());

        // Draw axes.
        painter->setPen(QPen(Qt::lightGray, 0.0));
        painter->drawLine(0, realTop, 0, realBottom);
        painter->drawLine(realLeft, 0, realRight, 0);
    }
}

My Line课程如下:

#include "line.h"

Line::Line(int i, QPointF p1, QPointF p2)
{
    // assigns id
    id = i;

    // set values of start point and end point of line
    startP = p1;
    endP = p2;
}

int Line::type() const
{
    // Enable the use of qgraphicsitem_cast with line item.
    return Type;
}

QRectF Line::boundingRect() const
{
    qreal extra = 1.0;

    // bounding rectangle for line
    return QRectF(line().p1(), QSizeF(line().p2().x() - line().p1().x(),
                                      line().p2().y() - line().p1().y()))
            .normalized()
            .adjusted(-extra, -extra, extra, extra);
}

void Line::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                  QWidget *widget)
{
    // draws/paints the path of line
    QPen paintpen;
    painter->setRenderHint(QPainter::Antialiasing);
    paintpen.setWidth(1);

    if (isSelected())
    {
        // sets brush for end points
        painter->setBrush(Qt::SolidPattern);
        paintpen.setColor(Qt::red);
        painter->setPen(paintpen);
        painter->drawEllipse(startP, 2, 2);
        painter->drawEllipse(endP, 2, 2);

        // sets pen for line path
        paintpen.setStyle(Qt::DashLine);
        paintpen.setColor(Qt::black);
        painter->setPen(paintpen);
        painter->drawLine(startP, endP);
    }

    else
    {
        painter->setBrush(Qt::SolidPattern);
        paintpen.setColor(Qt::black);
        painter->setPen(paintpen);
        painter->drawEllipse(startP, 2, 2);
        painter->drawEllipse(endP, 2, 2);
        painter->drawLine(startP, endP);
    }
}

snap.cpp

#include "snap.h"
#include <QApplication>

    Snap::Snap(const QRect& rect, QGraphicsItem* parent,
               QGraphicsScene* scene):
    QGraphicsRectItem(QRectF())
    {
        setFlags(QGraphicsItem::ItemIsSelectable |
                QGraphicsItem::ItemIsMovable |
                QGraphicsItem::ItemSendsGeometryChanges);
    }

    void Snap::mousePressEvent(QGraphicsSceneMouseEvent *event){
        offset = pos() - computeTopLeftGridPoint(pos());
        QGraphicsRectItem::mousePressEvent(event);
    }

    QVariant Snap::itemChange(GraphicsItemChange change,
    const QVariant &value)
    {
        if (change == ItemPositionChange && scene()) {
            QPointF newPos = value.toPointF();
            if(QApplication::mouseButtons() == Qt::LeftButton &&
                qobject_cast<CadGraphicsScene*> (scene())){
                    QPointF closestPoint = computeTopLeftGridPoint(newPos);
                    return closestPoint+=offset;
                }
            else
                return newPos;
        }
        else
            return QGraphicsItem::itemChange(change, value);
    }

    QPointF Snap::computeTopLeftGridPoint(const QPointF& pointP){
       CadGraphicsScene* customScene = qobject_cast<CadGraphicsScene*> (scene());
        int gridSize = customScene->getGridSize();
        qreal xV = floor(pointP.x()/gridSize)*gridSize;
        qreal yV = floor(pointP.y()/gridSize)*gridSize;
        return QPointF(xV, yV);
    }

有谁可以帮我解决这个问题?

1 个答案:

答案 0 :(得分:1)

在要隐藏的项目的MouseMoveEvent中:

检查项目和下一个GridLine之间的距离是否低于某个距离(项目将捕捉的距离)。 然后更改位置,使项目移动到下一个GridLine。

在Pseudo:

if( distance(Item.Position, Grid.NextLine) < SnapDistance)
    line.Position = Grid.MextLine.Position

你必须分开X和Y才能使其正常工作。