26#include <QGraphicsSceneHoverEvent>
27#include <QGraphicsView>
31#include "moc_qgsgraphicsviewmousehandles.cpp"
35QgsGraphicsViewMouseHandles::QgsGraphicsViewMouseHandles( QGraphicsView *view )
37 , QGraphicsRectItem( nullptr )
41 setAcceptHoverEvents(
true );
44 mRotationHandlePath.moveTo( 0, 14 );
45 mRotationHandlePath.lineTo( 6, 20 );
46 mRotationHandlePath.lineTo( 12, 14 );
47 mRotationHandlePath.arcTo( 8, 8, 12, 12, 180, -90 );
48 mRotationHandlePath.lineTo( 14, 12 );
49 mRotationHandlePath.lineTo( 20, 6 );
50 mRotationHandlePath.lineTo( 14, 0 );
51 mRotationHandlePath.arcTo( 4, 4, 20, 20, 90, 90 );
52 mRotationHandlePath.lineTo( 0, 14 );
55void QgsGraphicsViewMouseHandles::setRotationEnabled(
bool enable )
57 if ( mRotationEnabled == enable )
62 mRotationEnabled = enable;
66void QgsGraphicsViewMouseHandles::paintInternal( QPainter *painter,
bool showHandles,
bool showStaticBoundingBoxes,
bool showTemporaryBoundingBoxes,
const QStyleOptionGraphicsItem *, QWidget * )
73 if ( showStaticBoundingBoxes )
76 double rectHandlerSize = rectHandlerBorderTolerance();
77 drawHandles( painter, rectHandlerSize );
80 if ( showTemporaryBoundingBoxes && ( mIsResizing || mIsDragging || showStaticBoundingBoxes ) )
83 drawSelectedItemBounds( painter );
87QRectF QgsGraphicsViewMouseHandles::storedItemRect( QGraphicsItem *item )
const
89 return itemRect( item );
92void QgsGraphicsViewMouseHandles::rotateItem( QGraphicsItem *,
double,
double,
double )
94 QgsDebugError( QStringLiteral(
"Rotation is not implemented for this class" ) );
97void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *,
double,
double )
101QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
106void QgsGraphicsViewMouseHandles::startMacroCommand(
const QString & )
110void QgsGraphicsViewMouseHandles::endMacroCommand()
114void QgsGraphicsViewMouseHandles::endItemCommand( QGraphicsItem * )
118void QgsGraphicsViewMouseHandles::createItemCommand( QGraphicsItem * )
122QPointF QgsGraphicsViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode,
bool,
bool )
124 return originalPoint;
127void QgsGraphicsViewMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
132void QgsGraphicsViewMouseHandles::drawHandles( QPainter *painter,
double rectHandlerSize )
135 QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
136 handlePen.setWidth( 0 );
137 painter->setPen( handlePen );
140 painter->setBrush( Qt::NoBrush );
141 painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
144 painter->setBrush( QColor( 255, 255, 255, 255 ) );
146 painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
148 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
150 painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
152 painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
154 painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
156 painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
158 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
160 painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
162 if ( isRotationEnabled() )
165 const double scale = rectHandlerSize / mHandleSize;
166 const bool drawBottomRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().height();
167 const bool drawRightRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().width();
168 QTransform transform;
172 transform.translate( rectHandlerSize, rectHandlerSize );
173 transform.scale( scale, scale );
175 painter->setTransform( transform,
true );
176 painter->drawPath( mRotationHandlePath );
180 if ( drawRightRotationHandles )
183 transform.translate( rect().width() - rectHandlerSize, rectHandlerSize );
184 transform.rotate( 90 );
185 transform.scale( scale, scale );
187 painter->setTransform( transform,
true );
188 painter->drawPath( mRotationHandlePath );
192 if ( drawBottomRotationHandles )
196 transform.translate( rectHandlerSize, rect().height() - rectHandlerSize );
197 transform.rotate( 270 );
198 transform.scale( scale, scale );
200 painter->setTransform( transform,
true );
201 painter->drawPath( mRotationHandlePath );
205 if ( drawBottomRotationHandles && drawRightRotationHandles )
209 transform.translate( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize );
210 transform.rotate( 180 );
211 transform.scale( scale, scale );
213 painter->setTransform( transform,
true );
214 painter->drawPath( mRotationHandlePath );
220void QgsGraphicsViewMouseHandles::drawSelectedItemBounds( QPainter *painter )
223 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
224 if ( selectedItems.isEmpty() )
229 QList<QGraphicsItem *> itemsToDraw;
230 expandItemList( selectedItems, itemsToDraw );
232 if ( itemsToDraw.size() <= 1 )
240 painter->setCompositionMode( QPainter::CompositionMode_Difference );
243 QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
244 selectedItemPen.setStyle( Qt::DashLine );
245 selectedItemPen.setWidth( 0 );
246 painter->setPen( selectedItemPen );
247 painter->setBrush( Qt::NoBrush );
249 for ( QGraphicsItem *item : std::as_const( itemsToDraw ) )
252 QPolygonF itemBounds;
253 if ( isDragging() && !itemIsLocked( item ) )
257 QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
260 itemSceneBounds.translate( transform().dx(), transform().dy() );
262 itemBounds = mapFromScene( itemSceneBounds );
264 else if ( isResizing() && !itemIsLocked( item ) )
268 QRectF thisItemRect = mapRectFromItem( item, itemRect( item ) );
270 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
271 itemBounds = QPolygonF( thisItemRect );
273 else if ( isRotating() && !itemIsLocked( item ) )
275 const QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
276 const QPointF rotationCenter = sceneTransform().map( rect().center() );
278 QTransform transform;
279 transform.translate( rotationCenter.x(), rotationCenter.y() );
280 transform.rotate( mRotationDelta );
281 transform.translate( -rotationCenter.x(), -rotationCenter.y() );
282 itemBounds = mapFromScene( transform.map( itemSceneBounds ) );
287 itemBounds = item->mapToItem(
this, itemRect( item ) );
293 path.addPolygon( itemBounds );
294 painter->drawPath( path );
298double QgsGraphicsViewMouseHandles::rectHandlerBorderTolerance()
const
305 double viewScaleFactor = mView->transform().m11();
308 double rectHandlerSize = mHandleSize / viewScaleFactor;
311 if ( rectHandlerSize > ( rect().width() / 3 ) )
313 rectHandlerSize = rect().width() / 3;
315 if ( rectHandlerSize > ( rect().height() / 3 ) )
317 rectHandlerSize = rect().height() / 3;
319 return rectHandlerSize;
322Qt::CursorShape QgsGraphicsViewMouseHandles::cursorForPosition( QPointF itemCoordPos )
325 double normalizedRotation = std::fmod( rotation(), 360 );
326 if ( normalizedRotation < 0 )
328 normalizedRotation += 360;
330 switch ( mouseAction )
333 return Qt::ForbiddenCursor;
335 return Qt::SizeAllCursor;
339 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
341 return Qt::SizeVerCursor;
343 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
345 return Qt::SizeBDiagCursor;
347 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
349 return Qt::SizeHorCursor;
353 return Qt::SizeFDiagCursor;
358 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
360 return Qt::SizeHorCursor;
362 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
364 return Qt::SizeFDiagCursor;
366 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
368 return Qt::SizeVerCursor;
372 return Qt::SizeBDiagCursor;
378 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
380 return Qt::SizeFDiagCursor;
382 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
384 return Qt::SizeVerCursor;
386 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
388 return Qt::SizeBDiagCursor;
392 return Qt::SizeHorCursor;
397 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
399 return Qt::SizeBDiagCursor;
401 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
403 return Qt::SizeHorCursor;
405 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
407 return Qt::SizeFDiagCursor;
411 return Qt::SizeVerCursor;
414 return Qt::ArrowCursor;
420 return Qt::PointingHandCursor;
423 return Qt::ArrowCursor;
428 bool nearLeftBorder =
false;
429 bool nearRightBorder =
false;
430 bool nearLowerBorder =
false;
431 bool nearUpperBorder =
false;
433 bool nearLeftInner =
false;
434 bool nearRightInner =
false;
435 bool nearLowerInner =
false;
436 bool nearUpperInner =
false;
438 bool withinWidth =
false;
439 bool withinHeight =
false;
441 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() <= rect().width() )
445 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() <= rect().height() )
450 double borderTolerance = rectHandlerBorderTolerance();
451 double innerTolerance = mRotationHandleSize * borderTolerance / mHandleSize;
453 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
455 nearLeftBorder =
true;
457 else if ( isRotationEnabled() && itemCoordPos.x() >= borderTolerance && itemCoordPos.x() < ( borderTolerance + innerTolerance ) )
459 nearLeftInner =
true;
461 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
463 nearUpperBorder =
true;
465 else if ( isRotationEnabled() && itemCoordPos.y() >= borderTolerance && itemCoordPos.y() < ( borderTolerance + innerTolerance ) )
467 nearUpperInner =
true;
469 if ( itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
471 nearRightBorder =
true;
473 else if ( isRotationEnabled() && itemCoordPos.x() <= ( rect().width() - borderTolerance ) && itemCoordPos.x() > ( rect().width() - borderTolerance - innerTolerance ) )
475 nearRightInner =
true;
477 if ( itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
479 nearLowerBorder =
true;
481 else if ( isRotationEnabled() && itemCoordPos.y() <= ( rect().height() - borderTolerance ) && itemCoordPos.y() > ( rect().height() - borderTolerance - innerTolerance ) )
483 nearLowerInner =
true;
486 if ( nearLeftBorder && nearUpperBorder )
490 else if ( nearLeftBorder && nearLowerBorder )
494 else if ( nearRightBorder && nearUpperBorder )
498 else if ( nearRightBorder && nearLowerBorder )
502 else if ( nearLeftBorder && withinHeight )
506 else if ( nearRightBorder && withinHeight )
510 else if ( nearUpperBorder && withinWidth )
514 else if ( nearLowerBorder && withinWidth )
518 else if ( nearLeftInner && nearUpperInner )
522 else if ( nearRightInner && nearUpperInner )
526 else if ( nearLeftInner && nearLowerInner )
530 else if ( nearRightInner && nearLowerInner )
536 QPointF scenePoint = mapToScene( itemCoordPos );
537 const QList<QGraphicsItem *> itemsAtCursorPos = sceneItemsAtPoint( scenePoint );
538 if ( itemsAtCursorPos.isEmpty() )
543 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
545 if ( graphicsItem && graphicsItem->isSelected() )
559 QPointF itemPos = mapFromScene( sceneCoordPos );
560 return mouseActionForPosition( itemPos );
563bool QgsGraphicsViewMouseHandles::shouldBlockEvent( QInputEvent * )
const
565 return mIsDragging || mIsResizing;
568void QgsGraphicsViewMouseHandles::startMove( QPointF sceneCoordPos )
571 mMouseMoveStartPos = sceneCoordPos;
573 mBeginMouseEventPos = sceneCoordPos;
574 mBeginHandlePos = scenePos();
575 mBeginHandleWidth = rect().width();
576 mBeginHandleHeight = rect().height();
582 if ( mView->scene()->mouseGrabberItem() !=
this )
588void QgsGraphicsViewMouseHandles::selectedItemSizeChanged()
590 if ( !isDragging() && !isResizing() )
597void QgsGraphicsViewMouseHandles::selectedItemRotationChanged()
599 if ( !isDragging() && !isResizing() )
606void QgsGraphicsViewMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
608 setViewportCursor( cursorForPosition( event->pos() ) );
611void QgsGraphicsViewMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
614 setViewportCursor( Qt::ArrowCursor );
617void QgsGraphicsViewMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
619 if ( event->button() != Qt::LeftButton )
626 mMouseMoveStartPos =
event->lastScenePos();
628 mBeginMouseEventPos =
event->lastScenePos();
629 mBeginHandlePos = scenePos();
630 mBeginHandleWidth = rect().width();
631 mBeginHandleHeight = rect().height();
633 mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
637 switch ( mCurrentMouseMoveAction )
654 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
657 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
665 mRotationCenter = sceneTransform().map( rect().center() );
666 mRotationBegin = std::atan2( mMouseMoveStartPos.y() - mRotationCenter.y(), mMouseMoveStartPos.x() - mRotationCenter.x() ) * 180 / M_PI;
667 mRotationCurrent = 0.0;
676void QgsGraphicsViewMouseHandles::resetStatusBar()
678 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
679 int selectedCount = selectedItems.size();
683 showStatusMessage( tr(
"%n item(s) selected",
nullptr, selectedCount ) );
688 showStatusMessage( QString() );
692void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
699 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
701 else if ( isResizing() )
706 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
708 else if ( isRotating() )
712 rotateMouseMove( event->lastScenePos(), event->modifiers() & Qt::ControlModifier );
716void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
718 if ( event->button() != Qt::LeftButton )
724 if ( mDoubleClickInProgress )
726 mDoubleClickInProgress =
false;
735 QPointF mouseMoveStopPoint =
event->lastScenePos();
736 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
737 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
740 if ( std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min() )
753 startMacroCommand( tr(
"Move Items" ) );
755 QPointF mEndHandleMovePos = scenePos();
757 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
758 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
761 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
762 for ( QGraphicsItem *item : selectedItems )
764 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
770 createItemCommand( item );
771 moveItem( item, deltaX, deltaY );
772 endItemCommand( item );
778 else if ( mIsResizing )
781 startMacroCommand( tr(
"Resize Items" ) );
784 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
785 for ( QGraphicsItem *item : selectedItems )
787 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
792 createItemCommand( item );
795 if ( selectedItems.size() == 1 )
798 thisItemRect = mResizeRect;
803 thisItemRect = mapRectFromItem( item, itemRect( item ) );
804 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
807 thisItemRect = thisItemRect.normalized();
808 QPointF newPos = mapToScene( thisItemRect.topLeft() );
809 thisItemRect.moveTopLeft( newPos );
810 setItemRect( item, thisItemRect );
812 endItemCommand( item );
818 else if ( mIsRotating )
820 const QPointF itemRotationCenter = sceneTransform().map( rect().center() );
823 startMacroCommand( tr(
"Rotate Items" ) );
826 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
827 for ( QGraphicsItem *item : selectedItems )
829 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
835 const QPointF itemCenter = item->mapToScene( itemRect( item ) ).boundingRect().center();
837 QTransform transform;
838 transform.translate( itemRotationCenter.x(), itemRotationCenter.y() );
839 transform.rotate( mRotationDelta );
840 transform.translate( -itemRotationCenter.x(), -itemRotationCenter.y() );
841 const QPointF rotatedItemCenter = transform.map( itemCenter );
843 createItemCommand( item );
844 rotateItem( item, mRotationDelta, rotatedItemCenter.x() - itemCenter.x(), rotatedItemCenter.y() - itemCenter.y() );
845 endItemCommand( item );
863bool QgsGraphicsViewMouseHandles::selectionRotation(
double &rotation )
const
866 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
867 auto itemIter = selectedItems.constBegin();
870 double firstItemRotation = ( *itemIter )->rotation();
873 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
875 if ( !
qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
883 rotation = firstItemRotation;
887void QgsGraphicsViewMouseHandles::updateHandles()
892 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
893 if ( !selectedItems.isEmpty() )
899 if ( selectionRotation( rotation ) )
902 setRotation( rotation );
911 QRectF newHandleBounds = selectionBounds();
914 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
915 setPos( mapToScene( newHandleBounds.topLeft() ) );
928void QgsGraphicsViewMouseHandles::rotateMouseMove( QPointF currentPosition,
bool snapToCommonAngles )
935 mRotationCurrent = std::atan2( currentPosition.y() - mRotationCenter.y(), currentPosition.x() - mRotationCenter.x() ) * 180 / M_PI;
936 mRotationDelta = mRotationCurrent - mRotationBegin;
937 if ( snapToCommonAngles )
942 const double itemRotationRadian = rotation() * M_PI / 180;
943 const double deltaX = ( rect().width() / 2 ) * cos( itemRotationRadian ) - ( rect().height() / 2 ) * sin( itemRotationRadian );
944 const double deltaY = ( rect().width() / 2 ) * sin( itemRotationRadian ) + ( rect().height() / 2 ) * cos( itemRotationRadian );
946 QTransform rotateTransform;
947 rotateTransform.translate( deltaX, deltaY );
948 rotateTransform.rotate( mRotationDelta );
949 rotateTransform.translate( -deltaX, -deltaY );
950 setTransform( rotateTransform );
953 showStatusMessage( tr(
"rotation: %1°" ).arg( QString::number( mRotationDelta,
'f', 2 ) ) );
958void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition,
bool lockMovement,
bool preventSnap )
966 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
967 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
970 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
972 QPointF snappedLeftPoint;
978 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
983 snappedLeftPoint = upperLeftPoint;
988 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
989 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
995 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
1006 QTransform moveTransform;
1007 moveTransform.translate( moveRectX, moveRectY );
1008 setTransform( moveTransform );
1010 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1011 for ( QGraphicsItem *item : selectedItems )
1013 previewItemMove( item, moveRectX, moveRectY );
1016 showStatusMessage( tr(
"dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
1019void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition,
bool lockRatio,
bool fromCenter )
1026 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
1028 QPointF beginMousePos;
1029 QPointF finalPosition;
1040 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
1041 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
1042 finalPosition = mapFromScene( snappedPosition );
1047 beginMousePos = mapFromScene( mBeginMouseEventPos );
1048 finalPosition = mapFromScene( currentPosition );
1051 double diffX = finalPosition.x() - beginMousePos.x();
1052 double diffY = finalPosition.y() - beginMousePos.y();
1055 if ( lockRatio && !
qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
1057 ratio = mBeginHandleWidth / mBeginHandleHeight;
1060 switch ( mCurrentMouseMoveAction )
1067 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1087 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1108 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1127 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1148 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1150 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
1154 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
1157 mx = diffX, my = diffY;
1168 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1170 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1174 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1179 rx = diffX, ry = diffY;
1188 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1190 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1194 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
1198 my = diffY, rx = diffX, ry = -diffY;
1207 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1209 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
1213 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1244 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1245 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1248 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1249 translateLine.setAngle( translateLine.angle() - rotation() );
1250 QPointF sceneTranslate = translateLine.p2();
1253 QTransform itemTransform;
1254 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1255 setTransform( itemTransform );
1258 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1260 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1262 else if ( mBeginHandleHeight + ry >= 0 )
1264 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1266 else if ( mBeginHandleWidth + rx >= 0 )
1268 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1272 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1275 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1276 QRectF newHandleBounds;
1277 for ( QGraphicsItem *item : selectedItems )
1280 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1282 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1284 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1285 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1288 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1291 showStatusMessage( tr(
"width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1294void QgsGraphicsViewMouseHandles::setHandleSize(
double size )
1299void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1303 mDoubleClickInProgress =
true;
1306QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1309 QPointF sceneMousePos = mapFromScene( cursorPos );
1311 switch ( mCurrentMouseMoveAction )
1315 return QSizeF( 0, sceneMousePos.y() );
1318 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1322 return QSizeF( sceneMousePos.x(), 0 );
1325 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1329 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1332 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1335 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1338 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1353QRectF QgsGraphicsViewMouseHandles::selectionBounds()
const
1356 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1357 auto itemIter = selectedItems.constBegin();
1360 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1363 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1365 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1371void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize,
const QRectF &boundsBefore,
const QRectF &boundsAfter )
1374 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1375 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1376 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1377 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1379 rectToResize.setRect( left, top, right - left, bottom - top );
1382double QgsGraphicsViewMouseHandles::relativePosition(
double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
1385 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1386 double c = afterMin - ( beforeMin * m );
1389 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)