Skip to content

Commit

Permalink
Merge pull request #5045 from pblottiere/bugfix_refrel_218
Browse files Browse the repository at this point in the history
Fixes relation reference widget by refreshing filter lists. Fixes #16400 (backport)
  • Loading branch information
Hugo Mercier authored Aug 21, 2017
2 parents 7e4345a + d2315e4 commit 6380a97
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 47 deletions.
20 changes: 17 additions & 3 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,20 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
}
else
{
QString nullValue = QSettings().value( "qgis/nullValue", "NULL" ).toString();

if ( mChainFilters && mFeature.isValid() && mFilterComboBoxes.count() >= mFilterFields.count() )
{
QgsFeature feature = mFeature;

for ( int i = 0; i < mFilterFields.size(); i++ )
{
QVariant v = feature.attribute( mFilterFields[i] );
QString f = v.isNull() ? nullValue : v.toString();
mFilterComboBoxes.at( i )->setCurrentIndex( mFilterComboBoxes.at( i )->findText( f ) );
}
}

int i = mComboBox->findData( mFeature.id(), QgsAttributeTableModel::FeatureIdRole );
if ( i == -1 && mAllowNull )
{
Expand Down Expand Up @@ -951,12 +965,12 @@ void QgsRelationReferenceWidget::disableChainedComboBoxes( const QComboBox *scb
continue;
}

cb->setCurrentIndex( 0 );
if ( ccb->currentIndex() == 0 )
{
cb->setCurrentIndex( 0 );
cb->setEnabled( false );
}
else
ccb = cb;

ccb = cb;
}
}
129 changes: 85 additions & 44 deletions tests/src/gui/testqgsrelationreferencewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class TestQgsRelationReferenceWidget : public QObject
void cleanup(); // will be called after every testfunction.

void testChainFilter();
void testChainFilterRefreshed();

private:
QgsVectorLayer* mLayer1;
QgsVectorLayer* mLayer2;
std::unique_ptr<QgsRelation> mRelation;
};

void TestQgsRelationReferenceWidget::initTestCase()
Expand All @@ -53,79 +59,79 @@ void TestQgsRelationReferenceWidget::cleanupTestCase()

void TestQgsRelationReferenceWidget::init()
{
}

void TestQgsRelationReferenceWidget::cleanup()
{
}

