Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #42031 from elpaso/bugfix-gh42003-broken-value-rel…
…ation

Fix value relation widget
  • Loading branch information
elpaso authored and nyalldawson committed Mar 4, 2021
1 parent 7278ec8 commit 0515c31
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -325,7 +325,7 @@ void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
{
if ( ! mCache.isEmpty() )
{
updateValues( mCache.at( 0 ).key );
updateValues( formFeature().attribute( fieldIdx() ).isValid() ? formFeature().attribute( fieldIdx() ) : mCache.at( 0 ).key );
}
} );
}
Expand Down
69 changes: 69 additions & 0 deletions tests/src/gui/testqgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -57,6 +57,8 @@ class TestQgsValueRelationWidgetWrapper : public QObject
void testWithJsonInSpatialite();
void testWithJsonInSpatialiteTextFk();
void testMatchLayerName();
//! Check that setFeature works correctly after regression #42003
void testRegressionGH42003();
};

void TestQgsValueRelationWidgetWrapper::initTestCase()
Expand Down Expand Up @@ -1574,5 +1576,72 @@ void TestQgsValueRelationWidgetWrapper::testMatchLayerName()
QCOMPARE( w_municipality.mComboBox->currentText(), QStringLiteral( "Some Place By The River" ) );
}

void TestQgsValueRelationWidgetWrapper::testRegressionGH42003()
{
// create a vector layer
QgsVectorLayer vl1( QStringLiteral( "Polygon?crs=epsg:4326&field=pk:int&field=province:int&field=municipality:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
QgsVectorLayer vl2( QStringLiteral( "Point?crs=epsg:4326&field=pk:int&field=fk_province:int&field=fk_municipality:int" ), QStringLiteral( "vl2" ), QStringLiteral( "memory" ) );
QgsProject::instance()->addMapLayer( &vl1, false, false );
QgsProject::instance()->addMapLayer( &vl2, false, false );

// insert some features
QgsFeature f1( vl1.fields() );
f1.setAttribute( QStringLiteral( "pk" ), 1 );
f1.setAttribute( QStringLiteral( "province" ), 123 );
f1.setAttribute( QStringLiteral( "municipality" ), QStringLiteral( "Some Place By The River" ) );
f1.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POLYGON(( 0 0, 0 1, 1 1, 1 0, 0 0 ))" ) ) );
QVERIFY( f1.isValid() );
QgsFeature f2( vl1.fields() );
f2.setAttribute( QStringLiteral( "pk" ), 2 );
f2.setAttribute( QStringLiteral( "province" ), 245 );
f2.setAttribute( QStringLiteral( "municipality" ), QStringLiteral( "Dreamland By The Clouds" ) );
f2.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POLYGON(( 1 0, 1 1, 2 1, 2 0, 1 0 ))" ) ) );
QVERIFY( f2.isValid() );
QVERIFY( vl1.dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 ) );

QgsFeature f3( vl2.fields() );
f3.setAttribute( QStringLiteral( "fk_province" ), 123 );
f3.setAttribute( QStringLiteral( "fk_municipality" ), 1 );
f3.setGeometry( QgsGeometry::fromWkt( QStringLiteral( "POINT( 0.5 0.5)" ) ) );
QVERIFY( f3.isValid() );
QVERIFY( f3.geometry().isGeosValid() );
QVERIFY( vl2.dataProvider()->addFeature( f3 ) );

// build a value relation widget wrapper for municipality
QgsValueRelationWidgetWrapper w_municipality( &vl2, vl2.fields().indexOf( QLatin1String( "fk_municipality" ) ), nullptr, nullptr );
QVariantMap cfg_municipality;
cfg_municipality.insert( QStringLiteral( "Layer" ), vl1.id() );
cfg_municipality.insert( QStringLiteral( "Key" ), QStringLiteral( "pk" ) );
cfg_municipality.insert( QStringLiteral( "Value" ), QStringLiteral( "municipality" ) );
cfg_municipality.insert( QStringLiteral( "AllowMulti" ), false );
cfg_municipality.insert( QStringLiteral( "NofColumns" ), 1 );
cfg_municipality.insert( QStringLiteral( "AllowNull" ), false );
cfg_municipality.insert( QStringLiteral( "OrderByValue" ), true );
cfg_municipality.insert( QStringLiteral( "UseCompleter" ), false );
w_municipality.setConfig( cfg_municipality );
w_municipality.widget();
w_municipality.setEnabled( true );

w_municipality.setFeature( QgsFeature( vl2.fields() ) );
QCoreApplication::processEvents();

// Check first is selected (fid 2 because of OrderByValue)
QCOMPARE( w_municipality.mComboBox->currentIndex(), 0 );
QCOMPARE( w_municipality.mComboBox->count(), 2 );
QCOMPARE( w_municipality.mComboBox->itemText( 0 ), QStringLiteral( "Dreamland By The Clouds" ) );
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "2" ) );

// Simulate what happens in the attribute form initialization
w_municipality.setFeature( QgsFeature( vl2.fields() ) );
w_municipality.setFeature( vl2.getFeature( 1 ) );
QCoreApplication::processEvents();

// Check fid 1 is selected
QCOMPARE( w_municipality.mComboBox->currentIndex(), 1 );
QCOMPARE( w_municipality.mComboBox->currentText(), QStringLiteral( "Some Place By The River" ) );
QCOMPARE( w_municipality.value().toString(), QStringLiteral( "1" ) );

}

QGSTEST_MAIN( TestQgsValueRelationWidgetWrapper )
#include "testqgsvaluerelationwidgetwrapper.moc"

0 comments on commit 0515c31

Please sign in to comment.