From 2c5793b54e79ba324f44b721f863d22f79a48bc7 Mon Sep 17 00:00:00 2001
From: Craig Watson <craig@watsons.ch>
Date: Tue, 23 Feb 2016 11:39:06 +0100
Subject: [PATCH] Corrected stroke movement

- When clicking a stroke, they aren't moved immediately anymore; a
certain drag distance is necessary, which makes it easy (again) to
select a stroke with a stylus (which tends to move a little as it is
clicked, hence the problem).

- Removed duplicate code; the movement is now managed by
QGraphicsItemGroup::mouseMoveEvent. This prevents use of the transform()
method to get the stroke's transformation matrix; so sceneTransform() is
used instead when copying a strokes group.

- Also fixed an oversight in UBBoardView: Media items couldn't be moved
directly anymore.
---
 src/board/UBBoardView.cpp             |   1 +
 src/domain/UBGraphicsStrokesGroup.cpp | 106 ++------------------------
 src/domain/UBGraphicsStrokesGroup.h   |  28 -------
 3 files changed, 7 insertions(+), 128 deletions(-)

diff --git a/src/board/UBBoardView.cpp b/src/board/UBBoardView.cpp
index 7c5cf039..fb9ec7f2 100644
--- a/src/board/UBBoardView.cpp
+++ b/src/board/UBBoardView.cpp
@@ -663,6 +663,7 @@ bool UBBoardView::itemShouldBeMoved(QGraphicsItem *item)
         if (item->isSelected())
             return false;
     case UBGraphicsMediaItem::Type:
+        return true;
     case UBGraphicsStrokesGroup::Type:
         return false;
     case UBGraphicsTextItem::Type:
diff --git a/src/domain/UBGraphicsStrokesGroup.cpp b/src/domain/UBGraphicsStrokesGroup.cpp
index a6dbc2a8..b624946c 100644
--- a/src/domain/UBGraphicsStrokesGroup.cpp
+++ b/src/domain/UBGraphicsStrokesGroup.cpp
@@ -113,40 +113,23 @@ void UBGraphicsStrokesGroup::mousePressEvent(QGraphicsSceneMouseEvent *event)
 {
     Delegate()->startUndoStep();
 
-    mStartingPoint = event->scenePos();
-
-    initializeTransform();
-
-    mTranslateX = 0;
-    mTranslateY = 0;
-    mAngleOffset = 0;
-
-    mInitialTransform = buildTransform();
-
+    QGraphicsItemGroup::mousePressEvent(event);
     event->accept();
+
+    setSelected(false);
 }
 
 void UBGraphicsStrokesGroup::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
 {
-    QLineF move = QLineF(mStartingPoint, event->scenePos());
-
-    mTranslateX = move.dx();
-    mTranslateY = move.dy();
-    //Delegate()->frame()->moveLinkedItems(move);
+    QGraphicsItemGroup::mouseMoveEvent(event);
 
-    setTransform(buildTransform());
-    
     event->accept();
-     
+    setSelected(false);
 }
 
 void UBGraphicsStrokesGroup::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 {
     Delegate()->commitUndoStep();
-
-    mTotalTranslateX += mTranslateX;
-    mTotalTranslateY += mTranslateY;
-
     event->accept();
 
     Delegate()->mouseReleaseEvent(event);
@@ -183,7 +166,7 @@ UBItem* UBGraphicsStrokesGroup::deepCopy() const
         }
     }
     const_cast<UBGraphicsStrokesGroup*>(this)->setTransform(groupTransform);
-    copy->setTransform(groupTransform);
+    copy->setTransform(sceneTransform());
 
     return copy;
 }
@@ -240,80 +223,3 @@ QPainterPath UBGraphicsStrokesGroup::shape() const
 
     return path;
 }