void TestQgsRelationReferenceWidget::testChainFilter()
{
// create layers
QgsVectorLayer vl1( QString( "LineString?crs=epsg:3111&field=pk:int&field=fk:int" ), QString( "vl1" ), QString( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( &vl1 );
QgsVectorLayer vl2( QString( "LineString?field=pk:int&field=material:string&field=diameter:int&field=raccord:string" ), QString( "vl2" ), QString( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( &vl2 );

// create a relation between them
QgsRelation relation;
relation.setRelationId( QString( "vl1.vl2" ) );
relation.setRelationName( QString( "vl1.vl2" ) );
relation.setReferencingLayer( vl1.id() );
relation.setReferencedLayer( vl2.id() );
relation.addFieldPair( "fk", "pk" );
QVERIFY( relation.isValid() );
QgsProject::instance()->relationManager()->addRelation( relation );
// create layer
mLayer1 = new QgsVectorLayer( QString( "LineString?crs=epsg:3111&field=pk:int&field=fk:int" ), QString( "vl1" ), QString( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( mLayer1 );

mLayer2 = new QgsVectorLayer( QString( "LineString?field=pk:int&field=material:string&field=diameter:int&field=raccord:string" ), QString( "vl2" ), QString( "memory" ) );
QgsMapLayerRegistry::instance()->addMapLayer( mLayer2 );

// create relation
mRelation.reset( new QgsRelation() );
mRelation->setRelationId( QString( "vl1.vl2" ) );
mRelation->setRelationName( QString( "vl1.vl2" ) );
mRelation->setReferencingLayer( mLayer1->id() );
mRelation->setReferencedLayer( mLayer2->id() );
mRelation->addFieldPair( "fk", "pk" );
QVERIFY( mRelation->isValid() );
QgsProject::instance()->relationManager()->addRelation( *mRelation.get() );

// add features
QgsFeature ft0( vl1.fields() );
QgsFeature ft0( mLayer1->fields() );
ft0.setAttribute( QString( "pk" ), 0 );
ft0.setAttribute( QString( "fk" ), 0 );
vl1.startEditing();
vl1.addFeature( ft0 );
vl1.commitChanges();
mLayer1->startEditing();
mLayer1->addFeature( ft0 );
mLayer1->commitChanges();

QgsFeature ft1( vl1.fields() );
QgsFeature ft1( mLayer1->fields() );
ft1.setAttribute( QString( "pk" ), 1 );
ft1.setAttribute( QString( "fk" ), 1 );
vl1.startEditing();
vl1.addFeature( ft1 );
vl1.commitChanges();
mLayer1->startEditing();
mLayer1->addFeature( ft1 );
mLayer1->commitChanges();

QgsFeature ft2( vl2.fields() );
QgsFeature ft2( mLayer2->fields() );
ft2.setAttribute( QString( "pk" ), 10 );
ft2.setAttribute( QString( "material" ), "iron" );
ft2.setAttribute( QString( "diameter" ), 120 );
ft2.setAttribute( QString( "raccord" ), "brides" );
vl2.startEditing();
vl2.addFeature( ft2 );
vl2.commitChanges();
mLayer2->startEditing();
mLayer2->addFeature( ft2 );
mLayer2->commitChanges();

QgsFeature ft3( vl2.fields() );
QgsFeature ft3( mLayer2->fields() );
ft3.setAttribute( QString( "pk" ), 11 );
ft3.setAttribute( QString( "material" ), "iron" );
ft3.setAttribute( QString( "diameter" ), 120 );
ft3.setAttribute( QString( "raccord" ), "sleeve" );
vl2.startEditing();
vl2.addFeature( ft3 );
vl2.commitChanges();
mLayer2->startEditing();
mLayer2->addFeature( ft3 );
mLayer2->commitChanges();

QgsFeature ft4( vl2.fields() );
QgsFeature ft4( mLayer2->fields() );
ft4.setAttribute( QString( "pk" ), 12 );
ft4.setAttribute( QString( "material" ), "steel" );
ft4.setAttribute( QString( "diameter" ), 120 );
ft4.setAttribute( QString( "raccord" ), "collar" );
vl2.startEditing();
vl2.addFeature( ft4 );
vl2.commitChanges();
mLayer2->startEditing();
mLayer2->addFeature( ft4 );
mLayer2->commitChanges();
}

void TestQgsRelationReferenceWidget::cleanup()
{
}

void TestQgsRelationReferenceWidget::testChainFilter()
{
// init a relation reference widget
QStringList filterFields = QStringList() << "material" << "diameter" << "raccord";

QgsRelationReferenceWidget w( new QWidget() );
w.setChainFilters( true );
w.setFilterFields( filterFields );
w.setRelation( relation, true );
w.setRelation( *mRelation, true );
w.init();

// check default status for comboboxes
Expand Down Expand Up @@ -179,5 +185,40 @@ void TestQgsRelationReferenceWidget::testChainFilter()
QCOMPARE( w.mComboBox->count(), 4 );
}

void TestQgsRelationReferenceWidget::testChainFilterRefreshed()
{
// init a relation reference widget
QStringList filterFields = { "material", "diameter", "raccord" };

QgsRelationReferenceWidget w( new QWidget() );
w.setChainFilters( true );
w.setFilterFields( filterFields );
w.setRelation( *mRelation, true );
w.init();

// check default status for comboboxes
QList<QComboBox *> cbs = w.mFilterComboBoxes;
QCOMPARE( cbs.count(), 3 );
QCOMPARE( cbs[0]->currentText(), QString( "material" ) );
QCOMPARE( cbs[1]->currentText(), QString( "diameter" ) );
QCOMPARE( cbs[2]->currentText(), QString( "raccord" ) );

// update foreign key
w.setForeignKey( QVariant( 12 ) );
QCOMPARE( cbs[0]->currentText(), QString( "steel" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
QCOMPARE( cbs[2]->currentText(), QString( "collar" ) );

w.setForeignKey( QVariant( 10 ) );
QCOMPARE( cbs[0]->currentText(), QString( "iron" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
QCOMPARE( cbs[2]->currentText(), QString( "brides" ) );

w.setForeignKey( QVariant( 11 ) );
QCOMPARE( cbs[0]->currentText(), QString( "iron" ) );
QCOMPARE( cbs[1]->currentText(), QString( "120" ) );
QCOMPARE( cbs[2]->currentText(), QString( "sleeve" ) );
}

QTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"

0 comments on commit 6380a97

Please sign in to comment.