QGIS API Documentation 4.5.0-Master (3b5d115045b)
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 startMoveOperation();
695 mIsDragging = true;
696 break;
697
706 //resizing items
707 mIsResizing = true;
708 mResizeRect = QRectF( 0, 0, mBeginHandleWidth, mBeginHandleHeight );
709 mResizeMoveX = 0;
710 mResizeMoveY = 0;
711 mCursorOffset = calcCursorEdgeOffset( mMouseMoveStartPos );
712 break;
713
718 mIsRotating = true;
719 mRotationCenter = sceneTransform().map( rect().center() );
720 mRotationBegin = std::atan2( mMouseMoveStartPos.y() - mRotationCenter.y(), mMouseMoveStartPos.x() - mRotationCenter.x() ) * 180 / M_PI;
721 mRotationCurrent = 0.0;
722 break;
723
726 break;
727 }
728
729 if ( mCadMouseDigitizingMode && mIsDragging )
730 {
731 mIsDragStarting = true;
732 }
733}
734
735void QgsGraphicsViewMouseHandles::resetStatusBar()
736{
737 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
738 int selectedCount = selectedItems.size();
739 if ( selectedCount )
740 {
741 //set status bar message to count of selected items
742 showStatusMessage( tr( "%n item(s) selected", nullptr, selectedCount ) );
743 }
744 else
745 {
746 //clear status bar message
747 showStatusMessage( QString() );
748 }
749}
750
751void QgsGraphicsViewMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent *event )
752{
753 if ( isDragging() )
754 {
755 //currently dragging a selection
756 //if shift depressed, constrain movement to horizontal/vertical
757 //if control depressed, ignore snapping
758 dragMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::ControlModifier );
759 }
760 else if ( isResizing() )
761 {
762 //currently resizing a selection
763 //lock aspect ratio if shift depressed
764 //resize from center if alt depressed
765 resizeMouseMove( event->lastScenePos(), event->modifiers() & Qt::ShiftModifier, event->modifiers() & Qt::AltModifier );
766 }
767 else if ( isRotating() )
768 {
769 //currently rotating a selection
770 //snap to common angles if ctrl is pressed
771 rotateMouseMove( event->lastScenePos(), event->modifiers() & Qt::ControlModifier );
772 }
773}
774
775void QgsGraphicsViewMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent *event )
776{
777 if ( event->button() != Qt::LeftButton )
778 {
779 event->ignore();
780 return;
781 }
782
783 if ( mDoubleClickInProgress )
784 {
785 mDoubleClickInProgress = false;
786 event->accept();
787 return;
788 }
789
790 if ( mIsDragStarting )
791 {
792 mIsDragStarting = false;
793 return;
794 }
795
796 // Mouse may have been grabbed from the QgsLayoutViewSelectTool, so we need to release it explicitly
797 // otherwise, hover events will not be received
798 if ( mView->scene()->mouseGrabberItem() == this )
799 {
800 ungrabMouse();
801 }
802
803 QPointF mouseMoveStopPoint = event->lastScenePos();
804 double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
805 double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
806
807 const bool isClick = std::fabs( diffX ) < std::numeric_limits<double>::min() && std::fabs( diffY ) < std::numeric_limits<double>::min();
808 if ( isClick )
809 {
810 if ( mIsDragging )
811 {
812 mIsDragging = false;
813 endMoveOperation();
814 }
815 mIsResizing = false;
816 mIsRotating = false;
817 update();
818 hideAlignItems();
819 return;
820 }
821
822 if ( mIsDragging )
823 {
824 //move selected items
825 startMacroCommand( tr( "Move Items" ) );
826
827 QPointF mEndHandleMovePos = scenePos();
828
829 double deltaX = mEndHandleMovePos.x() - mBeginHandlePos.x();
830 double deltaY = mEndHandleMovePos.y() - mBeginHandlePos.y();
831
832 //move all selected items
833 const QList<QGraphicsItem *> selectedItems = filterItemListForMoveOperation( selectedSceneItems( false ) );
834 for ( QGraphicsItem *item : selectedItems )
835 {
836 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
837 {
838 //don't move locked items, or grouped items (group takes care of that)
839 continue;
840 }
841
842 createItemCommand( item );
843 moveItem( item, deltaX, deltaY );
844 endItemCommand( item );
845 }
846 endMacroCommand();
847
848 mIsDragging = false;
849 endMoveOperation();
850 }
851 else if ( mIsResizing )
852 {
853 //resize selected items
854 startMacroCommand( tr( "Resize Items" ) );
855
856 //resize all selected items
857 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
858 for ( QGraphicsItem *item : selectedItems )
859 {
860 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 )
861 {
862 //don't resize locked items or deselectable items (e.g., items which make up an item group)
863 continue;
864 }
865 createItemCommand( item );
866
867 QRectF thisItemRect;
868 if ( selectedItems.size() == 1 )
869 {
870 //only a single item is selected, so set its size to the final resized mouse handle size
871 thisItemRect = mResizeRect;
872 }
873 else
874 {
875 //multiple items selected, so each needs to be scaled relatively to the final size of the mouse handles
876 thisItemRect = mapRectFromItem( item, itemRect( item ) );
877 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
878 }
879
880 thisItemRect = thisItemRect.normalized();
881 QPointF newPos = mapToScene( thisItemRect.topLeft() );
882 thisItemRect.moveTopLeft( newPos );
883 setItemRect( item, thisItemRect );
884
885 endItemCommand( item );
886 }
887 endMacroCommand();
888
889 mIsResizing = false;
890 }
891 else if ( mIsRotating )
892 {
893 const QPointF itemRotationCenter = sceneTransform().map( rect().center() );
894
895 //move selected items
896 startMacroCommand( tr( "Rotate Items" ) );
897
898 //move all selected items
899 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
900 for ( QGraphicsItem *item : selectedItems )
901 {
902 if ( itemIsLocked( item ) || ( item->flags() & QGraphicsItem::ItemIsSelectable ) == 0 || itemIsGroupMember( item ) )
903 {
904 //don't move locked items, or grouped items (group takes care of that)
905 continue;
906 }
907
908 const QPointF itemCenter = item->mapToScene( itemRect( item ) ).boundingRect().center();
909
910 QTransform transform;
911 transform.translate( itemRotationCenter.x(), itemRotationCenter.y() );
912 transform.rotate( mRotationDelta );
913 transform.translate( -itemRotationCenter.x(), -itemRotationCenter.y() );
914 const QPointF rotatedItemCenter = transform.map( itemCenter );
915
916 createItemCommand( item );
917 rotateItem( item, mRotationDelta, rotatedItemCenter.x() - itemCenter.x(), rotatedItemCenter.y() - itemCenter.y() );
918 endItemCommand( item );
919 }
920 endMacroCommand();
921
922 mIsRotating = false;
923 }
924
925 reset();
926}
927
928void QgsGraphicsViewMouseHandles::startMoveOperation()
929{}
930
931void QgsGraphicsViewMouseHandles::endMoveOperation()
932{}
933
934bool QgsGraphicsViewMouseHandles::selectionRotation( double &rotation ) const
935{
936 //check if all selected items have same rotation
937 QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
938 auto itemIter = selectedItems.constBegin();
939
940 //start with rotation of first selected item
941 double firstItemRotation = ( *itemIter )->rotation();
942
943 //iterate through remaining items, checking if they have same rotation
944 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
945 {
946 if ( !qgsDoubleNear( ( *itemIter )->rotation(), firstItemRotation ) )
947 {
948 //item has a different rotation, so return false
949 return false;
950 }
951 }
952
953 //all items have the same rotation, so set the rotation variable and return true
954 rotation = firstItemRotation;
955 return true;
956}
957
958void QgsGraphicsViewMouseHandles::updateHandles()
959{
960 //recalculate size and position of handle item
961
962 //first check to see if any items are selected
963 QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
964 if ( !selectedItems.isEmpty() )
965 {
966 //one or more items are selected, get bounds of all selected items
967
968 //update rotation of handle object
969 double rotation;
970 if ( selectionRotation( rotation ) )
971 {
972 //all items share a common rotation value, so we rotate the mouse handles to match
973 setRotation( rotation );
974 }
975 else
976 {
977 //items have varying rotation values - we can't rotate the mouse handles to match
978 setRotation( 0 );
979 }
980
981 //get bounds of all selected items
982 QRectF newHandleBounds = selectionBounds();
983
984 //update size and position of handle object
985 setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
986 setPos( mapToScene( newHandleBounds.topLeft() ) );
987
988 show();
989 }
990 else
991 {
992 //no items selected, hide handles
993 hide();
994 }
995
996 //force redraw
997 update();
998}
999
1000void QgsGraphicsViewMouseHandles::rotateMouseMove( QPointF currentPosition, bool snapToCommonAngles )
1001{
1002 if ( !scene() )
1003 {
1004 return;
1005 }
1006
1007 mRotationCurrent = std::atan2( currentPosition.y() - mRotationCenter.y(), currentPosition.x() - mRotationCenter.x() ) * 180 / M_PI;
1008 mRotationDelta = mRotationCurrent - mRotationBegin;
1009 if ( snapToCommonAngles )
1010 {
1011 mRotationDelta = QgsLayoutUtils::snappedAngle( mRotationDelta );
1012 }
1013
1014 const double itemRotationRadian = rotation() * M_PI / 180;
1015 const double deltaX = ( rect().width() / 2 ) * cos( itemRotationRadian ) - ( rect().height() / 2 ) * sin( itemRotationRadian );
1016 const double deltaY = ( rect().width() / 2 ) * sin( itemRotationRadian ) + ( rect().height() / 2 ) * cos( itemRotationRadian );
1017
1018 QTransform rotateTransform;
1019 rotateTransform.translate( deltaX, deltaY );
1020 rotateTransform.rotate( mRotationDelta );
1021 rotateTransform.translate( -deltaX, -deltaY );
1022 setTransform( rotateTransform );
1023
1024 //show current selection rotation in status bar
1025 showStatusMessage( tr( "rotation: %1°" ).arg( QString::number( mRotationDelta, 'f', 2 ) ) );
1026
1027 return;
1028}
1029
1030void QgsGraphicsViewMouseHandles::dragMouseMove( QPointF currentPosition, bool lockMovement, bool preventSnap )
1031{
1032 if ( !scene() )
1033 {
1034 return;
1035 }
1036
1037 //calculate total amount of mouse movement since drag began
1038 double moveX = currentPosition.x() - mBeginMouseEventPos.x();
1039 double moveY = currentPosition.y() - mBeginMouseEventPos.y();
1040
1041 //find target position before snapping (in scene coordinates)
1042 QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
1043
1044 QPointF snappedLeftPoint;
1045
1046 //no snapping for rotated items for now
1047 if ( !preventSnap && qgsDoubleNear( rotation(), 0.0 ) )
1048 {
1049 //snap to grid and guides
1050 snappedLeftPoint = snapPoint( upperLeftPoint, Item );
1051 }
1052 else
1053 {
1054 //no snapping
1055 snappedLeftPoint = upperLeftPoint;
1056 hideAlignItems();
1057 }
1058
1059 //calculate total shift for item from beginning of drag operation to current position
1060 double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
1061 double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
1062
1063 if ( lockMovement )
1064 {
1065 //constrained (shift) moving should lock to horizontal/vertical movement
1066 //reset the smaller of the x/y movements
1067 if ( std::fabs( moveRectX ) <= std::fabs( moveRectY ) )
1068 {
1069 moveRectX = 0;
1070 }
1071 else
1072 {
1073 moveRectY = 0;
1074 }
1075 }
1076
1077 //shift handle item to new position
1078 QTransform moveTransform;
1079 moveTransform.translate( moveRectX, moveRectY );
1080 setTransform( moveTransform );
1081
1082 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
1083 for ( QGraphicsItem *item : selectedItems )
1084 {
1085 previewItemMove( item, moveRectX, moveRectY );
1086 }
1087 //show current displacement of selection in status bar
1088 showStatusMessage( tr( "dx: %1 mm dy: %2 mm" ).arg( moveRectX ).arg( moveRectY ) );
1089}
1090
1091void QgsGraphicsViewMouseHandles::resizeMouseMove( QPointF currentPosition, bool lockRatio, bool fromCenter )
1092{
1093 if ( !scene() )
1094 {
1095 return;
1096 }
1097
1098 double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
1099
1100 QPointF beginMousePos;
1101 QPointF finalPosition;
1102 if ( qgsDoubleNear( rotation(), 0.0 ) )
1103 {
1104 //snapping only occurs if handles are not rotated for now
1105
1106 bool snapVertical = mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeft
1107 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRight
1108 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftUp
1109 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightUp
1110 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftDown
1111 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightDown;
1112
1113 bool snapHorizontal = mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeUp
1114 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeDown
1115 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftUp
1116 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightUp
1117 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeLeftDown
1118 || mCurrentMouseMoveAction == Qgis::MouseHandlesAction::ResizeRightDown;
1119
1120 //subtract cursor edge offset from begin mouse event and current cursor position, so that snapping occurs to edge of mouse handles
1121 //rather then cursor position
1122 beginMousePos = mapFromScene( QPointF( mBeginMouseEventPos.x() - mCursorOffset.width(), mBeginMouseEventPos.y() - mCursorOffset.height() ) );
1123 QPointF snappedPosition = snapPoint( QPointF( currentPosition.x() - mCursorOffset.width(), currentPosition.y() - mCursorOffset.height() ), Point, snapHorizontal, snapVertical );
1124 finalPosition = mapFromScene( snappedPosition );
1125 }
1126 else
1127 {
1128 //no snapping for rotated items for now
1129 beginMousePos = mapFromScene( mBeginMouseEventPos );
1130 finalPosition = mapFromScene( currentPosition );
1131 }
1132
1133 double diffX = finalPosition.x() - beginMousePos.x();
1134 double diffY = finalPosition.y() - beginMousePos.y();
1135
1136 double ratio = 0;
1137 if ( lockRatio && !qgsDoubleNear( mBeginHandleHeight, 0.0 ) )
1138 {
1139 ratio = mBeginHandleWidth / mBeginHandleHeight;
1140 }
1141
1142 switch ( mCurrentMouseMoveAction )
1143 {
1144 //vertical resize
1146 {
1147 if ( ratio )
1148 {
1149 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1150 mx = -diffX / 2;
1151 my = diffY;
1152 rx = diffX;
1153 ry = -diffY;
1154 }
1155 else
1156 {
1157 mx = 0;
1158 my = diffY;
1159 rx = 0;
1160 ry = -diffY;
1161 }
1162 break;
1163 }
1164
1166 {
1167 if ( ratio )
1168 {
1169 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1170 mx = -diffX / 2;
1171 my = 0;
1172 rx = diffX;
1173 ry = diffY;
1174 }
1175 else
1176 {
1177 mx = 0;
1178 my = 0;
1179 rx = 0;
1180 ry = diffY;
1181 }
1182 break;
1183 }
1184
1185 //horizontal resize
1187 {
1188 if ( ratio )
1189 {
1190 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1191 mx = diffX;
1192 my = -diffY / 2;
1193 rx = -diffX;
1194 ry = diffY;
1195 }
1196 else
1197 {
1198 mx = diffX, my = 0;
1199 rx = -diffX;
1200 ry = 0;
1201 }
1202 break;
1203 }
1204
1206 {
1207 if ( ratio )
1208 {
1209 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1210 mx = 0;
1211 my = -diffY / 2;
1212 rx = diffX;
1213 ry = diffY;
1214 }
1215 else
1216 {
1217 mx = 0;
1218 my = 0;
1219 rx = diffX, ry = 0;
1220 }
1221 break;
1222 }
1223
1224 //diagonal resize
1226 {
1227 if ( ratio )
1228 {
1229 //ratio locked resize
1230 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1231 {
1232 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight - diffY ) * ratio );
1233 }
1234 else
1235 {
1236 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth - diffX ) / ratio );
1237 }
1238 }
1239 mx = diffX, my = diffY;
1240 rx = -diffX;
1241 ry = -diffY;
1242 break;
1243 }
1244
1246 {
1247 if ( ratio )
1248 {
1249 //ratio locked resize
1250 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1251 {
1252 diffX = ( ( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
1253 }
1254 else
1255 {
1256 diffY = ( ( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
1257 }
1258 }
1259 mx = 0;
1260 my = 0;
1261 rx = diffX, ry = diffY;
1262 break;
1263 }
1264
1266 {
1267 if ( ratio )
1268 {
1269 //ratio locked resize
1270 if ( ( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
1271 {
1272 diffX = ( ( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
1273 }
1274 else
1275 {
1276 diffY = mBeginHandleHeight - ( ( mBeginHandleWidth + diffX ) / ratio );
1277 }
1278 }
1279 mx = 0;
1280 my = diffY, rx = diffX, ry = -diffY;
1281 break;
1282 }
1283
1285 {
1286 if ( ratio )
1287 {
1288 //ratio locked resize
1289 if ( ( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
1290 {
1291 diffX = mBeginHandleWidth - ( ( mBeginHandleHeight + diffY ) * ratio );
1292 }
1293 else
1294 {
1295 diffY = ( ( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
1296 }
1297 }
1298 mx = diffX, my = 0;
1299 rx = -diffX;
1300 ry = diffY;
1301 break;
1302 }
1303
1311 break;
1312 }
1313
1314 //resizing from center of objects?
1315 if ( fromCenter )
1316 {
1317 my = -ry;
1318 mx = -rx;
1319 ry = 2 * ry;
1320 rx = 2 * rx;
1321 }
1322
1323 //update selection handle rectangle
1324
1325 //make sure selection handle size rectangle is normalized (ie, left coord < right coord)
1326 mResizeMoveX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
1327 mResizeMoveY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
1328
1329 //calculate movement in scene coordinates
1330 QLineF translateLine = QLineF( 0, 0, mResizeMoveX, mResizeMoveY );
1331 translateLine.setAngle( translateLine.angle() - rotation() );
1332 QPointF sceneTranslate = translateLine.p2();
1333
1334 //move selection handles
1335 QTransform itemTransform;
1336 itemTransform.translate( sceneTranslate.x(), sceneTranslate.y() );
1337 setTransform( itemTransform );
1338
1339 //handle non-normalised resizes - e.g., dragging the left handle so far to the right that it's past the right handle
1340 if ( mBeginHandleWidth + rx >= 0 && mBeginHandleHeight + ry >= 0 )
1341 {
1342 mResizeRect = QRectF( 0, 0, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
1343 }
1344 else if ( mBeginHandleHeight + ry >= 0 )
1345 {
1346 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), 0 ), QPointF( 0, mBeginHandleHeight + ry ) );
1347 }
1348 else if ( mBeginHandleWidth + rx >= 0 )
1349 {
1350 mResizeRect = QRectF( QPointF( 0, -( mBeginHandleHeight + ry ) ), QPointF( mBeginHandleWidth + rx, 0 ) );
1351 }
1352 else
1353 {
1354 mResizeRect = QRectF( QPointF( -( mBeginHandleWidth + rx ), -( mBeginHandleHeight + ry ) ), QPointF( 0, 0 ) );
1355 }
1356
1357 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
1358 QRectF newHandleBounds;
1359 for ( QGraphicsItem *item : selectedItems )
1360 {
1361 //get stored item bounds in mouse handle item's coordinate system
1362 QRectF thisItemRect = mapRectFromScene( storedItemRect( item ) );
1363 //now, resize it relative to the current resized dimensions of the mouse handles
1364 relativeResizeRect( thisItemRect, QRectF( -mResizeMoveX, -mResizeMoveY, mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
1365
1366 thisItemRect = mapRectFromScene( previewSetItemRect( item, mapRectToScene( thisItemRect ) ) );
1367 newHandleBounds = newHandleBounds.isValid() ? newHandleBounds.united( thisItemRect ) : thisItemRect;
1368 }
1369
1370 setRect( newHandleBounds.isValid() ? newHandleBounds : QRectF( 0, 0, std::fabs( mBeginHandleWidth + rx ), std::fabs( mBeginHandleHeight + ry ) ) );
1371
1372 //show current size of selection in status bar
1373 showStatusMessage( tr( "width: %1 mm height: %2 mm" ).arg( rect().width() ).arg( rect().height() ) );
1374}
1375
1376void QgsGraphicsViewMouseHandles::setHandleSize( double size )
1377{
1378 mHandleSize = size;
1379}
1380
1381void QgsGraphicsViewMouseHandles::mouseDoubleClickEvent( QGraphicsSceneMouseEvent *event )
1382{
1383 Q_UNUSED( event )
1384
1385 mDoubleClickInProgress = true;
1386}
1387
1388QSizeF QgsGraphicsViewMouseHandles::calcCursorEdgeOffset( QPointF cursorPos )
1389{
1390 //find offset between cursor position and actual edge of item
1391 QPointF sceneMousePos = mapFromScene( cursorPos );
1392
1393 switch ( mCurrentMouseMoveAction )
1394 {
1395 //vertical resize
1397 return QSizeF( 0, sceneMousePos.y() );
1398
1400 return QSizeF( 0, sceneMousePos.y() - rect().height() );
1401
1402 //horizontal resize
1404 return QSizeF( sceneMousePos.x(), 0 );
1405
1407 return QSizeF( sceneMousePos.x() - rect().width(), 0 );
1408
1409 //diagonal resize
1411 return QSizeF( sceneMousePos.x(), sceneMousePos.y() );
1412
1414 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() - rect().height() );
1415
1417 return QSizeF( sceneMousePos.x() - rect().width(), sceneMousePos.y() );
1418
1420 return QSizeF( sceneMousePos.x(), sceneMousePos.y() - rect().height() );
1421
1429 return QSizeF();
1430 }
1431
1432 return QSizeF();
1433}
1434
1435QRectF QgsGraphicsViewMouseHandles::selectionBounds() const
1436{
1437 //calculate bounds of all currently selected items in mouse handle coordinate system
1438 const QList<QGraphicsItem *> selectedItems = selectedSceneItems( false );
1439 auto itemIter = selectedItems.constBegin();
1440
1441 //start with handle bounds of first selected item
1442 QRectF bounds = mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect();
1443
1444 //iterate through remaining items, expanding the bounds as required
1445 for ( ++itemIter; itemIter != selectedItems.constEnd(); ++itemIter )
1446 {
1447 bounds = bounds.united( mapFromItem( ( *itemIter ), itemRect( *itemIter ) ).boundingRect() );
1448 }
1449
1450 return bounds;
1451}
1452
1453void QgsGraphicsViewMouseHandles::relativeResizeRect( QRectF &rectToResize, const QRectF &boundsBefore, const QRectF &boundsAfter )
1454{
1455 //linearly scale rectToResize relative to the scaling from boundsBefore to boundsAfter
1456 double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1457 double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
1458 double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1459 double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
1460
1461 rectToResize.setRect( left, top, right - left, bottom - top );
1462}
1463
1464double QgsGraphicsViewMouseHandles::relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax )
1465{
1466 //calculate parameters for linear scale between before and after ranges
1467 double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
1468 double c = afterMin - ( beforeMin * m );
1469
1470 //return linearly scaled position
1471 return m * position + c;
1472}
1473
1474QList<QGraphicsItem *> QgsGraphicsViewMouseHandles::filterItemListForMoveOperation( const QList<QGraphicsItem *> &items )
1475{
1476 return items;
1477}
1478
MouseHandlesAction
Action to be performed by the mouse handles.
Definition qgis.h:6949
@ ResizeRightDown
Resize right down (Bottom right handle).
Definition qgis.h:6958
@ NoAction
No action.
Definition qgis.h:6964
@ SelectItem
Select item.
Definition qgis.h:6963
@ ResizeLeftUp
Resize left up (Top left handle).
Definition qgis.h:6955
@ ResizeLeftDown
Resize left down (Bottom left handle).
Definition qgis.h:6957
@ ResizeRight
Resize right (Right handle).
Definition qgis.h:6954
@ MoveItem
Move item.
Definition qgis.h:6950
@ ResizeDown
Resize down (Bottom handle).
Definition qgis.h:6952
@ RotateTopRight
Rotate from top right handle.
Definition qgis.h:6960
@ RotateTopLeft
Rotate from top left handle.
Definition qgis.h:6959
@ RotateBottomRight
Rotate right bottom right handle.
Definition qgis.h:6962
@ ResizeRightUp
Resize right up (Top right handle).
Definition qgis.h:6956
@ ResizeLeft
Resize left (Left handle).
Definition qgis.h:6953
@ RotateBottomLeft
Rotate from bottom left handle.
Definition qgis.h:6961
@ ResizeUp
Resize up (Top handle).
Definition qgis.h:6951
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:7826
#define QgsDebugError(str)
Definition qgslogger.h:71