+ else
+ setCursor(Qt::ClosedHandCursor);
+ }
+ }
+ QGraphicsScene::mousePressEvent(e);
+ } else if (m_tool == TITLE_RECTANGLE) {
+ m_sceneClickPoint = e->scenePos();
+ m_selectedItem = NULL;
+ } else if (m_tool == TITLE_TEXT) {
+ m_selectedItem = addText(QString());
+ emit newText((QGraphicsTextItem *) m_selectedItem);
+ m_selectedItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
+ ((QGraphicsTextItem *)m_selectedItem)->setTextInteractionFlags(Qt::TextEditorInteraction);
+ m_selectedItem->setPos(e->scenePos() - QPointF(0, (int)(m_fontSize / 2)));
+ QGraphicsScene::mousePressEvent(e);
+ }
+
+ kDebug() << "////// MOUSE CLICK, RESIZE MODE: " << m_resizeMode;
+
+}
+
+void GraphicsSceneRectMove::clearTextSelection()
+{
+ if (m_selectedItem && m_selectedItem->type() == QGraphicsTextItem::Type) {
+ // disable text editing
+ QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
+ t->textCursor().setPosition(0);
+ QTextBlock cur = t->textCursor().block();
+ t->setTextCursor(QTextCursor(cur));
+ t->setTextInteractionFlags(Qt::NoTextInteraction);
+ }
+ m_selectedItem = NULL;
+ clearSelection();
+}
+
+void GraphicsSceneRectMove::mouseMoveEvent(QGraphicsSceneMouseEvent* e)
+{
+ 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() == QGraphicsRectItem::Type || m_selectedItem->type() == QGraphicsSvgItem::Type || m_selectedItem->type() == QGraphicsPixmapItem::Type) {
+ QRectF newrect;
+ if (m_selectedItem->type() == QGraphicsRectItem::Type)
+ newrect = ((QGraphicsRectItem*)m_selectedItem)->rect();
+ else
+ newrect = m_selectedItem->boundingRect();
+ QPointF newpoint = e->scenePos();
+ /*
+ * The vertices of the rectangle (check for matrix
+ * transformation); to be replaced by QTransform::map()?
+ */
+ QPointF itemOrigin = m_selectedItem->scenePos();
+ QTransform transform = m_selectedItem->transform();
+ QPointF topLeft(transform.m11() * newrect.toRect().left() + transform.m21() * newrect.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().top() + transform.m12() * newrect.toRect().left() + transform.m32() + itemOrigin.y());
+ QPointF bottomLeft(transform.m11() * newrect.toRect().left() + transform.m21() * newrect.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().bottom() + transform.m12() * newrect.toRect().left() + transform.m32() + itemOrigin.y());
+ QPointF topRight(transform.m11() * newrect.toRect().right() + transform.m21() * newrect.toRect().top() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().top() + transform.m12() * newrect.toRect().right() + transform.m32() + itemOrigin.y());
+ QPointF bottomRight(transform.m11() * newrect.toRect().right() + transform.m21() * newrect.toRect().bottom() + transform.m31() + itemOrigin.x(), transform.m22() * newrect.toRect().bottom() + transform.m12() * newrect.toRect().right() + transform.m32() + itemOrigin.y());
+ // Convert the mouse coordinates applying inverted transformation
+ QPointF newPointRelative = newpoint - itemOrigin;
+ QPointF resizePoint(transform.inverted().m11() * newPointRelative.x() + transform.inverted().m21() * newPointRelative.y() + transform.inverted().m31(), transform.inverted().m22() * newPointRelative.y() + transform.inverted().m12() * newPointRelative.x() + transform.inverted().m32());
+ /*
+ * Will check if the mouse is on the right of the limit lines with a
+ * determinant (it must be less than zero because the Y axis is
+ * 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());
+ 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() == 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 {
+ qreal s;
+ 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: "<<resizeMode<<", RECT:"<<m_selectedItem->boundingRect();
+ }*/
+ //gi->setPos(m_selectedItem->scenePos());
+ /*if (resizeMode == NoResize) {
+ QGraphicsScene::mouseMoveEvent(e);
+ return;
+ }*/
+ } else if (m_selectedItem->type() == QGraphicsTextItem::Type) {
+ QGraphicsTextItem *t = static_cast<QGraphicsTextItem *>(m_selectedItem);
+ if (t->textInteractionFlags() & Qt::TextEditorInteraction) {
+ QGraphicsScene::mouseMoveEvent(e);
+ return;
+ }
+ QPointF diff = e->scenePos() - m_sceneClickPoint;
+ m_sceneClickPoint = e->scenePos();
+ foreach (QGraphicsItem *qgi, selectedItems()) { qgi->moveBy(diff.x(), diff.y()); }