-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgscomposermousehandles.cpp
1080 lines (959 loc) · 33.1 KB
/
qgscomposermousehandles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgscomposermousehandles.cpp
-------------------
begin : September 2013
copyright : (C) 2013 by Nyall Dawson, Radim Blazek
email : nyall.dawson@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QPainter>
#include <QWidget>
#include <limits>
#include "qgscomposermousehandles.h"
#include "qgscomposeritem.h"
#include "qgscomposition.h"
#include "qgis.h"
#include "qgslogger.h"
QgsComposerMouseHandles::QgsComposerMouseHandles( QgsComposition *composition ) : QObject( 0 ),
QGraphicsRectItem( 0 ),
mComposition( composition ),
mGraphicsView( 0 ),
mBeginHandleWidth( 0 ),
mBeginHandleHeight( 0 ),
mIsDragging( false ),
mIsResizing( false ),
mHAlignSnapItem( 0 ),
mVAlignSnapItem( 0 )
{
//listen for selection changes, and update handles accordingly
QObject::connect( mComposition, SIGNAL( selectionChanged() ), this, SLOT( selectionChanged() ) );
//accept hover events, required for changing cursor to resize cursors
setAcceptsHoverEvents( true );
}
QgsComposerMouseHandles::~QgsComposerMouseHandles()
{
}
QGraphicsView* QgsComposerMouseHandles::graphicsView()
{
//have we already found the current view?
if ( mGraphicsView )
{
return mGraphicsView;
}
//otherwise, try and find current view attached to composition
if ( scene() )
{
QList<QGraphicsView*> viewList = scene()->views();
if ( viewList.size() > 0 )
{
mGraphicsView = viewList.at( 0 );
return mGraphicsView;
}
}
//no view attached to composition
return 0;
}
void QgsComposerMouseHandles::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
Q_UNUSED( itemStyle );
Q_UNUSED( pWidget );
if ( mComposition->plotStyle() != QgsComposition::Preview )
{
//don't draw selection handles in composition outputs
return;
}
//draw resize handles around bounds of entire selection
double rectHandlerSize = rectHandlerBorderTolerance();
drawHandles( painter, rectHandlerSize );
//draw dotted boxes around selected items
drawSelectedItemBounds( painter );
}
void QgsComposerMouseHandles::drawHandles( QPainter* painter, double rectHandlerSize )
{
//blue, zero width cosmetic pen for outline
QPen handlePen = QPen( QColor( 55, 140, 195, 255 ) );
handlePen.setWidth( 0 );
painter->setPen( handlePen );
//draw box around entire selection bounds
painter->setBrush( Qt::NoBrush );
painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
//draw resize handles, using a filled white box
painter->setBrush( QColor( 255, 255, 255, 255 ) );
//top left
painter->drawRect( QRectF( 0, 0, rectHandlerSize, rectHandlerSize ) );
//mid top
painter->drawRect( QRectF(( rect().width() - rectHandlerSize ) / 2, 0, rectHandlerSize, rectHandlerSize ) );
//top right
painter->drawRect( QRectF( rect().width() - rectHandlerSize, 0, rectHandlerSize, rectHandlerSize ) );
//mid left
painter->drawRect( QRectF( 0, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
//mid right
painter->drawRect( QRectF( rect().width() - rectHandlerSize, ( rect().height() - rectHandlerSize ) / 2, rectHandlerSize, rectHandlerSize ) );
//bottom left
painter->drawRect( QRectF( 0, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
//mid bottom
painter->drawRect( QRectF(( rect().width() - rectHandlerSize ) / 2, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
//bottom right
painter->drawRect( QRectF( rect().width() - rectHandlerSize, rect().height() - rectHandlerSize, rectHandlerSize, rectHandlerSize ) );
}
void QgsComposerMouseHandles::drawSelectedItemBounds( QPainter* painter )
{
//draw dotted border around selected items to give visual feedback which items are selected
QList<QgsComposerItem*> selectedItems = mComposition->selectedComposerItems();
if ( selectedItems.size() == 0 )
{
return;
}
//use difference mode so that they are visible regardless of item colors
painter->save();
painter->setCompositionMode( QPainter::CompositionMode_Difference );
// use a grey dashed pen - in difference mode this should always be visible
QPen selectedItemPen = QPen( QColor( 144, 144, 144, 255 ) );
selectedItemPen.setStyle( Qt::DashLine );
selectedItemPen.setWidth( 0 );
painter->setPen( selectedItemPen );
painter->setBrush( Qt::NoBrush );
QList<QgsComposerItem*>::iterator itemIter = selectedItems.begin();
for ( ; itemIter != selectedItems.end(); ++itemIter )
{
//scene bounds of selected item
QRectF itemSceneBounds = ( *itemIter )->sceneBoundingRect();
//convert scene bounds to handle item bounds
QRectF itemBounds;
if ( mIsDragging && !( *itemIter )->positionLock() )
{
//if currently dragging, draw selected item bounds relative to current mouse position
itemBounds = mapRectFromScene( itemSceneBounds );
itemBounds.translate( transform().dx(), transform().dy() );
}
else if ( mIsResizing && !( *itemIter )->positionLock() )
{
//if currently resizing, calculate relative resize of this item
itemBounds = itemSceneBounds;
relativeResizeRect( itemBounds, QRectF( mBeginHandlePos.x(), mBeginHandlePos.y(), mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
itemBounds = mapRectFromScene( itemBounds );
}
else
{
//not resizing or moving, so just map from scene bounds
itemBounds = mapRectFromScene( itemSceneBounds );
}
painter->drawRect( itemBounds );
}
painter->restore();
}
void QgsComposerMouseHandles::selectionChanged()
{
//listen out for selected items' sizeChanged signal
QList<QGraphicsItem *> itemList = composition()->items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
QgsComposerItem* item = dynamic_cast<QgsComposerItem *>( *itemIt );
if ( item )
{
if ( item->selected() )
{
QObject::connect( item, SIGNAL( sizeChanged() ), this, SLOT( selectedItemSizeChanged() ) );
}
else
{
QObject::disconnect( item, SIGNAL( sizeChanged() ), this, 0 );
}
}
}
updateHandles();
}
void QgsComposerMouseHandles::selectedItemSizeChanged()
{
if ( !mIsDragging && !mIsResizing )
{
//only required for non-mouse initiated size changes
updateHandles();
}
}
void QgsComposerMouseHandles::updateHandles()
{
//recalculate size and position of handle item
//first check to see if any items are selected
QList<QgsComposerItem*> selectedItems = mComposition->selectedComposerItems();
if ( selectedItems.size() > 0 )
{
//one or more items are selected, get bounds of all selected items
QRectF newHandleBounds = selectionBounds();
//update size and position of handle object
setRect( 0, 0, newHandleBounds.width(), newHandleBounds.height() );
setPos( newHandleBounds.topLeft() );
show();
}
else
{
//no items selected, hide handles
hide();
}
//force redraw
update();
}
QRectF QgsComposerMouseHandles::selectionBounds() const
{
//calculate scene bounds of all currently selected items
QList<QgsComposerItem*> selectedItems = mComposition->selectedComposerItems();
QList<QgsComposerItem*>::iterator itemIter = selectedItems.begin();
//start with scene bounds of first selected item
QRectF bounds = ( *itemIter )->sceneBoundingRect();
//iterate through remaining items, expanding the bounds as required
for ( ++itemIter; itemIter != selectedItems.end(); ++itemIter )
{
bounds = bounds.united(( *itemIter )->sceneBoundingRect() );
}
return bounds;
}
double QgsComposerMouseHandles::rectHandlerBorderTolerance()
{
//calculate size for resize handles
//get view scale factor
double viewScaleFactor = graphicsView()->transform().m11();
//size of handle boxes depends on zoom level in composer view
double rectHandlerSize = 10.0 / viewScaleFactor;
//make sure the boxes don't get too large
if ( rectHandlerSize > ( rect().width() / 3 ) )
{
rectHandlerSize = rect().width() / 3;
}
if ( rectHandlerSize > ( rect().height() / 3 ) )
{
rectHandlerSize = rect().height() / 3;
}
return rectHandlerSize;
}
Qt::CursorShape QgsComposerMouseHandles::cursorForPosition( const QPointF& itemCoordPos )
{
QgsComposerMouseHandles::MouseAction mouseAction = mouseActionForPosition( itemCoordPos );
switch ( mouseAction )
{
case NoAction:
return Qt::ForbiddenCursor;
case MoveItem:
return Qt::SizeAllCursor;
case ResizeUp:
case ResizeDown:
return Qt::SizeVerCursor;
case ResizeLeft:
case ResizeRight:
return Qt::SizeHorCursor;
case ResizeLeftUp:
case ResizeRightDown:
return Qt::SizeFDiagCursor;
case ResizeRightUp:
case ResizeLeftDown:
return Qt::SizeBDiagCursor;
case SelectItem:
default:
return Qt::ArrowCursor;
}
}
QgsComposerMouseHandles::MouseAction QgsComposerMouseHandles::mouseActionForPosition( const QPointF& itemCoordPos )
{
bool nearLeftBorder = false;
bool nearRightBorder = false;
bool nearLowerBorder = false;
bool nearUpperBorder = false;
double borderTolerance = rectHandlerBorderTolerance();
if ( itemCoordPos.x() >= 0 && itemCoordPos.x() < borderTolerance )
{
nearLeftBorder = true;
}
if ( itemCoordPos.y() >= 0 && itemCoordPos.y() < borderTolerance )
{
nearUpperBorder = true;
}
if ( itemCoordPos.x() <= rect().width() && itemCoordPos.x() > ( rect().width() - borderTolerance ) )
{
nearRightBorder = true;
}
if ( itemCoordPos.y() <= rect().height() && itemCoordPos.y() > ( rect().height() - borderTolerance ) )
{
nearLowerBorder = true;
}
if ( nearLeftBorder && nearUpperBorder )
{
return QgsComposerMouseHandles::ResizeLeftUp;
}
else if ( nearLeftBorder && nearLowerBorder )
{
return QgsComposerMouseHandles::ResizeLeftDown;
}
else if ( nearRightBorder && nearUpperBorder )
{
return QgsComposerMouseHandles::ResizeRightUp;
}
else if ( nearRightBorder && nearLowerBorder )
{
return QgsComposerMouseHandles::ResizeRightDown;
}
else if ( nearLeftBorder )
{
return QgsComposerMouseHandles::ResizeLeft;
}
else if ( nearRightBorder )
{
return QgsComposerMouseHandles::ResizeRight;
}
else if ( nearUpperBorder )
{
return QgsComposerMouseHandles::ResizeUp;
}
else if ( nearLowerBorder )
{
return QgsComposerMouseHandles::ResizeDown;
}
//find out if cursor position is over a selected item
QPointF scenePoint = mapToScene( itemCoordPos );
QList<QGraphicsItem *> itemsAtCursorPos = mComposition->items( scenePoint );
if ( itemsAtCursorPos.size() == 0 )
{
//no items at cursor position
return QgsComposerMouseHandles::SelectItem;
}
QList<QGraphicsItem*>::iterator itemIter = itemsAtCursorPos.begin();
for ( ; itemIter != itemsAtCursorPos.end(); ++itemIter )
{
QgsComposerItem* item = dynamic_cast<QgsComposerItem *>(( *itemIter ) );
if ( item && item->selected() )
{
//cursor is over a selected composer item
return QgsComposerMouseHandles::MoveItem;
}
}
//default
return QgsComposerMouseHandles::SelectItem;
}
QgsComposerMouseHandles::MouseAction QgsComposerMouseHandles::mouseActionForScenePos( const QPointF& sceneCoordPos )
{
// convert sceneCoordPos to item coordinates
QPointF itemPos = mapFromScene( sceneCoordPos );
return mouseActionForPosition( itemPos );
}
void QgsComposerMouseHandles::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
{
setViewportCursor( cursorForPosition( event->pos() ) );
}
void QgsComposerMouseHandles::hoverLeaveEvent( QGraphicsSceneHoverEvent * event )
{
Q_UNUSED( event );
setViewportCursor( Qt::ArrowCursor );
}
void QgsComposerMouseHandles::setViewportCursor( Qt::CursorShape cursor )
{
//workaround qt bug #3732 by setting cursor for QGraphicsView viewport,
//rather then setting it directly here
if ( !mComposition->preventCursorChange() )
{
graphicsView()->viewport()->setCursor( cursor );
}
}
void QgsComposerMouseHandles::mouseMoveEvent( QGraphicsSceneMouseEvent* event )
{
bool shiftModifier = false;
bool controlModifier = false;
if ( event->modifiers() & Qt::ShiftModifier )
{
//shift key depressed
shiftModifier = true;
}
if ( event->modifiers() & Qt::ControlModifier )
{
//shift key depressed
controlModifier = true;
}
if ( mIsDragging )
{
//currently dragging a selection
dragMouseMove( event->lastScenePos(), shiftModifier, controlModifier );
}
else if ( mIsResizing )
{
//currently resizing a selection
resizeMouseMove( event->lastScenePos(), shiftModifier, controlModifier );
}
mLastMouseEventPos = event->lastScenePos();
}
void QgsComposerMouseHandles::mouseReleaseEvent( QGraphicsSceneMouseEvent* event )
{
QPointF mouseMoveStopPoint = event->lastScenePos();
double diffX = mouseMoveStopPoint.x() - mMouseMoveStartPos.x();
double diffY = mouseMoveStopPoint.y() - mMouseMoveStartPos.y();
//it was only a click
if ( qAbs( diffX ) < std::numeric_limits<double>::min() && qAbs( diffY ) < std::numeric_limits<double>::min() )
{
mIsDragging = false;
mIsResizing = false;
return;
}
if ( mCurrentMouseMoveAction == QgsComposerMouseHandles::MoveItem )
{
//move selected items
QUndoCommand* parentCommand = new QUndoCommand( tr( "Change item position" ) );
QPointF mEndHandleMovePos = scenePos();
//move all selected items
QList<QgsComposerItem*> selectedItems = mComposition->selectedComposerItems();
QList<QgsComposerItem*>::iterator itemIter = selectedItems.begin();
for ( ; itemIter != selectedItems.end(); ++itemIter )
{
if (( *itemIter )->positionLock() )
{
//don't move locked items
continue;
}
QgsComposerItemCommand* subcommand = new QgsComposerItemCommand( *itemIter, "", parentCommand );
subcommand->savePreviousState();
( *itemIter )->move( mEndHandleMovePos.x() - mBeginHandlePos.x(), mEndHandleMovePos.y() - mBeginHandlePos.y() );
subcommand->saveAfterState();
}
mComposition->undoStack()->push( parentCommand );
}
else if ( mCurrentMouseMoveAction != QgsComposerMouseHandles::NoAction )
{
//resize selected items
QUndoCommand* parentCommand = new QUndoCommand( tr( "Change item size" ) );
//resize all selected items
QList<QgsComposerItem*> selectedItems = mComposition->selectedComposerItems();
QList<QgsComposerItem*>::iterator itemIter = selectedItems.begin();
for ( ; itemIter != selectedItems.end(); ++itemIter )
{
if (( *itemIter )->positionLock() )
{
//don't resize locked items
continue;
}
QgsComposerItemCommand* subcommand = new QgsComposerItemCommand( *itemIter, "", parentCommand );
subcommand->savePreviousState();
QRectF itemBounds = ( *itemIter )->sceneBoundingRect();
relativeResizeRect( itemBounds, QRectF( mBeginHandlePos.x(), mBeginHandlePos.y(), mBeginHandleWidth, mBeginHandleHeight ), mResizeRect );
( *itemIter )->setSceneRect( itemBounds );
subcommand->saveAfterState();
}
mComposition->undoStack()->push( parentCommand );
}
deleteAlignItems();
if ( mIsDragging )
{
mIsDragging = false;
}
if ( mIsResizing )
{
mIsResizing = false;
}
//reset default action
mCurrentMouseMoveAction = QgsComposerMouseHandles::MoveItem;
setViewportCursor( Qt::ArrowCursor );
//redraw handles
resetTransform();
updateHandles();
}
void QgsComposerMouseHandles::mousePressEvent( QGraphicsSceneMouseEvent* event )
{
//save current cursor position
mMouseMoveStartPos = event->lastScenePos();
mLastMouseEventPos = event->lastScenePos();
//save current item geometry
mBeginMouseEventPos = event->lastScenePos();
mBeginHandlePos = scenePos();
mBeginHandleWidth = rect().width();
mBeginHandleHeight = rect().height();
//type of mouse move action
mCurrentMouseMoveAction = mouseActionForPosition( event->pos() );
deleteAlignItems();
if ( mCurrentMouseMoveAction == QgsComposerMouseHandles::MoveItem )
{
mIsDragging = true;
}
else if ( mCurrentMouseMoveAction != QgsComposerMouseHandles::SelectItem &&
mCurrentMouseMoveAction != QgsComposerMouseHandles::NoAction )
{
mIsResizing = true;
mResizeRect = QRectF( mBeginHandlePos.x(), mBeginHandlePos.y(), mBeginHandleWidth, mBeginHandleHeight );
}
}
void QgsComposerMouseHandles::dragMouseMove( const QPointF& currentPosition, bool lockMovement, bool preventSnap )
{
if ( !mComposition )
{
return;
}
//calculate total amount of mouse movement since drag began
double moveX = currentPosition.x() - mBeginMouseEventPos.x();
double moveY = currentPosition.y() - mBeginMouseEventPos.y();
//find target position before snapping (in scene coordinates)
QPointF upperLeftPoint( mBeginHandlePos.x() + moveX, mBeginHandlePos.y() + moveY );
QPointF snappedLeftPoint;
if ( !preventSnap )
{
//snap to grid and guides
snappedLeftPoint = snapPoint( upperLeftPoint, QgsComposerMouseHandles::Item );
}
else
{
//no snapping
snappedLeftPoint = upperLeftPoint;
deleteAlignItems();
}
//calculate total shift for item from beginning of drag operation to current position
double moveRectX = snappedLeftPoint.x() - mBeginHandlePos.x();
double moveRectY = snappedLeftPoint.y() - mBeginHandlePos.y();
if ( lockMovement )
{
//constrained (shift) moving should lock to horizontal/vertical movement
//reset the smaller of the x/y movements
if ( abs( moveRectX ) <= abs( moveRectY ) )
{
moveRectX = 0;
}
else
{
moveRectY = 0;
}
}
//shift handle item to new position
QTransform moveTransform;
moveTransform.translate( moveRectX, moveRectY );
setTransform( moveTransform );
}
void QgsComposerMouseHandles::resizeMouseMove( const QPointF& currentPosition, bool lockRatio, bool fromCenter )
{
if ( !mComposition )
{
return;
}
double mx = 0.0, my = 0.0, rx = 0.0, ry = 0.0;
QPointF snappedPosition = snapPoint( currentPosition, QgsComposerMouseHandles::Point );
double diffX = 0;
double diffY = 0;
double ratio = 0;
if ( lockRatio && mBeginHandleHeight != 0 )
{
ratio = mBeginHandleWidth / mBeginHandleHeight;
}
switch ( mCurrentMouseMoveAction )
{
//vertical resize
case QgsComposerMouseHandles::ResizeUp:
diffY = snappedPosition.y() - mBeginHandlePos.y();
if ( ratio )
{
diffX = (( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
mx = -diffX / 2; my = diffY; rx = diffX; ry = -diffY;
}
else
{
mx = 0; my = diffY; rx = 0; ry = -diffY;
}
break;
case QgsComposerMouseHandles::ResizeDown:
diffY = snappedPosition.y() - ( mBeginHandlePos.y() + mBeginHandleHeight );
if ( ratio )
{
diffX = (( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
mx = -diffX / 2; my = 0; rx = diffX; ry = diffY;
}
else
{
mx = 0; my = 0; rx = 0; ry = diffY;
}
break;
//horizontal resize
case QgsComposerMouseHandles::ResizeLeft:
diffX = snappedPosition.x() - mBeginHandlePos.x();
if ( ratio )
{
diffY = (( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
mx = diffX; my = -diffY / 2; rx = -diffX; ry = diffY;
}
else
{
mx = diffX, my = 0; rx = -diffX; ry = 0;
}
break;
case QgsComposerMouseHandles::ResizeRight:
diffX = snappedPosition.x() - ( mBeginHandlePos.x() + mBeginHandleWidth );
if ( ratio )
{
diffY = (( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
mx = 0; my = -diffY / 2; rx = diffX; ry = diffY;
}
else
{
mx = 0; my = 0; rx = diffX, ry = 0;
}
break;
//diagonal resize
case QgsComposerMouseHandles::ResizeLeftUp:
diffX = snappedPosition.x() - mBeginHandlePos.x();
diffY = snappedPosition.y() - mBeginHandlePos.y();
if ( ratio )
{
//ratio locked resize
if (( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
{
diffX = mBeginHandleWidth - (( mBeginHandleHeight - diffY ) * ratio );
}
else
{
diffY = mBeginHandleHeight - (( mBeginHandleWidth - diffX ) / ratio );
}
}
mx = diffX, my = diffY; rx = -diffX; ry = -diffY;
break;
case QgsComposerMouseHandles::ResizeRightDown:
diffX = snappedPosition.x() - ( mBeginHandlePos.x() + mBeginHandleWidth );
diffY = snappedPosition.y() - ( mBeginHandlePos.y() + mBeginHandleHeight );
if ( ratio )
{
//ratio locked resize
if (( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
{
diffX = (( mBeginHandleHeight + diffY ) * ratio ) - mBeginHandleWidth;
}
else
{
diffY = (( mBeginHandleWidth + diffX ) / ratio ) - mBeginHandleHeight;
}
}
mx = 0; my = 0; rx = diffX, ry = diffY;
break;
case QgsComposerMouseHandles::ResizeRightUp:
diffX = snappedPosition.x() - ( mBeginHandlePos.x() + mBeginHandleWidth );
diffY = snappedPosition.y() - mBeginHandlePos.y();
if ( ratio )
{
//ratio locked resize
if (( mBeginHandleWidth + diffX ) / ( mBeginHandleHeight - diffY ) > ratio )
{
diffX = (( mBeginHandleHeight - diffY ) * ratio ) - mBeginHandleWidth;
}
else
{
diffY = mBeginHandleHeight - (( mBeginHandleWidth + diffX ) / ratio );
}
}
mx = 0; my = diffY, rx = diffX, ry = -diffY;
break;
case QgsComposerMouseHandles::ResizeLeftDown:
diffX = snappedPosition.x() - mBeginHandlePos.x();
diffY = snappedPosition.y() - ( mBeginHandlePos.y() + mBeginHandleHeight );
if ( ratio )
{
//ratio locked resize
if (( mBeginHandleWidth - diffX ) / ( mBeginHandleHeight + diffY ) > ratio )
{
diffX = mBeginHandleWidth - (( mBeginHandleHeight + diffY ) * ratio );
}
else
{
diffY = (( mBeginHandleWidth - diffX ) / ratio ) - mBeginHandleHeight;
}
}
mx = diffX, my = 0; rx = -diffX; ry = diffY;
break;
case QgsComposerMouseHandles::MoveItem:
case QgsComposerMouseHandles::SelectItem:
case QgsComposerMouseHandles::NoAction:
break;
}
//resizing from center of objects?
if ( fromCenter )
{
my = -ry;
mx = -rx;
ry = 2 * ry;
rx = 2 * rx;
}
//update selection handle rectangle
QTransform itemTransform;
//make sure selection handle size rectangle is normalized (ie, left coord < right coord)
double translateX = mBeginHandleWidth + rx > 0 ? mx : mx + mBeginHandleWidth + rx;
double translateY = mBeginHandleHeight + ry > 0 ? my : my + mBeginHandleHeight + ry;
itemTransform.translate( translateX, translateY );
setTransform( itemTransform );
mResizeRect = QRectF( mBeginHandlePos.x() + mx, mBeginHandlePos.y() + my, mBeginHandleWidth + rx, mBeginHandleHeight + ry );
setRect( 0, 0, fabs( mBeginHandleWidth + rx ), fabs( mBeginHandleHeight + ry ) );
}
void QgsComposerMouseHandles::relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter )
{
//linearly scale rectToResize relative to the scaling from boundsBefore to boundsAfter
double left = relativePosition( rectToResize.left(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
double right = relativePosition( rectToResize.right(), boundsBefore.left(), boundsBefore.right(), boundsAfter.left(), boundsAfter.right() );
double top = relativePosition( rectToResize.top(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
double bottom = relativePosition( rectToResize.bottom(), boundsBefore.top(), boundsBefore.bottom(), boundsAfter.top(), boundsAfter.bottom() );
rectToResize.setRect( left, top, right - left, bottom - top );
}
double QgsComposerMouseHandles::relativePosition( double position, double beforeMin, double beforeMax, double afterMin, double afterMax )
{
//calculate parameters for linear scale between before and after ranges
double m = ( afterMax - afterMin ) / ( beforeMax - beforeMin );
double c = afterMin - ( beforeMin * m );
//return linearly scaled position
return m * position + c;
}
QPointF QgsComposerMouseHandles::snapPoint( const QPointF& point, QgsComposerMouseHandles::SnapGuideMode mode )
{
//snap to grid
QPointF snappedPoint = mComposition->snapPointToGrid( point );
if ( snappedPoint != point ) //don't do align snap if grid snap has been done
{
deleteAlignItems();
return snappedPoint;
}
//align item
if ( !mComposition->alignmentSnap() )
{
return point;
}
double alignX = 0;
double alignY = 0;
//depending on the mode, we either snap just the single point, or all the bounds of the selection
switch ( mode )
{
case QgsComposerMouseHandles::Item:
snappedPoint = alignItem( alignX, alignY, point.x(), point.y() );
break;
case QgsComposerMouseHandles::Point:
snappedPoint = alignPos( point, alignX, alignY );
break;
}
if ( alignX != -1 )
{
QGraphicsLineItem* item = hAlignSnapItem();
int numPages = mComposition->numPages();
double yLineCoord = 300; //default in case there is no single page
if ( numPages > 0 )
{
yLineCoord = mComposition->paperHeight() * numPages + mComposition->spaceBetweenPages() * ( numPages - 1 );
}
item->setLine( QLineF( alignX, 0, alignX, yLineCoord ) );
item->show();
}
else
{
deleteHAlignSnapItem();
}
if ( alignY != -1 )
{
QGraphicsLineItem* item = vAlignSnapItem();
item->setLine( QLineF( 0, alignY, mComposition->paperWidth(), alignY ) );
item->show();
}
else
{
deleteVAlignSnapItem();
}
return snappedPoint;
}
QGraphicsLineItem* QgsComposerMouseHandles::hAlignSnapItem()
{
if ( !mHAlignSnapItem )
{
mHAlignSnapItem = new QGraphicsLineItem( 0 );
mHAlignSnapItem->setPen( QPen( QColor( Qt::red ) ) );
scene()->addItem( mHAlignSnapItem );
mHAlignSnapItem->setZValue( 90 );
}
return mHAlignSnapItem;
}
QGraphicsLineItem* QgsComposerMouseHandles::vAlignSnapItem()
{
if ( !mVAlignSnapItem )
{
mVAlignSnapItem = new QGraphicsLineItem( 0 );
mVAlignSnapItem->setPen( QPen( QColor( Qt::red ) ) );
scene()->addItem( mVAlignSnapItem );
mVAlignSnapItem->setZValue( 90 );
}
return mVAlignSnapItem;
}
void QgsComposerMouseHandles::deleteHAlignSnapItem()
{
if ( mHAlignSnapItem )
{
scene()->removeItem( mHAlignSnapItem );
delete mHAlignSnapItem;
mHAlignSnapItem = 0;
}
}
void QgsComposerMouseHandles::deleteVAlignSnapItem()
{
if ( mVAlignSnapItem )
{
scene()->removeItem( mVAlignSnapItem );
delete mVAlignSnapItem;
mVAlignSnapItem = 0;
}
}
void QgsComposerMouseHandles::deleteAlignItems()
{
deleteHAlignSnapItem();
deleteVAlignSnapItem();
}
QPointF QgsComposerMouseHandles::alignItem( double& alignX, double& alignY, double unalignedX, double unalignedY )
{
double left = unalignedX;
double right = left + rect().width();
double midH = ( left + right ) / 2.0;
double top = unalignedY;
double bottom = top + rect().height();
double midV = ( top + bottom ) / 2.0;
QMap<double, const QgsComposerItem* > xAlignCoordinates;
QMap<double, const QgsComposerItem* > yAlignCoordinates;
collectAlignCoordinates( xAlignCoordinates, yAlignCoordinates );
//find nearest matches x
double xItemLeft = left; //new left coordinate of the item
double xAlignCoord = 0;
double smallestDiffX = DBL_MAX;
checkNearestItem( left, xAlignCoordinates, smallestDiffX, 0, xItemLeft, xAlignCoord );
checkNearestItem( midH, xAlignCoordinates, smallestDiffX, ( left - right ) / 2.0, xItemLeft, xAlignCoord );
checkNearestItem( right, xAlignCoordinates, smallestDiffX, left - right, xItemLeft, xAlignCoord );
//find nearest matches y
double yItemTop = top; //new top coordinate of the item
double yAlignCoord = 0;
double smallestDiffY = DBL_MAX;
checkNearestItem( top, yAlignCoordinates, smallestDiffY, 0, yItemTop, yAlignCoord );
checkNearestItem( midV, yAlignCoordinates, smallestDiffY, ( top - bottom ) / 2.0, yItemTop, yAlignCoord );
checkNearestItem( bottom, yAlignCoordinates, smallestDiffY, top - bottom, yItemTop, yAlignCoord );
double xCoord = ( smallestDiffX < 5 ) ? xItemLeft : unalignedX;
alignX = ( smallestDiffX < 5 ) ? xAlignCoord : -1;
double yCoord = ( smallestDiffY < 5 ) ? yItemTop : unalignedY;
alignY = ( smallestDiffY < 5 ) ? yAlignCoord : -1;
return QPointF( xCoord, yCoord );
}
QPointF QgsComposerMouseHandles::alignPos( const QPointF& pos, double& alignX, double& alignY )
{
QMap<double, const QgsComposerItem* > xAlignCoordinates;
QMap<double, const QgsComposerItem* > yAlignCoordinates;
collectAlignCoordinates( xAlignCoordinates, yAlignCoordinates );
double nearestX = pos.x();
double nearestY = pos.y();
if ( !nearestItem( xAlignCoordinates, pos.x(), nearestX )
|| !nearestItem( yAlignCoordinates, pos.y(), nearestY ) )
{
alignX = -1;
alignY = -1;
return pos;
}
QPointF result( pos.x(), pos.y() );
if ( abs( nearestX - pos.x() ) < mComposition->alignmentSnapTolerance() )
{
result.setX( nearestX );
alignX = nearestX;
}
else
{
alignX = -1;
}
if ( abs( nearestY - pos.y() ) < mComposition->alignmentSnapTolerance() )
{
result.setY( nearestY );
alignY = nearestY;
}
else
{
alignY = -1;
}
return result;
}
void QgsComposerMouseHandles::collectAlignCoordinates( QMap< double, const QgsComposerItem* >& alignCoordsX, QMap< double, const QgsComposerItem* >& alignCoordsY )
{
alignCoordsX.clear();
alignCoordsY.clear();
QList<QGraphicsItem *> itemList = mComposition->items();
QList<QGraphicsItem *>::iterator itemIt = itemList.begin();
for ( ; itemIt != itemList.end(); ++itemIt )
{
const QgsComposerItem* currentItem = dynamic_cast<const QgsComposerItem *>( *itemIt );
//don't snap to selected items, since they're the ones that will be snapping to something else
if ( !currentItem || currentItem->selected() )
{
continue;
}
alignCoordsX.insert( currentItem->transform().dx(), currentItem );
alignCoordsX.insert( currentItem->transform().dx() + currentItem->rect().width(), currentItem );
alignCoordsX.insert( currentItem->transform().dx() + currentItem->rect().center().x(), currentItem );