]> git.sesse.net Git - kdenlive/commitdiff
make the titler respect rectangles transformations when resizing or aligning:
authorAlberto Villa <avilla@FreeBSD.org>
Wed, 17 Jun 2009 18:52:27 +0000 (18:52 +0000)
committerAlberto Villa <avilla@FreeBSD.org>
Wed, 17 Jun 2009 18:52:27 +0000 (18:52 +0000)
http://www.kdenlive.org/mantis/view.php?id=919

svn path=/trunk/kdenlive/; revision=3559

src/graphicsscenerectmove.cpp
src/titlewidget.cpp

index 4b99f332a616834d58a9e856884680032234f834..c5fb33390674c64817cb55ee00bd621c53558a11 100644 (file)
@@ -193,28 +193,47 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
                 setCursor(Qt::ClosedHandCursor);
             } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) {
                 QRectF r;
-                if (m_selectedItem->type() == 3) {
+                if (m_selectedItem->type() == 3)
                     r = ((QGraphicsRectItem*)m_selectedItem)->rect();
-                } else r = m_selectedItem->boundingRect();
-
-                r.translate(item->scenePos());
-                if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else
+                    r = m_selectedItem->boundingRect();
+                /*
+                 * The vertices of the rectangle (check for matrix
+                 * transformation); i hope there is a shorter way to do this
+                 */
+                QPointF itemOrigin = item->scenePos();
+                QTransform transform = item->transform();
+                QPointF topLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
+                QPointF bottomLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
+                QPointF topRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
+                QPointF bottomRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
+                // The borders (using the transformed coordinates)
+                QGraphicsLineItem borderTop(topLeft.x(), topLeft.y(), topRight.x(), topRight.y());
+                QGraphicsLineItem borderRight(topRight.x(), topRight.y(), bottomRight.x(), bottomRight.y());
+                QGraphicsLineItem borderBottom(bottomRight.x(), bottomRight.y(), bottomLeft.x(), bottomLeft.y());
+                QGraphicsLineItem borderLeft(bottomLeft.x(), bottomLeft.y(), topLeft.x(), topLeft.y());
+                // The area interested by the mouse pointer
+                QPainterPath mouseArea;
+                mouseArea.addRect(e->scenePos().toPoint().x() - 3 / m_zoom, e->scenePos().toPoint().y() - 3 / m_zoom, 6 / m_zoom, 6 / m_zoom);
+                // Check for collisions between the mouse and the borders
+                if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
                     m_resizeMode = TopLeft;
-                } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else if (borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
                     m_resizeMode = BottomLeft;
-                } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else if (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
                     m_resizeMode = TopRight;
-                } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else if (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
                     m_resizeMode = BottomRight;
-                } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
+                else if (borderLeft.collidesWithPath(mouseArea))
                     m_resizeMode = Left;
-                } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
+                else if (borderRight.collidesWithPath(mouseArea))
                     m_resizeMode = Right;
-                } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
+                else if (borderTop.collidesWithPath(mouseArea))
                     m_resizeMode = Up;
-                } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
+                else if (borderBottom.collidesWithPath(mouseArea))
                     m_resizeMode = Down;
-                } else setCursor(Qt::ClosedHandCursor);
+                else
+                    setCursor(Qt::ClosedHandCursor);
             }
         }
         QGraphicsScene::mousePressEvent(e);
