@@ -43,6 +43,7 @@ class TestQgsValueRelationWidgetWrapper : public QObject
43
43
44
44
void testScrollBarUnlocked ();
45
45
void testDrillDown ();
46
+ void testDrillDownMulti ();
46
47
};
47
48
48
49
void TestQgsValueRelationWidgetWrapper::initTestCase ()
@@ -63,6 +64,7 @@ void TestQgsValueRelationWidgetWrapper::init()
63
64
64
65
void TestQgsValueRelationWidgetWrapper::cleanup ()
65
66
{
67
+
66
68
}
67
69
68
70
void TestQgsValueRelationWidgetWrapper::testScrollBarUnlocked ()
@@ -166,8 +168,10 @@ void TestQgsValueRelationWidgetWrapper::testDrillDown()
166
168
w_municipality.setFeature ( f3 );
167
169
QCOMPARE ( w_municipality.mCache .size (), 1 );
168
170
171
+ // Check first is selected
169
172
QCOMPARE ( w_municipality.mComboBox ->count (), 1 );
170
173
QCOMPARE ( w_municipality.mComboBox ->itemText ( 0 ), QStringLiteral ( " Some Place By The River" ) );
174
+ QCOMPARE ( w_municipality.value ().toString (), QStringLiteral ( " 1" ) );
171
175
172
176
// Filter by geometry
173
177
cfg_municipality[ QStringLiteral ( " FilterExpression" ) ] = QStringLiteral ( " contains(buffer(@current_geometry, 1 ), $geometry)" );
@@ -190,6 +194,123 @@ void TestQgsValueRelationWidgetWrapper::testDrillDown()
190
194
QCOMPARE ( w_municipality.mComboBox ->itemText ( 0 ), QStringLiteral ( " Dreamland By The Clouds" ) );
191
195
QCOMPARE ( w_municipality.mComboBox ->itemText ( 1 ), QStringLiteral ( " Some Place By The River" ) );
192
196
197
+ // Check with allow null
198
+ cfg_municipality[QStringLiteral ( " AllowNull" )] = true ;
199
+ w_municipality.setConfig ( cfg_municipality );
200
+ w_municipality.setFeature ( QgsFeature () );
201
+
202
+ // Check null is selected
203
+ QCOMPARE ( w_municipality.mComboBox ->count (), 3 );
204
+ QCOMPARE ( w_municipality.mComboBox ->itemText ( 0 ), QStringLiteral ( " (no selection)" ) );
205
+ QCOMPARE ( w_municipality.value ().toString (), QStringLiteral ( " " ) );
206
+
207
+ // Check order by value false
208
+ cfg_municipality[QStringLiteral ( " AllowNull" )] = false ;
209
+ cfg_municipality[QStringLiteral ( " OrderByValue" )] = false ;
210
+ w_municipality.setConfig ( cfg_municipality );
211
+ w_municipality.setFeature ( f3 );
212
+ QCOMPARE ( w_municipality.mComboBox ->itemText ( 1 ), QStringLiteral ( " Dreamland By The Clouds" ) );
213
+ QCOMPARE ( w_municipality.mComboBox ->itemText ( 0 ), QStringLiteral ( " Some Place By The River" ) );
214
+
215
+ }
216
+
217
+
218
+ void TestQgsValueRelationWidgetWrapper::testDrillDownMulti ()
219
+ {
220
+ // create a vector layer
221
+ QgsVectorLayer vl1 ( QStringLiteral ( " Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ), QStringLiteral ( " vl1" ), QStringLiteral ( " memory" ) );
222
+ QgsVectorLayer vl2 ( QStringLiteral ( " Point?crs=epsg:4326&field=pk:int&field=fk_province:int&field=fk_municipality:int" ), QStringLiteral ( " vl2" ), QStringLiteral ( " memory" ) );
223
+ QgsProject::instance ()->addMapLayer ( &vl1, false , false );
224
+ QgsProject::instance ()->addMapLayer ( &vl2, false , false );
225
+
226
+ // insert some features
227
+ QgsFeature f1 ( vl1.fields () );
228
+ f1.setAttribute ( QStringLiteral ( " pk" ), 1 );
229
+ f1.setAttribute ( QStringLiteral ( " province" ), 123 );
230
+ f1.setAttribute ( QStringLiteral ( " municipality" ), QStringLiteral ( " Some Place By The River" ) );
231
+ f1.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " POLYGON(( 0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
232
+ QVERIFY ( f1.isValid () );
233
+ QgsFeature f2 ( vl1.fields () );
234
+ f2.setAttribute ( QStringLiteral ( " pk" ), 2 );
235
+ f2.setAttribute ( QStringLiteral ( " province" ), 245 );
236
+ f2.setAttribute ( QStringLiteral ( " municipality" ), QStringLiteral ( " Dreamland By The Clouds" ) );
237
+ f2.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " POLYGON(( 1 0, 1 1, 2 1, 2 0, 1 0 ))" ) ) );
238
+ QVERIFY ( f2.isValid () );
239
+ QVERIFY ( vl1.dataProvider ()->addFeatures ( QgsFeatureList () << f1 << f2 ) );
240
+
241
+ QgsFeature f3 ( vl2.fields () );
242
+ f3.setAttribute ( QStringLiteral ( " fk_province" ), 123 );
243
+ f3.setAttribute ( QStringLiteral ( " fk_municipality" ), QStringLiteral ( " {1}" ) );
244
+ f3.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " POINT( 0.5 0.5)" ) ) );
245
+ QVERIFY ( f3.isValid () );
246
+ QVERIFY ( f3.geometry ().isGeosValid () );
247
+ QVERIFY ( vl2.dataProvider ()->addFeature ( f3 ) );
248
+
249
+ // build a value relation widget wrapper for municipality
250
+ QgsValueRelationWidgetWrapper w_municipality ( &vl2, vl2.fields ().indexOf ( QStringLiteral ( " fk_municipality" ) ), nullptr , nullptr );
251
+ QVariantMap cfg_municipality;
252
+ cfg_municipality.insert ( QStringLiteral ( " Layer" ), vl1.id () );
253
+ cfg_municipality.insert ( QStringLiteral ( " Key" ), QStringLiteral ( " pk" ) );
254
+ cfg_municipality.insert ( QStringLiteral ( " Value" ), QStringLiteral ( " municipality" ) );
255
+ cfg_municipality.insert ( QStringLiteral ( " AllowMulti" ), true );
256
+ cfg_municipality.insert ( QStringLiteral ( " NofColumns" ), 1 );
257
+ cfg_municipality.insert ( QStringLiteral ( " AllowNull" ), false );
258
+ cfg_municipality.insert ( QStringLiteral ( " OrderByValue" ), true );
259
+ cfg_municipality.insert ( QStringLiteral ( " FilterExpression" ), QStringLiteral ( " \" province\" = current_value('fk_province')" ) );
260
+ cfg_municipality.insert ( QStringLiteral ( " UseCompleter" ), false );
261
+ w_municipality.setConfig ( cfg_municipality );
262
+ w_municipality.widget ();
263
+ w_municipality.setEnabled ( true );
264
+
265
+ QCOMPARE ( w_municipality.mCache .size (), 2 );
266
+ QCOMPARE ( w_municipality.mTableWidget ->rowCount (), 2 );
267
+ w_municipality.setFeature ( f3 );
268
+ QCOMPARE ( w_municipality.mCache .size (), 1 );
269
+
270
+ QCOMPARE ( w_municipality.mTableWidget ->rowCount (), 1 );
271
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->text (), QStringLiteral ( " Some Place By The River" ) );
272
+ QCOMPARE ( w_municipality.value ().toString (), QStringLiteral ( " {1}" ) );
273
+
274
+ // Filter by geometry
275
+ cfg_municipality[ QStringLiteral ( " FilterExpression" ) ] = QStringLiteral ( " contains(buffer(@current_geometry, 1 ), $geometry)" );
276
+ w_municipality.setConfig ( cfg_municipality );
277
+ w_municipality.setFeature ( f3 );
278
+ QCOMPARE ( w_municipality.mTableWidget ->rowCount (), 1 );
279
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->text (), QStringLiteral ( " Some Place By The River" ) );
280
+
281
+ // Move the point to 1.5 0.5
282
+ f3.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " POINT( 1.5 0.5)" ) ) );
283
+ w_municipality.setFeature ( f3 );
284
+ QCOMPARE ( w_municipality.mTableWidget ->rowCount (), 1 );
285
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->text (), QStringLiteral ( " Dreamland By The Clouds" ) );
286
+
287
+ // Enlarge the buffer
288
+ cfg_municipality[ QStringLiteral ( " FilterExpression" ) ] = QStringLiteral ( " contains(buffer(@current_geometry, 3 ), $geometry)" );
289
+ w_municipality.setConfig ( cfg_municipality );
290
+ w_municipality.setFeature ( f3 );
291
+ QCOMPARE ( w_municipality.mTableWidget ->rowCount (), 2 );
292
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->text (), QStringLiteral ( " Dreamland By The Clouds" ) );
293
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->data ( Qt::UserRole ).toString (), QStringLiteral ( " 2" ) );
294
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 1 , 0 )->text (), QStringLiteral ( " Some Place By The River" ) );
295
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 1 , 0 )->data ( Qt::UserRole ).toString (), QStringLiteral ( " 1" ) );
296
+ QCOMPARE ( w_municipality.value ().toString (), QStringLiteral ( " {1}" ) );
297
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->checkState (), Qt::Unchecked );
298
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 1 , 0 )->checkState (), Qt::Checked );
299
+ w_municipality.setValue ( QStringLiteral ( " {1,2}" ) );
300
+ QCOMPARE ( w_municipality.value ().toString (), QStringLiteral ( " {2,1}" ) );
301
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->checkState (), Qt::Checked );
302
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 1 , 0 )->checkState (), Qt::Checked );
303
+
304
+ // Check values are checked
305
+ f3.setAttribute ( QStringLiteral ( " fk_municipality" ), QStringLiteral ( " {1,2}" ) );
306
+ w_municipality.setFeature ( f3 );
307
+ QCOMPARE ( w_municipality.mTableWidget ->rowCount (), 2 );
308
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->text (), QStringLiteral ( " Dreamland By The Clouds" ) );
309
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 1 , 0 )->text (), QStringLiteral ( " Some Place By The River" ) );
310
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 0 , 0 )->checkState (), Qt::Checked );
311
+ QCOMPARE ( w_municipality.mTableWidget ->item ( 1 , 0 )->checkState (), Qt::Checked );
312
+ QCOMPARE ( w_municipality.value ().toString (), QStringLiteral ( " {2,1}" ) );
313
+
193
314
}
194
315
195
316
QGSTEST_MAIN ( TestQgsValueRelationWidgetWrapper )
0 commit comments