Skip to content

Commit 86f35e8

Browse files
authoredMay 22, 2019
Merge pull request #10047 from elpaso/bugfix-22071-relation-reference-identify-on-map
Fix identify on map in relation reference widget
2 parents bb0d449 + ad019c4 commit 86f35e8

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed
 

‎python/gui/auto_generated/editorwidgets/qgsrelationreferencewidget.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ determines if the foreign key is shown in a combox box or a read-only line edit
7272

7373
bool allowMapIdentification();
7474
%Docstring
75-
determines if the widge offers the possibility to select the related feature on the map (using a dedicated map tool)
75+
determines if the widget offers the possibility to select the related feature on the map (using a dedicated map tool)
7676
%End
7777
void setAllowMapIdentification( bool allowMapIdentification );
7878

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include "qgsfeatureiterator.h"
4242
#include "qgsfeaturelistcombobox.h"
4343
#include "qgsexpressioncontextutils.h"
44-
44+
#include "qgsfeaturefiltermodel.h"
4545

4646
QgsRelationReferenceWidget::QgsRelationReferenceWidget( QWidget *parent )
4747
: QWidget( parent )
@@ -746,7 +746,7 @@ void QgsRelationReferenceWidget::featureIdentified( const QgsFeature &feature )
746746
}
747747
else
748748
{
749-
mComboBox->setCurrentIndex( mComboBox->findData( feature.id(), QgsAttributeTableModel::FeatureIdRole ) );
749+
mComboBox->setCurrentIndex( mComboBox->findData( feature.attribute( mReferencedFieldIdx ), QgsFeatureFilterModel::Role::IdentifierValueRole ) );
750750
mFeature = feature;
751751
}
752752

‎src/gui/editorwidgets/qgsrelationreferencewidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class GUI_EXPORT QgsRelationReferenceWidget : public QWidget
9999
bool readOnlySelector() { return mReadOnlySelector; }
100100
void setReadOnlySelector( bool readOnly );
101101

102-
//! determines if the widge offers the possibility to select the related feature on the map (using a dedicated map tool)
102+
//! determines if the widget offers the possibility to select the related feature on the map (using a dedicated map tool)
103103
bool allowMapIdentification() { return mAllowMapIdentification; }
104104
void setAllowMapIdentification( bool allowMapIdentification );
105105

‎tests/src/gui/testqgsrelationreferencewidget.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TestQgsRelationReferenceWidget : public QObject
4747
void testChainFilterDeleteForeignKey();
4848
void testInvalidRelation();
4949
void testSetGetForeignKey();
50+
void testIdentifyOnMap();
5051

5152
private:
5253
std::unique_ptr<QgsVectorLayer> mLayer1;
@@ -340,5 +341,38 @@ void TestQgsRelationReferenceWidget::testSetGetForeignKey()
340341
QCOMPARE( spy.count(), 3 );
341342
}
342343

344+
345+
// Test issue https://issues.qgis.org/issues/22071
346+
// Relation reference widget wrong feature when "on map identification"
347+
void TestQgsRelationReferenceWidget::testIdentifyOnMap()
348+
{
349+
QWidget parentWidget;
350+
QgsRelationReferenceWidget w( &parentWidget );
351+
QVERIFY( mLayer1->startEditing() );
352+
w.setRelation( *mRelation, true );
353+
w.setAllowMapIdentification( true );
354+
w.init();
355+
QEventLoop loop;
356+
// Populate model (I tried to listen to signals but the module reload() runs twice
357+
// (the first load triggers a second one which does the population of the combo)
358+
// and I haven't fin a way to properly wait for it.
359+
QTimer::singleShot( 300, [&] { loop.quit(); } );
360+
loop.exec();
361+
QgsFeature feature;
362+
mLayer2->getFeatures( QStringLiteral( "pk = %1" ).arg( 11 ) ).nextFeature( feature );
363+
QVERIFY( feature.isValid() );
364+
QCOMPARE( feature.attribute( QStringLiteral( "pk" ) ).toInt(), 11 );
365+
w.featureIdentified( feature );
366+
QCOMPARE( w.mComboBox->currentData( Qt::DisplayRole ).toInt(), 11 );
367+
368+
mLayer2->getFeatures( QStringLiteral( "pk = %1" ).arg( 10 ) ).nextFeature( feature );
369+
QVERIFY( feature.isValid() );
370+
QCOMPARE( feature.attribute( QStringLiteral( "pk" ) ).toInt(), 10 );
371+
w.featureIdentified( feature );
372+
QCOMPARE( w.mComboBox->currentData( Qt::DisplayRole ).toInt(), 10 );
373+
374+
mLayer1->rollBack();
375+
}
376+
343377
QGSTEST_MAIN( TestQgsRelationReferenceWidget )
344378
#include "testqgsrelationreferencewidget.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.