0

I have a custom line item which is subclassed from QGraphicsLineItem. When mouse move event occurs on the line item edge, I rotate or resize the line accordingly. My problem is that before perfroming rotation or resizing, the line item exists in local coordinate system with P1(0,0) and P2(x,y).

When P2 is used as anchor for resizing and rotating and mouse is placed on P1, the P1 takes the mouse event->pos() cordinates. Once the resize is done, I need to transform the line item to a local coordinate system. How do I do that? I tried using setTransformOriginPoint(event->pos()) but it doesnt translate my P1 to origin.

The code used for rotation/resize is as follows:

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());

}

jeevan_reddy
  • 43
  • 1
  • 10
  • what's dragIndex and where does it changes its value? – mhcuervo Mar 20 '14 at 05:33
  • @mhcuervo - dragIndex is a flag to identify the particular end point on which mouse event occured(select), Based on drag Index, we decide the anchor point on which line rotates or resizes. – jeevan_reddy Mar 20 '14 at 07:31
  • @mhcuervo: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 ); } – jeevan_reddy Mar 20 '14 at 07:35
  • Can you edit the question and include the whole code of `CustomGraphicsLineItem` class? – mhcuervo Mar 20 '14 at 13:32

0 Answers0