@@ -258,63 +277,99 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
     if (m_selectedItem && e->buttons() & Qt::LeftButton) {
         if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) {
             QRectF newrect;
-            if (m_selectedItem->type() == 3) {
+            if (m_selectedItem->type() == 3)
                 newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
-            } else newrect = m_selectedItem->boundingRect();
-
+            else
+                newrect = m_selectedItem->boundingRect();
             QPointF newpoint = e->scenePos();
-            //newpoint -= m_selectedItem->scenePos();
+            /*
+             * The vertices of the rectangle (check for matrix
+             * transformation); i hope there is a shorter way to do this
+             */
+            QPointF itemOrigin = m_selectedItem->scenePos();
+            QTransform transform = m_selectedItem->transform();
+            QPointF topLeft(transform.m11() * newrect.toRect().left() + transform.m21() * newrect.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().top() + transform.m12() * newrect.toRect().left() + transform.m32() + itemOrigin.y());
+            QPointF bottomLeft(transform.m11() * newrect.toRect().left() + transform.m21() * newrect.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().bottom() + transform.m12() * newrect.toRect().left() + transform.m32() + itemOrigin.y());
+            QPointF topRight(transform.m11() * newrect.toRect().right() + transform.m21() * newrect.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().top() + transform.m12() * newrect.toRect().right() + transform.m32() + itemOrigin.y());
+            QPointF bottomRight(transform.m11() * newrect.toRect().right() + transform.m21() * newrect.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().bottom() + transform.m12() * newrect.toRect().right() + transform.m32() + itemOrigin.y());
+            // Convert the mouse coordinates applying inverted transformation
+            QPointF newPointRelative = newpoint - itemOrigin;
+            QPointF resizePoint(transform.inverted().m11() * newPointRelative.x() + transform.inverted().m21() * newPointRelative.y() + transform.inverted().m31(), transform.inverted().m22() * newPointRelative.y() + transform.inverted().m12() * newPointRelative.x() + transform.inverted().m32());
+            /*
+             * Will check if the mouse is on the right of the limit lines with a
+             * determinant (it must be less than zero because the Y axis is
+             * inverted)
+             */
+            int determinant;
             switch (m_resizeMode) {
-            case TopLeft:
-                if (newpoint.x() < newrect.right() + m_selectedItem->pos().x() && newpoint.y() < newrect.bottom() + m_selectedItem->pos().y()) {
-                    newrect.setBottomRight(newrect.bottomRight() + m_selectedItem->pos() - newpoint);
-                    m_selectedItem->setPos(newpoint);
-                }
-                break;
-            case BottomLeft:
-                if (newpoint.x() < newrect.right() + m_selectedItem->pos().x() && newpoint.y() > m_selectedItem->pos().y()) {
-                    newrect.setBottomRight(QPointF(newrect.bottomRight().x() + m_selectedItem->pos().x() - newpoint.x(), newpoint.y() - m_selectedItem->pos().y()));
-                    m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
-                }
-                break;
-            case TopRight:
-                if (newpoint.x() > m_selectedItem->pos().x() && newpoint.y() < newrect.bottom() + m_selectedItem->pos().y()) {
-                    newrect.setBottomRight(QPointF(newpoint.x() - m_selectedItem->pos().x(), newrect.bottom() + m_selectedItem->pos().y() - newpoint.y()));
-                    m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
-                }
-                break;
-            case BottomRight:
-                if (newpoint.x() > m_selectedItem->pos().x() && newpoint.y() > m_selectedItem->pos().y()) {
-                    newrect.setBottomRight(newpoint - m_selectedItem->pos());
-                }
-                break;
-            case Left:
-                if (newpoint.x() < newrect.right() + m_selectedItem->pos().x()) {
-                    newrect.setRight(m_selectedItem->pos().x() + newrect.width() - newpoint.x());
-                    m_selectedItem->setPos(QPointF(newpoint.x(), m_selectedItem->pos().y()));
-                }
-                break;
-            case Right:
-                if (newpoint.x() > m_selectedItem->pos().x()) {
-                    newrect.setRight(newpoint.x() - m_selectedItem->pos().x());
-                }
-                break;
-            case Up:
-                if (newpoint.y() < newrect.bottom() + m_selectedItem->pos().y()) {
-                    newrect.setBottom(m_selectedItem->pos().y() + newrect.bottom() - newpoint.y());
-                    m_selectedItem->setPos(QPointF(m_selectedItem->pos().x(), newpoint.y()));
-                }
-                break;
-            case Down:
-                if (newpoint.y() > m_selectedItem->pos().y()) {
-                    newrect.setBottom(newpoint.y() - m_selectedItem->pos().y());
-                }
-                break;
-            default:
-                QPointF diff = e->scenePos() - m_sceneClickPoint;
-                m_sceneClickPoint = e->scenePos();
-                m_selectedItem->moveBy(diff.x(), diff.y());
-                break;
+                case TopLeft:
+                    determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
+                    if (determinant < 0) {
+                        determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
+                        if (determinant < 0) {
+                            // resizePoint is not working for some reason
+                            newrect.setBottomRight(QPointF(newrect.width() - (transform.inverted().m11() * resizePoint.x() + transform.inverted().m21() * resizePoint.y() + transform.inverted().m31()), newrect.bottom() - (transform.inverted().m22() * resizePoint.y() + transform.inverted().m12() * resizePoint.x() + transform.inverted().m32())));
+                            m_selectedItem->setPos(resizePoint + itemOrigin);
+                        }
+                    }
+                    break;
+                case BottomLeft:
+                    determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
+                    if (determinant < 0) {
+                        determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
+                        if (determinant < 0) {
+                            newrect.setBottomRight(QPointF(newrect.width() - resizePoint.x(), resizePoint.y()));
+                            m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m21() * (newrect.bottom() - resizePoint.y()) + transform.m31() + itemOrigin.x(), transform.m22() * (newrect.bottom() - resizePoint.y()) + transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
+                        }
+                    }
+                    break;
+                case TopRight:
+                    determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
+                    if (determinant < 0) {
+                        determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
+                        if (determinant < 0) {
+                            newrect.setBottomRight(QPointF(resizePoint.x(), newrect.bottom() - resizePoint.y()));
+                            m_selectedItem->setPos(QPointF(transform.m11() * (newrect.width() - resizePoint.x()) + transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m12() * (newrect.width() - resizePoint.x()) + transform.m32() + itemOrigin.y()));
+                        }
+                    }
+                    break;
+                case BottomRight:
+                    determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
+                    if (determinant < 0) {
+                        determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
+                        if (determinant < 0)
+                            newrect.setBottomRight(resizePoint);
+                    }
+                    break;
+                case Left:
+                    determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
+                    if (determinant < 0) {
+                        newrect.setRight(newrect.width() - resizePoint.x());
+                        m_selectedItem->setPos(QPointF(transform.m11() * resizePoint.x() + transform.m31() + itemOrigin.x(), transform.m12() * resizePoint.x() + transform.m32() + itemOrigin.y()));
+                    }
+                    break;
+                case Right:
+                    determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
+                    if (determinant < 0)
+                        newrect.setRight(resizePoint.x());
+                    break;
+                case Up:
+                    determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
+                    if (determinant < 0) {
+                        newrect.setBottom(newrect.bottom() - resizePoint.y());
+                        m_selectedItem->setPos(QPointF(transform.m21() * resizePoint.y() + transform.m31() + itemOrigin.x(), transform.m22() * resizePoint.y() + transform.m32() + itemOrigin.y()));
+                    }
+                    break;
+                case Down:
+                    determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
+                    if (determinant < 0)
+                        newrect.setBottom(resizePoint.y());
+                    break;
+                default:
+                    QPointF diff = e->scenePos() - m_sceneClickPoint;
+                    m_sceneClickPoint = e->scenePos();
+                    m_selectedItem->moveBy(diff.x(), diff.y());
+                    break;
             }
             if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) {
                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
@@ -355,25 +410,44 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
                 break;
             } else if (g->type() == 3 && g->zValue() > -1000) {
                 QRectF r = ((const QGraphicsRectItem*)g)->rect();
-                r.translate(g->scenePos());
                 itemFound = true;
-                if ((r.toRect().topLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                /*
+                 * The vertices of the rectangle (check for matrix
+                 * transformation); i hope there is a shorter way to do this
+                 */
+                QPointF itemOrigin = g->scenePos();
+                QTransform transform = g->transform();
+                QPointF topLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
+                QPointF bottomLeft(transform.m11() * r.toRect().left() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().left() + transform.m32() + itemOrigin.y());
+                QPointF topRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().top() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
+                QPointF bottomRight(transform.m11() * r.toRect().right() + transform.m21() * r.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * r.toRect().bottom() + transform.m12() * r.toRect().right() + transform.m32() + itemOrigin.y());
+                // The borders (using the transformed coordinates)
+                QGraphicsLineItem borderTop(topLeft.x(), topLeft.y(), topRight.x(), topRight.y());
+                QGraphicsLineItem borderRight(topRight.x(), topRight.y(), bottomRight.x(), bottomRight.y());
+                QGraphicsLineItem borderBottom(bottomRight.x(), bottomRight.y(), bottomLeft.x(), bottomLeft.y());
+                QGraphicsLineItem borderLeft(bottomLeft.x(), bottomLeft.y(), topLeft.x(), topLeft.y());
+                // The area interested by the mouse pointer
+                QPainterPath mouseArea;
+                mouseArea.addRect(e->scenePos().toPoint().x() - 3 / m_zoom, e->scenePos().toPoint().y() - 3 / m_zoom, 6 / m_zoom, 6 / m_zoom);
+                // Check for collisions between the mouse and the borders
+                if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
                     setCursor(QCursor(Qt::SizeFDiagCursor));
-                } else if ((r.toRect().bottomLeft() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else if (borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
                     setCursor(QCursor(Qt::SizeBDiagCursor));
-                } else if ((r.toRect().topRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else if (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
                     setCursor(QCursor(Qt::SizeBDiagCursor));
-                } else if ((r.toRect().bottomRight() - e->scenePos().toPoint()).manhattanLength() < 6 / m_zoom) {
+                else if (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
                     setCursor(QCursor(Qt::SizeFDiagCursor));
-                } else if (qAbs(r.toRect().left() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
+                else if (borderLeft.collidesWithPath(mouseArea))
                     setCursor(Qt::SizeHorCursor);
-                } else if (qAbs(r.toRect().right() - e->scenePos().toPoint().x()) < 3 / m_zoom) {
+                else if (borderRight.collidesWithPath(mouseArea))
                     setCursor(Qt::SizeHorCursor);
-                } else if (qAbs(r.toRect().top() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
+                else if (borderTop.collidesWithPath(mouseArea))
                     setCursor(Qt::SizeVerCursor);
-                } else if (qAbs(r.toRect().bottom() - e->scenePos().toPoint().y()) < 3 / m_zoom) {
+                else if (borderBottom.collidesWithPath(mouseArea))
                     setCursor(Qt::SizeVerCursor);
-                } else setCursor(Qt::OpenHandCursor);
+                else
+                    setCursor(Qt::OpenHandCursor);
                 break;
             }
             if (!itemFound) setCursor(Qt::ArrowCursor);
index 7f8773f4fd4ccbdd9eda922df6dbb53e9002fa91..ef68ffeb21ead5888c8b2f20fcc284b3502e65a7 100644 (file)
@@ -59,7 +59,7 @@ TitleWidget::TitleWidget(KUrl url, QString projectPath, Render *render, QWidget
     m_frameHeight = render->renderHeight();
     connect(kcolorbutton, SIGNAL(clicked()), this, SLOT(slotChangeBackground())) ;
     connect(horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(slotChangeBackground())) ;
-       
+
 
     connect(fontColorButton, SIGNAL(clicked()), this, SLOT(slotUpdateText())) ;
     connect(font_family, SIGNAL(currentFontChanged(const QFont &)), this, SLOT(slotUpdateText())) ;
@@ -551,7 +551,7 @@ void TitleWidget::selectionChanged()
                        rect_properties->setEnabled(false);
             value_w->setEnabled(false);
             value_h->setEnabled(false);
-                       
+
         } else if ((l.at(0))->type() == RECTITEM) {
             rect_properties->setHidden(false);
             text_properties->setHidden(true);
@@ -570,7 +570,7 @@ void TitleWidget::selectionChanged()
             rectBColor->setColor(bcol);
             settingUp = false;
             rectLineWidth->setValue(rec->pen().width());
-                       
+
                        updateAxisButtons(l.at(0));
                        updateCoordinates(rec);
             updateDimension(rec);
@@ -581,18 +581,18 @@ void TitleWidget::selectionChanged()
                        rect_properties->setEnabled(true);
             value_w->setEnabled(true);
             value_h->setEnabled(true);
-                       
+
         } else if (l.at(0)->type() == IMAGEITEM) {
                        updateCoordinates(l.at(0));
                        updateDimension(l.at(0));
-                       
+
                        frame_properties->setEnabled(true);
                        text_properties->setEnabled(false);
                        rect_properties->setEnabled(false);
                        value_x->setEnabled(true);
                        value_w->setEnabled(false);
                        value_h->setEnabled(false);
-                       
+
                } else {
             //toolBox->setCurrentIndex(0);
             frame_properties->setEnabled(false);
@@ -633,25 +633,25 @@ void TitleWidget::slotAdjustSelectedItem()
 }
 
 /** \brief Updates width/height int the text fields, regarding transformation matrix */
-void TitleWidget::updateDimension(QGraphicsItem *i) 
+void TitleWidget::updateDimension(QGraphicsItem *i)
 {
        bool blockW = !value_w->signalsBlocked();
        bool blockH = !value_h->signalsBlocked();
-       
+
        if (blockW) value_w->blockSignals(true);
        if (blockH) value_h->blockSignals(true);
-       
-       
+
+
        if (i->type() == IMAGEITEM) {
                // Get multipliers for rotation/scaling
-               
+
                /*Transform t = m_transformations.value(i);
                QRectF r = i->boundingRect();
                int width = (int) ( abs(r.width()*t.scalex * cos(t.rotate/180.0*M_PI))
                                        + abs(r.height()*t.scaley * sin(t.rotate/180.0*M_PI)) );
                int height = (int) ( abs(r.height()*t.scaley * cos(t.rotate/180*M_PI))
                                        + abs(r.width()*t.scalex * sin(t.rotate/180*M_PI)) );*/
-               
+
                value_w->setValue(i->sceneBoundingRect().width());
                value_h->setValue(i->sceneBoundingRect().height());
        } else if (i->type() == RECTITEM) {
@@ -663,76 +663,76 @@ void TitleWidget::updateDimension(QGraphicsItem *i)
                value_w->setValue((int) t->boundingRect().width());
                value_h->setValue((int) t->boundingRect().height());
        }
-       
+
        if (blockW) value_w->blockSignals(false);
        if (blockH) value_h->blockSignals(false);
 }
 
 /** \brief Updates the coordinates in the text fields from the item */
-void TitleWidget::updateCoordinates(QGraphicsItem *i) 
+void TitleWidget::updateCoordinates(QGraphicsItem *i)
 {
-       
+
        bool blockX = !value_x->signalsBlocked();
        bool blockY = !value_y->signalsBlocked();
-       
+
        // Block signals emitted by this method
        if (blockX) value_x->blockSignals(true);
        if (blockY) value_y->blockSignals(true);
-       
+
        if (i->type() == TEXTITEM) {
-               
+
                QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *> (i);
-               
+
                // Set the correct x coordinate value
                if (origin_x_left->isChecked()) {
                        // Origin (0 point) is at m_frameWidth, coordinate axis is inverted
-                       value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width())); 
+                       value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->boundingRect().width()));
                } else {
                        // Origin is at 0 (default)
                        value_x->setValue((int) rec->pos().x());
                }
-               
+
                // Same for y
                if (origin_y_top->isChecked()) {
                        value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->boundingRect().height()));
                } else {
                        value_y->setValue((int) rec->pos().y());
                }
-               
+
        } else if (i->type() == RECTITEM) {
-               
+
                QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
-               
+
                if (origin_x_left->isChecked()) {
                        // Origin (0 point) is at m_frameWidth
-                       value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width())); 
+                       value_x->setValue((int) (m_frameWidth - rec->pos().x() - rec->rect().width()));
                } else {
                        // Origin is at 0 (default)
                        value_x->setValue((int) rec->pos().x());
                }
-               
+
                if (origin_y_top->isChecked()) {
                        value_y->setValue((int) (m_frameHeight - rec->pos().y() - rec->rect().height()));
                } else {
                        value_y->setValue((int) rec->pos().y());
                }
-               
+
        } else if (i->type() == IMAGEITEM) {
-               
+
                if (origin_x_left->isChecked()) {
                        value_x->setValue((int) (m_frameWidth - i->pos().x() - i->sceneBoundingRect().width()));
-               } else {                
+               } else {
                        value_x->setValue((int) i->pos().x());
                }
-               
+
                if (origin_y_top->isChecked()) {
                        value_y->setValue((int) (m_frameHeight - i->pos().y() - i->sceneBoundingRect().height()));
                } else {
                        value_y->setValue((int) i->pos().y());
                }
-               
+
        }
-       
+
        // Stop blocking signals now
        if (!blockX) value_x->blockSignals(false);
        if (!blockY) value_y->blockSignals(false);
@@ -740,10 +740,10 @@ void TitleWidget::updateCoordinates(QGraphicsItem *i)
 
 /** \brief Updates the position of an item by reading coordinates from the text fields */
 void TitleWidget::updatePosition(QGraphicsItem *i) {
-       
+
        if (i->type() == TEXTITEM) {
                QGraphicsTextItem *rec = static_cast <QGraphicsTextItem *>(i);
-               
+
                int posX;
                if (origin_x_left->isChecked()) {
                        /* Origin of the x axis is at m_frameWidth,
@@ -755,7 +755,7 @@ void TitleWidget::updatePosition(QGraphicsItem *i) {
                } else {
                        posX = value_x->value();
                }
-       
+
                int posY;
                if (origin_y_top->isChecked()) {
                        /* Same for y axis */
@@ -763,31 +763,31 @@ void TitleWidget::updatePosition(QGraphicsItem *i) {
                } else {
                        posY = value_y->value();
                }
-               
+
                rec->setPos(posX, posY);
-               
+
        } else if (i->type() == RECTITEM) {
-               
+
                QGraphicsRectItem *rec = static_cast <QGraphicsRectItem *> (i);
-               
+
                int posX;
                if (origin_x_left->isChecked()) {
                        posX = m_frameWidth - value_x->value() - rec->rect().width();
                } else {
                        posX = value_x->value();
                }
-               
+
                int posY;
                if (origin_y_top->isChecked()) {
                        posY = m_frameHeight - value_y->value() - rec->rect().height();
                } else {
                        posY = value_y->value();
                }
-               
+
                rec->setPos(posX, posY);
-               
+
        } else if (i->type() == IMAGEITEM) {
-               
+
                int posX;
                if (origin_x_left->isChecked()) {
                        // Use the sceneBoundingRect because this also regards transformations like zoom
@@ -795,21 +795,21 @@ void TitleWidget::updatePosition(QGraphicsItem *i) {
                } else {
                        posX = value_x->value();
                }
-               
+
                int posY;
                if (origin_y_top->isChecked()) {
                        posY = m_frameHeight - value_y->value() - i->sceneBoundingRect().height();
                } else {
                        posY = value_y->value();
                }
-               
+
                i->setPos(posX, posY);
-               
+
        }
-       
+
 }
 
-void TitleWidget::updateTextOriginX() 
+void TitleWidget::updateTextOriginX()
 {
        if (origin_x_left->isChecked()) {
                origin_x_left->setText(i18n("\u2212X"));
@@ -822,13 +822,13 @@ void TitleWidget::slotOriginXClicked()
 {
        // Update the text displayed on the button.
        updateTextOriginX();
-       
+
        QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
        if (l.size() >= 1) {
                updateCoordinates(l.at(0));
-               
+
                // Remember x axis setting
-               l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()? 
+               l.at(0)->setData(TitleDocument::OriginXLeft, origin_x_left->isChecked()?
                        TitleDocument::AxisInverted : TitleDocument::AxisDefault);
        }
 }
@@ -846,38 +846,38 @@ void TitleWidget::slotOriginYClicked()
 {
        // Update the text displayed on the button.
        updateTextOriginY();
-       
+
        QList<QGraphicsItem*> l = graphicsView->scene()->selectedItems();
        if (l.size() >= 1) {
                updateCoordinates(l.at(0));
-               
-               l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked()? 
+
+               l.at(0)->setData(TitleDocument::OriginYTop, origin_y_top->isChecked()?
                        TitleDocument::AxisInverted : TitleDocument::AxisDefault);
-                       
+
        }
 }
 
-void TitleWidget::updateAxisButtons(QGraphicsItem *i) 
+void TitleWidget::updateAxisButtons(QGraphicsItem *i)
 {
        int xAxis = i->data(TitleDocument::OriginXLeft).toInt();
        int yAxis = i->data(TitleDocument::OriginYTop).toInt();
        origin_x_left->blockSignals(true);
        origin_y_top->blockSignals(true);
-       
+
        if (xAxis == TitleDocument::AxisInverted) {
                origin_x_left->setChecked(true);
        } else {
                origin_x_left->setChecked(false);
        }
        updateTextOriginX();
-       
+
        if (yAxis == TitleDocument::AxisInverted) {
                origin_y_top->setChecked(true);
        } else {
                origin_y_top->setChecked(false);
        }
        updateTextOriginY();
-       
+
        origin_x_left->blockSignals(false);
        origin_y_top->blockSignals(false);
 }
@@ -901,26 +901,26 @@ void TitleWidget::slotChanged() {
 
 /**
  * If the user has set origin_x_left (everything also for y),
- * we need to look whether a text element has been selected. If yes, 
- * we need to ensure that the right border of the text field 
+ * we need to look whether a text element has been selected. If yes,
+ * we need to ensure that the right border of the text field
  * remains fixed also when some text has been entered.
- * 
+ *
  * This is also known as right-justified, with the difference that
- * it is not valid for text but for its boundingRect. Text may still 
+ * it is not valid for text but for its boundingRect. Text may still
  * be left-justified.
  */
 void TitleWidget::textChanged(QGraphicsTextItem *i) {
-       
+
        updateDimension(i);
-       
+
        if (origin_x_left->isChecked() || origin_y_top->isChecked()) {
-                       
+
                if (!i->toPlainText().isEmpty()) {
                        updatePosition(i);
                } else {
                        /*
                         * Don't do anything if the string is empty. If the position
-                        * would be updated here, a newly created text field would 
+                        * would be updated here, a newly created text field would
                         * be set to the position of the last selected text field.
                         */
                }
@@ -1037,11 +1037,12 @@ void TitleWidget::itemHCenter()
     if (l.size() == 1) {
         QGraphicsItem *item = l.at(0);
         QRectF br;
-        if (item->type() == RECTITEM) {
-            br = ((QGraphicsRectItem*)item)->rect();
-        } else br = item->sceneBoundingRect();
-        int width = (int) br.width();
+        br = item->sceneBoundingRect();
+        int width = (int)br.width();
         int newPos = (int)((m_frameWidth - width) / 2);
+        // Check item transformation
+        if (item->type() == RECTITEM)
+            newPos += item->pos().x() - br.left();
         item->setPos(newPos, item->pos().y());
                updateCoordinates(item);
     }
@@ -1053,11 +1054,12 @@ void TitleWidget::itemVCenter()
     if (l.size() == 1) {
         QGraphicsItem *item = l.at(0);
         QRectF br;
-        if (item->type() == RECTITEM) {
-            br = ((QGraphicsRectItem*)item)->rect();
-        } else br = item->sceneBoundingRect();
-        int height = (int) br.height();
+        br = item->sceneBoundingRect();
+        int height = (int)br.height();
         int newPos = (int)((m_frameHeight - height) / 2);
+        // Check item transformation
+        if (item->type() == RECTITEM)
+            newPos += item->pos().y() - br.top();
         item->setPos(item->pos().x(), newPos);
                updateCoordinates(item);
     }