PDA

View Full Version : coordinate transformation of graphics item



sajis997
10th February 2012, 11:10
Hello forum,

There are three functions i confused to use, when to use, they are:

1. mapFromItem(....)

2. mapToItem(...)

3. mapFromParent(....)

4. mapFromScene(...)


Please take a look at the attached image. 7397


You can see that a small ellipse is positioned inside the large ellipse.


While defining the bounding rectangle for each of them, it of its center point is defined to be at (0,0) as follows:


Bounding rectangle for the large ellipse


QRectF RootGraphicsItem::boundingRect() const
{

//!get the bounding rectangle of the enclosing text
QRectF rect = m_textGraphicsItem->boundingRect();

// qDebug() << "************** the bounding rectangle of the text graphics item: *************" << endl;
// qDebug() << "Top left: " << rect.topLeft() << endl;
// qDebug() << "Center: " << rect.center() << endl;
// qDebug() << "Bottom right: " << rect.bottomRight() << endl;

//the following condition decides if the boundary needs
//to be changed
if( rect.width() < m_drawRectMinimumWidth )
rect.setWidth(m_drawRectMinimumWidth);

if(rect.height() < m_drawRectMinimumHeight)
rect.setHeight(m_drawRectMinimumHeight);

//set the rectangle width with the text item spacing
rect.setWidth(rect.width() + getTextItemSpacing() );

//the bounding rectangle will have its center at (0,0)
rect.translate(-rect.center());


// qDebug() << "After the adjustments" << endl;
// qDebug() << "Center: " << rect.center() << endl;
// qDebug() << "Top Left: " << rect.topLeft() << endl;
// qDebug() << "Bottom Right: " << rect.bottomRight() << endl;



//and return the rectangle
return rect;
}



and Bounding rectangle for the small ellipse



QRectF H3DHierarchyArrowDockGraphicsItem::boundingRect() const
{
QRectF rect(0,0,sizeNormal.width(),sizeNormal.height());

//center will be (0,0)
rect.translate(-rect.center());

return rect;
}



Now when i layout the smaller one in the function of large one's function definition, which of the mapping function i should be using ?


Right now i am doing it as follows, seems like a hard-coding to me, What you guys think ?




void RootGraphicsItem::layoutChildItems()
{
...................................

...................................
x = 0.0;

//the first bounding rectangle is large ellipse's bounding rectangle
// and the second one is the small ellipses bounding rectangle
y = (boundingRect().height()/2.0) - (m_dockOutGraphicsItem->boundingRect().height()/2.0);


m_dockOutGraphicsItem->setPos(x,-y);

...................................

...................................
}



The arrangement is functional, but might be more accurate if i do the mapping before setting the position. since i am rendering in big ellipses coordinate system now, do i have to call mapToItem(...). I called it as follows:



m_dockOutGraphicsItem->setPos(mapFromItem(m_dockOutGraphicsItem,x,-y));


Bu the effect is the same, so what is the use of this mapping and when to use it.

A more explanation will be very helpful, any reference ?


Regards
Sajjad

Spitfire
14th February 2012, 10:19
It's a complex subject.
If you haven't read The Graphics View Coordinate System (http://developer.qt.nokia.com/doc/qt-4.8/graphicsview.html) I would suggest "Coordinate Mapping" there as a starting point.