20
20
#include < qgstracer.h>
21
21
#include < qgsvectorlayer.h>
22
22
#include " qgsproject.h"
23
+ #include " qgscategorizedsymbolrenderer.h"
24
+ #include " qgssettings.h"
25
+ #include " qgslayertree.h"
26
+ #include " qgslayertreemodel.h"
27
+ #include " qgsmapsettings.h"
28
+ #include " qgssnappingutils.h"
23
29
24
30
class TestQgsTracer : public QObject
25
31
{
@@ -36,6 +42,7 @@ class TestQgsTracer : public QObject
36
42
void testReprojection ();
37
43
void testCurved ();
38
44
void testOffset ();
45
+ void testInvisible ();
39
46
40
47
private:
41
48
@@ -156,6 +163,97 @@ void TestQgsTracer::testSimple()
156
163
delete vl;
157
164
}
158
165
166
+ void TestQgsTracer::testInvisible ()
167
+ {
168
+ QgsVectorLayer *mVL = new QgsVectorLayer ( QStringLiteral ( " Linestring?field=fld:int" ), QStringLiteral ( " x" ), QStringLiteral ( " memory" ) );
169
+ QgsFeature f1, f2, f3, f4;
170
+ int idx = mVL ->fields ().indexFromName ( QStringLiteral ( " fld" ) );
171
+ QVERIFY ( idx != -1 );
172
+ f1.initAttributes ( 1 );
173
+ f2.initAttributes ( 1 );
174
+ f3.initAttributes ( 1 );
175
+ f4.initAttributes ( 1 );
176
+
177
+ /* This shape - nearly a square (one side is shifted to have exactly one shortest
178
+ * path between corners):
179
+ * 0,10 +----+ 20,10
180
+ * | /
181
+ * 0,0 +--+ 10,0
182
+ */
183
+ QgsGeometry geom = QgsGeometry::fromWkt ( " LINESTRING(0 0, 0 10)" );
184
+ f1.setGeometry ( geom );
185
+ f1.setAttribute ( idx, QVariant ( 2 ) );
186
+ geom = QgsGeometry::fromWkt ( " LINESTRING(0 0, 10 0)" );
187
+ f2.setGeometry ( geom );
188
+ f2.setAttribute ( idx, QVariant ( 1 ) );
189
+ geom = QgsGeometry::fromWkt ( " LINESTRING(0 10, 20 10)" );
190
+ f3.setGeometry ( geom );
191
+ f3.setAttribute ( idx, QVariant ( 1 ) );
192
+ geom = QgsGeometry::fromWkt ( " LINESTRING(10 0, 20 10)" );
193
+ f4.setGeometry ( geom );
194
+ f4.setAttribute ( idx, QVariant ( 1 ) );
195
+ QgsFeatureList flist;
196
+ flist << f1 << f2 << f3 << f4;
197
+
198
+ mVL ->dataProvider ()->addFeatures ( flist );
199
+
200
+ QgsProject::instance ()->addMapLayer ( mVL );
201
+
202
+ QgsCategorizedSymbolRenderer *renderer = new QgsCategorizedSymbolRenderer ();
203
+ renderer->setClassAttribute ( QStringLiteral ( " fld" ) );
204
+ renderer->setSourceSymbol ( QgsSymbol::defaultSymbol ( QgsWkbTypes::LineGeometry ) );
205
+ renderer->addCategory ( QgsRendererCategory ( " 2" , QgsSymbol::defaultSymbol ( QgsWkbTypes::LineGeometry ), QStringLiteral ( " 2" ) ) );
206
+ mVL ->setRenderer ( renderer );
207
+
208
+ // create legend with symbology nodes for categorized renderer
209
+ QgsLayerTree *root = new QgsLayerTree ();
210
+ QgsLayerTreeLayer *n = new QgsLayerTreeLayer ( mVL );
211
+ root->addChildNode ( n );
212
+ QgsLayerTreeModel *m = new QgsLayerTreeModel ( root, nullptr );
213
+ m->refreshLayerLegend ( n );
214
+
215
+ QList<QgsLayerTreeModelLegendNode *> nodes = m->layerLegendNodes ( n );
216
+ QCOMPARE ( nodes.length (), 1 );
217
+ // uncheck all and test that all nodes are unchecked
218
+ static_cast < QgsSymbolLegendNode * >( nodes.at ( 0 ) )->uncheckAllItems ();
219
+ Q_FOREACH ( QgsLayerTreeModelLegendNode *ln, nodes )
220
+ {
221
+ QVERIFY ( ln->data ( Qt::CheckStateRole ) == Qt::Unchecked );
222
+ }
223
+
224
+ QgsMapSettings mapSettings;
225
+ mapSettings.setOutputSize ( QSize ( 100 , 100 ) );
226
+ mapSettings.setExtent ( QgsRectangle ( 0 , 0 , 1 , 1 ) );
227
+ QVERIFY ( mapSettings.hasValidSettings () );
228
+ mapSettings.setLayers ( QList<QgsMapLayer *>() << mVL );
229
+
230
+ QgsSnappingUtils u;
231
+ u.setMapSettings ( mapSettings );
232
+ u.setEnableSnappingForInvisibleFeature ( false );
233
+ u.setCurrentLayer ( mVL );
234
+
235
+ QgsSnappingConfig snappingConfig = u.config ();
236
+ snappingConfig.setEnabled ( true );
237
+ snappingConfig.setTolerance ( 10 );
238
+ snappingConfig.setUnits ( QgsTolerance::Pixels );
239
+ snappingConfig.setMode ( QgsSnappingConfig::ActiveLayer );
240
+ u.setConfig ( snappingConfig );
241
+ QgsTracer tracer;
242
+ tracer.setLayers ( QList<QgsVectorLayer *>() << mVL );
243
+
244
+ QgsPolylineXY points1 = tracer.findShortestPath ( QgsPointXY ( 10 , 0 ), QgsPointXY ( 0 , 10 ) );
245
+ QCOMPARE ( points1.count (), 3 );
246
+ QCOMPARE ( points1[0 ], QgsPointXY ( 10 , 0 ) );
247
+ QCOMPARE ( points1[1 ], QgsPointXY ( 0 , 0 ) );
248
+ QCOMPARE ( points1[2 ], QgsPointXY ( 0 , 10 ) );
249
+
250
+ QgsRenderContext renderContext = QgsRenderContext::fromMapSettings ( mapSettings );
251
+ tracer.setRenderContext ( &renderContext );
252
+ points1 = tracer.findShortestPath ( QgsPointXY ( 10 , 0 ), QgsPointXY ( 0 , 10 ) );
253
+ QCOMPARE ( points1.count (), 0 );
254
+
255
+ }
256
+
159
257
void TestQgsTracer::testPolygon ()
160
258
{
161
259
// the same shape as in testSimple() but with just one polygon ring
0 commit comments