X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fgraphicsscenerectmove.cpp;h=c190a62de701b6d30d640205f1eb8301e2712eb5;hb=a059577142fd9fd62df939f53dfade469ba93de1;hp=76db7c47f3f21a5f41dbbcb64465dbadf80f0a87;hpb=cccc66f85a3a909bebe91a5ba8fb14b6203d2d3e;p=kdenlive diff --git a/src/graphicsscenerectmove.cpp b/src/graphicsscenerectmove.cpp index 76db7c47..c190a62d 100644 --- a/src/graphicsscenerectmove.cpp +++ b/src/graphicsscenerectmove.cpp @@ -214,7 +214,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; @@ -300,76 +300,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 +459,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 +530,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); +}