Skip to content

Commit

Permalink
Correctly emit attributeValueChanged when changing an editable joined…
Browse files Browse the repository at this point in the history
… field

Fixes edits to a joined field value aren't immediately shown in attribute tables,
and instead appear as though they were discarded (until the attribute table is
reloaded)
  • Loading branch information
nyalldawson authored and nirvn committed Aug 6, 2019
1 parent b10d9e5 commit 63bfafb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2616,6 +2616,8 @@ bool QgsVectorLayer::changeAttributeValue( QgsFeatureId fid, int field, const QV
{
case QgsFields::OriginJoin:
result = mJoinBuffer->changeAttributeValue( fid, field, newValue, oldValue );
if ( result )
emit attributeValueChanged( fid, field, newValue );
break;

case QgsFields::OriginProvider:
Expand Down
70 changes: 70 additions & 0 deletions tests/src/core/testqgsvectorlayerjoinbuffer.cpp
Expand Up @@ -65,6 +65,7 @@ class TestVectorLayerJoinBuffer : public QObject
void testCacheUpdate();
void testRemoveJoinOnLayerDelete();
void testResolveReferences();
void testSignals();

private:
QgsProject mProject;
Expand Down Expand Up @@ -666,6 +667,75 @@ void TestVectorLayerJoinBuffer::testResolveReferences()
delete vlA;
}

void TestVectorLayerJoinBuffer::testSignals()
{
mProject.clear();
QgsVectorLayer *vlA = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "cacheA" ), QStringLiteral( "memory" ) );
QVERIFY( vlA->isValid() );
QgsVectorLayer *vlB = new QgsVectorLayer( QStringLiteral( "Point?field=id_b:integer&field=value_b" ), QStringLiteral( "cacheB" ), QStringLiteral( "memory" ) );
QVERIFY( vlB->isValid() );
mProject.addMapLayer( vlA );
mProject.addMapLayer( vlB );

QgsFeature fA1( vlA->dataProvider()->fields(), 1 );
fA1.setAttribute( QStringLiteral( "id_a" ), 1 );
QgsFeature fA2( vlA->dataProvider()->fields(), 2 );
fA2.setAttribute( QStringLiteral( "id_a" ), 2 );

vlA->dataProvider()->addFeatures( QgsFeatureList() << fA1 << fA2 );

QgsVectorLayerJoinInfo joinInfo;
joinInfo.setTargetFieldName( QStringLiteral( "id_a" ) );
joinInfo.setJoinLayer( vlB );
joinInfo.setJoinFieldName( QStringLiteral( "id_b" ) );
joinInfo.setPrefix( QStringLiteral( "B_" ) );
joinInfo.setEditable( true );
joinInfo.setUpsertOnEdit( true );
vlA->addJoin( joinInfo );

QgsFeatureIterator fi = vlA->getFeatures();
fi.nextFeature( fA1 );
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
QVERIFY( !fA1.attribute( "B_value_b" ).isValid() );
fi.nextFeature( fA2 );
QCOMPARE( fA2.attribute( "id_a" ).toInt(), 2 );
QVERIFY( !fA2.attribute( "B_value_b" ).isValid() );

// change value in join target layer, check for signals
QSignalSpy spy( vlA, &QgsVectorLayer::attributeValueChanged );
vlA->startEditing();
vlB->startEditing();
// adds new feature to second layer
QVERIFY( vlA->changeAttributeValue( 1, 1, 111 ) );
fi = vlA->getFeatures();
fi.nextFeature( fA1 );
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
QCOMPARE( fA1.attribute( "B_value_b" ).toInt(), 111 );
fi.nextFeature( fA2 );
QCOMPARE( fA2.attribute( "id_a" ).toInt(), 2 );
QVERIFY( !fA2.attribute( "B_value_b" ).isValid() );
QCOMPARE( spy.count(), 1 );
QVERIFY( vlA->changeAttributeValue( 2, 1, 222 ) );
fi = vlA->getFeatures();
fi.nextFeature( fA1 );
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
QCOMPARE( fA1.attribute( "B_value_b" ).toInt(), 111 );
fi.nextFeature( fA2 );
QCOMPARE( fA2.attribute( "id_a" ).toInt(), 2 );
QCOMPARE( fA2.attribute( "B_value_b" ).toInt(), 222 );
QCOMPARE( spy.count(), 2 );
// changes existing feature in second layer
QVERIFY( vlA->changeAttributeValue( 1, 1, 112 ) );
fi = vlA->getFeatures();
fi.nextFeature( fA1 );
QCOMPARE( fA1.attribute( "id_a" ).toInt(), 1 );
QCOMPARE( fA1.attribute( "B_value_b" ).toInt(), 112 );
fi.nextFeature( fA2 );
QCOMPARE( fA2.attribute( "id_a" ).toInt(), 2 );
QCOMPARE( fA2.attribute( "B_value_b" ).toInt(), 222 );
QCOMPARE( spy.count(), 3 );
}


QGSTEST_MAIN( TestVectorLayerJoinBuffer )
#include "testqgsvectorlayerjoinbuffer.moc"
Expand Down

0 comments on commit 63bfafb

Please sign in to comment.