19#include "moc_qgsgraphicsviewmousehandles.cpp"
22#include <QGraphicsView>
23#include <QGraphicsSceneHoverEvent>
30QgsGraphicsViewMouseHandles::QgsGraphicsViewMouseHandles( QGraphicsView *view )
32 , QGraphicsRectItem( nullptr )
36 setAcceptHoverEvents(
true );
39void QgsGraphicsViewMouseHandles::paintInternal( QPainter *painter,
bool showHandles,
bool showStaticBoundingBoxes,
bool showTemporaryBoundingBoxes,
const QStyleOptionGraphicsItem *, QWidget * )
46 if ( showStaticBoundingBoxes )
49 double rectHandlerSize = rectHandlerBorderTolerance();
50 drawHandles( painter, rectHandlerSize );
53 if ( showTemporaryBoundingBoxes && ( mIsResizing || mIsDragging || showStaticBoundingBoxes ) )
56 drawSelectedItemBounds( painter );
60QRectF QgsGraphicsViewMouseHandles::storedItemRect( QGraphicsItem *item )
const
62 return itemRect( item );
65void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *,
double,
double )
70QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
75void QgsGraphicsViewMouseHandles::startMacroCommand(
const QString & )
80void QgsGraphicsViewMouseHandles::endMacroCommand()
85void QgsGraphicsViewMouseHandles::endItemCommand( QGraphicsItem * )
90void QgsGraphicsViewMouseHandles::createItemCommand( QGraphicsItem * )
95QPointF QgsGraphicsViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode,
bool,
bool )
100void QgsGraphicsViewMouseHandles::expandItemList(
const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected )
const
105void QgsGraphicsViewMouseHandles::drawHandles( QPainter *painter,
double rectHandlerSize )
108 QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
109 handlePen.setWidth( 0 );
110 painter->setPen( handlePen );
113 painter->setBrush( Qt::NoBrush );
114 painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
117 painter->setBrush( QColor( 255, 255, 255, 255 ) );
119 painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
121 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
123 painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
125 painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
127 painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
129 painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
131 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
133 painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
136void QgsGraphicsViewMouseHandles::drawSelectedItemBounds( QPainter *painter )
139 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
140 if ( selectedItems.isEmpty() )
145 QList< QGraphicsItem * > itemsToDraw;
146 expandItemList( selectedItems, itemsToDraw );
148 if ( itemsToDraw.size() <= 1 )
156 painter->setCompositionMode( QPainter::CompositionMode_Difference );
159 QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
160 selectedItemPen.setStyle( Qt::DashLine );
161 selectedItemPen.setWidth( 0 );
162 painter->setPen( selectedItemPen );
163 painter->setBrush( Qt::NoBrush );
165 for ( QGraphicsItem *item : std::as_const( itemsToDraw ) )
168 QPolygonF itemBounds;
169 if ( isDragging() && !itemIsLocked( item ) )
173 QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
176 itemSceneBounds.translate( transform().dx(), transform().dy() );
178 itemBounds = mapFromScene( itemSceneBounds );
180 else if ( isResizing() && !itemIsLocked( item ) )
184 QRectF thisItemRect = mapRectFromItem( item, itemRect( item ) );
186 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
187 itemBounds = QPolygonF( thisItemRect );
192 itemBounds = item->mapToItem(
this, itemRect( item ) );
198 path.addPolygon( itemBounds );
199 painter->drawPath( path );
203double QgsGraphicsViewMouseHandles::rectHandlerBorderTolerance()
210 double viewScaleFactor = mView->transform().m11();
213 double rectHandlerSize = mHandleSize / viewScaleFactor;
216 if ( rectHandlerSize > ( rect().width() / 3 ) )
218 rectHandlerSize = rect().width() / 3;
220 if ( rectHandlerSize > ( rect().height() / 3 ) )
222 rectHandlerSize = rect().height() / 3;
224 return rectHandlerSize;
227Qt::CursorShape QgsGraphicsViewMouseHandles::cursorForPosition( QPointF itemCoordPos )
229 QgsGraphicsViewMouseHandles::MouseAction mouseAction = mouseActionForPosition( itemCoordPos );
230 double normalizedRotation = std::fmod( rotation(), 360 );
231 if ( normalizedRotation < 0 )
233 normalizedRotation += 360;
235 switch ( mouseAction )
238 return Qt::ForbiddenCursor;
240 return Qt::SizeAllCursor;
244 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
246 return Qt::SizeVerCursor;
248 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
250 return Qt::SizeBDiagCursor;
252 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
254 return Qt::SizeHorCursor;
258 return Qt::SizeFDiagCursor;
263 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
265 return Qt::SizeHorCursor;
267 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
269 return Qt::SizeFDiagCursor;
271 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
273 return Qt::SizeVerCursor;
277 return Qt::SizeBDiagCursor;
281 case ResizeRightDown:
283 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
285 return Qt::SizeFDiagCursor;
287 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
289 return Qt::SizeVerCursor;
291 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
293 return Qt::SizeBDiagCursor;
297 return Qt::SizeHorCursor;
302 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
304 return Qt::SizeBDiagCursor;
306 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
308 return Qt::SizeHorCursor;
310 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
312 return Qt::SizeFDiagCursor;
316 return Qt::SizeVerCursor;
319 return Qt::ArrowCursor;
322 return Qt::ArrowCursor;
325QgsGraphicsViewMouseHandles::MouseAction QgsGraphicsViewMouseHandles::mouseActionForPosition( QPointF itemCoordPos )
327 bool nearLeftBorder =
false;
328 bool nearRightBorder =
false;
329 bool nearLowerBorder =
false;
330 bool nearUpperBorder =
false;
332 bool withinWidth =
false;
333 bool withinHeight =
false;
334 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() <= rect().width() )
338 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() <= rect().height() )
343 double borderTolerance = rectHandlerBorderTolerance();
345 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
347 nearLeftBorder =
true;
349 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
351 nearUpperBorder =
true;
353 if ( itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
355 nearRightBorder =
true;
357 if ( itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
359 nearLowerBorder =
true;
362 if ( nearLeftBorder && nearUpperBorder )
364 return QgsGraphicsViewMouseHandles::ResizeLeftUp;
366 else if ( nearLeftBorder && nearLowerBorder )
368 return QgsGraphicsViewMouseHandles::ResizeLeftDown;
370 else if ( nearRightBorder && nearUpperBorder )
372 return QgsGraphicsViewMouseHandles::ResizeRightUp;
374 else if ( nearRightBorder && nearLowerBorder )
376 return QgsGraphicsViewMouseHandles::ResizeRightDown;
378 else if ( nearLeftBorder && withinHeight )
380 return QgsGraphicsViewMouseHandles::ResizeLeft;
382 else if ( nearRightBorder && withinHeight )
384 return QgsGraphicsViewMouseHandles::ResizeRight;
386 else if ( nearUpperBorder && withinWidth )
388 return QgsGraphicsViewMouseHandles::ResizeUp;
390 else if ( nearLowerBorder && withinWidth )
392 return QgsGraphicsViewMouseHandles::ResizeDown;
396 QPointF scenePoint = mapToScene( itemCoordPos );
397 const QList<QGraphicsItem *> itemsAtCursorPos = sceneItemsAtPoint( scenePoint );
398 if ( itemsAtCursorPos.isEmpty() )
401 return QgsGraphicsViewMouseHandles::SelectItem;
403 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
405 if ( graphicsItem && graphicsItem->isSelected() )
408 return QgsGraphicsViewMouseHandles::MoveItem;
413 return QgsGraphicsViewMouseHandles::SelectItem;
416QgsGraphicsViewMouseHandles::MouseAction QgsGraphicsViewMouseHandles::mouseActionForScenePos( QPointF sceneCoordPos )
419 QPointF itemPos = mapFromScene( sceneCoordPos );
420 return mouseActionForPosition( itemPos );
423bool QgsGraphicsViewMouseHandles::shouldBlockEvent( QInputEvent * )
const
425 return mIsDragging || mIsResizing;
428void QgsGraphicsViewMouseHandles::startMove( QPointF sceneCoordPos )
431 mMouseMoveStartPos = sceneCoordPos;
433 mBeginMouseEventPos = sceneCoordPos;
434 mBeginHandlePos = scenePos();
435 mBeginHandleWidth = rect().width();
436 mBeginHandleHeight = rect().height();
437 mCurrentMouseMoveAction = MoveItem;
442 if ( mView->scene()->mouseGrabberItem() !=
this )
449void QgsGraphicsViewMouseHandles::selectedItemSizeChanged()
451 if ( !isDragging() && !isResizing() )
458void QgsGraphicsViewMouseHandles::selectedItemRotationChanged()
460 if ( !isDragging() && !isResizing() )
467void QgsGraphicsViewMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
469 setViewportCursor( cursorForPosition( event->pos() ) );
472void QgsGraphicsViewMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
475 setViewportCursor( Qt::ArrowCursor );
478void QgsGraphicsViewMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
480 if ( event->button() != Qt::LeftButton )
487 mMouseMoveStartPos =
event->lastScenePos();
489 mBeginMouseEventPos =
event->lastScenePos();
490 mBeginHandlePos = scenePos();
491 mBeginHandleWidth = rect().width();
492 mBeginHandleHeight = rect().height();
494 mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
498 if ( mCurrentMouseMoveAction == MoveItem )
503 else if ( mCurrentMouseMoveAction != SelectItem &&
504 mCurrentMouseMoveAction != NoAction )
508 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
511 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
516void QgsGraphicsViewMouseHandles::resetStatusBar()
518 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
519 int selectedCount = selectedItems.size();
523 showStatusMessage( tr(
"%n item(s) selected",
nullptr, selectedCount ) );
528 showStatusMessage( QString() );
532void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
539 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
541 else if ( isResizing() )
546 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
550void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
552 if ( event->button() != Qt::LeftButton )
562 QPointF mouseMoveStopPoint =
event->lastScenePos();
563 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
564 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
567 if ( std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min() )
576 if ( mCurrentMouseMoveAction == MoveItem )
579 startMacroCommand( tr(
"Move Items" ) );
581 QPointF mEndHandleMovePos = scenePos();
583 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
584 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
587 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
588 for ( QGraphicsItem *item : selectedItems )
590 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
596 createItemCommand( item );
597 moveItem( item, deltaX, deltaY );
598 endItemCommand( item );
602 else if ( mCurrentMouseMoveAction != NoAction )
605 startMacroCommand( tr(
"Resize Items" ) );
608 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
609 for ( QGraphicsItem *item : selectedItems )
611 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
616 createItemCommand( item );
619 if ( selectedItems.size() == 1 )
622 thisItemRect = mResizeRect;
627 thisItemRect = mapRectFromItem( item, itemRect( item ) );
628 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
631 thisItemRect = thisItemRect.normalized();
632 QPointF newPos = mapToScene( thisItemRect.topLeft() );
633 thisItemRect.moveTopLeft( newPos );
634 setItemRect( item, thisItemRect );
636 endItemCommand( item );
652 mCurrentMouseMoveAction = MoveItem;
660bool QgsGraphicsViewMouseHandles::selectionRotation(
double &rotation )
const
663 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
664 auto itemIter = selectedItems.constBegin();
667 double firstItemRotation = ( *itemIter )->rotation();
670 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
672 if ( !
qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
680 rotation = firstItemRotation;
684void QgsGraphicsViewMouseHandles::updateHandles()
689 QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
690 if ( !selectedItems.isEmpty() )
696 if ( selectionRotation( rotation ) )
699 setRotation( rotation );
708 QRectF newHandleBounds = selectionBounds();
711 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
712 setPos( mapToScene( newHandleBounds.topLeft() ) );
725void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition,
bool lockMovement,
bool preventSnap )
733 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
734 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
737 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
739 QPointF snappedLeftPoint;
745 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
750 snappedLeftPoint = upperLeftPoint;
755 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
756 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
762 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
773 QTransform moveTransform;
774 moveTransform.translate( moveRectX, moveRectY );
775 setTransform( moveTransform );
777 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
778 for ( QGraphicsItem *item : selectedItems )
780 previewItemMove( item, moveRectX, moveRectY );
783 showStatusMessage( tr(
"dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
786void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition,
bool lockRatio,
bool fromCenter )
793 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
795 QPointF beginMousePos;
796 QPointF finalPosition;
801 bool snapVertical = mCurrentMouseMoveAction == ResizeLeft ||
802 mCurrentMouseMoveAction == ResizeRight ||
803 mCurrentMouseMoveAction == ResizeLeftUp ||
804 mCurrentMouseMoveAction == ResizeRightUp ||
805 mCurrentMouseMoveAction == ResizeLeftDown ||
806 mCurrentMouseMoveAction == ResizeRightDown;
808 bool snapHorizontal = mCurrentMouseMoveAction == ResizeUp ||
809 mCurrentMouseMoveAction == ResizeDown ||
810 mCurrentMouseMoveAction == ResizeLeftUp ||
811 mCurrentMouseMoveAction == ResizeRightUp ||
812 mCurrentMouseMoveAction == ResizeLeftDown ||
813 mCurrentMouseMoveAction == ResizeRightDown;
817 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
818 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
819 finalPosition = mapFromScene( snappedPosition );
824 beginMousePos = mapFromScene( mBeginMouseEventPos );
825 finalPosition = mapFromScene( currentPosition );
828 double diffX = finalPosition.x() - beginMousePos.x();
829 double diffY = finalPosition.y() - beginMousePos.y();
832 if ( lockRatio && !
qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
834 ratio = mBeginHandleWidth / mBeginHandleHeight;
837 switch ( mCurrentMouseMoveAction )
844 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
864 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
885 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
904 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
925 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
927 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
931 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
934 mx = diffX, my = diffY;
940 case ResizeRightDown:
945 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
947 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
951 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
956 rx = diffX, ry = diffY;
965 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
967 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
971 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
975 my = diffY, rx = diffX, ry = -diffY;
984 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
986 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
990 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1017 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1018 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1021 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1022 translateLine.setAngle( translateLine.angle() - rotation() );
1023 QPointF sceneTranslate = translateLine.p2();
1026 QTransform itemTransform;
1027 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1028 setTransform( itemTransform );
1031 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1033 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1035 else if ( mBeginHandleHeight + ry >= 0 )
1037 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1039 else if ( mBeginHandleWidth + rx >= 0 )
1041 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1045 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1048 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1049 QRectF newHandleBounds;
1050 for ( QGraphicsItem *item : selectedItems )
1053 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1055 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1057 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1058 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1061 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1064 showStatusMessage( tr(
"width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1068void QgsGraphicsViewMouseHandles::setHandleSize(
double size )
1073void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1078QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1081 QPointF sceneMousePos = mapFromScene( cursorPos );
1083 switch ( mCurrentMouseMoveAction )
1086 case QgsGraphicsViewMouseHandles::ResizeUp:
1087 return QSizeF( 0, sceneMousePos.y() );
1089 case QgsGraphicsViewMouseHandles::ResizeDown:
1090 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1093 case QgsGraphicsViewMouseHandles::ResizeLeft:
1094 return QSizeF( sceneMousePos.x(), 0 );
1096 case QgsGraphicsViewMouseHandles::ResizeRight:
1097 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1100 case QgsGraphicsViewMouseHandles::ResizeLeftUp:
1101 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1103 case QgsGraphicsViewMouseHandles::ResizeRightDown:
1104 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1106 case QgsGraphicsViewMouseHandles::ResizeRightUp:
1107 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1109 case QgsGraphicsViewMouseHandles::ResizeLeftDown:
1110 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1121QRectF QgsGraphicsViewMouseHandles::selectionBounds()
const
1124 const QList<QGraphicsItem *> selectedItems = selectedSceneItems(
false );
1125 auto itemIter = selectedItems.constBegin();
1128 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1131 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1133 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1139void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize,
const QRectF &boundsBefore,
const QRectF &boundsAfter )
1142 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1143 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1144 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1145 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1147 rectToResize.setRect( left, top, right - left, bottom - top );
1150double QgsGraphicsViewMouseHandles::relativePosition(
double position,
double beforeMin,
double beforeMax,
double afterMin,
double afterMax )
1153 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1154 double c = afterMin - ( beforeMin * m );
1157 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)