17
17
#include < stdexcept>
18
18
19
19
#include " qgscomposition.h"
20
- #include " qgscomposeritem.h"
21
20
#include " qgscomposerarrow.h"
22
21
#include " qgscomposerframe.h"
23
22
#include " qgscomposerhtml.h"
@@ -1077,25 +1076,28 @@ void QgsComposition::removeSnapLine( QGraphicsLineItem* line )
1077
1076
delete line;
1078
1077
}
1079
1078
1080
- QGraphicsLineItem* QgsComposition::nearestSnapLine ( bool horizontal, double x, double y, double tolerance )
1079
+ QGraphicsLineItem* QgsComposition::nearestSnapLine ( bool horizontal, double x, double y, double tolerance,
1080
+ QList< QPair< QgsComposerItem*, QgsComposerItem::ItemPositionMode> >& snappedItems )
1081
1081
{
1082
- bool xDirection = doubleNear ( y, 0.0 );
1083
1082
double minSqrDist = DBL_MAX;
1084
1083
QGraphicsLineItem* item = 0 ;
1085
1084
double currentXCoord = 0 ;
1086
1085
double currentYCoord = 0 ;
1087
1086
double currentSqrDist = 0 ;
1088
1087
double sqrTolerance = tolerance * tolerance;
1089
1088
1089
+ snappedItems.clear ();
1090
+
1090
1091
QList< QGraphicsLineItem* >::const_iterator it = mSnapLines .constBegin ();
1091
1092
for ( ; it != mSnapLines .constEnd (); ++it )
1092
1093
{
1093
- if ( horizontal )
1094
+ bool itemHorizontal = doubleNear (( *it )->line ().y2 () - ( *it )->line ().y1 (), 0 );
1095
+ if ( horizontal && itemHorizontal )
1094
1096
{
1095
1097
currentYCoord = ( *it )->line ().y1 ();
1096
1098
currentSqrDist = ( y - currentYCoord ) * ( y - currentYCoord );
1097
1099
}
1098
- else
1100
+ else if ( !itemHorizontal )
1099
1101
{
1100
1102
currentXCoord = ( *it )->line ().x1 ();
1101
1103
currentSqrDist = ( x - currentXCoord ) * ( x - currentXCoord );
@@ -1108,6 +1110,53 @@ QGraphicsLineItem* QgsComposition::nearestSnapLine( bool horizontal, double x, d
1108
1110
}
1109
1111
}
1110
1112
1113
+ double itemTolerance = 0.0000001 ;
1114
+ if ( item )
1115
+ {
1116
+ // go through all the items to find items snapped to this snap line
1117
+ QList<QGraphicsItem *> itemList = items ();
1118
+ QList<QGraphicsItem *>::iterator itemIt = itemList.begin ();
1119
+ for ( ; itemIt != itemList.end (); ++itemIt )
1120
+ {
1121
+ QgsComposerItem* currentItem = dynamic_cast <QgsComposerItem*>( *itemIt );
1122
+ if ( !currentItem || currentItem->type () == QgsComposerItem::ComposerPaper )
1123
+ {
1124
+ continue ;
1125
+ }
1126
+
1127
+ if ( horizontal )
1128
+ {
1129
+ if ( doubleNear ( currentYCoord, currentItem->transform ().dy () + currentItem->rect ().top (), itemTolerance ) )
1130
+ {
1131
+ snappedItems.append ( qMakePair ( currentItem, QgsComposerItem::UpperMiddle ) );
1132
+ }
1133
+ else if ( doubleNear ( currentYCoord, currentItem->transform ().dy () + currentItem->rect ().center ().y (), itemTolerance ) )
1134
+ {
1135
+ snappedItems.append ( qMakePair ( currentItem, QgsComposerItem::Middle ) );
1136
+ }
1137
+ else if ( doubleNear ( currentYCoord, currentItem->transform ().dy () + currentItem->rect ().bottom (), itemTolerance ) )
1138
+ {
1139
+ snappedItems.append ( qMakePair ( currentItem, QgsComposerItem::LowerMiddle ) );
1140
+ }
1141
+ }
1142
+ else
1143
+ {
1144
+ if ( doubleNear ( currentXCoord, currentItem->transform ().dx (), itemTolerance ) )
1145
+ {
1146
+ snappedItems.append ( qMakePair ( currentItem, QgsComposerItem::MiddleLeft ) );
1147
+ }
1148
+ else if ( doubleNear ( currentXCoord, currentItem->transform ().dx () + currentItem->rect ().center ().x (), itemTolerance ) )
1149
+ {
1150
+ snappedItems.append ( qMakePair ( currentItem, QgsComposerItem::Middle ) );
1151
+ }
1152
+ else if ( doubleNear ( currentXCoord, currentItem->transform ().dx () + currentItem->rect ().width (), itemTolerance ) )
1153
+ {
1154
+ snappedItems.append ( qMakePair ( currentItem, QgsComposerItem::MiddleRight ) );
1155
+ }
1156
+ }
1157
+ }
1158
+ }
1159
+
1111
1160
return item;
1112
1161
}
1113
1162
0 commit comments