@@ -170,6 +170,52 @@ class TestQgsSnappingUtils : public QObject
170
170
QVERIFY ( !m2.isValid () );
171
171
}
172
172
173
+ void testSnapOnIntersection ()
174
+ {
175
+ // testing with a layer with two crossing linestrings
176
+ // (0,1) x x (1,1)
177
+ // \/
178
+ // /\ .
179
+ // (0,0) x x (1,0)
180
+ QgsVectorLayer* vl = new QgsVectorLayer ( " LineString" , " x" , " memory" );
181
+ QgsPolyline polyline1, polyline2;
182
+ polyline1 << QgsPoint ( 0 , 0 ) << QgsPoint ( 1 , 1 );
183
+ polyline2 << QgsPoint ( 1 , 0 ) << QgsPoint ( 0 , 1 );
184
+ QgsFeature f1;
185
+ f1.setGeometry ( QgsGeometry::fromPolyline ( polyline1 ) );
186
+ QgsFeature f2;
187
+ f2.setGeometry ( QgsGeometry::fromPolyline ( polyline2 ) );
188
+ QgsFeatureList flist;
189
+ flist << f1 << f2;
190
+ vl->dataProvider ()->addFeatures ( flist );
191
+
192
+ QVERIFY ( vl->dataProvider ()->featureCount () == 2 );
193
+
194
+ QgsMapSettings mapSettings;
195
+ mapSettings.setOutputSize ( QSize ( 100 , 100 ) );
196
+ mapSettings.setExtent ( QgsRectangle ( 0 , 0 , 1 , 1 ) );
197
+ QVERIFY ( mapSettings.hasValidSettings () );
198
+
199
+ QgsSnappingUtils u;
200
+ u.setMapSettings ( mapSettings );
201
+ u.setSnapToMapMode ( QgsSnappingUtils::SnapAdvanced );
202
+ QList<QgsSnappingUtils::LayerConfig> layers;
203
+ layers << QgsSnappingUtils::LayerConfig ( vl, QgsPointLocator::Vertex, 0.1 , QgsTolerance::MapUnits );
204
+ u.setLayers ( layers );
205
+
206
+ // no snapping on intersections by default - should find nothing
207
+ QgsPointLocator::Match m = u.snapToMap ( QgsPoint ( 0.45 , 0.5 ) );
208
+ QVERIFY ( !m.isValid () );
209
+
210
+ u.setSnapOnIntersections ( true );
211
+
212
+ QgsPointLocator::Match m2 = u.snapToMap ( QgsPoint ( 0.45 , 0.5 ) );
213
+ QVERIFY ( m2.isValid () );
214
+ QCOMPARE ( m2.type (), QgsPointLocator::Vertex );
215
+ QCOMPARE ( m2.point (), QgsPoint ( 0.5 , 0.5 ) );
216
+
217
+ delete vl;
218
+ }
173
219
};
174
220
175
221
QTEST_MAIN ( TestQgsSnappingUtils )
0 commit comments