26#include <QGraphicsSceneHoverEvent>
27#include <QGraphicsView>
32#include "moc_qgsgraphicsviewmousehandles.cpp"
34using namespace Qt::StringLiterals;
38QgsGraphicsViewMouseHandles::QgsGraphicsViewMouseHandles( QGraphicsView *view )
40 , QGraphicsRectItem( nullptr )
44 setAcceptHoverEvents(
true );
47 mRotationHandlePath.moveTo( 0, 14 );
48 mRotationHandlePath.lineTo( 6, 20 );
49 mRotationHandlePath.lineTo( 12, 14 );
50 mRotationHandlePath.arcTo( 8, 8, 12, 12, 180, -90 );
51 mRotationHandlePath.lineTo( 14, 12 );
52 mRotationHandlePath.lineTo( 20, 6 );
53 mRotationHandlePath.lineTo( 14, 0 );
54 mRotationHandlePath.arcTo( 4, 4, 20, 20, 90, 90 );
55 mRotationHandlePath.lineTo( 0, 14 );
58void QgsGraphicsViewMouseHandles::setRotationEnabled(
bool enable )
60 if ( mRotationEnabled == enable )
65 mRotationEnabled = enable;
69void QgsGraphicsViewMouseHandles::setCadMouseDigitizingModeEnabled(
bool enable )
71 if ( mCadMouseDigitizingMode == enable )
76 mCadMouseDigitizingMode = enable;
79void QgsGraphicsViewMouseHandles::paintInternal( QPainter *painter,
bool showHandles,
bool showStaticBoundingBoxes,
bool showTemporaryBoundingBoxes,
const QStyleOptionGraphicsItem *, QWidget * )
86 if ( showStaticBoundingBoxes )
89 double rectHandlerSize = rectHandlerBorderTolerance();
90 drawHandles( painter, rectHandlerSize );
93 if ( showTemporaryBoundingBoxes && ( mIsResizing || mIsDragging || showStaticBoundingBoxes ) )
96 drawSelectedItemBounds( painter );
100QRectF QgsGraphicsViewMouseHandles::storedItemRect( QGraphicsItem *item )
const
102 return itemRect( item );
105void QgsGraphicsViewMouseHandles::rotateItem( QGraphicsItem *,
double,
double,
double )
107 QgsDebugError( u
"Rotation is not implemented for this class"_s );
110void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *,
double,
double )
113QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
118void QgsGraphicsViewMouseHandles::startMacroCommand(
const QString & )
121void QgsGraphicsViewMouseHandles::endMacroCommand()
124void QgsGraphicsViewMouseHandles::endItemCommand( QGraphicsItem * )
127void QgsGraphicsViewMouseHandles::createItemCommand( QGraphicsItem * )
130QPointF QgsGraphicsViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode,
bool,
bool )
132 return originalPoint;
135void QgsGraphicsViewMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
140void QgsGraphicsViewMouseHandles::drawHandles( QPainter *painter,
double rectHandlerSize )
143 QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
144 handlePen.setWidth( 0 );
145 painter->setPen( handlePen );
148 painter->setBrush( Qt::NoBrush );
149 painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
152 painter->setBrush( QColor( 255, 255, 255, 255 ) );
154 painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
156 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
158 painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
160 painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
162 painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
164 painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
166 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
168 painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
170 if ( isRotationEnabled() )
173 const double scale = rectHandlerSize / mHandleSize;
174 const bool drawBottomRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().height();
175 const bool drawRightRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().width();
176 QTransform transform;
180 transform.translate( rectHandlerSize, rectHandlerSize );
181 transform.scale( scale, scale );
183 painter->setTransform( transform,
true );
184 painter->drawPath( mRotationHandlePath );
188 if ( drawRightRotationHandles )
191 transform.translate( rect().width() - rectHandlerSize, rectHandlerSize );
192 transform.rotate( 90 );
193 transform.scale( scale, scale );
195 painter->setTransform( transform,
true );
196 painter->drawPath( mRotationHandlePath );
200 if ( drawBottomRotationHandles )
204 transform.translate( rectHandlerSize, rect().height() - rectHandlerSize );
205 transform.rotate( 270 );
206 transform.scale( scale, scale );
208 painter->setTransform( transform,
true );
209 painter->drawPath( mRotationHandlePath );
213 if ( drawBottomRotationHandles && drawRightRotationHandles )
217 transform.translate( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize );
218 transform.rotate( 180 );
219 transform.scale( scale, scale );
221 painter->setTransform( transform,
true );
222 painter->drawPath( mRotationHandlePath );
228void QgsGraphicsViewMouseHandles::drawSelectedItemBounds( QPainter *painter )
231 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
232 if ( selectedItems.isEmpty() )
237 QList<QGraphicsItem *> itemsToDraw;
238 expandItemList( selectedItems, itemsToDraw );
240 if ( itemsToDraw.size() <= 1 )
248 painter->setCompositionMode( QPainter::CompositionMode_Difference );
251 QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
252 selectedItemPen.setStyle( Qt::DashLine );
253 selectedItemPen.setWidth( 0 );
254 painter->setPen( selectedItemPen );
255 painter->setBrush( Qt::NoBrush );
257 for ( QGraphicsItem *item : std::as_const( itemsToDraw ) )
260 QPolygonF itemBounds;
261 if ( isDragging() && !itemIsLocked( item ) )
265 QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
268 itemSceneBounds.translate( transform().dx(), transform().dy() );
270 itemBounds = mapFromScene( itemSceneBounds );
272 else if ( isResizing() && !itemIsLocked( item ) )
276 QRectF thisItemRect = mapRectFromItem( item, itemRect( item ) );
278 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
279 itemBounds = QPolygonF( thisItemRect );
281 else if ( isRotating() && !itemIsLocked( item ) )
283 const QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
284 const QPointF rotationCenter = sceneTransform().map( rect().center() );
286 QTransform transform;
287 transform.translate( rotationCenter.x(), rotationCenter.y() );
288 transform.rotate( mRotationDelta );
289 transform.translate( -rotationCenter.x(), -rotationCenter.y() );
290 itemBounds = mapFromScene( transform.map( itemSceneBounds ) );
295 itemBounds = item->mapToItem(
this, itemRect( item ) );
301 path.addPolygon( itemBounds );
302 painter->drawPath( path );
306double QgsGraphicsViewMouseHandles::rectHandlerBorderTolerance()
const
313 double viewScaleFactor = mView->transform().m11();
316 double rectHandlerSize = mHandleSize / viewScaleFactor;
319 if ( rectHandlerSize > ( rect().width() / 3 ) )
321 rectHandlerSize = rect().width() / 3;
323 if ( rectHandlerSize > ( rect().height() / 3 ) )
325 rectHandlerSize = rect().height() / 3;
327 return rectHandlerSize;
330Qt::CursorShape QgsGraphicsViewMouseHandles::cursorForPosition( QPointF itemCoordPos )
333 double normalizedRotation = std::fmod( rotation(), 360 );
334 if ( normalizedRotation < 0 )
336 normalizedRotation += 360;
338 switch ( mouseAction )
341 return Qt::ForbiddenCursor;
343 return Qt::SizeAllCursor;
347 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
349 return Qt::SizeVerCursor;
351 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
353 return Qt::SizeBDiagCursor;
355 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
357 return Qt::SizeHorCursor;
361 return Qt::SizeFDiagCursor;
366 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
368 return Qt::SizeHorCursor;
370 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
372 return Qt::SizeFDiagCursor;
374 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
376 return Qt::SizeVerCursor;
380 return Qt::SizeBDiagCursor;
386 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
388 return Qt::SizeFDiagCursor;
390 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
392 return Qt::SizeVerCursor;
394 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
396 return Qt::SizeBDiagCursor;
400 return Qt::SizeHorCursor;
405 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
407 return Qt::SizeBDiagCursor;
409 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
411 return Qt::SizeHorCursor;
413 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
415 return Qt::SizeFDiagCursor;
419 return Qt::SizeVerCursor;
422 return Qt::ArrowCursor;
428 return Qt::PointingHandCursor;
431 return Qt::ArrowCursor;
436 bool nearLeftBorder =
false;
437 bool nearRightBorder =
false;
438 bool nearLowerBorder =
false;
439 bool nearUpperBorder =
false;
441 bool nearLeftInner =
false;
442 bool nearRightInner =
false;
443 bool nearLowerInner =
false;
444 bool nearUpperInner =
false;
446 bool withinWidth =
false;
447 bool withinHeight =
false;
449 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() <= rect().width() )
453 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() <= rect().height() )
458 double borderTolerance = rectHandlerBorderTolerance();
459 double innerTolerance = mRotationHandleSize * borderTolerance / mHandleSize;
461 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
463 nearLeftBorder =
true;
465 else if ( isRotationEnabled() && itemCoordPos.x() >= borderTolerance && itemCoordPos.x() < ( borderTolerance + innerTolerance ) )
467 nearLeftInner =
true;
469 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
471 nearUpperBorder =
true;
473 else if ( isRotationEnabled() && itemCoordPos.y() >= borderTolerance && itemCoordPos.y() < ( borderTolerance + innerTolerance ) )
475 nearUpperInner =
true;
477 if ( itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
479 nearRightBorder =
true;
481 else if ( isRotationEnabled() && itemCoordPos.x() <= ( rect().width() - borderTolerance ) && itemCoordPos.x() > ( rect().width() - borderTolerance - innerTolerance ) )
483 nearRightInner =
true;
485 if ( itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
487 nearLowerBorder =
true;
489 else if ( isRotationEnabled() && itemCoordPos.y() <= ( rect().height() - borderTolerance ) && itemCoordPos.y() > ( rect().height() - borderTolerance - innerTolerance ) )
491 nearLowerInner =
true;
494 if ( nearLeftBorder && nearUpperBorder )
498 else if ( nearLeftBorder && nearLowerBorder )
502 else if ( nearRightBorder && nearUpperBorder )
506 else if ( nearRightBorder && nearLowerBorder )
510 else if ( nearLeftBorder && withinHeight )
514 else if ( nearRightBorder && withinHeight )
518 else if ( nearUpperBorder && withinWidth )
522 else if ( nearLowerBorder && withinWidth )
526 else if ( nearLeftInner && nearUpperInner )
530 else if ( nearRightInner && nearUpperInner )
534 else if ( nearLeftInner && nearLowerInner )
538 else if ( nearRightInner && nearLowerInner )
544 QPointF scenePoint = mapToScene( itemCoordPos );
545 const QList<QGraphicsItem *> itemsAtCursorPos = sceneItemsAtPoint( scenePoint );
546 if ( itemsAtCursorPos.isEmpty() )
551 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
553 if ( graphicsItem && graphicsItem->isSelected() )
567 QPointF itemPos = mapFromScene( sceneCoordPos );
568 return mouseActionForPosition( itemPos );
571bool QgsGraphicsViewMouseHandles::shouldBlockEvent( QInputEvent * )
const
573 return mIsDragging || mIsResizing || mIsRotating;
576void QgsGraphicsViewMouseHandles::startMove( QPointF sceneCoordPos )
579 mMouseMoveStartPos = sceneCoordPos;
581 mBeginMouseEventPos = sceneCoordPos;
582 mBeginHandlePos = scenePos();
583 mBeginHandleWidth = rect().width();
584 mBeginHandleHeight = rect().height();
590 if ( mView->scene()->mouseGrabberItem() !=
this )
596void QgsGraphicsViewMouseHandles::selectedItemSizeChanged()
598 if ( !isDragging() && !isResizing() )
605void QgsGraphicsViewMouseHandles::selectedItemRotationChanged()
607 if ( !isDragging() && !isResizing() )
614void QgsGraphicsViewMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
616 setViewportCursor( cursorForPosition( event->pos() ) );
619void QgsGraphicsViewMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
622 setViewportCursor( Qt::ArrowCursor );
625void QgsGraphicsViewMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
627 if ( event->button() != Qt::LeftButton )
633 if ( mCadMouseDigitizingMode && ( mIsDragging || mIsResizing || mIsRotating ) )
639 mMouseMoveStartPos =
event->lastScenePos();
641 mBeginMouseEventPos =
event->lastScenePos();
642 mBeginHandlePos = scenePos();
643 mBeginHandleWidth = rect().width();
644 mBeginHandleHeight = rect().height();
646 mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
650 switch ( mCurrentMouseMoveAction )
667 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
670 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
678 mRotationCenter = sceneTransform().map( rect().center() );
679 mRotationBegin = std::atan2( mMouseMoveStartPos.y() - mRotationCenter.y(), mMouseMoveStartPos.x() - mRotationCenter.x() ) * 180 / M_PI;
680 mRotationCurrent = 0.0;
688 if ( mCadMouseDigitizingMode && mIsDragging )
690 mIsDragStarting =
true;
694void QgsGraphicsViewMouseHandles::resetStatusBar()
696 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
697 int selectedCount = selectedItems.size();
701 showStatusMessage( tr(
"%n item(s) selected",
nullptr, selectedCount ) );
706 showStatusMessage( QString() );
710void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
717 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
719 else if ( isResizing() )
724 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
726 else if ( isRotating() )
730 rotateMouseMove( event->lastScenePos(), event->modifiers() & Qt::ControlModifier );
734void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
736 if ( event->button() != Qt::LeftButton )
742 if ( mDoubleClickInProgress )
744 mDoubleClickInProgress =
false;
749 if ( mIsDragStarting )
751 mIsDragStarting =
false;
757 if ( mView->scene()->mouseGrabberItem() ==
this )
762 QPointF mouseMoveStopPoint =
event->lastScenePos();
763 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
764 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
766 const bool isClick = std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min();
780 startMacroCommand( tr(
"Move Items" ) );
782 QPointF mEndHandleMovePos = scenePos();
784 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
785 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
788 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
789 for ( QGraphicsItem *item : selectedItems )
791 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
797 createItemCommand( item );
798 moveItem( item, deltaX, deltaY );
799 endItemCommand( item );
805 else if ( mIsResizing )
808 startMacroCommand( tr(
"Resize Items" ) );
811 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
812 for ( QGraphicsItem *item : selectedItems )
814 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
819 createItemCommand( item );
822 if ( selectedItems.size() == 1 )
825 thisItemRect = mResizeRect;
830 thisItemRect = mapRectFromItem( item, itemRect( item ) );
831 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
834 thisItemRect = thisItemRect.normalized();
835 QPointF newPos = mapToScene( thisItemRect.topLeft() );
836 thisItemRect.moveTopLeft( newPos );
837 setItemRect( item, thisItemRect );
839 endItemCommand( item );
845 else if ( mIsRotating )
847 const QPointF itemRotationCenter = sceneTransform().map( rect().center() );
850 startMacroCommand( tr(
"Rotate Items" ) );
853 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
854 for ( QGraphicsItem *item : selectedItems )
856 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
862 const QPointF itemCenter = item->mapToScene( itemRect( item ) ).boundingRect().center();
864 QTransform transform;
865 transform.translate( itemRotationCenter.x(), itemRotationCenter.y() );
866 transform.rotate( mRotationDelta );
867 transform.translate( -itemRotationCenter.x(), -itemRotationCenter.y() );
868 const QPointF rotatedItemCenter = transform.map( itemCenter );
870 createItemCommand( item );
871 rotateItem( item, mRotationDelta, rotatedItemCenter.x() - itemCenter.x(), rotatedItemCenter.y() - itemCenter.y() );
872 endItemCommand( item );
891bool QgsGraphicsViewMouseHandles::selectionRotation(
double &rotation )
const
894 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
895 auto itemIter = selectedItems.constBegin();
898 double firstItemRotation = ( *itemIter )->rotation();
901 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
903 if ( !
qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
911 rotation = firstItemRotation;
915void QgsGraphicsViewMouseHandles::updateHandles()
920 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
921 if ( !selectedItems.isEmpty() )
927 if ( selectionRotation( rotation ) )
930 setRotation( rotation );
939 QRectF newHandleBounds = selectionBounds();
942 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
943 setPos( mapToScene( newHandleBounds.topLeft() ) );
957void QgsGraphicsViewMouseHandles::rotateMouseMove( QPointF currentPosition,
bool snapToCommonAngles )
964 mRotationCurrent = std::atan2( currentPosition.y() - mRotationCenter.y(), currentPosition.x() - mRotationCenter.x() ) * 180 / M_PI;
965 mRotationDelta = mRotationCurrent - mRotationBegin;
966 if ( snapToCommonAngles )
971 const double itemRotationRadian = rotation() * M_PI / 180;
972 const double deltaX = ( rect().width() / 2 ) * cos( itemRotationRadian ) - ( rect().height() / 2 ) * sin( itemRotationRadian );
973 const double deltaY = ( rect().width() / 2 ) * sin( itemRotationRadian ) + ( rect().height() / 2 ) * cos( itemRotationRadian );
975 QTransform rotateTransform;
976 rotateTransform.translate( deltaX, deltaY );
977 rotateTransform.rotate( mRotationDelta );
978 rotateTransform.translate( -deltaX, -deltaY );
979 setTransform( rotateTransform );
982 showStatusMessage( tr(
"rotation: %1°" ).arg( QString::number( mRotationDelta,
'f', 2 ) ) );
987void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition,
bool lockMovement,
bool preventSnap )
995 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
996 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
999 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
1001 QPointF snappedLeftPoint;
1007 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
1012 snappedLeftPoint = upperLeftPoint;
1017 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
1018 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
1024 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
1035 QTransform moveTransform;
1036 moveTransform.translate( moveRectX, moveRectY );
1037 setTransform( moveTransform );
1039 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1040 for ( QGraphicsItem *item : selectedItems )
1042 previewItemMove( item, moveRectX, moveRectY );
1045 showStatusMessage( tr(
"dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
1048void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition,
bool lockRatio,
bool fromCenter )
1055 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
1057 QPointF beginMousePos;
1058 QPointF finalPosition;
1079 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
1080 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
1081 finalPosition = mapFromScene( snappedPosition );
1086 beginMousePos = mapFromScene( mBeginMouseEventPos );
1087 finalPosition = mapFromScene( currentPosition );
1090 double diffX = finalPosition.x() - beginMousePos.x();
1091 double diffY = finalPosition.y() - beginMousePos.y();
1094 if ( lockRatio && !
qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
1096 ratio = mBeginHandleWidth / mBeginHandleHeight;
1099 switch ( mCurrentMouseMoveAction )
1106 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1126 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1147 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1166 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1187 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1189 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
1193 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
1196 mx = diffX, my = diffY;
1207 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1209 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1213 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1218 rx = diffX, ry = diffY;
1227 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1229 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1233 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
1237 my = diffY, rx = diffX, ry = -diffY;
1246 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1248 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
1252 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1283 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1284 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1287 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1288 translateLine.setAngle( translateLine.angle() - rotation() );
1289 QPointF sceneTranslate = translateLine.p2();
1292 QTransform itemTransform;
1293 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1294 setTransform( itemTransform );
1297 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1299 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1301 else if ( mBeginHandleHeight + ry >= 0 )
1303 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1305 else if ( mBeginHandleWidth + rx >= 0 )
1307 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1311 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1314 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1315 QRectF newHandleBounds;
1316 for ( QGraphicsItem *item : selectedItems )
1319 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1321 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1323 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1324 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1327 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1330 showStatusMessage( tr(
"width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1333void QgsGraphicsViewMouseHandles::setHandleSize(
double size )
1338void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1342 mDoubleClickInProgress =
true;
1345QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1348 QPointF sceneMousePos = mapFromScene( cursorPos );
1350 switch ( mCurrentMouseMoveAction )
1354 return QSizeF( 0, sceneMousePos.y() );
1357 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1361 return QSizeF( sceneMousePos.x(), 0 );
1364 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1368 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1371 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1374 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1377 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1392QRectF QgsGraphicsViewMouseHandles::selectionBounds()
const
1395 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1396 auto itemIter = selectedItems.constBegin();
1399 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1402 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1404 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1410void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize,
const QRectF &boundsBefore,
const QRectF &boundsAfter )
1413 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1414 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1415 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1416 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1418 rectToResize.setRect( left, top, right - left, bottom - top );
1421double QgsGraphicsViewMouseHandles::relativePosition(
double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
1424 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1425 double c = afterMin - ( beforeMin * m );
1428 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.
Scoped object for saving and restoring a QPainter object's state.
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)