Skip to content

Commit 9f3d571

Browse files
committed
clean tests and add a new test for snapping on inivisble feature
1 parent 68f463e commit 9f3d571

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

tests/src/core/testqgssnappingutils.cpp

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "qgssnappingconfig.h"
2727
#include "qgscategorizedsymbolrenderer.h"
2828
#include "qgssettings.h"
29+
#include "qgslayertree.h"
30+
#include "qgslayertreemodel.h"
2931

3032
struct FilterExcludePoint : public QgsPointLocator::MatchFilter
3133
{
@@ -75,19 +77,13 @@ class TestQgsSnappingUtils : public QObject
7577
QgsGeometry polygonGeom = QgsGeometry::fromPolygonXY( polygon );
7678
f1.setGeometry( polygonGeom );
7779
f1.setAttribute( idx, QVariant( 2 ) );
78-
79-
polyline << QgsPointXY( 10, 11 ) << QgsPointXY( 11, 10 ) << QgsPointXY( 11, 11 ) << QgsPointXY( 10, 11 );
80-
polygon << polyline;
81-
polygonGeom = QgsGeometry::fromPolygonXY( polygon );
82-
f2.setGeometry( polygonGeom );
83-
f2.setAttribute( idx, QVariant( 20 ) );
8480
QgsFeatureList flist;
85-
flist << f1 << f2;
86-
81+
flist << f1;
8782

8883
mVL->dataProvider()->addFeatures( flist );
8984

9085
QgsProject::instance()->addMapLayer( mVL );
86+
9187
}
9288

9389
void cleanupTestCase()
@@ -97,21 +93,13 @@ class TestQgsSnappingUtils : public QObject
9793

9894
void testSnapModeCurrent()
9995
{
100-
QgsSymbol *s1 = QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry );
101-
QgsSymbol *s2 = QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry );
102-
QgsRendererCategory c1( 2, s1, "f1", true );
103-
QgsRendererCategory c2( 20, s2, "f2", false );
104-
QgsCategoryList cl;
105-
cl << c1 << c2;
106-
10796
QgsMapSettings mapSettings;
10897
mapSettings.setOutputSize( QSize( 100, 100 ) );
10998
mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
11099
QVERIFY( mapSettings.hasValidSettings() );
111100

112101
QgsSnappingUtils u;
113102
u.setMapSettings( mapSettings );
114-
u.setEnableSnappingForInvisibleFeature( false );
115103
u.setCurrentLayer( mVL );
116104

117105
// first try with no snapping enabled
@@ -122,7 +110,7 @@ class TestQgsSnappingUtils : public QObject
122110
snappingConfig.setMode( QgsSnappingConfig::ActiveLayer );
123111
u.setConfig( snappingConfig );
124112

125-
QgsPointLocator::Match m0 = u.snapToMap( QPoint( 2, 2 ) );
113+
QgsPointLocator::Match m0 = u.snapToMap( QPoint( 100, 100 ) );
126114
QVERIFY( !m0.isValid() );
127115
QVERIFY( !m0.hasVertex() );
128116

@@ -131,15 +119,10 @@ class TestQgsSnappingUtils : public QObject
131119
snappingConfig.setType( QgsSnappingConfig::Vertex );
132120
u.setConfig( snappingConfig );
133121

134-
QgsPointLocator::Match m = u.snapToMap( QPoint( 11, 11 ) );
135-
QVERIFY( !m.isValid() );
136-
QVERIFY( !m.hasVertex() );
137-
138-
u.setEnableSnappingForInvisibleFeature( true );
139-
m = u.snapToMap( QPoint( 2, 2 ) );
122+
QgsPointLocator::Match m = u.snapToMap( QPoint( 100, 100 ) );
140123
QVERIFY( m.isValid() );
141124
QVERIFY( m.hasVertex() );
142-
QCOMPARE( m.point(), QgsPointXY( 0, 1 ) );
125+
QCOMPARE( m.point(), QgsPointXY( 1, 0 ) );
143126

