]> git.sesse.net Git - kdenlive/blobdiff - src/graphicsscenerectmove.cpp
Various fixes to improve general stability in Qt 4.5.2
[kdenlive] / src / graphicsscenerectmove.cpp
index 76db7c47f3f21a5f41dbbcb64465dbadf80f0a87..275c2a4da4fa0e8271d1a44ce541f5a8be78c8be 100644 (file)
@@ -72,7 +72,6 @@ void GraphicsSceneRectMove::setTool(TITLETOOL tool)
     }
 }
 
-//virtual
 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
 {
     if (m_selectedItem == NULL) {
@@ -117,14 +116,18 @@ void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
     emit actionFinished();
 }
 
-//virtual
 void GraphicsSceneRectMove::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* e)
 {
     QPointF p = e->scenePos();
     p += QPoint(-2, -2);
     m_resizeMode = NoResize;
     m_selectedItem = NULL;
-    QGraphicsItem* g = items(QRectF(p , QSizeF(4, 4)).toRect()).at(0);
+
+    // http://www.kdenlive.org/mantis/view.php?id=1035
+    QList<QGraphicsItem*> i = items(QRectF(p , QSizeF(4, 4)).toRect());
+    if (i.size() <= 0) return;
+
+    QGraphicsItem* g = i.at(0);
     if (g) {
         if (g->type() == 8) {
             QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(g);
@@ -214,7 +217,7 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
                 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);
+                mouseArea.addRect(e->scenePos().toPoint().x() - 4 / m_zoom, e->scenePos().toPoint().y() - 4 / m_zoom, 8 / m_zoom, 8 / m_zoom);
                 // Check for collisions between the mouse and the borders
                 if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
                     m_resizeMode = TopLeft;
@@ -267,7 +270,6 @@ void GraphicsSceneRectMove::clearTextSelection()
     clearSelection();
 }
 
-//virtual
 void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
 {
     if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) {
@@ -300,76 +302,107 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
              * determinant (it must be less than zero because the Y axis is
              * inverted)
              */
-            int determinant;
+            int determinantH, determinantV;
             switch (m_resizeMode) {
-                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;
+            case TopLeft:
+                determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
+                determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
+                if (determinantV < 0) {
+                    if (determinantH < 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);
+                    } else
+                        m_resizeMode = BottomLeft;
+                } else {
+                    if (determinantH < 0)
+                        m_resizeMode = TopRight;
+                    else
+                        m_resizeMode = BottomRight;
+                }
+                break;
+            case BottomLeft:
+                determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
+                determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
+                if (determinantV < 0) {
+                    if (determinantH < 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()));
+                    } else
+                        m_resizeMode = TopLeft;
+                } else {
+                    if (determinantH < 0)
+                        m_resizeMode = BottomRight;
+                    else
+                        m_resizeMode = TopRight;
+                }
+                break;
+            case TopRight:
+                determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
+                determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
+                if (determinantV < 0) {
+                    if (determinantH < 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()));
+                    } else
+                        m_resizeMode = BottomRight;
+                } else {
+                    if (determinantH < 0)
+                        m_resizeMode = TopLeft;
+                    else
+                        m_resizeMode = BottomLeft;
+                }
+                break;
+            case BottomRight:
+                determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
+                determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
+                if (determinantV < 0) {
+                    if (determinantH < 0)
+                        newrect.setBottomRight(resizePoint);
+                    else
+                        m_resizeMode = TopRight;
+                } else {
+                    if (determinantH < 0)
+                        m_resizeMode = BottomLeft;
+                    else
+                        m_resizeMode = TopLeft;
+                }
+                break;
+            case Left:
+                determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
+                if (determinantV < 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()));
+                } else
+                    m_resizeMode = Right;
+                break;
+            case Right:
+                determinantV = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
+                if (determinantV < 0)
+                    newrect.setRight(resizePoint.x());
+                else
+                    m_resizeMode = Left;
+                break;
+            case Up:
+                determinantH = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
+                if (determinantH < 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()));
+                } else
+                    m_resizeMode = Down;
+                break;
+            case Down:
+                determinantH = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
+                if (determinantH < 0)
+                    newrect.setBottom(resizePoint.y());
+                else
+                    m_resizeMode = Up;
+                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;
@@ -428,24 +461,16 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
                 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);
+                mouseArea.addRect(e->scenePos().toPoint().x() - 4 / m_zoom, e->scenePos().toPoint().y() - 4 / m_zoom, 8 / m_zoom, 8 / m_zoom);
                 // Check for collisions between the mouse and the borders
-                if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
-                    setCursor(QCursor(Qt::SizeFDiagCursor));
-                else if (borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
-                    setCursor(QCursor(Qt::SizeBDiagCursor));
-                else if (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea))
-                    setCursor(QCursor(Qt::SizeBDiagCursor));
-                else if (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea))
-                    setCursor(QCursor(Qt::SizeFDiagCursor));
-                else if (borderLeft.collidesWithPath(mouseArea))
-                    setCursor(Qt::SizeHorCursor);
-                else if (borderRight.collidesWithPath(mouseArea))
-                    setCursor(Qt::SizeHorCursor);
-                else if (borderTop.collidesWithPath(mouseArea))
-                    setCursor(Qt::SizeVerCursor);
-                else if (borderBottom.collidesWithPath(mouseArea))
-                    setCursor(Qt::SizeVerCursor);
+                if ((borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea)) || (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea)))
+                    setResizeCursor(borderLeft.line().angle() - 45);
+                else if ((borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea)) || (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea)))
+                    setResizeCursor(borderLeft.line().angle() + 45);
+                else if (borderLeft.collidesWithPath(mouseArea) || borderRight.collidesWithPath(mouseArea))
+                    setResizeCursor(borderLeft.line().angle());
+                else if (borderTop.collidesWithPath(mouseArea) || borderBottom.collidesWithPath(mouseArea))
+                    setResizeCursor(borderTop.line().angle());
                 else
                     setCursor(Qt::OpenHandCursor);
                 break;
@@ -507,3 +532,20 @@ void GraphicsSceneRectMove::setCursor(QCursor c)
         v->setCursor(c);
     }
 }
+
+void GraphicsSceneRectMove::setResizeCursor(qreal angle)
+{
+    // % is not working...
+    while (angle < 0)
+        angle += 180;
+    while (angle >= 180)
+        angle -= 180;
+    if (angle > 157.5 || angle <= 22.5)
+        setCursor(Qt::SizeVerCursor);
+    else if (angle > 22.5 && angle <= 67.5)
+        setCursor(Qt::SizeFDiagCursor);
+    else if (angle > 67.5 && angle <= 112.5)
+        setCursor(Qt::SizeHorCursor);
+    else if (angle > 112.5 && angle <= 157.5)
+        setCursor(Qt::SizeBDiagCursor);
+}