27#include <QGraphicsSceneHoverEvent>
28#include <QGraphicsView>
33#include "moc_qgsgraphicsviewmousehandles.cpp"
35using namespace Qt::StringLiterals;
39QgsGraphicsViewMouseHandles::QgsGraphicsViewMouseHandles( QGraphicsView *view )
41 , QGraphicsRectItem( nullptr )
45 setAcceptHoverEvents(
true );
48 mRotationHandlePath.moveTo( 0, 14 );
49 mRotationHandlePath.lineTo( 6, 20 );
50 mRotationHandlePath.lineTo( 12, 14 );
51 mRotationHandlePath.arcTo( 8, 8, 12, 12, 180, -90 );
52 mRotationHandlePath.lineTo( 14, 12 );
53 mRotationHandlePath.lineTo( 20, 6 );
54 mRotationHandlePath.lineTo( 14, 0 );
55 mRotationHandlePath.arcTo( 4, 4, 20, 20, 90, 90 );
56 mRotationHandlePath.lineTo( 0, 14 );
59void QgsGraphicsViewMouseHandles::setRotationEnabled(
bool enable )
61 if ( mRotationEnabled == enable )
66 mRotationEnabled = enable;
70void QgsGraphicsViewMouseHandles::setCadMouseDigitizingModeEnabled(
bool enable )
72 if ( mCadMouseDigitizingMode == enable )
77 mCadMouseDigitizingMode = enable;
80void QgsGraphicsViewMouseHandles::setResizeEnabled(
bool enable )
82 if ( mResizeEnabled == enable )
87 mResizeEnabled = enable;
91void QgsGraphicsViewMouseHandles::reset()
94 mIsDragStarting =
false;
110void QgsGraphicsViewMouseHandles::paintInternal( QPainter *painter,
bool showHandles,
bool showStaticBoundingBoxes,
bool showTemporaryBoundingBoxes,
const QStyleOptionGraphicsItem *, QWidget * )
117 if ( showStaticBoundingBoxes )
120 double rectHandlerSize = rectHandlerBorderTolerance();
121 drawHandles( painter, rectHandlerSize );
124 if ( showTemporaryBoundingBoxes && ( mIsResizing || mIsDragging || showStaticBoundingBoxes ) )
127 drawSelectedItemBounds( painter );
131QRectF QgsGraphicsViewMouseHandles::storedItemRect( QGraphicsItem *item )
const
133 return itemRect( item );
136void QgsGraphicsViewMouseHandles::rotateItem( QGraphicsItem *,
double,
double,
double )
138 QgsDebugError( u
"Rotation is not implemented for this class"_s );
141void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *,
double,
double )
144void QgsGraphicsViewMouseHandles::setItemRect( QGraphicsItem *, QRectF )
146 QgsDebugError( u
"Resize is not implemented for this class"_s );
149QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
154void QgsGraphicsViewMouseHandles::startMacroCommand(
const QString & )
157void QgsGraphicsViewMouseHandles::endMacroCommand()
160void QgsGraphicsViewMouseHandles::endItemCommand( QGraphicsItem * )
163void QgsGraphicsViewMouseHandles::createItemCommand( QGraphicsItem * )
166QPointF QgsGraphicsViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode,
bool,
bool )
168 return originalPoint;
171void QgsGraphicsViewMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
176void QgsGraphicsViewMouseHandles::drawHandles( QPainter *painter,
double rectHandlerSize )
179 QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
180 handlePen.setWidth( 0 );
181 painter->setPen( handlePen );
184 painter->setBrush( Qt::NoBrush );
185 painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
187 if ( isResizeEnabled() )
190 painter->setBrush( QColor( 255, 255, 255, 255 ) );
192 painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
194 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
196 painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
198 painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
200 painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
202 painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
204 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
206 painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
209 if ( isRotationEnabled() )
212 const double scale = rectHandlerSize / mHandleSize;
213 const bool drawBottomRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().height();
214 const bool drawRightRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().width();
215 QTransform transform;
219 transform.translate( rectHandlerSize, rectHandlerSize );
220 transform.scale( scale, scale );
222 QgsScopedQPainterState painterState( painter );
223 painter->setTransform( transform,
true );
224 painter->drawPath( mRotationHandlePath );
225 painterState.restore();
228 if ( drawRightRotationHandles )
231 transform.translate( rect().width() - rectHandlerSize, rectHandlerSize );
232 transform.rotate( 90 );
233 transform.scale( scale, scale );
235 painter->setTransform( transform,
true );
236 painter->drawPath( mRotationHandlePath );
237 painterState.restore();
240 if ( drawBottomRotationHandles )
244 transform.translate( rectHandlerSize, rect().height() - rectHandlerSize );
245 transform.rotate( 270 );
246 transform.scale( scale, scale );
248 painter->setTransform( transform,
true );
249 painter->drawPath( mRotationHandlePath );
250 painterState.restore();
253 if ( drawBottomRotationHandles && drawRightRotationHandles )
257 transform.translate( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize );
258 transform.rotate( 180 );
259 transform.scale( scale, scale );
261 painter->setTransform( transform,
true );
262 painter->drawPath( mRotationHandlePath );
263 painterState.restore();
268void QgsGraphicsViewMouseHandles::drawSelectedItemBounds( QPainter *painter )
271 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
272 if ( selectedItems.isEmpty() )
277 QList<QGraphicsItem *> itemsToDraw;
278 expandItemList( selectedItems, itemsToDraw );
280 if ( itemsToDraw.size() <= 1 )
287 QgsScopedQPainterState painterState( painter );
288 painter->setCompositionMode( QPainter::CompositionMode_Difference );
291 QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
292 selectedItemPen.setStyle( Qt::DashLine );
293 selectedItemPen.setWidth( 0 );
294 painter->setPen( selectedItemPen );
295 painter->setBrush( Qt::NoBrush );
297 for ( QGraphicsItem *item : std::as_const( itemsToDraw ) )
300 QPolygonF itemBounds;
301 if ( isDragging() && !itemIsLocked( item ) )
305 QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
308 itemSceneBounds.translate( transform().dx(), transform().dy() );
310 itemBounds = mapFromScene( itemSceneBounds );
312 else if ( isResizing() && !itemIsLocked( item ) )
316 QRectF thisItemRect = mapRectFromItem( item, itemRect( item ) );
318 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
319 itemBounds = QPolygonF( thisItemRect );
321 else if ( isRotating() && !itemIsLocked( item ) )
323 const QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
324 const QPointF rotationCenter = sceneTransform().map( rect().center() );
326 QTransform transform;
327 transform.translate( rotationCenter.x(), rotationCenter.y() );
328 transform.rotate( mRotationDelta );
329 transform.translate( -rotationCenter.x(), -rotationCenter.y() );
330 itemBounds = mapFromScene( transform.map( itemSceneBounds ) );
335 itemBounds = item->mapToItem(
this, itemRect( item ) );
341 path.addPolygon( itemBounds );
342 painter->drawPath( path );
346double QgsGraphicsViewMouseHandles::rectHandlerBorderTolerance()
const
353 double viewScaleFactor = mView->transform().m11();
356 double rectHandlerSize = mHandleSize / viewScaleFactor;
359 if ( rectHandlerSize > ( rect().width() / 3 ) )
361 rectHandlerSize = rect().width() / 3;
363 if ( rectHandlerSize > ( rect().height() / 3 ) )
365 rectHandlerSize = rect().height() / 3;
367 return rectHandlerSize;
370Qt::CursorShape QgsGraphicsViewMouseHandles::cursorForPosition( QPointF itemCoordPos )
373 double normalizedRotation = std::fmod( rotation(), 360 );
374 if ( normalizedRotation < 0 )
376 normalizedRotation += 360;
378 switch ( mouseAction )
381 return Qt::ForbiddenCursor;
383 return Qt::SizeAllCursor;
387 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
389 return Qt::SizeVerCursor;
391 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
393 return Qt::SizeBDiagCursor;
395 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
397 return Qt::SizeHorCursor;
401 return Qt::SizeFDiagCursor;
406 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
408 return Qt::SizeHorCursor;
410 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
412 return Qt::SizeFDiagCursor;
414 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
416 return Qt::SizeVerCursor;
420 return Qt::SizeBDiagCursor;
426 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
428 return Qt::SizeFDiagCursor;
430 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
432 return Qt::SizeVerCursor;
434 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
436 return Qt::SizeBDiagCursor;
440 return Qt::SizeHorCursor;
445 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
447 return Qt::SizeBDiagCursor;
449 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
451 return Qt::SizeHorCursor;
453 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
455 return Qt::SizeFDiagCursor;
459 return Qt::SizeVerCursor;
462 return Qt::ArrowCursor;
468 return Qt::PointingHandCursor;
471 return Qt::ArrowCursor;
476 bool nearLeftBorder =
false;
477 bool nearRightBorder =
false;
478 bool nearLowerBorder =
false;
479 bool nearUpperBorder =
false;
481 bool nearLeftInner =
false;
482 bool nearRightInner =
false;
483 bool nearLowerInner =
false;
484 bool nearUpperInner =
false;
486 bool withinWidth =
false;
487 bool withinHeight =
false;
489 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() <= rect().width() )
493 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() <= rect().height() )
498 double borderTolerance = rectHandlerBorderTolerance();
499 double innerTolerance = mRotationHandleSize * borderTolerance / mHandleSize;
501 if ( isResizeEnabled() && itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
503 nearLeftBorder =
true;
505 else if ( isRotationEnabled() && itemCoordPos.x() >= borderTolerance && itemCoordPos.x() < ( borderTolerance + innerTolerance ) )
507 nearLeftInner =
true;
509 if ( isResizeEnabled() && itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
511 nearUpperBorder =
true;
513 else if ( isRotationEnabled() && itemCoordPos.y() >= borderTolerance && itemCoordPos.y() < ( borderTolerance + innerTolerance ) )
515 nearUpperInner =
true;
517 if ( isResizeEnabled() && itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
519 nearRightBorder =
true;
521 else if ( isRotationEnabled() && itemCoordPos.x() <= ( rect().width() - borderTolerance ) && itemCoordPos.x() > ( rect().width() - borderTolerance - innerTolerance ) )
523 nearRightInner =
true;
525 if ( isResizeEnabled() && itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
527 nearLowerBorder =
true;
529 else if ( isRotationEnabled() && itemCoordPos.y() <= ( rect().height() - borderTolerance ) && itemCoordPos.y() > ( rect().height() - borderTolerance - innerTolerance ) )
531 nearLowerInner =
true;
534 if ( nearLeftBorder && nearUpperBorder )
538 else if ( nearLeftBorder && nearLowerBorder )
542 else if ( nearRightBorder && nearUpperBorder )
546 else if ( nearRightBorder && nearLowerBorder )
550 else if ( nearLeftBorder && withinHeight )
554 else if ( nearRightBorder && withinHeight )
558 else if ( nearUpperBorder && withinWidth )
562 else if ( nearLowerBorder && withinWidth )
566 else if ( nearLeftInner && nearUpperInner )
570 else if ( nearRightInner && nearUpperInner )
574 else if ( nearLeftInner && nearLowerInner )
578 else if ( nearRightInner && nearLowerInner )
584 QPointF scenePoint = mapToScene( itemCoordPos );
585 const QList<QGraphicsItem *> itemsAtCursorPos = sceneItemsAtPoint( scenePoint );
586 if ( itemsAtCursorPos.isEmpty() )
591 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
593 if ( graphicsItem && graphicsItem->isSelected() )
607 QPointF itemPos = mapFromScene( sceneCoordPos );
608 return mouseActionForPosition( itemPos );
611bool QgsGraphicsViewMouseHandles::shouldBlockEvent( QInputEvent * )
const
613 return mIsDragging || mIsResizing || mIsRotating;
616void QgsGraphicsViewMouseHandles::startMove( QPointF sceneCoordPos )
619 mMouseMoveStartPos = sceneCoordPos;
621 mBeginMouseEventPos = sceneCoordPos;
622 mBeginHandlePos = scenePos();
623 mBeginHandleWidth = rect().width();
624 mBeginHandleHeight = rect().height();
630 if ( mView->scene()->mouseGrabberItem() !=
this )
636void QgsGraphicsViewMouseHandles::selectedItemSizeChanged()
638 if ( !isDragging() && !isResizing() )
645void QgsGraphicsViewMouseHandles::selectedItemRotationChanged()
647 if ( !isDragging() && !isResizing() )
654void QgsGraphicsViewMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
656 setViewportCursor( cursorForPosition( event->pos() ) );
659void QgsGraphicsViewMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
662 setViewportCursor( Qt::ArrowCursor );
665void QgsGraphicsViewMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
667 if ( event->button() != Qt::LeftButton )
673 if ( mCadMouseDigitizingMode && ( mIsDragging || mIsResizing || mIsRotating ) )
679 mMouseMoveStartPos =
event->lastScenePos();
681 mBeginMouseEventPos =
event->lastScenePos();
682 mBeginHandlePos = scenePos();
683 mBeginHandleWidth = rect().width();
684 mBeginHandleHeight = rect().height();
686 mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
690 switch ( mCurrentMouseMoveAction )
694 startMoveOperation();
708 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
711 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
719 mRotationCenter = sceneTransform().map( rect().center() );
720 mRotationBegin = std::atan2( mMouseMoveStartPos.y() - mRotationCenter.y(), mMouseMoveStartPos.x() - mRotationCenter.x() ) * 180 / M_PI;
721 mRotationCurrent = 0.0;
729 if ( mCadMouseDigitizingMode && mIsDragging )
731 mIsDragStarting =
true;
735void QgsGraphicsViewMouseHandles::resetStatusBar()
737 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
738 int selectedCount = selectedItems.size();
742 showStatusMessage( tr(
"%n item(s) selected",
nullptr, selectedCount ) );
747 showStatusMessage( QString() );
751void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
758 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
760 else if ( isResizing() )
765 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
767 else if ( isRotating() )
771 rotateMouseMove( event->lastScenePos(), event->modifiers() & Qt::ControlModifier );
775void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
777 if ( event->button() != Qt::LeftButton )
783 if ( mDoubleClickInProgress )
785 mDoubleClickInProgress =
false;
790 if ( mIsDragStarting )
792 mIsDragStarting =
false;
798 if ( mView->scene()->mouseGrabberItem() ==
this )
803 QPointF mouseMoveStopPoint =
event->lastScenePos();
804 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
805 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
807 const bool isClick = std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min();
825 startMacroCommand( tr(
"Move Items" ) );
827 QPointF mEndHandleMovePos = scenePos();
829 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
830 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
833 const QList<QGraphicsItem *> selectedItems = filterItemListForMoveOperation( selectedSceneItems(
false ) );
834 for ( QGraphicsItem *item : selectedItems )
836 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
842 createItemCommand( item );
843 moveItem( item, deltaX, deltaY );
844 endItemCommand( item );
851 else if ( mIsResizing )
854 startMacroCommand( tr(
"Resize Items" ) );
857 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
858 for ( QGraphicsItem *item : selectedItems )
860 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
865 createItemCommand( item );
868 if ( selectedItems.size() == 1 )
871 thisItemRect = mResizeRect;
876 thisItemRect = mapRectFromItem( item, itemRect( item ) );
877 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
880 thisItemRect = thisItemRect.normalized();
881 QPointF newPos = mapToScene( thisItemRect.topLeft() );
882 thisItemRect.moveTopLeft( newPos );
883 setItemRect( item, thisItemRect );
885 endItemCommand( item );
891 else if ( mIsRotating )
893 const QPointF itemRotationCenter = sceneTransform().map( rect().center() );
896 startMacroCommand( tr(
"Rotate Items" ) );
899 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
900 for ( QGraphicsItem *item : selectedItems )
902 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
908 const QPointF itemCenter = item->mapToScene( itemRect( item ) ).boundingRect().center();
910 QTransform transform;
911 transform.translate( itemRotationCenter.x(), itemRotationCenter.y() );
912 transform.rotate( mRotationDelta );
913 transform.translate( -itemRotationCenter.x(), -itemRotationCenter.y() );
914 const QPointF rotatedItemCenter = transform.map( itemCenter );
916 createItemCommand( item );
917 rotateItem( item, mRotationDelta, rotatedItemCenter.x() - itemCenter.x(), rotatedItemCenter.y() - itemCenter.y() );
918 endItemCommand( item );
928void QgsGraphicsViewMouseHandles::startMoveOperation()
931void QgsGraphicsViewMouseHandles::endMoveOperation()
934bool QgsGraphicsViewMouseHandles::selectionRotation(
double &rotation )
const
937 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
938 auto itemIter = selectedItems.constBegin();
941 double firstItemRotation = ( *itemIter )->rotation();
944 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
946 if ( !
qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
954 rotation = firstItemRotation;
958void QgsGraphicsViewMouseHandles::updateHandles()
963 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
964 if ( !selectedItems.isEmpty() )
970 if ( selectionRotation( rotation ) )
973 setRotation( rotation );
982 QRectF newHandleBounds = selectionBounds();
985 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
986 setPos( mapToScene( newHandleBounds.topLeft() ) );
1000void QgsGraphicsViewMouseHandles::rotateMouseMove( QPointF currentPosition,
bool snapToCommonAngles )
1007 mRotationCurrent = std::atan2( currentPosition.y() - mRotationCenter.y(), currentPosition.x() - mRotationCenter.x() ) * 180 / M_PI;
1008 mRotationDelta = mRotationCurrent - mRotationBegin;
1009 if ( snapToCommonAngles )
1014 const double itemRotationRadian = rotation() * M_PI / 180;
1015 const double deltaX = ( rect().width() / 2 ) * cos( itemRotationRadian ) - ( rect().height() / 2 ) * sin( itemRotationRadian );
1016 const double deltaY = ( rect().width() / 2 ) * sin( itemRotationRadian ) + ( rect().height() / 2 ) * cos( itemRotationRadian );
1018 QTransform rotateTransform;
1019 rotateTransform.translate( deltaX, deltaY );
1020 rotateTransform.rotate( mRotationDelta );
1021 rotateTransform.translate( -deltaX, -deltaY );
1022 setTransform( rotateTransform );
1025 showStatusMessage( tr(
"rotation: %1°" ).arg( QString::number( mRotationDelta,
'f', 2 ) ) );
1030void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition,
bool lockMovement,
bool preventSnap )
1038 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
1039 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
1042 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
1044 QPointF snappedLeftPoint;
1050 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
1055 snappedLeftPoint = upperLeftPoint;
1060 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
1061 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
1067 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
1078 QTransform moveTransform;
1079 moveTransform.translate( moveRectX, moveRectY );
1080 setTransform( moveTransform );
1082 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1083 for ( QGraphicsItem *item : selectedItems )
1085 previewItemMove( item, moveRectX, moveRectY );
1088 showStatusMessage( tr(
"dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
1091void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition,
bool lockRatio,
bool fromCenter )
1098 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
1100 QPointF beginMousePos;
1101 QPointF finalPosition;
1122 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
1123 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
1124 finalPosition = mapFromScene( snappedPosition );
1129 beginMousePos = mapFromScene( mBeginMouseEventPos );
1130 finalPosition = mapFromScene( currentPosition );
1133 double diffX = finalPosition.x() - beginMousePos.x();
1134 double diffY = finalPosition.y() - beginMousePos.y();
1137 if ( lockRatio && !
qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
1139 ratio = mBeginHandleWidth / mBeginHandleHeight;
1142 switch ( mCurrentMouseMoveAction )
1149 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1169 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1190 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1209 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1230 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1232 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
1236 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
1239 mx = diffX, my = diffY;
1250 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1252 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1256 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1261 rx = diffX, ry = diffY;
1270 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1272 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1276 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
1280 my = diffY, rx = diffX, ry = -diffY;
1289 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1291 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
1295 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1326 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1327 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1330 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1331 translateLine.setAngle( translateLine.angle() - rotation() );
1332 QPointF sceneTranslate = translateLine.p2();
1335 QTransform itemTransform;
1336 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1337 setTransform( itemTransform );
1340 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1342 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1344 else if ( mBeginHandleHeight + ry >= 0 )
1346 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1348 else if ( mBeginHandleWidth + rx >= 0 )
1350 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1354 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1357 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1358 QRectF newHandleBounds;
1359 for ( QGraphicsItem *item : selectedItems )
1362 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1364 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1366 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1367 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1370 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1373 showStatusMessage( tr(
"width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1376void QgsGraphicsViewMouseHandles::setHandleSize(
double size )
1381void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1385 mDoubleClickInProgress =
true;
1388QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1391 QPointF sceneMousePos = mapFromScene( cursorPos );
1393 switch ( mCurrentMouseMoveAction )
1397 return QSizeF( 0, sceneMousePos.y() );
1400 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1404 return QSizeF( sceneMousePos.x(), 0 );
1407 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1411 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1414 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1417 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1420 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1435QRectF QgsGraphicsViewMouseHandles::selectionBounds()
const
1438 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1439 auto itemIter = selectedItems.constBegin();
1442 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1445 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1447 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1453void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize,
const QRectF &boundsBefore,
const QRectF &boundsAfter )
1456 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1457 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1458 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1459 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1461 rectToResize.setRect( left, top, right - left, bottom - top );
1464double QgsGraphicsViewMouseHandles::relativePosition(
double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
1467 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1468 double c = afterMin - ( beforeMin * m );
1471 return m * position +
c;
1474QList<QGraphicsItem *> QgsGraphicsViewMouseHandles::filterItemListForMoveOperation(
const QList<QGraphicsItem *> &items )
MouseHandlesAction
Action to be performed by the mouse handles.
@ ResizeRightDown
Resize right down (Bottom right handle).
@ ResizeLeftUp
Resize left up (Top left handle).
@ ResizeLeftDown
Resize left down (Bottom left handle).
@ ResizeRight
Resize right (Right handle).
@ ResizeDown
Resize down (Bottom handle).
@ RotateTopRight
Rotate from top right handle.
@ RotateTopLeft
Rotate from top left handle.
@ RotateBottomRight
Rotate right bottom right handle.
@ ResizeRightUp
Resize right up (Top right handle).
@ ResizeLeft
Resize left (Left handle).
@ RotateBottomLeft
Rotate from bottom left handle.
@ ResizeUp
Resize up (Top handle).
static double snappedAngle(double angle)
Snaps an angle (in degrees) to its closest 45 degree angle.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
#define QgsDebugError(str)