Skip to content

Commit

Permalink
fix: snaping rotated items
Browse files Browse the repository at this point in the history
- snap rotated item when moving w/o delegate frame
- snap rotated item when moving by delegate frame
- snap resizing for rotation angles 0 / 90 / 180 / 270
- snap newly drawn rotated line at line end points
  instead of outline
  • Loading branch information
letsfindaway committed Nov 21, 2024
1 parent 6012e25 commit 8e8aacd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/board/UBBoardView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,11 @@ void UBBoardView::handleItemsSelection(QGraphicsItem *item)

// calculate initial corner points
mCornerPoints.clear();
const auto bounds = scene()->itemRect(item);
mCornerPoints << bounds.topLeft();
mCornerPoints << bounds.topRight();
mCornerPoints << bounds.bottomLeft();
mCornerPoints << bounds.bottomRight();
const auto bounds = UBGraphicsScene::itemRect(item);
mCornerPoints << item->mapToScene(bounds.topLeft());
mCornerPoints << item->mapToScene(bounds.topRight());
mCornerPoints << item->mapToScene(bounds.bottomLeft());
mCornerPoints << item->mapToScene(bounds.bottomRight());
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/domain/UBGraphicsDelegateFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ void UBGraphicsDelegateFrame::mousePressEvent(QGraphicsSceneMouseEvent *event)
mDelegate->startUndoStep();

mStartingPoint = event->scenePos();
mStartingBounds = UBGraphicsScene::itemRect(delegated());

initializeTransform();

Expand All @@ -296,7 +295,7 @@ void UBGraphicsDelegateFrame::mousePressEvent(QGraphicsSceneMouseEvent *event)

// calculate initial corner points and respect mirroring
mCornerPoints.clear();
const auto bounds = delegated()->boundingRect();
const auto bounds = UBGraphicsScene::itemRect(delegated());
mCornerPoints << delegated()->mapToScene(bounds.topLeft());
mCornerPoints << delegated()->mapToScene(bounds.topRight());
mCornerPoints << delegated()->mapToScene(bounds.bottomLeft());
Expand Down Expand Up @@ -452,8 +451,9 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
if(!rotating())
{
const auto* ubscene = dynamic_cast<UBGraphicsScene*>(scene());
constexpr double epsilon = 0.0001;

if (ubscene && ubscene->isSnapping() && std::fmod(mAngle, 90.) == 0.)
if (ubscene && ubscene->isSnapping() && std::fmod(mAngle + epsilon, 90.) <= 2. * epsilon)
{
QPointF snap = snapVector(event->scenePos());

Expand All @@ -473,8 +473,12 @@ void UBGraphicsDelegateFrame::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
}
}

moveX += snap.x();
moveY += snap.y();
// rotate the snap according to item angle
QLineF snapLine{{}, snap};
snapLine.setAngle(snapLine.angle() - mAngle);
const auto rotSnap = snapLine.p2();
moveX += rotSnap.x();
moveY += rotSnap.y();
move.setP2(move.p2() + snap);
}

Expand Down
1 change: 0 additions & 1 deletion src/domain/UBGraphicsDelegateFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class UBGraphicsDelegateFrame: public QGraphicsRectItem, public QObject
QRect mAngleRect;

QPointF mStartingPoint;
QRectF mStartingBounds;
QTransform mInitialTransform;
QList<QPointF> mCornerPoints;
QSizeF mOriginalSize;
Expand Down
6 changes: 2 additions & 4 deletions src/domain/UBGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2626,7 +2626,7 @@ QPointF UBGraphicsScene::snap(const QRectF& rect, Qt::Corner* corner) const

QRectF UBGraphicsScene::itemRect(const QGraphicsItem* item)
{
// compute an item's rectangle in scene coordinates
// compute an item's rectangle in item coordinates
// taking into account the shape of the item and
// the nature of nominal lines
QRectF bounds = item->boundingRect();
Expand Down Expand Up @@ -2655,9 +2655,7 @@ QRectF UBGraphicsScene::itemRect(const QGraphicsItem* item)
}
}

QRectF rect = item->mapRectToScene(bounds);

return rect;
return bounds;
}

void UBGraphicsScene::addMask(const QPointF &center)
Expand Down

0 comments on commit 8e8aacd

Please sign in to comment.