如何将旋转/调整大小的线项坐标(点P1)转换为局部坐标系?

时间:2014-03-19 13:53:27

标签: qt qgraphicsview qgraphicsitem

我有一个自定义订单项,它是QGraphicsLineItem的子类。当在项目边缘上发生鼠标移动事件时,我会相应地旋转或调整线条大小。我的问题是在执行旋转或调整大小之前,行项目存在于P1(0,0)和P2(x,y)的局部坐标系中。

当P2用作调整大小和旋转的锚点并且鼠标放在P1上时,P1采用鼠标事件 - > gt; pos()坐标。完成调整大小后,我需要将订单项转换为本地坐标系。我怎么做?我尝试使用setTransformOriginPoint(event-> pos()),但它没有将我的P1转换为原点。

用于旋转/调整大小的代码如下:

void CustomGraphicsLineItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    qDebug()<<"Line mouse move event";
    if( dragIndex != -1 )
    {
        const QPointF anchor = dragIndex == 0 ? this->line().p1() : this->line().p2();
        this->setLine(dragIndex == 0 ? QLineF(anchor, event->pos()) : QLineF(event->pos(),anchor));
    }
    if(dragIndex == 1)
    {
        this->setTransformOriginPoint(event->pos());
    }
    this->update();
}



 void CustomGraphicsLineItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        qDebug()<<"Line mouse press event";
        //  initialPos = mapToScene(event->pos());
        dragIndex = -1;
        const QPointF event_pos = event->pos();
        const qreal l1 = QLineF( event_pos, this->line().p1() ).length();
        const qreal l2 = QLineF( event_pos, this->line().p2() ).length();
        //threshold indicates the area of influence of the mouse click event, which is set to  5.0 pixels
        const qreal threshold = 15.0;

        if(l1 < threshold || l2 < threshold)
        {
            if( l1 < l2 && l1 < threshold )
            {
                dragIndex = 1;
            }
            else if ( l2 < l1 && l2 < threshold )
            {
                dragIndex = 0;
            }
            else
            {
                dragIndex = -1;
            }
            event->setAccepted( dragIndex != -1 );
        }

        //if we click anywhere other than the end points, then consider it to be a drag
        if(l1 > threshold && l2 > threshold)
        {
            QMimeData * mimeData = new QMimeData;
            CustomGraphicsLineItem * item = this;
            QByteArray byteArray(reinterpret_cast<char*>(&item),sizeof(CustomGraphicsLineItem*));
            mimeData->setData("Item",byteArray);

            // start the event
            QDrag * drag = new QDrag(event->widget());
            drag->setMimeData(mimeData);
            drag->exec();
            dragStart = event->pos();
            event->accept();
        } 

    }

void CustomGraphicsLineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    dragIndex = -1;
    QGraphicsLineItem::mouseReleaseEvent(event);
}

void CustomGraphicsLineItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{   
 // qDebug() << "CustomGraphicsLineItem::paint called ";
    QPen pen=QPen();
    pen.setColor(Qt::red);
    pen.setWidth(5);
    painter->setPen(pen);
    painter->setBrush(Qt::red);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->drawLine(line());

}

0 个答案:

没有答案