PDA

View Full Version : howto flip and rotate and keep track of coordinates?



barrygp
22nd September 2010, 03:10
I am using QTransform for svg widgets and need to flip and rotate. Any advice on how to fixup the coordinates so the image can be seen. I am having trouble finding where the coordinates are after the transforms. thanks in advance. Code follows.

void DcsWidget::paintEvent(QPaintEvent * event)
{
static bool inEvent = false;

if (inEvent) return; // setElementId() calls update() which triggers paintEvent(), so don't recurse.

inEvent = true;

QString id;
deriveId(id);
setElementId(id);

// Scale the SVG drawing as large as possible, while maintaining
// aspect ratio. Center it in the widget.

QPainter painter(this);

// current size of widget's drawing area
qreal myW = 1.0 * size().width();
qreal myH = 1.0 * size().height();
// qDebug() << "Width: " << myW << " Height: " << myH;

// SVG drawing's bounding rect, based on element ID
QRectF idBB = renderer()->boundsOnElement(p_elementID);

// ratios of SVG width and height to available width and height
qreal wRatio = myW / idBB.width();
qreal hRatio = myH / idBB.height();

// find the minimum of the two ratios for fit
qreal scaleF = qMin(wRatio, hRatio);

// scale up the width/height up the target bounding rect
qreal boundW = idBB.width() * scaleF;
qreal boundH = idBB.height() * scaleF;

// find the X/Y of the target bounding rect to center drawing in
// both dimensions
qreal boundX = (myW - boundW) / 2.0;
qreal boundY = (myH - boundH) / 2.0;

QRectF *drawBB = new QRectF();
//the property that holds each of the values in the check boxes is converted to a local bool
bool fh = property("flipHoriz").toBool();
bool fv = property("flipVert").toBool();
bool ok;
uint rot = property("rotation").toString().toUInt(&ok, 10);

//if any of the boxes are checked do this (rotate 0 degrees should at least be checked by default)
QTransform t;
if(fh){
t.scale(-1.0,1.0);
t.translate(-boundW,1.0);
}
if(fv){
t.scale(1.0,-1.0);
t.translate(1.0,-boundH);
// boundY = -boundY;
}
if(rot == 0){ //rotate 0 draws object in normal bounds
//drawBB->setRect(boundX, boundY, boundW, boundH);
}
//note: transformations occur with the origin being the top left corner of our viewing area
else if(rot == 90){ //rotate 90 draws box above viewing area and it is rotated in to viewing area
if(!fv){
t.rotate(90);
// boundY = -boundH-boundY;
}else{
t.rotate(270);
t.translate(-boundH,1.0);
}
}
else if(rot == 180){ //rotate 180 draws box above and to left of viewing area and it is rotated in to viewing area
t.rotate(180);
// boundX = -boundW-boundX;
// boundY = -boundH-boundY;
}
else if(rot == 270){ //rotate 270 draws box to left of viewing area and it is rotated in to viewing area
if(!fv){
// boundX = -boundW-boundX;
t.rotate(270);
}else{
t.rotate(90);
t.translate(boundH-boundW, -boundH+boundY);

}
}
painter.setTransform(t);
drawBB->setRect(boundX, boundY, boundW, boundH);

renderer()->render(&painter, p_elementID, *drawBB);


inEvent = false;
}