144127
QgsPointLocator::Match m2 = u.snapToMap( QPoint( 0, 100 ) );
145128
QVERIFY( !m2.isValid() );
@@ -155,8 +138,75 @@ class TestQgsSnappingUtils : public QObject
155138
FilterExcludePoint myFilter( QgsPointXY( 1, 0 ) );
156139
QgsPointLocator::Match m3 = u.snapToMap( QPoint( 100, 100 ), &myFilter );
157140
QVERIFY( !m3.isValid() );
141+
}
142+
143+
void testSnapInvisible()
144+
{
145+
QgsCategorizedSymbolRenderer *renderer = new QgsCategorizedSymbolRenderer();
146+
renderer->setClassAttribute( QStringLiteral( "fld" ) );
147+
renderer->setSourceSymbol( QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry ) );
148+
renderer->addCategory( QgsRendererCategory( "2", QgsSymbol::defaultSymbol( QgsWkbTypes::PolygonGeometry ), QStringLiteral( "2" ) ) );
149+
mVL->setRenderer( renderer );
150+
151+
//create legend with symbology nodes for categorized renderer
152+
QgsLayerTree *root = new QgsLayerTree();
153+
QgsLayerTreeLayer *n = new QgsLayerTreeLayer( mVL );
154+
root->addChildNode( n );
155+
QgsLayerTreeModel *m = new QgsLayerTreeModel( root, nullptr );
156+
m->refreshLayerLegend( n );
157+
158+
//test that all nodes are initially checked
159+
QList<QgsLayerTreeModelLegendNode *> nodes = m->layerLegendNodes( n );
160+
QCOMPARE( nodes.length(), 1 );
161+
Q_FOREACH ( QgsLayerTreeModelLegendNode *ln, nodes )
162+
{
163+
QVERIFY( ln->data( Qt::CheckStateRole ) == Qt::Checked );
164+
}
165+
166+
167+
QgsMapSettings mapSettings;
168+
mapSettings.setOutputSize( QSize( 100, 100 ) );
169+
mapSettings.setExtent( QgsRectangle( 0, 0, 1, 1 ) );
170+
QVERIFY( mapSettings.hasValidSettings() );
158171

172+
QgsSnappingUtils u;
173+
u.setMapSettings( mapSettings );
174+
u.setEnableSnappingForInvisibleFeature( false );
175+
u.setCurrentLayer( mVL );
176+
177+
// first try with no snapping enabled
178+
QgsSnappingConfig snappingConfig = u.config();
179+
snappingConfig.setEnabled( false );
180+
snappingConfig.setTolerance( 10 );
181+
snappingConfig.setUnits( QgsTolerance::Pixels );
182+
snappingConfig.setMode( QgsSnappingConfig::ActiveLayer );
183+
u.setConfig( snappingConfig );
184+
185+
QgsPointLocator::Match m0 = u.snapToMap( QPoint( 2, 2 ) );
186+
QVERIFY( !m0.isValid() );
187+
QVERIFY( !m0.hasVertex() );
188+
189+
// now enable snapping
190+
snappingConfig.setEnabled( true );
191+
snappingConfig.setType( QgsSnappingConfig::Vertex );
192+
u.setConfig( snappingConfig );
159193

194+
QgsPointLocator::Match m5 = u.snapToMap( QPoint( 2, 2 ) );
195+
QVERIFY( m5.isValid() );
196+
QVERIFY( m5.hasVertex() );
197+
QCOMPARE( m5.point(), QgsPointXY( 0, 1 ) );
198+
199+
//uncheck all and test that all nodes are unchecked
200+
static_cast< QgsSymbolLegendNode * >( nodes.at( 0 ) )->uncheckAllItems();
201+
Q_FOREACH ( QgsLayerTreeModelLegendNode *ln, nodes )
202+
{
203+
QVERIFY( ln->data( Qt::CheckStateRole ) == Qt::Unchecked );
204+
}
205+
mVL->dataChanged(); /* refresh index */
206+
207+
m5 = u.snapToMap( QPoint( 2, 2 ) );
208+
QVERIFY( !m5.isValid() );
209+
QVERIFY( !m5.hasVertex() );
160210
}
161211

162212
void testSnapModeAll()

0 commit comments

Comments
 (0)