QGIS API Documentation 4.3.0-Master (0cef2469b72)
Loading...
Searching...
No Matches
qgsgraphicsviewmousehandles.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgraphicsviewmousehandles.cpp
3 ------------------------
4 begin : March 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall.dawson@gmail.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include <limits>
21
22#include "qgis.h"
23#include "qgslayoututils.h"
24#include "qgspainting.h"
25#include "qgsrendercontext.h"
26
27#include <QGraphicsSceneHoverEvent>
28#include <QGraphicsView>
29#include <QPainter>
30#include <QString>
31#include <QWidget>
32
33#include "moc_qgsgraphicsviewmousehandles.cpp"
34
35using namespace Qt::StringLiterals;
36
38
39QgsGraphicsViewMouseHandles::QgsGraphicsViewMouseHandles( QGraphicsView *view )
40 : QObject( nullptr )
41 , QGraphicsRectItem( nullptr )
42 , mView( view )
43{
44 //accept hover events, required for changing cursor to resize cursors
45 setAcceptHoverEvents( true );
46
47 //prepare rotation handle path
48 mRotationHandlePath.moveTo( 0, 14 );
49 mRotationHandlePath.lineTo( 6, 20 );
50 mRotationHandlePath.lineTo( 12, 14 );
51 mRotationHandlePath.arcTo( 8, 8, 12, 12, 180, -90 );
52 mRotationHandlePath.lineTo( 14, 12 );
53 mRotationHandlePath.lineTo( 20, 6 );
54 mRotationHandlePath.lineTo( 14, 0 );
55 mRotationHandlePath.arcTo( 4, 4, 20, 20, 90, 90 );
56 mRotationHandlePath.lineTo( 0, 14 );
57}
58
59void QgsGraphicsViewMouseHandles::setRotationEnabled( bool enable )
60{
61 if ( mRotationEnabled == enable )
62 {
63 return;
64 }
65
66 mRotationEnabled = enable;
67 update();
68}
69
70void QgsGraphicsViewMouseHandles::setCadMouseDigitizingModeEnabled( bool enable )
71{
72 if ( mCadMouseDigitizingMode == enable )
73 {
74 return;
75 }
76
77 mCadMouseDigitizingMode = enable;
78}
79
80void QgsGraphicsViewMouseHandles::setResizeEnabled( bool enable )
81{
82 if ( mResizeEnabled == enable )
83 {
84 return;
85 }
86
87 mResizeEnabled = enable;
88 update();
89}
90
91void QgsGraphicsViewMouseHandles::reset()
92{
93 mIsDragging = false;
94 mIsDragStarting = false;
95 mIsResizing = false;
96 mIsRotating = false;
97
98 hideAlignItems();
99
100 //reset default action
101 mCurrentMouseMoveAction = Qgis::MouseHandlesAction::MoveItem;
102 //redraw handles
103 resetTransform();
104 update();
105 updateHandles();
106 //reset status bar message
107 resetStatusBar();
108}
109
110void QgsGraphicsViewMouseHandles::paintInternal( QPainter *painter, bool showHandles, bool showStaticBoundingBoxes, bool showTemporaryBoundingBoxes, const QStyleOptionGraphicsItem *, QWidget * )
111{
112 if ( !showHandles )
113 {
114 return;
115 }
116
117 if ( showStaticBoundingBoxes )
118 {
119 //draw resize handles around bounds of entire selection
120 double rectHandlerSize = rectHandlerBorderTolerance();
121 drawHandles( painter, rectHandlerSize );
122 }
123
124 if ( showTemporaryBoundingBoxes && ( mIsResizing || mIsDragging || showStaticBoundingBoxes ) )
125 {
126 //draw dotted boxes around selected items
127 drawSelectedItemBounds( painter );
128 }
129}
130
131QRectF QgsGraphicsViewMouseHandles::storedItemRect( QGraphicsItem *item ) const
132{
133 return itemRect( item );
134}
135
136void QgsGraphicsViewMouseHandles::rotateItem( QGraphicsItem *, double, double, double )
137{
138 QgsDebugError( u"Rotation is not implemented for this class"_s );
139}
140
141void QgsGraphicsViewMouseHandles::previewItemMove( QGraphicsItem *, double, double )
142{}
143
144void QgsGraphicsViewMouseHandles::setItemRect( QGraphicsItem *, QRectF )
145{
146 QgsDebugError( u"Resize is not implemented for this class"_s );
147}
148
149QRectF QgsGraphicsViewMouseHandles::previewSetItemRect( QGraphicsItem *, QRectF )
150{
151 return QRectF();
152}
153
154void QgsGraphicsViewMouseHandles::startMacroCommand( const QString & )
155{}
156
157void QgsGraphicsViewMouseHandles::endMacroCommand()
158{}
159
160void QgsGraphicsViewMouseHandles::endItemCommand( QGraphicsItem * )
161{}
162
163void QgsGraphicsViewMouseHandles::createItemCommand( QGraphicsItem * )
164{}
165
166QPointF QgsGraphicsViewMouseHandles::snapPoint( QPointF originalPoint, QgsGraphicsViewMouseHandles::SnapGuideMode, bool, bool )
167{
168 return originalPoint;
169}
170
171void QgsGraphicsViewMouseHandles::expandItemList( const QList<QGraphicsItem *> &items, QList<QGraphicsItem *> &collected ) const
172{
173 collected = items;
174}
175
176void QgsGraphicsViewMouseHandles::drawHandles( QPainter *painter, double rectHandlerSize )
177{
178 //blue, zero width cosmetic pen for outline
179 QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
180 handlePen.setWidth( 0 );
181 painter->setPen( handlePen );
182
183 //draw box around entire selection bounds
184 painter->setBrush( Qt::NoBrush );
185 painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
186
187 if ( isResizeEnabled() )
188 {
189 //draw resize handles, using filled white boxes
190 painter->setBrush( QColor( 255, 255, 255, 255 ) );
191 //top left
192 painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
193 //mid top
194 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
195 //top right
196 painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
197 //mid left
198 painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
199 //mid right
200 painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
201 //bottom left
202 painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
203 //mid bottom
204 painter->drawRect( QRectF( ( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
205 //bottom right
206 painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
207 }
208
209 if ( isRotationEnabled() )
210 {
211 //draw rotate handles
212 const double scale = rectHandlerSize / mHandleSize;
213 const bool drawBottomRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().height();
214 const bool drawRightRotationHandles = ( rectHandlerSize * 2 ) + ( mRotationHandleSize * scale * 2 ) < rect().width();
215 QTransform transform;
216
217 //top left
218 transform.reset();
219 transform.translate( rectHandlerSize, rectHandlerSize );
220 transform.scale( scale, scale );
221
222 QgsScopedQPainterState painterState( painter );
223 painter->setTransform( transform, true );
224 painter->drawPath( mRotationHandlePath );
225 painterState.restore();
226
227 //top right
228 if ( drawRightRotationHandles )
229 {
230 transform.reset();
231 transform.translate( rect().width() - rectHandlerSize, rectHandlerSize );
232 transform.rotate( 90 );
233 transform.scale( scale, scale );
234 painterState.save();
235 painter->setTransform( transform, true );
236 painter->drawPath( mRotationHandlePath );
237 painterState.restore();
238 }
239
240 if ( drawBottomRotationHandles )
241 {
242 //bottom left
243 transform.reset();
244 transform.translate( rectHandlerSize, rect().height() - rectHandlerSize );
245 transform.rotate( 270 );
246 transform.scale( scale, scale );
247 painterState.save();
248 painter->setTransform( transform, true );
249 painter->drawPath( mRotationHandlePath );
250 painterState.restore();
251 }
252
253 if ( drawBottomRotationHandles && drawRightRotationHandles )
254 {
255 //bottom right
256 transform.reset();
257 transform.translate( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize );
258 transform.rotate( 180 );
259 transform.scale( scale, scale );
260 painterState.save();
261 painter->setTransform( transform, true );
262 painter->drawPath( mRotationHandlePath );
263 painterState.restore();
264 }
265 }
266}
267
268void QgsGraphicsViewMouseHandles::drawSelectedItemBounds( QPainter *painter )
269{
270 //draw dotted border around selected items to give visual feedback which items are selected
271 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
272 if ( selectedItems.isEmpty() )
273 {
274 return;
275 }
276
277 QList<QGraphicsItem *> itemsToDraw;
278 expandItemList( selectedItems, itemsToDraw );
279
280 if ( itemsToDraw.size() <= 1 )
281 {
282 // Single item selected. The items bounds are drawn by the MouseHandles itself.
283 return;
284 }
285
286 //use difference mode so that they are visible regardless of item colors
287 QgsScopedQPainterState painterState( painter );
288 painter->setCompositionMode( QPainter::CompositionMode_Difference );
289
290 // use a grey dashed pen - in difference mode this should always be visible
291 QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
292 selectedItemPen.setStyle( Qt::DashLine );
293 selectedItemPen.setWidth( 0 );
294 painter->setPen( selectedItemPen );
295 painter->setBrush( Qt::NoBrush );
296
297 for ( QGraphicsItem *item : std::as_const( itemsToDraw ) )
298 {
299 //get bounds of selected item
300 QPolygonF itemBounds;
301 if ( isDragging() && !itemIsLocked( item ) )
302 {
303 //if currently dragging, draw selected item bounds relative to current mouse position
304 //first, get bounds of current item in scene coordinates
305 QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
306 //now, translate it by the current movement amount
307 //IMPORTANT - this is done in scene coordinates, since we don't want any rotation/non-translation transforms to affect the movement
308 itemSceneBounds.translate( transform().dx(), transform().dy() );
309 //finally, remap it to the mouse handle item's coordinate system so it's ready for drawing
310 itemBounds = mapFromScene( itemSceneBounds );
311 }
312 else if ( isResizing() && !itemIsLocked( item ) )
313 {
314 //if currently resizing, calculate relative resize of this item
315 //get item bounds in mouse handle item's coordinate system
316 QRectF thisItemRect = mapRectFromItem( item, itemRect( item ) );
317 //now, resize it relative to the current resized dimensions of the mouse handles
318 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
319 itemBounds = QPolygonF( thisItemRect );
320 }
321 else if ( isRotating() && !itemIsLocked( item ) )
322 {
323 const QPolygonF itemSceneBounds = item->mapToScene( itemRect( item ) );
324 const QPointF rotationCenter = sceneTransform().map( rect().center() );
325
326 QTransform transform;
327 transform.translate( rotationCenter.x(), rotationCenter.y() );
328 transform.rotate( mRotationDelta );
329 transform.translate( -rotationCenter.x(), -rotationCenter.y() );
330 itemBounds = mapFromScene( transform.map( itemSceneBounds ) );
331 }
332 else
333 {
334 // not resizing or moving, so just map the item's bounds to the mouse handle item's coordinate system
335 itemBounds = item->mapToItem( this, itemRect( item ) );
336 }
337
338 // drawPolygon causes issues on windows - corners of path may be missing resulting in triangles being drawn
339 // instead of rectangles! (Same cause as #13343)
340 QPainterPath path;
341 path.addPolygon( itemBounds );
342 painter->drawPath( path );
343 }
344}
345
346double QgsGraphicsViewMouseHandles::rectHandlerBorderTolerance() const
347{
348 if ( !mView )
349 return 0;
350
351 //calculate size for resize handles
352 //get view scale factor
353 double viewScaleFactor = mView->transform().m11();
354
355 //size of handle boxes depends on zoom level in layout view
356 double rectHandlerSize = mHandleSize / viewScaleFactor;
357
358 //make sure the boxes don't get too large
359 if ( rectHandlerSize > ( rect().width() / 3 ) )
360 {
361 rectHandlerSize = rect().width() / 3;
362 }
363 if ( rectHandlerSize > ( rect().height() / 3 ) )
364 {
365 rectHandlerSize = rect().height() / 3;
366 }
367 return rectHandlerSize;
368}
369
370Qt::CursorShape QgsGraphicsViewMouseHandles::cursorForPosition( QPointF itemCoordPos )
371{
372 Qgis::MouseHandlesAction mouseAction = mouseActionForPosition( itemCoordPos );
373 double normalizedRotation = std::fmod( rotation(), 360 );
374 if ( normalizedRotation < 0 )
375 {
376 normalizedRotation += 360;
377 }
378 switch ( mouseAction )
379 {
381 return Qt::ForbiddenCursor;
383 return Qt::SizeAllCursor;
386 //account for rotation
387 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
388 {
389 return Qt::SizeVerCursor;
390 }
391 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
392 {
393 return Qt::SizeBDiagCursor;
394 }
395 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
396 {
397 return Qt::SizeHorCursor;
398 }
399 else
400 {
401 return Qt::SizeFDiagCursor;
402 }
405 //account for rotation
406 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
407 {
408 return Qt::SizeHorCursor;
409 }
410 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
411 {
412 return Qt::SizeFDiagCursor;
413 }
414 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
415 {
416 return Qt::SizeVerCursor;
417 }
418 else
419 {
420 return Qt::SizeBDiagCursor;
421 }
422
425 //account for rotation
426 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
427 {
428 return Qt::SizeFDiagCursor;
429 }
430 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
431 {
432 return Qt::SizeVerCursor;
433 }
434 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
435 {
436 return Qt::SizeBDiagCursor;
437 }
438 else
439 {
440 return Qt::SizeHorCursor;
441 }
444 //account for rotation
445 if ( ( normalizedRotation <= 22.5 || normalizedRotation >= 337.5 ) || ( normalizedRotation >= 157.5 && normalizedRotation <= 202.5 ) )
446 {
447 return Qt::SizeBDiagCursor;
448 }
449 else if ( ( normalizedRotation >= 22.5 && normalizedRotation <= 67.5 ) || ( normalizedRotation >= 202.5 && normalizedRotation <= 247.5 ) )
450 {
451 return Qt::SizeHorCursor;
452 }
453 else if ( ( normalizedRotation >= 67.5 && normalizedRotation <= 112.5 ) || ( normalizedRotation >= 247.5 && normalizedRotation <= 292.5 ) )
454 {
455 return Qt::SizeFDiagCursor;
456 }
457 else
458 {
459 return Qt::SizeVerCursor;
460 }
462 return Qt::ArrowCursor;
463
468 return Qt::PointingHandCursor;
469 }
470
471 return Qt::ArrowCursor;
472}
473
474Qgis::MouseHandlesAction QgsGraphicsViewMouseHandles::mouseActionForPosition( QPointF itemCoordPos )
475{
476 bool nearLeftBorder = false;
477 bool nearRightBorder = false;
478 bool nearLowerBorder = false;
479 bool nearUpperBorder = false;
480
481 bool nearLeftInner = false;
482 bool nearRightInner = false;
483 bool nearLowerInner = false;
484 bool nearUpperInner = false;
485
486 bool withinWidth = false;
487 bool withinHeight = false;
488
489 if ( itemCoordPos.x() >= 0 && itemCoordPos.x() <= rect().width() )
490 {
491 withinWidth = true;
492 }
493 if ( itemCoordPos.y() >= 0 && itemCoordPos.y() <= rect().height() )
494 {
495 withinHeight = true;
496 }
497
498 double borderTolerance = rectHandlerBorderTolerance();
499 double innerTolerance = mRotationHandleSize * borderTolerance / mHandleSize;
500
501 if ( isResizeEnabled() && itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
502 {
503 nearLeftBorder = true;
504 }
505 else if ( isRotationEnabled() && itemCoordPos.x() >= borderTolerance && itemCoordPos.x() < ( borderTolerance + innerTolerance ) )
506 {
507 nearLeftInner = true;
508 }
509 if ( isResizeEnabled() && itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
510 {
511 nearUpperBorder = true;
512 }
513 else if ( isRotationEnabled() && itemCoordPos.y() >= borderTolerance && itemCoordPos.y() < ( borderTolerance + innerTolerance ) )
514 {
515 nearUpperInner = true;
516 }
517 if ( isResizeEnabled() && itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
518 {
519 nearRightBorder = true;
520 }
521 else if ( isRotationEnabled() && itemCoordPos.x() <= ( rect().width() - borderTolerance ) && itemCoordPos.x() > ( rect().width() - borderTolerance - innerTolerance ) )
522 {
523 nearRightInner = true;
524 }
525 if ( isResizeEnabled() && itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
526 {
527 nearLowerBorder = true;
528 }
529 else if ( isRotationEnabled() && itemCoordPos.y() <= ( rect().height() - borderTolerance ) && itemCoordPos.y() > ( rect().height() - borderTolerance - innerTolerance ) )
530 {
531 nearLowerInner = true;
532 }
533
534 if ( nearLeftBorder && nearUpperBorder )
535 {
537 }
538 else if ( nearLeftBorder && nearLowerBorder )
539 {
541 }
542 else if ( nearRightBorder && nearUpperBorder )
543 {
545 }
546 else if ( nearRightBorder && nearLowerBorder )
547 {
549 }
550 else if ( nearLeftBorder && withinHeight )
551 {
553 }
554 else if ( nearRightBorder && withinHeight )
555 {
557 }
558 else if ( nearUpperBorder && withinWidth )
559 {
561 }
562 else if ( nearLowerBorder && withinWidth )
563 {
565 }
566 else if ( nearLeftInner && nearUpperInner )
567 {
569 }
570 else if ( nearRightInner && nearUpperInner )
571 {
573 }
574 else if ( nearLeftInner && nearLowerInner )
575 {
577 }
578 else if ( nearRightInner && nearLowerInner )
579 {
581 }
582
583 //find out if cursor position is over a selected item
584 QPointF scenePoint = mapToScene( itemCoordPos );
585 const QList<QGraphicsItem *> itemsAtCursorPos = sceneItemsAtPoint( scenePoint );
586 if ( itemsAtCursorPos.isEmpty() )
587 {
588 //no items at cursor position
590 }
591 for ( QGraphicsItem *graphicsItem : itemsAtCursorPos )
592 {
593 if ( graphicsItem && graphicsItem->isSelected() )
594 {
595 //cursor is over a selected layout item
597 }
598 }
599
600 //default
602}
603
604Qgis::MouseHandlesAction QgsGraphicsViewMouseHandles::mouseActionForScenePos( QPointF sceneCoordPos )
605{
606 // convert sceneCoordPos to item coordinates
607 QPointF itemPos = mapFromScene( sceneCoordPos );
608 return mouseActionForPosition( itemPos );
609}
610
611bool QgsGraphicsViewMouseHandles::shouldBlockEvent( QInputEvent * ) const
612{
613 return mIsDragging || mIsResizing || mIsRotating;
614}
615
616void QgsGraphicsViewMouseHandles::startMove( QPointF sceneCoordPos )
617{
618 //save current cursor position
619 mMouseMoveStartPos = sceneCoordPos;
620 //save current item geometry
621 mBeginMouseEventPos = sceneCoordPos;
622 mBeginHandlePos = scenePos();
623 mBeginHandleWidth = rect().width();
624 mBeginHandleHeight = rect().height();
625 mCurrentMouseMoveAction = Qgis::MouseHandlesAction::MoveItem;
626 mIsDragging = true;
627 hideAlignItems();
628
629 // Explicitly call grabMouse to ensure the mouse handles receive the subsequent mouse move events.
630 if ( mView->scene()->mouseGrabberItem() != this )
631 {
632 grabMouse();
633 }
634}
635
636void QgsGraphicsViewMouseHandles::selectedItemSizeChanged()
637{
638 if ( !isDragging() && !isResizing() )
639 {
640 //only required for non-mouse initiated size changes
641 updateHandles();
642 }
643}
644
645void QgsGraphicsViewMouseHandles::selectedItemRotationChanged()
646{
647 if ( !isDragging() && !isResizing() )
648 {
649 //only required for non-mouse initiated rotation changes
650 updateHandles();
651 }
652}
653
654void QgsGraphicsViewMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent *event )
655{
656 setViewportCursor( cursorForPosition( event->pos() ) );
657}
658
659void QgsGraphicsViewMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent *event )
660{
661 Q_UNUSED( event )
662 setViewportCursor( Qt::ArrowCursor );
663}
664
665void QgsGraphicsViewMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent *event )
666{
667 if ( event->button() != Qt::LeftButton )
668 {
669 event->ignore();
670 return;
671 }
672
673 if ( mCadMouseDigitizingMode && ( mIsDragging || mIsResizing || mIsRotating ) )
674 {
675 return;
676 }
677
678 //save current cursor position
679 mMouseMoveStartPos = event->lastScenePos();
680 //save current item geometry
681 mBeginMouseEventPos = event->lastScenePos();
682 mBeginHandlePos = scenePos();
683 mBeginHandleWidth = rect().width();
684 mBeginHandleHeight = rect().height();
685 //type of mouse move action
686 mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
687
688 hideAlignItems();
689
690 switch ( mCurrentMouseMoveAction )
691 {
693 //moving items
694 mIsDragging = true;
695 break;
696
705 //resizing items
706 mIsResizing = true;
707 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
708 mResizeMoveX = 0;
709 mResizeMoveY = 0;
710 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
711 break;
712
717 mIsRotating = true;
718 mRotationCenter = sceneTransform().map( rect().center() );
719 mRotationBegin = std::atan2( mMouseMoveStartPos.y() - mRotationCenter.y(), mMouseMoveStartPos.x() - mRotationCenter.x() ) * 180 / M_PI;
720 mRotationCurrent = 0.0;
721 break;
722
725 break;
726 }
727
728 if ( mCadMouseDigitizingMode && mIsDragging )
729 {
730 mIsDragStarting = true;
731 }
732}
733
734void QgsGraphicsViewMouseHandles::resetStatusBar()
735{
736 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
737 int selectedCount = selectedItems.size();
738 if ( selectedCount )
739 {
740 //set status bar message to count of selected items
741 showStatusMessage( tr( "%n item(s) selected", nullptr, selectedCount ) );
742 }
743 else
744 {
745 //clear status bar message
746 showStatusMessage( QString() );
747 }
748}
749
750void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
751{
752 if ( isDragging() )
753 {
754 //currently dragging a selection
755 //if shift depressed, constrain movement to horizontal/vertical
756 //if control depressed, ignore snapping
757 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
758 }
759 else if ( isResizing() )
760 {
761 //currently resizing a selection
762 //lock aspect ratio if shift depressed
763 //resize from center if alt depressed
764 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
765 }
766 else if ( isRotating() )
767 {
768 //currently rotating a selection
769 //snap to common angles if ctrl is pressed
770 rotateMouseMove( event->lastScenePos(), event->modifiers() & Qt::ControlModifier );
771 }
772}
773
774void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
775{
776 if ( event->button() != Qt::LeftButton )
777 {
778 event->ignore();
779 return;
780 }
781
782 if ( mDoubleClickInProgress )
783 {
784 mDoubleClickInProgress = false;
785 event->accept();
786 return;
787 }
788
789 if ( mIsDragStarting )
790 {
791 mIsDragStarting = false;
792 return;
793 }
794
795 // Mouse may have been grabbed from the QgsLayoutViewSelectTool, so we need to release it explicitly
796 // otherwise, hover events will not be received
797 if ( mView->scene()->mouseGrabberItem() == this )
798 {
799 ungrabMouse();
800 }
801
802 QPointF mouseMoveStopPoint = event->lastScenePos();
803 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
804 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
805
806 const bool isClick = std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min();
807 if ( isClick )
808 {
809 mIsDragging = false;
810 mIsResizing = false;
811 mIsRotating = false;
812 update();
813 hideAlignItems();
814 return;
815 }
816
817 if ( mIsDragging )
818 {
819 //move selected items
820 startMacroCommand( tr( "Move Items" ) );
821
822 QPointF mEndHandleMovePos = scenePos();
823
824 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
825 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
826
827 //move all selected items
828 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
829 for ( QGraphicsItem *item : selectedItems )
830 {
831 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
832 {
833 //don't move locked items, or grouped items (group takes care of that)
834 continue;
835 }
836
837 createItemCommand( item );
838 moveItem( item, deltaX, deltaY );
839 endItemCommand( item );
840 }
841 endMacroCommand();
842
843 mIsDragging = false;
844 }
845 else if ( mIsResizing )
846 {
847 //resize selected items
848 startMacroCommand( tr( "Resize Items" ) );
849
850 //resize all selected items
851 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
852 for ( QGraphicsItem *item : selectedItems )
853 {
854 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
855 {
856 //don't resize locked items or deselectable items (e.g., items which make up an item group)
857 continue;
858 }
859 createItemCommand( item );
860
861 QRectF thisItemRect;
862 if ( selectedItems.size() == 1 )
863 {
864 //only a single item is selected, so set its size to the final resized mouse handle size
865 thisItemRect = mResizeRect;
866 }
867 else
868 {
869 //multiple items selected, so each needs to be scaled relatively to the final size of the mouse handles
870 thisItemRect = mapRectFromItem( item, itemRect( item ) );
871 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
872 }
873
874 thisItemRect = thisItemRect.normalized();
875 QPointF newPos = mapToScene( thisItemRect.topLeft() );
876 thisItemRect.moveTopLeft( newPos );
877 setItemRect( item, thisItemRect );
878
879 endItemCommand( item );
880 }
881 endMacroCommand();
882
883 mIsResizing = false;
884 }
885 else if ( mIsRotating )
886 {
887 const QPointF itemRotationCenter = sceneTransform().map( rect().center() );
888
889 //move selected items
890 startMacroCommand( tr( "Rotate Items" ) );
891
892 //move all selected items
893 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
894 for ( QGraphicsItem *item : selectedItems )
895 {
896 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
897 {
898 //don't move locked items, or grouped items (group takes care of that)
899 continue;
900 }
901
902 const QPointF itemCenter = item->mapToScene( itemRect( item ) ).boundingRect().center();
903
904 QTransform transform;
905 transform.translate( itemRotationCenter.x(), itemRotationCenter.y() );
906 transform.rotate( mRotationDelta );
907 transform.translate( -itemRotationCenter.x(), -itemRotationCenter.y() );
908 const QPointF rotatedItemCenter = transform.map( itemCenter );
909
910 createItemCommand( item );
911 rotateItem( item, mRotationDelta, rotatedItemCenter.x() - itemCenter.x(), rotatedItemCenter.y() - itemCenter.y() );
912 endItemCommand( item );
913 }
914 endMacroCommand();
915
916 mIsRotating = false;
917 }
918
919 reset();
920}
921
922bool QgsGraphicsViewMouseHandles::selectionRotation( double &rotation ) const
923{
924 //check if all selected items have same rotation
925 QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
926 auto itemIter = selectedItems.constBegin();
927
928 //start with rotation of first selected item
929 double firstItemRotation = ( *itemIter )->rotation();
930
931 //iterate through remaining items, checking if they have same rotation
932 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
933 {
934 if ( !qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
935 {
936 //item has a different rotation, so return false
937 return false;
938 }
939 }
940
941 //all items have the same rotation, so set the rotation variable and return true
942 rotation = firstItemRotation;
943 return true;
944}
945
946void QgsGraphicsViewMouseHandles::updateHandles()
947{
948 //recalculate size and position of handle item
949
950 //first check to see if any items are selected
951 QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
952 if ( !selectedItems.isEmpty() )
953 {
954 //one or more items are selected, get bounds of all selected items
955
956 //update rotation of handle object
957 double rotation;
958 if ( selectionRotation( rotation ) )
959 {
960 //all items share a common rotation value, so we rotate the mouse handles to match
961 setRotation( rotation );
962 }
963 else
964 {
965 //items have varying rotation values - we can't rotate the mouse handles to match
966 setRotation( 0 );
967 }
968
969 //get bounds of all selected items
970 QRectF newHandleBounds = selectionBounds();
971
972 //update size and position of handle object
973 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
974 setPos( mapToScene( newHandleBounds.topLeft() ) );
975
976 show();
977 }
978 else
979 {
980 //no items selected, hide handles
981 hide();
982 }
983
984 //force redraw
985 update();
986}
987
988void QgsGraphicsViewMouseHandles::rotateMouseMove( QPointF currentPosition, bool snapToCommonAngles )
989{
990 if ( !scene() )
991 {
992 return;
993 }
994
995 mRotationCurrent = std::atan2( currentPosition.y() - mRotationCenter.y(), currentPosition.x() - mRotationCenter.x() ) * 180 / M_PI;
996 mRotationDelta = mRotationCurrent - mRotationBegin;
997 if ( snapToCommonAngles )
998 {
999 mRotationDelta = QgsLayoutUtils::snappedAngle( mRotationDelta );
1000 }
1001
1002 const double itemRotationRadian = rotation() * M_PI / 180;
1003 const double deltaX = ( rect().width() / 2 ) * cos( itemRotationRadian ) - ( rect().height() / 2 ) * sin( itemRotationRadian );
1004 const double deltaY = ( rect().width() / 2 ) * sin( itemRotationRadian ) + ( rect().height() / 2 ) * cos( itemRotationRadian );
1005
1006 QTransform rotateTransform;
1007 rotateTransform.translate( deltaX, deltaY );
1008 rotateTransform.rotate( mRotationDelta );
1009 rotateTransform.translate( -deltaX, -deltaY );
1010 setTransform( rotateTransform );
1011
1012 //show current selection rotation in status bar
1013 showStatusMessage( tr( "rotation: %1°" ).arg( QString::number( mRotationDelta, 'f', 2 ) ) );
1014
1015 return;
1016}
1017
1018void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition, bool lockMovement, bool preventSnap )
1019{
1020 if ( !scene() )
1021 {
1022 return;
1023 }
1024
1025 //calculate total amount of mouse movement since drag began
1026 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
1027 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
1028
1029 //find target position before snapping (in scene coordinates)
1030 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
1031
1032 QPointF snappedLeftPoint;
1033
1034 //no snapping for rotated items for now
1035 if ( !preventSnap && qgsDoubleNear( rotation(), 0.0 ) )
1036 {
1037 //snap to grid and guides
1038 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
1039 }
1040 else
1041 {
1042 //no snapping
1043 snappedLeftPoint = upperLeftPoint;
1044 hideAlignItems();
1045 }
1046
1047 //calculate total shift for item from beginning of drag operation to current position
1048 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
1049 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
1050
1051 if ( lockMovement )
1052 {
1053 //constrained (shift) moving should lock to horizontal/vertical movement
1054 //reset the smaller of the x/y movements
1055 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
1056 {
1057 moveRectX = 0;
1058 }
1059 else
1060 {
1061 moveRectY = 0;
1062 }
1063 }
1064
1065 //shift handle item to new position
1066 QTransform moveTransform;
1067 moveTransform.translate( moveRectX, moveRectY );
1068 setTransform( moveTransform );
1069
1070 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
1071 for ( QGraphicsItem *item : selectedItems )
1072 {
1073 previewItemMove( item, moveRectX, moveRectY );
1074 }
1075 //show current displacement of selection in status bar
1076 showStatusMessage( tr( "dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
1077}
1078
1079void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition, bool lockRatio, bool fromCenter )
1080{
1081 if ( !scene() )
1082 {
1083 return;
1084 }
1085
1086 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
1087
1088 QPointF beginMousePos;
1089 QPointF finalPosition;
1090 if ( qgsDoubleNear( rotation(), 0.0 ) )
1091 {
1092 //snapping only occurs if handles are not rotated for now
1093
1094 bool snapVertical = mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeft
1095 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRight
1096 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftUp
1097 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightUp
1098 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftDown
1099 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightDown;
1100
1101 bool snapHorizontal = mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeUp
1102 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeDown
1103 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftUp
1104 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightUp
1105 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftDown
1106 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightDown;
1107
1108 //subtract cursor edge offset from begin mouse event and current cursor position, so that snapping occurs to edge of mouse handles
1109 //rather then cursor position
1110 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
1111 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
1112 finalPosition = mapFromScene( snappedPosition );
1113 }
1114 else
1115 {
1116 //no snapping for rotated items for now
1117 beginMousePos = mapFromScene( mBeginMouseEventPos );
1118 finalPosition = mapFromScene( currentPosition );
1119 }
1120
1121 double diffX = finalPosition.x() - beginMousePos.x();
1122 double diffY = finalPosition.y() - beginMousePos.y();
1123
1124 double ratio = 0;
1125 if ( lockRatio && !qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
1126 {
1127 ratio = mBeginHandleWidth / mBeginHandleHeight;
1128 }
1129
1130 switch ( mCurrentMouseMoveAction )
1131 {
1132 //vertical resize
1134 {
1135 if ( ratio )
1136 {
1137 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1138 mx = -diffX / 2;
1139 my = diffY;
1140 rx = diffX;
1141 ry = -diffY;
1142 }
1143 else
1144 {
1145 mx = 0;
1146 my = diffY;
1147 rx = 0;
1148 ry = -diffY;
1149 }
1150 break;
1151 }
1152
1154 {
1155 if ( ratio )
1156 {
1157 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1158 mx = -diffX / 2;
1159 my = 0;
1160 rx = diffX;
1161 ry = diffY;
1162 }
1163 else
1164 {
1165 mx = 0;
1166 my = 0;
1167 rx = 0;
1168 ry = diffY;
1169 }
1170 break;
1171 }
1172
1173 //horizontal resize
1175 {
1176 if ( ratio )
1177 {
1178 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1179 mx = diffX;
1180 my = -diffY / 2;
1181 rx = -diffX;
1182 ry = diffY;
1183 }
1184 else
1185 {
1186 mx = diffX, my = 0;
1187 rx = -diffX;
1188 ry = 0;
1189 }
1190 break;
1191 }
1192
1194 {
1195 if ( ratio )
1196 {
1197 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1198 mx = 0;
1199 my = -diffY / 2;
1200 rx = diffX;
1201 ry = diffY;
1202 }
1203 else
1204 {
1205 mx = 0;
1206 my = 0;
1207 rx = diffX, ry = 0;
1208 }
1209 break;
1210 }
1211
1212 //diagonal resize
1214 {
1215 if ( ratio )
1216 {
1217 //ratio locked resize
1218 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1219 {
1220 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
1221 }
1222 else
1223 {
1224 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
1225 }
1226 }
1227 mx = diffX, my = diffY;
1228 rx = -diffX;
1229 ry = -diffY;
1230 break;
1231 }
1232
1234 {
1235 if ( ratio )
1236 {
1237 //ratio locked resize
1238 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1239 {
1240 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1241 }
1242 else
1243 {
1244 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1245 }
1246 }
1247 mx = 0;
1248 my = 0;
1249 rx = diffX, ry = diffY;
1250 break;
1251 }
1252
1254 {
1255 if ( ratio )
1256 {
1257 //ratio locked resize
1258 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1259 {
1260 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1261 }
1262 else
1263 {
1264 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
1265 }
1266 }
1267 mx = 0;
1268 my = diffY, rx = diffX, ry = -diffY;
1269 break;
1270 }
1271
1273 {
1274 if ( ratio )
1275 {
1276 //ratio locked resize
1277 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1278 {
1279 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
1280 }
1281 else
1282 {
1283 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1284 }
1285 }
1286 mx = diffX, my = 0;
1287 rx = -diffX;
1288 ry = diffY;
1289 break;
1290 }
1291
1299 break;
1300 }
1301
1302 //resizing from center of objects?
1303 if ( fromCenter )
1304 {
1305 my = -ry;
1306 mx = -rx;
1307 ry = 2 * ry;
1308 rx = 2 * rx;
1309 }
1310
1311 //update selection handle rectangle
1312
1313 //make sure selection handle size rectangle is normalized (ie, left coord < right coord)
1314 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1315 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1316
1317 //calculate movement in scene coordinates
1318 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1319 translateLine.setAngle( translateLine.angle() - rotation() );
1320 QPointF sceneTranslate = translateLine.p2();
1321
1322 //move selection handles
1323 QTransform itemTransform;
1324 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1325 setTransform( itemTransform );
1326
1327 //handle non-normalised resizes - e.g., dragging the left handle so far to the right that it's past the right handle
1328 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1329 {
1330 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1331 }
1332 else if ( mBeginHandleHeight + ry >= 0 )
1333 {
1334 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1335 }
1336 else if ( mBeginHandleWidth + rx >= 0 )
1337 {
1338 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1339 }
1340 else
1341 {
1342 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1343 }
1344
1345 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
1346 QRectF newHandleBounds;
1347 for ( QGraphicsItem *item : selectedItems )
1348 {
1349 //get stored item bounds in mouse handle item's coordinate system
1350 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1351 //now, resize it relative to the current resized dimensions of the mouse handles
1352 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1353
1354 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1355 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1356 }
1357
1358 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1359
1360 //show current size of selection in status bar
1361 showStatusMessage( tr( "width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1362}
1363
1364void QgsGraphicsViewMouseHandles::setHandleSize( double size )
1365{
1366 mHandleSize = size;
1367}
1368
1369void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1370{
1371 Q_UNUSED( event )
1372
1373 mDoubleClickInProgress = true;
1374}
1375
1376QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1377{
1378 //find offset between cursor position and actual edge of item
1379 QPointF sceneMousePos = mapFromScene( cursorPos );
1380
1381 switch ( mCurrentMouseMoveAction )
1382 {
1383 //vertical resize
1385 return QSizeF( 0, sceneMousePos.y() );
1386
1388 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1389
1390 //horizontal resize
1392 return QSizeF( sceneMousePos.x(), 0 );
1393
1395 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1396
1397 //diagonal resize
1399 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1400
1402 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1403
1405 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1406
1408 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1409
1417 return QSizeF();
1418 }
1419
1420 return QSizeF();
1421}
1422
1423QRectF QgsGraphicsViewMouseHandles::selectionBounds() const
1424{
1425 //calculate bounds of all currently selected items in mouse handle coordinate system
1426 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
1427 auto itemIter = selectedItems.constBegin();
1428
1429 //start with handle bounds of first selected item
1430 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1431
1432 //iterate through remaining items, expanding the bounds as required
1433 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1434 {
1435 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1436 }
1437
1438 return bounds;
1439}
1440
1441void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter )
1442{
1443 //linearly scale rectToResize relative to the scaling from boundsBefore to boundsAfter
1444 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1445 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1446 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1447 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1448
1449 rectToResize.setRect( left, top, right - left, bottom - top );
1450}
1451
1452double QgsGraphicsViewMouseHandles::relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax )
1453{
1454 //calculate parameters for linear scale between before and after ranges
1455 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1456 double c = afterMin - ( beforeMin * m );
1457
1458 //return linearly scaled position
1459 return m * position + c;
1460}
1461
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:6934
@ ResizeRightDown
Resize right down (Bottom right handle).
Definition qgis.h:6943
@ NoAction
No action.
Definition qgis.h:6949
@ SelectItem
Select item.
Definition qgis.h:6948
@ ResizeLeftUp
Resize left up (Top left handle).
Definition qgis.h:6940
@ ResizeLeftDown
Resize left down (Bottom left handle).
Definition qgis.h:6942
@ ResizeRight
Resize right (Right handle).
Definition qgis.h:6939
@ MoveItem
Move item.
Definition qgis.h:6935
@ ResizeDown
Resize down (Bottom handle).
Definition qgis.h:6937
@ RotateTopRight
Rotate from top right handle.
Definition qgis.h:6945
@ RotateTopLeft
Rotate from top left handle.
Definition qgis.h:6944
@ RotateBottomRight
Rotate right bottom right handle.
Definition qgis.h:6947
@ ResizeRightUp
Resize right up (Top right handle).
Definition qgis.h:6941
@ ResizeLeft
Resize left (Left handle).
Definition qgis.h:6938
@ RotateBottomLeft
Rotate from bottom left handle.
Definition qgis.h:6946
@ ResizeUp
Resize up (Top handle).
Definition qgis.h:6936
static double snappedAngle(double angle)
Snaps an angle (in degrees) to its closest 45 degree angle.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7811
#define QgsDebugError(str)
Definition qgslogger.h:71