Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, December 2006
svgRenderer / flipping / mirror


Message 1 in thread

Hello,

I've a QGraphicsItem that is using QSvgRenderer to render an
SVG into it's bounding rectangle. This works like a charm -
but as soon as bounding rect has a negaive width, the SVG
renderer does something strange.
It does draw the image mirrored, but it draws it starting
at the top left corner as if the width is positive.
Basically, it draws not inside the rectangle submitted to
render().

My work-around right now is that I always normalize the
rect that I pass to the render. While this solves the
out-of-bounds drawing, I can't  flip / mirror the image
any longer.

Is there a way I can tell the renderer (or the painter)
that it should mirror / flip the graphic?

Thanks a lot,

Lenny


Message 2 in thread

Lennart Steinke wrote:
> Hello,
> 
> I've a QGraphicsItem that is using QSvgRenderer to render an
> SVG into it's bounding rectangle. This works like a charm -
> but as soon as bounding rect has a negaive width, the SVG
> renderer does something strange.
> It does draw the image mirrored, but it draws it starting
> at the top left corner as if the width is positive.
> Basically, it draws not inside the rectangle submitted to
> render().

To flip the y-axis of drawing the most common approach is to specify a 
scale of -1 for the y-axis. Depending on how you want the flip, you may 
also want to translate the object by its height. I've attached a small 
example that uses QGraphicsSvgItem and the function scale(1, -1) to 
illustrate this. If you run it out of the qtjambi directory it should 
pick up the right svg for you.

Hope it helps

best regards,
Gunnar
package com.trolltech.tests;
package com.trolltech.tests;

import com.trolltech.qt.gui.*;
import com.trolltech.qt.svg.*;

public class GraphicsViewSvgItem {

    public static void main(String args[]) {
        QApplication.initialize(args);

        // Set up the scene..
        QGraphicsScene scene = new QGraphicsScene();

        QGraphicsSvgItem item = new QGraphicsSvgItem("classpath:com/trolltech/images/svg-cards.svg");
        item.setElementId("black_joker");
        item.scale(1, -1);

        scene.addItem(item);

        QGraphicsView view = new QGraphicsView();
        view.setScene(scene);
        view.show();

        QApplication.exec();
    }
}

Message 3 in thread

Lennart Steinke wrote:
> Hello,
> thanks for the fast reply - but I'm not using a QGraphicsSvgItem -
> I'm using a QSvgRenderer. And if I scale the painter by -1 in any
> direction, I don't see the image anymore.
 >
> Do you have any tips what might go wrong?
 >
> public void paint(QPainter painter, QStyleOptionGraphicsItem arg1, 
> QWidget arg2) {
>     painter.save();
>     if (image != null) {
>                 // draw image
>         painter.scale(isMirrored() ? -1 : 1, isFlipped() ? -1 : 1);
>         painter.drawImage(rectangle, image);
>     } else if (svgRenderer != null) {
>                 // draw svg
>         painter.scale(isMirrored() ? -1 : 1, isFlipped() ? -1 : 
> 1);           
>         if (rectangle.width() < 0 || rectangle.height() <0) {
>             QRectF rect = rectangle.normalized();
>             svgRenderer.render(painter, rect);               
>         } else {
>             svgRenderer.render(painter, rectangle);
>         }           
>     }
>     painter.restore();
> }

When you flip the coordinate system of the painter you may need to 
translate the coordinate system, depending on how your bounding 
rectangle looks.

Say for instance that your bounding rectangle is in 0x0 with dimenstions 
of 100x100 you would do the image drawing like this:

painter.translate(0, 100);
painter.scale(1, -1);
painter.drawImage(0, 0, image);

These links:
http://doc.trolltech.com/qtjambi-1.0/com/trolltech/qt/gui/QPainter.html#coordinate-transformations
http://doc.trolltech.com/qtjambi-1.0/com/trolltech/qt/coordsys.html

Should tell you what you need to know.

---

I don't understand why you don't just flip the item though, using 
scale(1, -1) and possibly add the translate as suggested above ;-)

best regards,
Gunnar