PDA

View Full Version : QGraphicsItem scale problem



qlands
8th June 2010, 15:08
Hi,

I am writing a graphical application that require scale of graphic items. In the application the user drags and drops polygon from a toolbar (like Dia does). When the polygon lands into the scene it has an standard size. I need to implement the scale of the items. I'm using the setScale function. The problem that I have happens when the user want to scale an already scaled item. For example if I scale an item to 2.0 the result is a item twice its original size, if I do 2.0 again the function uses the original polygon and not the scaled one resulting in the same size.

I tried to work around the problem with the following code:


//Scaled polygon
QPolygonF spoly;

//One polygon in the scene
tnkPolygonSymbol *item;

//Gets one polygon from the scene
item = qgraphicsitem_cast<tnkPolygonSymbol *>(scenes[currentPage]->items()[0]);

//Scale the item to double
item->setScale(2.0);

//Gets the scaled polygon from the scene
spoly = item->mapToScene(item->polygon());

//Restores the polygon to the original size
item->setScale(1.0);

//Replaces the current polygon with the scaled polygon
item->setPolygon(spoly);


However this does not work. The result of the process is shown in this image:
http://www.qlands.com/other_files/scene02.jpg

After the process, the bounding rectangle is double its original size but the resized polygon is away from its bounding rectangle. This may happen because the coordinate values of the spoly are not the same as the item in the scene. I tried to set the coordinates of spoly with moveTo, CeterOn, etc and nothing seems to work.

Any ideas will be appreciated.

Thanks,
Carlos.

tbscope
8th June 2010, 15:18
I think you're looking too far.

setScale(scale() * magnification)

qlands
8th June 2010, 15:39
Hi,

Sorry but I don't understand your reply.

I'm using setScale to set the scale but how can I scale an already scaled object? For example to scale a polygon to twice it size and then to a scale that mirrors it? The result should be a mirror of twice the original size. I can do this by recording the progressive scales given to an item but in the event of saving the scene to a file I would need to save such progression in the scale so that I can recreated it when opening the file again. A better idea would be by swapping the original object by its scaled version: Original size -> Scale -> new original size -> Scale -> new original size -> ........ So I would need just to save the final version of the item.

tbscope
8th June 2010, 15:43
Well, if I understand you correctly, you want this:

Original object, scale factor = 1
First scale, double the size -> scale factor = 2
Second scale, double the size -> scale factor = 4
etc..

Then use:
setScale(scale() * magnification) where magnification = 2 for doubling

qlands
8th June 2010, 15:58
ok. It make sense.

And how can I adjust the bounding rectangle to match the rescaled object? The application behaves like InkScape. When selecting an item a set of drag points will appear around the bounding rectangle of the item so the user can change its scale in an arbitrary form.

qlands
9th June 2010, 17:43
I'm replying to my own post.

The problem is solved with the following code:


//Scaled polygon
QPolygonF spoly;
//Current polygon
QPolygonF cpoly;
//One polygon in the scene
tnkPolygonSymbol *item;
//Gets one polygon from the scene
item = qgraphicsitem_cast<tnkPolygonSymbol *>(scenes[currentPage]->items()[0]);
//Gets the current polygon from the item
cpoly = item->polygon();
//Transformation matrix
QTranform trans;
//Transform the matrix to an scale of double it size;
trans = trans.scale(2.0,2.0);
//Scale the current polygon to the scaled polygon using the tranformation matrix
spoly = trans.map(cpoly)
//Replaces the current polygon with the scaled polygon
item->setPolygon(spoly);


With this the item will have the current scaled polygon and the bounding rectangle will adapt to it.

Thanks for your replies.
Carlos.