X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fgraphicsscenerectmove.cpp;h=99f93eeb0a12c5e3dd6bf9bb80e458b05c09aa9d;hb=4e2125a93b533b2d29232d9072a5a757ce2616e1;hp=5274a461f2ac2025a82181d3989795ae30ddab56;hpb=9eb2cc81613ff72e6907c93fb788bfe02a3761c4;p=kdenlive diff --git a/src/graphicsscenerectmove.cpp b/src/graphicsscenerectmove.cpp index 5274a461..99f93eeb 100644 --- a/src/graphicsscenerectmove.cpp +++ b/src/graphicsscenerectmove.cpp @@ -42,6 +42,7 @@ GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent) : //grabMouse(); m_zoom = 1.0; setBackgroundBrush(QBrush(Qt::transparent)); + m_fontSize = 0; } void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) @@ -72,10 +73,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 +117,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 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(g); @@ -150,19 +154,17 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) m_resizeMode = NoResize; const QList list = items(QRectF(p , QSizeF(4, 4)).toRect()); QGraphicsItem *item = NULL; - bool hasSelected = false; if (m_tool == TITLE_SELECT) { foreach(QGraphicsItem *g, list) { kDebug() << " - - CHECKING ITEM Z:" << g->zValue() << ", TYPE: " << g->type(); // check is there is a selected item in list if (g->zValue() > -1000 && g->isSelected()) { - hasSelected = true; item = g; 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(m_selectedItem); @@ -179,10 +181,10 @@ 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(); + kDebug() << "///////// ITEM TYPE: " << item->type(); if (item->type() == 8) { QGraphicsTextItem *t = static_cast(item); if (t->textInteractionFlags() == Qt::TextEditorInteraction) { @@ -192,46 +194,43 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) t->setTextInteractionFlags(Qt::NoTextInteraction); setCursor(Qt::ClosedHandCursor); } else if (item->type() == 3 || item->type() == 13 || item->type() == 7) { - QRectF r; + QRectF r1; if (m_selectedItem->type() == 3) - r = ((QGraphicsRectItem*)m_selectedItem)->rect(); + r1 = ((QGraphicsRectItem*)m_selectedItem)->rect().normalized(); else - r = m_selectedItem->boundingRect(); - /* - * The vertices of the rectangle (check for matrix - * transformation); to be replaced by QTransform::map()? - */ - 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()); + r1 = m_selectedItem->boundingRect().normalized(); + + QList viewlist = views(); + QGraphicsView *view = NULL; + if (viewlist.size() > 0) view = viewlist[0]; + if (view == NULL) return; + // Item mapped coordinates + QPolygon r = m_selectedItem->deviceTransform(view->viewportTransform()).map(r1).toPolygon(); + QPainterPath top(r.point(0)); + top.lineTo(r.point(1)); + QPainterPath bottom(r.point(2)); + bottom.lineTo(r.point(3)); + QPainterPath left(r.point(0)); + left.lineTo(r.point(3)); + QPainterPath right(r.point(1)); + right.lineTo(r.point(2)); + + // The area interested by the mouse pointer + QPoint viewPos = view->mapFromScene(e->scenePos()); QPainterPath mouseArea; - mouseArea.addRect(e->scenePos().toPoint().x() - 4 / m_zoom, e->scenePos().toPoint().y() - 4 / m_zoom, 8 / m_zoom, 8 / m_zoom); + mouseArea.addRect(viewPos.x() - 4, viewPos.y() - 4, 8, 8); + // Check for collisions between the mouse and the borders - if (borderLeft.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea)) - m_resizeMode = TopLeft; - else if (borderLeft.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea)) - m_resizeMode = BottomLeft; - else if (borderRight.collidesWithPath(mouseArea) && borderTop.collidesWithPath(mouseArea)) - m_resizeMode = TopRight; - else if (borderRight.collidesWithPath(mouseArea) && borderBottom.collidesWithPath(mouseArea)) - m_resizeMode = BottomRight; - else if (borderLeft.collidesWithPath(mouseArea)) - m_resizeMode = Left; - else if (borderRight.collidesWithPath(mouseArea)) - m_resizeMode = Right; - else if (borderTop.collidesWithPath(mouseArea)) - m_resizeMode = Up; - else if (borderBottom.collidesWithPath(mouseArea)) - m_resizeMode = Down; + if (mouseArea.contains(r.point(0))) m_resizeMode = TopLeft; + else if (mouseArea.contains(r.point(2))) m_resizeMode = BottomRight; + else if (mouseArea.contains(r.point(1))) m_resizeMode = TopRight; + else if (mouseArea.contains(r.point(3))) m_resizeMode = BottomLeft; + else if (top.intersects(mouseArea)) m_resizeMode = Up; + else if (bottom.intersects(mouseArea)) m_resizeMode = Down; + else if (right.intersects(mouseArea)) m_resizeMode = Right; + else if (left.intersects(mouseArea)) m_resizeMode = Left; + else setCursor(Qt::ClosedHandCursor); } @@ -245,11 +244,11 @@ void GraphicsSceneRectMove::mousePressEvent(QGraphicsSceneMouseEvent* e) emit newText((QGraphicsTextItem *) m_selectedItem); m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable); ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction); - m_selectedItem->setPos(e->scenePos()); + m_selectedItem->setPos(e->scenePos() - QPointF(0, (int)(m_fontSize / 2))); QGraphicsScene::mousePressEvent(e); } - kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << m_resizeMode; + kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << m_resizeMode; } @@ -267,10 +266,9 @@ void GraphicsSceneRectMove::clearTextSelection() clearSelection(); } -//virtual void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) { - if ((e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) { + if (e->buttons() != Qt::NoButton && (e->screenPos() - m_clickPoint).manhattanLength() < QApplication::startDragDistance()) { e->accept(); return; } @@ -301,6 +299,7 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) * inverted) */ int determinantH, determinantV; + // Check whether to resize or to just move the item(s) switch (m_resizeMode) { case TopLeft: determinantV = (bottomRight.x() - newpoint.x()) * (topRight.y() - newpoint.y()) - (bottomRight.y() - newpoint.y()) * (topRight.x() - newpoint.x()); @@ -404,6 +403,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 { @@ -430,45 +438,46 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) } emit itemMoved(); } else if (m_tool == TITLE_SELECT) { + QList viewlist = views(); + QGraphicsView *view = NULL; + if (viewlist.size() > 0) view = viewlist[0]; + QPointF p = e->scenePos(); p += QPoint(-2, -2); m_resizeMode = NoResize; bool itemFound = false; - foreach(const QGraphicsItem* g, items(QRectF(p , QSizeF(4, 4)).toRect())) { + QList list = items(QRectF(p , QSizeF(4, 4)).toRect()); + foreach(const QGraphicsItem* g, list) { if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) { // image or svg item setCursor(Qt::OpenHandCursor); break; } else if (g->type() == 3 && g->zValue() > -1000) { - QRectF r = ((const QGraphicsRectItem*)g)->rect(); + if (view == NULL) continue; + QRectF r1 = ((const QGraphicsRectItem*)g)->rect().normalized(); itemFound = true; - /* - * The vertices of the rectangle (check for matrix - * transformation); to be replaced by QTransform::map()? - */ - 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()); + + // Item mapped coordinates + QPolygon r = g->deviceTransform(view->viewportTransform()).map(r1).toPolygon(); + QPainterPath top(r.point(0)); + top.lineTo(r.point(1)); + QPainterPath bottom(r.point(2)); + bottom.lineTo(r.point(3)); + QPainterPath left(r.point(0)); + left.lineTo(r.point(3)); + QPainterPath right(r.point(1)); + right.lineTo(r.point(2)); + // The area interested by the mouse pointer + QPoint viewPos = view->mapFromScene(e->scenePos()); QPainterPath mouseArea; - mouseArea.addRect(e->scenePos().toPoint().x() - 4 / m_zoom, e->scenePos().toPoint().y() - 4 / m_zoom, 8 / m_zoom, 8 / m_zoom); + mouseArea.addRect(viewPos.x() - 4, viewPos.y() - 4, 8, 8); + // Check for collisions between the mouse and the borders - 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()); + if (mouseArea.contains(r.point(0)) || mouseArea.contains(r.point(2))) setCursor(Qt::SizeFDiagCursor); + else if (mouseArea.contains(r.point(1)) || mouseArea.contains(r.point(3))) setCursor(Qt::SizeBDiagCursor); + else if (top.intersects(mouseArea) || bottom.intersects(mouseArea)) setCursor(Qt::SizeVerCursor); + else if (right.intersects(mouseArea) || left.intersects(mouseArea)) setCursor(Qt::SizeHorCursor); else setCursor(Qt::OpenHandCursor); break; @@ -477,9 +486,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); @@ -491,12 +502,14 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) void GraphicsSceneRectMove::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent) { - QList 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 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) @@ -547,3 +560,10 @@ void GraphicsSceneRectMove::setResizeCursor(qreal angle) else if (angle > 112.5 && angle <= 157.5) setCursor(Qt::SizeBDiagCursor); } + +void GraphicsSceneRectMove::slotUpdateFontSize(int s) +{ + m_fontSize = s; +} + +#include "graphicsscenerectmove.moc"