X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=src%2Fgraphicsscenerectmove.cpp;h=ed563581621e5b04f990b3bbafc8bde1c8a7565e;hb=56aee6aedeeed3efd10ada8fe3c229eddc01ef05;hp=76db7c47f3f21a5f41dbbcb64465dbadf80f0a87;hpb=cccc66f85a3a909bebe91a5ba8fb14b6203d2d3e;p=kdenlive diff --git a/src/graphicsscenerectmove.cpp b/src/graphicsscenerectmove.cpp index 76db7c47..ed563581 100644 --- a/src/graphicsscenerectmove.cpp +++ b/src/graphicsscenerectmove.cpp @@ -34,14 +34,15 @@ GraphicsSceneRectMove::GraphicsSceneRectMove(QObject *parent) : - QGraphicsScene(parent), - m_selectedItem(NULL), - m_resizeMode(NoResize), - m_tool(TITLE_RECTANGLE) + QGraphicsScene(parent), + m_selectedItem(NULL), + m_resizeMode(NoResize), + m_tool(TITLE_RECTANGLE) { //grabMouse(); m_zoom = 1.0; setBackgroundBrush(QBrush(Qt::transparent)); + m_fontSize = 0; } void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) @@ -52,7 +53,7 @@ void GraphicsSceneRectMove::setSelectedItem(QGraphicsItem *item) update(); } -TITLETOOL GraphicsSceneRectMove::tool() +TITLETOOL GraphicsSceneRectMove::tool() const { return m_tool; } @@ -72,42 +73,45 @@ 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; } - int diff = 1; - if (m_selectedItem->type() == 8) { + if (m_selectedItem->type() == QGraphicsTextItem::Type) { QGraphicsTextItem *t = static_cast(m_selectedItem); if (t->textInteractionFlags() & Qt::TextEditorInteraction) { QGraphicsScene::keyPressEvent(keyEvent); return; } } + int diff = 1; if (keyEvent->modifiers() & Qt::ControlModifier) diff = 10; switch (keyEvent->key()) { case Qt::Key_Left: - m_selectedItem->setPos(m_selectedItem->pos() - QPointF(diff, 0)); + foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(-diff,0); } emit itemMoved(); break; case Qt::Key_Right: - m_selectedItem->setPos(m_selectedItem->pos() + QPointF(diff, 0)); + foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy( diff,0); } emit itemMoved(); break; case Qt::Key_Up: - m_selectedItem->setPos(m_selectedItem->pos() - QPointF(0, diff)); + foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(0,-diff); } emit itemMoved(); break; case Qt::Key_Down: - m_selectedItem->setPos(m_selectedItem->pos() + QPointF(0, diff)); + foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(0, diff); } emit itemMoved(); break; case Qt::Key_Delete: case Qt::Key_Backspace: - delete m_selectedItem; + foreach (QGraphicsItem *qgi, selectedItems()) { + if (qgi->data(-1).toInt() == -1) continue; + removeItem(qgi); + delete qgi; + } m_selectedItem = NULL; emit selectionChanged(); break; @@ -117,21 +121,23 @@ 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); - if (g) { - if (g->type() == 8) { - QGraphicsTextItem *t = static_cast(g); - m_selectedItem = g; - t->setTextInteractionFlags(Qt::TextEditorInteraction); - } else emit doubleClickEvent(); - } + + // http://www.kdenlive.org/mantis/view.php?id=1035 + QList i = items(QRectF(p , QSizeF(4, 4)).toRect()); + if (i.isEmpty()) return; + + QGraphicsItem* g = i.first(); + if (g->type() == QGraphicsTextItem::Type) { + m_selectedItem = g; + QGraphicsTextItem *t = static_cast(g); + t->setTextInteractionFlags(Qt::TextEditorInteraction); + } else emit doubleClickEvent(); QGraphicsScene::mouseDoubleClickEvent(e); } @@ -150,21 +156,19 @@ 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 (m_selectedItem && m_selectedItem->type() == 8) { - // disable text editing + if (item == NULL || !(item->flags() & QGraphicsItem::ItemIsSelectable)) { + if (m_selectedItem && m_selectedItem->type() == QGraphicsTextItem::Type) { + // disable text editing QGraphicsTextItem *t = static_cast(m_selectedItem); t->textCursor().setPosition(0); QTextBlock cur = t->textCursor().block(); @@ -179,11 +183,11 @@ 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(); - if (item->type() == 8) { + kDebug() << "///////// ITEM TYPE: " << item->type(); + if (item->type() == QGraphicsTextItem::Type) { QGraphicsTextItem *t = static_cast(item); if (t->textInteractionFlags() == Qt::TextEditorInteraction) { QGraphicsScene::mousePressEvent(e); @@ -191,47 +195,44 @@ 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; - if (m_selectedItem->type() == 3) - r = ((QGraphicsRectItem*)m_selectedItem)->rect(); + } else if (item->type() == QGraphicsRectItem::Type || item->type() == QGraphicsSvgItem::Type || item->type() == QGraphicsPixmapItem::Type) { + QRectF r1; + if (m_selectedItem->type() == QGraphicsRectItem::Type) + 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() - 3 / m_zoom, e->scenePos().toPoint().y() - 3 / m_zoom, 6 / m_zoom, 6 / 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,18 +246,18 @@ 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; } void GraphicsSceneRectMove::clearTextSelection() { - if (m_selectedItem && m_selectedItem->type() == 8) { - // disable text editing + if (m_selectedItem && m_selectedItem->type() == QGraphicsTextItem::Type) { + // disable text editing QGraphicsTextItem *t = static_cast(m_selectedItem); t->textCursor().setPosition(0); QTextBlock cur = t->textCursor().block(); @@ -267,17 +268,16 @@ 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; } if (m_selectedItem && e->buttons() & Qt::LeftButton) { - if (m_selectedItem->type() == 3 || m_selectedItem->type() == 13 || m_selectedItem->type() == 7) { + if (m_selectedItem->type() == QGraphicsRectItem::Type || m_selectedItem->type() == QGraphicsSvgItem::Type || m_selectedItem->type() == QGraphicsPixmapItem::Type) { QRectF newrect; - if (m_selectedItem->type() == 3) + if (m_selectedItem->type() == QGraphicsRectItem::Type) newrect = ((QGraphicsRectItem*)m_selectedItem)->rect(); else newrect = m_selectedItem->boundingRect(); @@ -300,79 +300,120 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) * determinant (it must be less than zero because the Y axis is * inverted) */ - int determinant; + int determinantH, determinantV; + // Check whether to resize or to just move the item(s) 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(); + foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(diff.x(), diff.y()); } + break; } - if (m_selectedItem->type() == 3 && m_resizeMode != NoResize) { + if (m_selectedItem->type() == QGraphicsRectItem::Type && 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 { @@ -380,14 +421,14 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) if (resizeMode == Left || resizeMode == Right ) s = m_selectedItem->boundingRect().width() / newrect.width(); else s = m_selectedItem->boundingRect().height() / newrect.height(); m_selectedItem->scale( 1 / s, 1 / s ); - kDebug()<<"/// SCALING SVG, RESIZE MODE: "<boundingRect(); + kDebug()<<"/// SCALING SVG, RESIZE MODE: "<boundingRect(); }*/ //gi->setPos(m_selectedItem->scenePos()); /*if (resizeMode == NoResize) { QGraphicsScene::mouseMoveEvent(e); return; }*/ - } else if (m_selectedItem->type() == 8) { + } else if (m_selectedItem->type() == QGraphicsTextItem::Type) { QGraphicsTextItem *t = static_cast(m_selectedItem); if (t->textInteractionFlags() & Qt::TextEditorInteraction) { QGraphicsScene::mouseMoveEvent(e); @@ -395,57 +436,50 @@ void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e) } QPointF diff = e->scenePos() - m_sceneClickPoint; m_sceneClickPoint = e->scenePos(); - m_selectedItem->moveBy(diff.x(), diff.y()); + foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(diff.x(), diff.y()); } } 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())) { - if ((g->type() == 13 || g->type() == 7) && g->zValue() > -1000) { + QList list = items(QRectF(p , QSizeF(4, 4)).toRect()); + foreach(const QGraphicsItem* g, list) { + if ((g->type() == QGraphicsSvgItem::Type || g->type() == QGraphicsPixmapItem::Type) && 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(); + } else if (g->type() == QGraphicsRectItem::Type && g->zValue() > -1000) { + 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() - 3 / m_zoom, e->scenePos().toPoint().y() - 3 / m_zoom, 6 / m_zoom, 6 / 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)) - 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 (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; @@ -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,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) @@ -507,3 +545,27 @@ 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); +} + +void GraphicsSceneRectMove::slotUpdateFontSize(int s) +{ + m_fontSize = s; +} + +#include "graphicsscenerectmove.moc"