]> git.sesse.net Git - kdenlive/blobdiff - src/graphicsscenerectmove.cpp
Fix small issues with titler widget
[kdenlive] / src / graphicsscenerectmove.cpp
index d385ac71ab9402463659f158ac8407fdc7b1440e..dd233457f0e2b03767cf4235749415956628763a 100644 (file)
@@ -72,10 +72,9 @@ void GraphicsSceneRectMove::setTool(TITLETOOL tool)
     }
 }
 
-//virtual
 void GraphicsSceneRectMove::keyPressEvent(QKeyEvent * keyEvent)
 {
-    if (m_selectedItem == NULL) {
+    if (m_selectedItem == NULL || !(m_selectedItem->flags() & QGraphicsItem::ItemIsMovable)) {
         QGraphicsScene::keyPressEvent(keyEvent);
         return;
     }
@@ -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);
@@ -162,7 +165,7 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
                 break;
             }
         }
-        if (item == NULL) {
+        if (item == NULL  || !(item->flags() & QGraphicsItem::ItemIsSelectable)) {
             if (m_selectedItem && m_selectedItem->type() == 8) {
                 // disable text editing
                 QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
@@ -179,7 +182,7 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e)
                 }
             }
         }
-        if (item != NULL) {
+        if (item != NULL && item->flags() & QGraphicsItem::ItemIsMovable) {
             m_sceneClickPoint = e->scenePos();
             m_selectedItem = item;
             kDebug() << "/////////  ITEM TYPE: " << item->type();
@@ -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,70 +302,101 @@ 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) {
+                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:
-                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) {
+                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:
-                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) {
+                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:
-                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)
+                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:
-                determinant = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x());
-                if (determinant < 0) {
+                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:
-                determinant = (topLeft.x() - newpoint.x()) * (bottomLeft.y() - newpoint.y()) - (topLeft.y() - newpoint.y()) * (bottomLeft.x() - newpoint.x());
-                if (determinant < 0)
+                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:
-                determinant = (bottomLeft.x() - newpoint.x()) * (bottomRight.y() - newpoint.y()) - (bottomLeft.y() - newpoint.y()) * (bottomRight.x() - newpoint.x());
-                if (determinant < 0) {
+                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:
-                determinant = (topRight.x() - newpoint.x()) * (topLeft.y() - newpoint.y()) - (topRight.y() - newpoint.y()) * (topLeft.x() - newpoint.x());
-                if (determinant < 0)
+                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;
@@ -373,6 +406,15 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
             }
             if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) {
                 QGraphicsRectItem *gi = (QGraphicsRectItem*)m_selectedItem;
+                // Resize using aspect ratio
+                if (!m_selectedItem->data(0).isNull()) {
+                    // we want to keep aspect ratio
+                    double hRatio = (double) newrect.width() / m_selectedItem->data(0).toInt();
+                    double vRatio = (double) newrect.height() / m_selectedItem->data(1).toInt();
+                    if (hRatio < vRatio) newrect.setHeight(m_selectedItem->data(1).toInt() * hRatio);
+                    else newrect.setWidth(m_selectedItem->data(0).toInt() * vRatio);
+                }
+
                 gi->setRect(newrect);
             }
             /*else {
@@ -428,24 +470,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;
@@ -454,9 +488,11 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
         }
         QGraphicsScene::mouseMoveEvent(e);
     } else if (m_tool == TITLE_RECTANGLE && e->buttons() & Qt::LeftButton) {
-        if (m_selectedItem == NULL && (m_clickPoint - e->screenPos()).manhattanLength() >= QApplication::startDragDistance()) {
+        if (m_selectedItem == NULL) {
             // create new rect item
-            m_selectedItem = addRect(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
+            QRectF r(0, 0, e->scenePos().x() - m_sceneClickPoint.x(), e->scenePos().y() - m_sceneClickPoint.y());
+            r = r.normalized();
+            m_selectedItem = addRect(QRectF(0, 0, r.width(), r.height()));
             emit newRect((QGraphicsRectItem *) m_selectedItem);
             m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
             m_selectedItem->setPos(m_sceneClickPoint);
@@ -468,12 +504,15 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
 
 void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
 {
-    QList<QGraphicsView*> viewlist = views();
-    //kDebug() << wheelEvent->delta() << " " << zoom;
-    if (viewlist.size() > 0) {
-        if (wheelEvent->delta() < 0) emit sceneZoom(true);
-        else emit sceneZoom(false);
+    if (wheelEvent->modifiers() == Qt::ControlModifier) {
+        QList<QGraphicsView*> viewlist = views();
+        //kDebug() << wheelEvent->delta() << " " << zoom;
+        if (viewlist.size() > 0) {
+            if (wheelEvent->delta() > 0) emit sceneZoom(true);
+            else emit sceneZoom(false);
+        }
     }
+    else wheelEvent->setAccepted(false);
 }
 
 void GraphicsSceneRectMove::setScale(double s)
@@ -507,3 +546,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);
+}