Fix addEntry for reference relation widget and use referenced field

index and not referencing one
This commit is contained in:
Julien Cabieces 2019-07-16 15:55:14 +02:00
parent 956c6d694e
commit dc51505541
2 changed files with 46 additions and 2 deletions

View File

@ -208,7 +208,6 @@ void QgsRelationReferenceWidget::setRelation( const QgsRelation &relation, bool
mComboBox->setIdentifierField( mReferencedField );
mReferencedFieldIdx = mReferencedLayer->fields().lookupField( relation.fieldPairs().at( 0 ).second );
mReferencingFieldIdx = mReferencingLayer->fields().lookupField( relation.fieldPairs().at( 0 ).first );
mAttributeEditorFrame->setObjectName( QStringLiteral( "referencing/" ) + relation.name() );
@ -897,7 +896,7 @@ void QgsRelationReferenceWidget::addEntry()
if ( mEditorContext.vectorLayerTools()->addFeature( mReferencedLayer, attributes, QgsGeometry(), &f ) )
{
mComboBox->setIdentifierValue( f.attribute( mReferencingFieldIdx ) );
mComboBox->setIdentifierValue( f.attribute( mReferencedFieldIdx ) );
mAddEntryButton->setEnabled( false );
}
}

View File

@ -29,6 +29,7 @@
#include "qgsfeaturefiltermodel.h"
#include "qgsgui.h"
#include "qgsmapcanvas.h"
#include "qgsvectorlayertools.h"
class TestQgsRelationReferenceWidget : public QObject
{
@ -48,6 +49,7 @@ class TestQgsRelationReferenceWidget : public QObject
void testInvalidRelation();
void testSetGetForeignKey();
void testIdentifyOnMap();
void testAddEntry();
private:
std::unique_ptr<QgsVectorLayer> mLayer1;
@ -374,5 +376,48 @@ void TestQgsRelationReferenceWidget::testIdentifyOnMap()
mLayer1->rollBack();
}
void TestQgsRelationReferenceWidget::testAddEntry()
{
// check that a new added entry in referenced layer populate correctly the
// referencing combobox
QWidget parentWidget;
QgsRelationReferenceWidget w( &parentWidget );
QVERIFY( mLayer1->startEditing() );
w.setRelation( *mRelation, true );
w.init();
// Monkey patch gui vector layer tool in order to simple add a new feature in
// referenced layer
class DummyVectorLayerTools : public QgsVectorLayerTools
{
bool addFeature( QgsVectorLayer *layer, const QgsAttributeMap &, const QgsGeometry &, QgsFeature *feat = nullptr ) const override
{
feat->setAttribute( QStringLiteral( "pk" ), 13 );
feat->setAttribute( QStringLiteral( "material" ), "steel" );
feat->setAttribute( QStringLiteral( "diameter" ), 140 );
feat->setAttribute( QStringLiteral( "raccord" ), "collar" );
layer->addFeature( *feat );
return true;
}
bool startEditing( QgsVectorLayer * ) const override {return true;}
bool stopEditing( QgsVectorLayer *, bool = true ) const override {return true;};
bool saveEdits( QgsVectorLayer * ) const override {return true;};
};
QgsAttributeEditorContext context;
DummyVectorLayerTools tools;
context.setVectorLayerTools( &tools );
w.setEditorContext( context, nullptr, nullptr );
w.addEntry();
QCOMPARE( w.mComboBox->identifierValue(), 13 );
}
QGSTEST_MAIN( TestQgsRelationReferenceWidget )
#include "testqgsrelationreferencewidget.moc"