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 )
707 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
710 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
718 mRotationCenter = sceneTransform().map( rect().center() );
719 mRotationBegin = std::atan2( mMouseMoveStartPos.y() - mRotationCenter.y(), mMouseMoveStartPos.x() - mRotationCenter.x() ) * 180 / M_PI;
720 mRotationCurrent = 0.0;
728 if ( mCadMouseDigitizingMode && mIsDragging )
730 mIsDragStarting =
true;
734void QgsGraphicsViewMouseHandles::resetStatusBar()
736 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
737 int selectedCount = selectedItems.size();
741 showStatusMessage( tr(
"%n item(s) selected",
nullptr, selectedCount ) );
746 showStatusMessage( QString() );
750void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
757 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
759 else if ( isResizing() )
764 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
766 else if ( isRotating() )
770 rotateMouseMove( event->lastScenePos(), event->modifiers() & Qt::ControlModifier );
774void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
776 if ( event->button() != Qt::LeftButton )
782 if ( mDoubleClickInProgress )
784 mDoubleClickInProgress =
false;
789 if ( mIsDragStarting )
791 mIsDragStarting =
false;
797 if ( mView->scene()->mouseGrabberItem() ==
this )
802 QPointF mouseMoveStopPoint =
event->lastScenePos();
803 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
804 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
806 const bool isClick = std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min();
820 startMacroCommand( tr(
"Move Items" ) );
822 QPointF mEndHandleMovePos = scenePos();
824 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
825 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
828 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
829 for ( QGraphicsItem *item : selectedItems )
831 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
837 createItemCommand( item );
838 moveItem( item, deltaX, deltaY );
839 endItemCommand( item );
845 else if ( mIsResizing )
848 startMacroCommand( tr(
"Resize Items" ) );
851 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
852 for ( QGraphicsItem *item : selectedItems )
854 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
859 createItemCommand( item );
862 if ( selectedItems.size() == 1 )
865 thisItemRect = mResizeRect;
870 thisItemRect = mapRectFromItem( item, itemRect( item ) );
871 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
874 thisItemRect = thisItemRect.normalized();
875 QPointF newPos = mapToScene( thisItemRect.topLeft() );
876 thisItemRect.moveTopLeft( newPos );
877 setItemRect( item, thisItemRect );
879 endItemCommand( item );
885 else if ( mIsRotating )
887 const QPointF itemRotationCenter = sceneTransform().map( rect().center() );
890 startMacroCommand( tr(
"Rotate Items" ) );
893 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
894 for ( QGraphicsItem *item : selectedItems )
896 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
902 const QPointF itemCenter = item->mapToScene( itemRect( item ) ).boundingRect().center();
904 QTransform transform;
905 transform.translate( itemRotationCenter.x(), itemRotationCenter.y() );
906 transform.rotate( mRotationDelta );
907 transform.translate( -itemRotationCenter.x(), -itemRotationCenter.y() );
908 const QPointF rotatedItemCenter = transform.map( itemCenter );
910 createItemCommand( item );
911 rotateItem( item, mRotationDelta, rotatedItemCenter.x() - itemCenter.x(), rotatedItemCenter.y() - itemCenter.y() );
912 endItemCommand( item );
922bool QgsGraphicsViewMouseHandles::selectionRotation(
double &rotation )
const
925 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
926 auto itemIter = selectedItems.constBegin();
929 double firstItemRotation = ( *itemIter )->rotation();
932 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
934 if ( !
qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
942 rotation = firstItemRotation;
946void QgsGraphicsViewMouseHandles::updateHandles()
951 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
952 if ( !selectedItems.isEmpty() )
958 if ( selectionRotation( rotation ) )
961 setRotation( rotation );
970 QRectF newHandleBounds = selectionBounds();
973 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
974 setPos( mapToScene( newHandleBounds.topLeft() ) );
988void QgsGraphicsViewMouseHandles::rotateMouseMove( QPointF currentPosition,
bool snapToCommonAngles )
995 mRotationCurrent = std::atan2( currentPosition.y() - mRotationCenter.y(), currentPosition.x() - mRotationCenter.x() ) * 180 / M_PI;
996 mRotationDelta = mRotationCurrent - mRotationBegin;
997 if ( snapToCommonAngles )
1002 const double itemRotationRadian = rotation() * M_PI / 180;
1003 const double deltaX = ( rect().width() / 2 ) * cos( itemRotationRadian ) - ( rect().height() / 2 ) * sin( itemRotationRadian );
1004 const double deltaY = ( rect().width() / 2 ) * sin( itemRotationRadian ) + ( rect().height() / 2 ) * cos( itemRotationRadian );
1006 QTransform rotateTransform;
1007 rotateTransform.translate( deltaX, deltaY );
1008 rotateTransform.rotate( mRotationDelta );
1009 rotateTransform.translate( -deltaX, -deltaY );
1010 setTransform( rotateTransform );
1013 showStatusMessage( tr(
"rotation: %1°" ).arg( QString::number( mRotationDelta,
'f', 2 ) ) );
1018void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition,
bool lockMovement,
bool preventSnap )
1026 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
1027 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
1030 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
1032 QPointF snappedLeftPoint;
1038 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
1043 snappedLeftPoint = upperLeftPoint;
1048 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
1049 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
1055 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
1066 QTransform moveTransform;
1067 moveTransform.translate( moveRectX, moveRectY );
1068 setTransform( moveTransform );
1070 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1071 for ( QGraphicsItem *item : selectedItems )
1073 previewItemMove( item, moveRectX, moveRectY );
1076 showStatusMessage( tr(
"dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
1079void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition,
bool lockRatio,
bool fromCenter )
1086 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
1088 QPointF beginMousePos;
1089 QPointF finalPosition;
1110 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
1111 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
1112 finalPosition = mapFromScene( snappedPosition );
1117 beginMousePos = mapFromScene( mBeginMouseEventPos );
1118 finalPosition = mapFromScene( currentPosition );
1121 double diffX = finalPosition.x() - beginMousePos.x();
1122 double diffY = finalPosition.y() - beginMousePos.y();
1125 if ( lockRatio && !
qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
1127 ratio = mBeginHandleWidth / mBeginHandleHeight;
1130 switch ( mCurrentMouseMoveAction )
1137 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1157 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1178 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1197 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1218 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1220 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
1224 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
1227 mx = diffX, my = diffY;
1238 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1240 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1244 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1249 rx = diffX, ry = diffY;
1258 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1260 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1264 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
1268 my = diffY, rx = diffX, ry = -diffY;
1277 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1279 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
1283 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1314 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1315 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1318 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1319 translateLine.setAngle( translateLine.angle() - rotation() );
1320 QPointF sceneTranslate = translateLine.p2();
1323 QTransform itemTransform;
1324 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1325 setTransform( itemTransform );
1328 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1330 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1332 else if ( mBeginHandleHeight + ry >= 0 )
1334 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1336 else if ( mBeginHandleWidth + rx >= 0 )
1338 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1342 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1345 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1346 QRectF newHandleBounds;
1347 for ( QGraphicsItem *item : selectedItems )
1350 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1352 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1354 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1355 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1358 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1361 showStatusMessage( tr(
"width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1364void QgsGraphicsViewMouseHandles::setHandleSize(
double size )
1369void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1373 mDoubleClickInProgress =
true;
1376QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1379 QPointF sceneMousePos = mapFromScene( cursorPos );
1381 switch ( mCurrentMouseMoveAction )
1385 return QSizeF( 0, sceneMousePos.y() );
1388 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1392 return QSizeF( sceneMousePos.x(), 0 );
1395 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1399 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1402 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1405 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1408 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1423QRectF QgsGraphicsViewMouseHandles::selectionBounds()
const
1426 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1427 auto itemIter = selectedItems.constBegin();
1430 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1433 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1435 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1441void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize,
const QRectF &boundsBefore,
const QRectF &boundsAfter )
1444 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1445 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1446 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1447 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1449 rectToResize.setRect( left, top, right - left, bottom - top );
1452double QgsGraphicsViewMouseHandles::relativePosition(
double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
1455 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1456 double c = afterMin - ( beforeMin * m );
1459 return m * position +
c;
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)