-
-void UBGraphicsStrokesGroup::initializeTransform()
-{
-    
-    QTransform itemTransform = sceneTransform();
-    QRectF itemRect = boundingRect();
-    QPointF topLeft = itemTransform.map(itemRect.topLeft());
-    QPointF topRight = itemTransform.map(itemRect.topRight());
-    QPointF bottomLeft = itemTransform.map(itemRect.bottomLeft());
-
-    qreal horizontalFlip = (topLeft.x() > topRight.x()) ? -1 : 1;
-    mMirrorX = horizontalFlip < 0 ;
-    if(horizontalFlip < 0){
-        // why this is because of the way of calculating the translations that checks which side is the most is the
-        // nearest instead of checking which one is the left side.
-        QPointF tmp = topLeft;
-        topLeft = topRight;
-        topRight = tmp;
-
-        // because of the calculation of the height is done by lenght and not deltaY
-        bottomLeft = itemTransform.map(itemRect.bottomRight());
-    }
-
-    qreal verticalFlip = (bottomLeft.y() < topLeft.y()) ? -1 : 1;
-    // not sure that is usefull
-    mMirrorY = verticalFlip < 0;
-    if(verticalFlip < 0 && !mMirrorX){
-        topLeft = itemTransform.map(itemRect.bottomLeft());
-        topRight = itemTransform.map(itemRect.bottomRight());
-        bottomLeft = itemTransform.map(itemRect.topLeft());
-    }
-
-    QLineF topLine(topLeft, topRight);
-    QLineF leftLine(topLeft, bottomLeft);
-    qreal width = topLine.length();
-    qreal height = leftLine.length();
-
-    mAngle = topLine.angle();
-
-    // the fact the the length is used we loose the horizontalFlip information
-    // a better way to do this is using DeltaX that preserve the direction information.
-    mTotalScaleX = (width / itemRect.width()) * horizontalFlip;
-    mTotalScaleY = height / itemRect.height() * verticalFlip;
-    
-    
-
-    QTransform tr;
-    QPointF center = boundingRect().center();
-    tr.translate(center.x() * mTotalScaleX, center.y() * mTotalScaleY);
-    tr.rotate(-mAngle);
-    tr.translate(-center.x() * mTotalScaleX, -center.y() * mTotalScaleY);
-    tr.scale(mTotalScaleX, mTotalScaleY);
-
-    mTotalTranslateX = transform().dx() - tr.dx();
-    mTotalTranslateY = transform().dy() - tr.dy();
-    
-    
-}
-
-QTransform UBGraphicsStrokesGroup::buildTransform()
-{
-    QTransform tr;
-    QPointF center = boundingRect().center();
-
-    // Translate
-    tr.translate(mTotalTranslateX + mTranslateX, mTotalTranslateY + mTranslateY);
-
-    // Set angle
-    tr.translate(center.x() * mTotalScaleX, center.y() * mTotalScaleY);
-    tr.rotate(-mAngle);
-    tr.translate(-center.x() * mTotalScaleX, -center.y() * mTotalScaleY);
-
-    // Scale
-    tr.scale(mTotalScaleX, mTotalScaleY );
-
-    return tr;
-}
diff --git a/src/domain/UBGraphicsStrokesGroup.h b/src/domain/UBGraphicsStrokesGroup.h
index 545c8c9d..5ddf789e 100644
--- a/src/domain/UBGraphicsStrokesGroup.h
+++ b/src/domain/UBGraphicsStrokesGroup.h
@@ -66,34 +66,6 @@ protected:
     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
     virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
     virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
-
- private:
-    qreal mTranslateX;
-    qreal mTranslateY;
-    qreal mTotalTranslateX;
-    qreal mTotalTranslateY;
-
-    qreal mAngle;
-    qreal mAngleOffset;
-
-    qreal mTotalScaleX;
-    qreal mTotalScaleY;
-    qreal mScaleX;
-    qreal mScaleY;
-
-    bool mFlippedX;
-    bool mFlippedY;
-    bool mMirrorX;
-    bool mMirrorY;
-    bool mResizing;
-    bool mMirroredXAtStart;
-    bool mMirroredYAtStart;
-
-    QPointF mStartingPoint;
-    QTransform mInitialTransform;
-
-    QTransform buildTransform ();
-    void  initializeTransform ();
 };
 
 #endif // UBGRAPHICSSTROKESGROUP_H