21#include <QGraphicsView>
22#include <QGraphicsSceneHoverEvent>
29QgsGraphicsViewMouseHandles::QgsGraphicsViewMouseHandles( QGraphicsView *view )
31 , QGraphicsRectItem( nullptr )
35 setAcceptHoverEvents(
true );
38void QgsGraphicsViewMouseHandles::paintInternal( QPainter *painter,
bool showHandles,
bool showStaticBoundingBoxes,
bool showTemporaryBoundingBoxes,
const QStyleOptionGraphicsItem *, QWidget * )
45 if ( showStaticBoundingBoxes )
48 double rectHandlerSize = rectHandlerBorderTolerance();
49 drawHandles( painter, rectHandlerSize );
52 if ( showTemporaryBoundingBoxes && ( mIsResizing || mIsDragging || showStaticBoundingBoxes ) )
55 drawSelectedItemBounds( painter );
59QRectF QgsGraphicsViewMouseHandles::storedItemRect( QGraphicsItem *item )
const
61 return itemRect( item );
64void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *,
double,
double )
69QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
74void QgsGraphicsViewMouseHandles::startMacroCommand(
const QString & )
79void QgsGraphicsViewMouseHandles::endMacroCommand()
84void QgsGraphicsViewMouseHandles::endItemCommand( QGraphicsItem * )
89void QgsGraphicsViewMouseHandles::createItemCommand( QGraphicsItem * )
94QPointF QgsGraphicsViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode,
bool,
bool )
99void QgsGraphicsViewMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
104void QgsGraphicsViewMouseHandles::drawHandles( QPainter *painter,
double rectHandlerSize )
107 QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
108 handlePen.setWidth( 0 );
109 painter->setPen( handlePen );
112 painter->setBrush( Qt::NoBrush );
113 painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
116 painter->setBrush( QColor( 255, 255, 255, 255 ) );
118 painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
120 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
122 painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
124 painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
126 painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
128 painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
130 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
132 painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
135void QgsGraphicsViewMouseHandles::drawSelectedItemBounds( QPainter *painter )
138 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
139 if ( selectedItems.isEmpty() )
144 QList< QGraphicsItem * > itemsToDraw;
145 expandItemList( selectedItems, itemsToDraw );
147 if ( itemsToDraw.size() <= 1 )
155 painter->setCompositionMode( QPainter::CompositionMode_Difference );
158 QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
159 selectedItemPen.setStyle( Qt::DashLine );
160 selectedItemPen.setWidth( 0 );
161 painter->setPen( selectedItemPen );
162 painter->setBrush( Qt::NoBrush );
164 for ( QGraphicsItem *item : std::as_const( itemsToDraw ) )
167 QPolygonF itemBounds;
168 if ( isDragging() && !itemIsLocked( item ) )
172 QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
175 itemSceneBounds.translate( transform().dx(), transform().dy() );
177 itemBounds = mapFromScene( itemSceneBounds );
179 else if ( isResizing() && !itemIsLocked( item ) )
183 QRectF thisItemRect = mapRectFromItem( item, itemRect( item ) );
185 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
186 itemBounds = QPolygonF( thisItemRect );
191 itemBounds = item->mapToItem(
this, itemRect( item ) );
197 path.addPolygon( itemBounds );
198 painter->drawPath( path );
202double QgsGraphicsViewMouseHandles::rectHandlerBorderTolerance()
209 double viewScaleFactor = mView->transform().m11();
212 double rectHandlerSize = mHandleSize / viewScaleFactor;
215 if ( rectHandlerSize > ( rect().width() / 3 ) )
217 rectHandlerSize = rect().width() / 3;
219 if ( rectHandlerSize > ( rect().height() / 3 ) )
221 rectHandlerSize = rect().height() / 3;
223 return rectHandlerSize;
226Qt::CursorShape QgsGraphicsViewMouseHandles::cursorForPosition( QPointF itemCoordPos )
228 QgsGraphicsViewMouseHandles::MouseAction mouseAction = mouseActionForPosition( itemCoordPos );
229 double normalizedRotation = std::fmod( rotation(), 360 );
230 if ( normalizedRotation < 0 )
232 normalizedRotation += 360;
234 switch ( mouseAction )
237 return Qt::ForbiddenCursor;
239 return Qt::SizeAllCursor;
243 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
245 return Qt::SizeVerCursor;
247 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
249 return Qt::SizeBDiagCursor;
251 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
253 return Qt::SizeHorCursor;
257 return Qt::SizeFDiagCursor;
262 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
264 return Qt::SizeHorCursor;
266 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
268 return Qt::SizeFDiagCursor;
270 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
272 return Qt::SizeVerCursor;
276 return Qt::SizeBDiagCursor;
280 case ResizeRightDown:
282 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
284 return Qt::SizeFDiagCursor;
286 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
288 return Qt::SizeVerCursor;
290 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
292 return Qt::SizeBDiagCursor;
296 return Qt::SizeHorCursor;
301 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
303 return Qt::SizeBDiagCursor;
305 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
307 return Qt::SizeHorCursor;
309 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
311 return Qt::SizeFDiagCursor;
315 return Qt::SizeVerCursor;
318 return Qt::ArrowCursor;
321 return Qt::ArrowCursor;
324QgsGraphicsViewMouseHandles::MouseAction QgsGraphicsViewMouseHandles::mouseActionForPosition( QPointF itemCoordPos )
326 bool nearLeftBorder =
false;
327 bool nearRightBorder =
false;
328 bool nearLowerBorder =
false;
329 bool nearUpperBorder =
false;
331 bool withinWidth =
false;
332 bool withinHeight =
false;
333 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() <= rect().width() )
337 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() <= rect().height() )
342 double borderTolerance = rectHandlerBorderTolerance();
344 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
346 nearLeftBorder =
true;
348 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
350 nearUpperBorder =
true;
352 if ( itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
354 nearRightBorder =
true;
356 if ( itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
358 nearLowerBorder =
true;
361 if ( nearLeftBorder && nearUpperBorder )
363 return QgsGraphicsViewMouseHandles::ResizeLeftUp;
365 else if ( nearLeftBorder && nearLowerBorder )
367 return QgsGraphicsViewMouseHandles::ResizeLeftDown;
369 else if ( nearRightBorder && nearUpperBorder )
371 return QgsGraphicsViewMouseHandles::ResizeRightUp;
373 else if ( nearRightBorder && nearLowerBorder )
375 return QgsGraphicsViewMouseHandles::ResizeRightDown;
377 else if ( nearLeftBorder && withinHeight )
379 return QgsGraphicsViewMouseHandles::ResizeLeft;
381 else if ( nearRightBorder && withinHeight )
383 return QgsGraphicsViewMouseHandles::ResizeRight;
385 else if ( nearUpperBorder && withinWidth )
387 return QgsGraphicsViewMouseHandles::ResizeUp;
389 else if ( nearLowerBorder && withinWidth )
391 return QgsGraphicsViewMouseHandles::ResizeDown;
395 QPointF scenePoint = mapToScene( itemCoordPos );
396 const QList<QGraphicsItem *> itemsAtCursorPos = sceneItemsAtPoint( scenePoint );
397 if ( itemsAtCursorPos.isEmpty() )
400 return QgsGraphicsViewMouseHandles::SelectItem;
402 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
404 if ( graphicsItem && graphicsItem->isSelected() )
407 return QgsGraphicsViewMouseHandles::MoveItem;
412 return QgsGraphicsViewMouseHandles::SelectItem;
415QgsGraphicsViewMouseHandles::MouseAction QgsGraphicsViewMouseHandles::mouseActionForScenePos( QPointF sceneCoordPos )
418 QPointF itemPos = mapFromScene( sceneCoordPos );
419 return mouseActionForPosition( itemPos );
422bool QgsGraphicsViewMouseHandles::shouldBlockEvent( QInputEvent * )
const
424 return mIsDragging || mIsResizing;
427void QgsGraphicsViewMouseHandles::startMove( QPointF sceneCoordPos )
430 mMouseMoveStartPos = sceneCoordPos;
432 mBeginMouseEventPos = sceneCoordPos;
433 mBeginHandlePos = scenePos();
434 mBeginHandleWidth = rect().width();
435 mBeginHandleHeight = rect().height();
436 mCurrentMouseMoveAction = MoveItem;
441 if ( mView->scene()->mouseGrabberItem() !=
this )
448void QgsGraphicsViewMouseHandles::selectedItemSizeChanged()
450 if ( !isDragging() && !isResizing() )
457void QgsGraphicsViewMouseHandles::selectedItemRotationChanged()
459 if ( !isDragging() && !isResizing() )
466void QgsGraphicsViewMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
468 setViewportCursor( cursorForPosition( event->pos() ) );
471void QgsGraphicsViewMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
474 setViewportCursor( Qt::ArrowCursor );
477void QgsGraphicsViewMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
479 if ( event->button() != Qt::LeftButton )
486 mMouseMoveStartPos =
event->lastScenePos();
488 mBeginMouseEventPos =
event->lastScenePos();
489 mBeginHandlePos = scenePos();
490 mBeginHandleWidth = rect().width();
491 mBeginHandleHeight = rect().height();
493 mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
497 if ( mCurrentMouseMoveAction == MoveItem )
502 else if ( mCurrentMouseMoveAction != SelectItem &&
503 mCurrentMouseMoveAction != NoAction )
507 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
510 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
515void QgsGraphicsViewMouseHandles::resetStatusBar()
517 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
518 int selectedCount = selectedItems.size();
522 showStatusMessage( tr(
"%n item(s) selected",
nullptr, selectedCount ) );
527 showStatusMessage( QString() );
531void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
538 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
540 else if ( isResizing() )
545 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
549void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
551 if ( event->button() != Qt::LeftButton )
561 QPointF mouseMoveStopPoint =
event->lastScenePos();
562 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
563 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
566 if ( std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min() )
575 if ( mCurrentMouseMoveAction == MoveItem )
578 startMacroCommand( tr(
"Move Items" ) );
580 QPointF mEndHandleMovePos = scenePos();
582 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
583 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
586 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
587 for ( QGraphicsItem *item : selectedItems )
589 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
595 createItemCommand( item );
596 moveItem( item, deltaX, deltaY );
597 endItemCommand( item );
601 else if ( mCurrentMouseMoveAction != NoAction )
604 startMacroCommand( tr(
"Resize Items" ) );
607 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
608 for ( QGraphicsItem *item : selectedItems )
610 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
615 createItemCommand( item );
618 if ( selectedItems.size() == 1 )
621 thisItemRect = mResizeRect;
626 thisItemRect = mapRectFromItem( item, itemRect( item ) );
627 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
630 thisItemRect = thisItemRect.normalized();
631 QPointF newPos = mapToScene( thisItemRect.topLeft() );
632 thisItemRect.moveTopLeft( newPos );
633 setItemRect( item, thisItemRect );
635 endItemCommand( item );
651 mCurrentMouseMoveAction = MoveItem;
659bool QgsGraphicsViewMouseHandles::selectionRotation(
double &rotation )
const
662 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
663 auto itemIter = selectedItems.constBegin();
666 double firstItemRotation = ( *itemIter )->rotation();
669 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
671 if ( !
qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
679 rotation = firstItemRotation;
683void QgsGraphicsViewMouseHandles::updateHandles()
688 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
689 if ( !selectedItems.isEmpty() )
695 if ( selectionRotation( rotation ) )
698 setRotation( rotation );
707 QRectF newHandleBounds = selectionBounds();
710 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
711 setPos( mapToScene( newHandleBounds.topLeft() ) );
724void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition,
bool lockMovement,
bool preventSnap )
732 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
733 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
736 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
738 QPointF snappedLeftPoint;
744 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
749 snappedLeftPoint = upperLeftPoint;
754 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
755 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
761 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
772 QTransform moveTransform;
773 moveTransform.translate( moveRectX, moveRectY );
774 setTransform( moveTransform );
776 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
777 for ( QGraphicsItem *item : selectedItems )
779 previewItemMove( item, moveRectX, moveRectY );
782 showStatusMessage( tr(
"dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
785void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition,
bool lockRatio,
bool fromCenter )
792 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
794 QPointF beginMousePos;
795 QPointF finalPosition;
800 bool snapVertical = mCurrentMouseMoveAction == ResizeLeft ||
801 mCurrentMouseMoveAction == ResizeRight ||
802 mCurrentMouseMoveAction == ResizeLeftUp ||
803 mCurrentMouseMoveAction == ResizeRightUp ||
804 mCurrentMouseMoveAction == ResizeLeftDown ||
805 mCurrentMouseMoveAction == ResizeRightDown;
807 bool snapHorizontal = mCurrentMouseMoveAction == ResizeUp ||
808 mCurrentMouseMoveAction == ResizeDown ||
809 mCurrentMouseMoveAction == ResizeLeftUp ||
810 mCurrentMouseMoveAction == ResizeRightUp ||
811 mCurrentMouseMoveAction == ResizeLeftDown ||
812 mCurrentMouseMoveAction == ResizeRightDown;
816 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
817 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
818 finalPosition = mapFromScene( snappedPosition );
823 beginMousePos = mapFromScene( mBeginMouseEventPos );
824 finalPosition = mapFromScene( currentPosition );
827 double diffX = finalPosition.x() - beginMousePos.x();
828 double diffY = finalPosition.y() - beginMousePos.y();
831 if ( lockRatio && !
qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
833 ratio = mBeginHandleWidth / mBeginHandleHeight;
836 switch ( mCurrentMouseMoveAction )
843 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
863 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
884 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
903 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
924 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
926 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
930 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
933 mx = diffX, my = diffY;
939 case ResizeRightDown:
944 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
946 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
950 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
955 rx = diffX, ry = diffY;
964 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
966 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
970 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
974 my = diffY, rx = diffX, ry = -diffY;
983 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
985 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
989 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1016 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1017 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1020 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1021 translateLine.setAngle( translateLine.angle() - rotation() );
1022 QPointF sceneTranslate = translateLine.p2();
1025 QTransform itemTransform;
1026 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1027 setTransform( itemTransform );
1030 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1032 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1034 else if ( mBeginHandleHeight + ry >= 0 )
1036 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1038 else if ( mBeginHandleWidth + rx >= 0 )
1040 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1044 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1047 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1048 QRectF newHandleBounds;
1049 for ( QGraphicsItem *item : selectedItems )
1052 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1054 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1056 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1057 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1060 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1063 showStatusMessage( tr(
"width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1067void QgsGraphicsViewMouseHandles::setHandleSize(
double size )
1072void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1077QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1080 QPointF sceneMousePos = mapFromScene( cursorPos );
1082 switch ( mCurrentMouseMoveAction )
1085 case QgsGraphicsViewMouseHandles::ResizeUp:
1086 return QSizeF( 0, sceneMousePos.y() );
1088 case QgsGraphicsViewMouseHandles::ResizeDown:
1089 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1092 case QgsGraphicsViewMouseHandles::ResizeLeft:
1093 return QSizeF( sceneMousePos.x(), 0 );
1095 case QgsGraphicsViewMouseHandles::ResizeRight:
1096 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1099 case QgsGraphicsViewMouseHandles::ResizeLeftUp:
1100 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1102 case QgsGraphicsViewMouseHandles::ResizeRightDown:
1103 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1105 case QgsGraphicsViewMouseHandles::ResizeRightUp:
1106 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1108 case QgsGraphicsViewMouseHandles::ResizeLeftDown:
1109 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1120QRectF QgsGraphicsViewMouseHandles::selectionBounds()
const
1123 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1124 auto itemIter = selectedItems.constBegin();
1127 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1130 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1132 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1138void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize,
const QRectF &boundsBefore,
const QRectF &boundsAfter )
1141 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1142 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1143 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1144 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1146 rectToResize.setRect( left, top, right - left, bottom - top );
1149double QgsGraphicsViewMouseHandles::relativePosition(
double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
1152 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1153 double c = afterMin - ( beforeMin * m );
1156 return m * position +
c;
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)