26
26
#include " qgssnappingconfig.h"
27
27
#include " qgscategorizedsymbolrenderer.h"
28
28
#include " qgssettings.h"
29
+ #include " qgslayertree.h"
30
+ #include " qgslayertreemodel.h"
29
31
30
32
struct FilterExcludePoint : public QgsPointLocator ::MatchFilter
31
33
{
@@ -75,19 +77,13 @@ class TestQgsSnappingUtils : public QObject
75
77
QgsGeometry polygonGeom = QgsGeometry::fromPolygonXY ( polygon );
76
78
f1.setGeometry ( polygonGeom );
77
79
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 ) );
84
80
QgsFeatureList flist;
85
- flist << f1 << f2;
86
-
81
+ flist << f1;
87
82
88
83
mVL ->dataProvider ()->addFeatures ( flist );
89
84
90
85
QgsProject::instance ()->addMapLayer ( mVL );
86
+
91
87
}
92
88
93
89
void cleanupTestCase ()
@@ -97,21 +93,13 @@ class TestQgsSnappingUtils : public QObject
97
93
98
94
void testSnapModeCurrent ()
99
95
{
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
-
107
96
QgsMapSettings mapSettings;
108
97
mapSettings.setOutputSize ( QSize ( 100 , 100 ) );
109
98
mapSettings.setExtent ( QgsRectangle ( 0 , 0 , 1 , 1 ) );
110
99
QVERIFY ( mapSettings.hasValidSettings () );
111
100
112
101
QgsSnappingUtils u;
113
102
u.setMapSettings ( mapSettings );
114
- u.setEnableSnappingForInvisibleFeature ( false );
115
103
u.setCurrentLayer ( mVL );
116
104
117
105
// first try with no snapping enabled
@@ -122,7 +110,7 @@ class TestQgsSnappingUtils : public QObject
122
110
snappingConfig.setMode ( QgsSnappingConfig::ActiveLayer );
123
111
u.setConfig ( snappingConfig );
124
112
125
- QgsPointLocator::Match m0 = u.snapToMap ( QPoint ( 2 , 2 ) );
113
+ QgsPointLocator::Match m0 = u.snapToMap ( QPoint ( 100 , 100 ) );
126
114
QVERIFY ( !m0.isValid () );
127
115
QVERIFY ( !m0.hasVertex () );
128
116
@@ -131,15 +119,10 @@ class TestQgsSnappingUtils : public QObject
131
119
snappingConfig.setType ( QgsSnappingConfig::Vertex );
132
120
u.setConfig ( snappingConfig );
133
121
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 ) );
140
123
QVERIFY ( m.isValid () );
141
124
QVERIFY ( m.hasVertex () );
142
- QCOMPARE ( m.point (), QgsPointXY ( 0 , 1 ) );
125
+ QCOMPARE ( m.point (), QgsPointXY ( 1 , 0 ) );
143
126
144
127
QgsPointLocator::Match m2 = u.snapToMap ( QPoint ( 0 , 100 ) );
145
128
QVERIFY ( !m2.isValid () );
@@ -155,8 +138,75 @@ class TestQgsSnappingUtils : public QObject
155
138
FilterExcludePoint myFilter ( QgsPointXY ( 1 , 0 ) );
156
139
QgsPointLocator::Match m3 = u.snapToMap ( QPoint ( 100 , 100 ), &myFilter );
157
140
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 () );
158
171
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 );
159
193
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 () );
160
210
}
161
211
162
212
void testSnapModeAll ()
0 commit comments