Skip to content

Commit

Permalink
Nicer mouse handle alignment behaviour
Browse files Browse the repository at this point in the history
Don't show horizontal/vertical align snap lines if the resize
isn't allowing resizing in that axis
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent 051ed1e commit 6289367
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
41 changes: 29 additions & 12 deletions src/gui/layout/qgslayoutmousehandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
mIsDragging = false;
mIsResizing = false;
update();
hideAlignItems();
return;
}

Expand Down Expand Up @@ -661,9 +662,7 @@ void QgsLayoutMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
mLayout->undoStack()->endMacro();
}

#if 0
deleteAlignItems();
#endif
hideAlignItems();
if ( mIsDragging )
{
mIsDragging = false;
Expand Down Expand Up @@ -705,7 +704,7 @@ void QgsLayoutMouseHandles::resetStatusBar()
}
}

QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode )
QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseHandles::SnapGuideMode mode, bool snapHorizontal, bool snapVertical )
{
//align item
double alignX = 0;
Expand All @@ -723,7 +722,8 @@ QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseH
//snappedPoint = alignItem( alignX, alignY, point.x(), point.y() );
break;
case Point:
snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, mHorizontalSnapLine.get(), mVerticalSnapLine.get(), &selectedItems );
snappedPoint = mLayout->snapper().snapPoint( originalPoint, mView->transform().m11(), snapped, snapHorizontal ? mHorizontalSnapLine.get() : nullptr,
snapVertical ? mVerticalSnapLine.get() : nullptr, &selectedItems );
break;
}
#if 0
Expand Down Expand Up @@ -757,6 +757,12 @@ QPointF QgsLayoutMouseHandles::snapPoint( QPointF originalPoint, QgsLayoutMouseH
return snappedPoint;
}

void QgsLayoutMouseHandles::hideAlignItems()
{
mHorizontalSnapLine->hide();
mVerticalSnapLine->hide();
}

void QgsLayoutMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
{
//save current cursor position
Expand All @@ -770,9 +776,7 @@ void QgsLayoutMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
//type of mouse move action
mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );

#if 0
deleteAlignItems();
#endif
hideAlignItems();

if ( mCurrentMouseMoveAction == QgsLayoutMouseHandles::MoveItem )
{
Expand Down Expand Up @@ -862,14 +866,13 @@ void QgsLayoutMouseHandles::dragMouseMove( QPointF currentPosition, bool lockMov
{
//snap to grid and guides
snappedLeftPoint = snapPoint( upperLeftPoint, QgsLayoutMouseHandles::Item );

}
else
{
//no snapping
snappedLeftPoint = upperLeftPoint;
#if 0
deleteAlignItems();
#endif
hideAlignItems();
}

//calculate total shift for item from beginning of drag operation to current position
Expand Down Expand Up @@ -915,10 +918,24 @@ void QgsLayoutMouseHandles::resizeMouseMove( QPointF currentPosition, bool lockR
{
//snapping only occurs if handles are not rotated for now

bool snapVertical = mCurrentMouseMoveAction == ResizeLeft ||
mCurrentMouseMoveAction == ResizeRight ||
mCurrentMouseMoveAction == ResizeLeftUp ||
mCurrentMouseMoveAction == ResizeRightUp ||
mCurrentMouseMoveAction == ResizeLeftDown ||
mCurrentMouseMoveAction == ResizeRightDown;

bool snapHorizontal = mCurrentMouseMoveAction == ResizeUp ||
mCurrentMouseMoveAction == ResizeDown ||
mCurrentMouseMoveAction == ResizeLeftUp ||
mCurrentMouseMoveAction == ResizeRightUp ||
mCurrentMouseMoveAction == ResizeLeftDown ||
mCurrentMouseMoveAction == ResizeRightDown;

//subtract cursor edge offset from begin mouse event and current cursor position, so that snapping occurs to edge of mouse handles
//rather then cursor position
beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), QgsLayoutMouseHandles::Point );
QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), QgsLayoutMouseHandles::Point, snapHorizontal, snapVertical );
finalPosition = mapFromScene( snappedPosition );
}
else
Expand Down
4 changes: 3 additions & 1 deletion src/gui/layout/qgslayoutmousehandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ class GUI_EXPORT QgsLayoutMouseHandles: public QObject, public QGraphicsRectItem
void resetStatusBar();

//! Snaps an item or point (depending on mode) originating at originalPoint to the grid or align rulers
QPointF snapPoint( QPointF originalPoint, SnapGuideMode mode );
QPointF snapPoint( QPointF originalPoint, SnapGuideMode mode, bool snapHorizontal = true, bool snapVertical = true );

void hideAlignItems();

};

Expand Down

0 comments on commit 6289367

Please sign in to comment.