Skip to content

Commit

Permalink
Enhancement feature duplication by ensuring children of relations wit…
Browse files Browse the repository at this point in the history
…h composition strength are duplicated too
  • Loading branch information
nirvn committed Aug 4, 2023
1 parent cdc5dfc commit af01c81
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/core/utils/layerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,21 +312,46 @@ QgsFeature LayerUtils::duplicateFeature( QgsVectorLayer *layer, const QgsFeature
} );
auto sweaper = qScopeGuard( [layer, connection] { layer->disconnect( connection ); } );

duplicatedFeature = QgsVectorLayerUtils::createFeature( layer, feature.geometry(), feature.attributes().toMap() );
if ( layer->addFeature( duplicatedFeature ) )
QgsVectorLayerUtils::QgsDuplicateFeatureContext duplicateFeatureContext;
duplicatedFeature = QgsVectorLayerUtils::duplicateFeature( layer, feature, QgsProject::instance(), duplicateFeatureContext );
const auto duplicateFeatureContextLayers = duplicateFeatureContext.layers();

// commit changes
if ( !layer->commitChanges() )
{
// commit changes
if ( !layer->commitChanges() )
const QString msgs = layer->commitErrors().join( QStringLiteral( "\n" ) );
QgsMessageLog::logMessage( tr( "Cannot add new feature in layer \"%1\". Reason:\n%2" ).arg( layer->name(), msgs ), "QField", Qgis::Warning );

for ( QgsVectorLayer *chl : duplicateFeatureContextLayers )
{
const QString msgs = layer->commitErrors().join( QStringLiteral( "\n" ) );
QgsMessageLog::logMessage( tr( "Cannot add new feature in layer \"%1\". Reason:\n%2" ).arg( layer->name(), msgs ), "QField", Qgis::Warning );
return QgsFeature();
chl->rollBack();
}

return QgsFeature();
}
else

// we have to re-apply referenced feature attribute value to take into account value changes form the
// data provider (e.g. autogenerated fid)
const QList<QgsRelation> relations = QgsProject::instance()->relationManager()->referencedRelations( layer );
for ( QgsVectorLayer *chl : duplicateFeatureContextLayers )
{
QgsMessageLog::logMessage( tr( "Cannot add new feature in layer \"%1\"." ).arg( layer->name() ), "QField", Qgis::Warning );
return QgsFeature();
const QgsFeatureIds fids = duplicateFeatureContext.duplicatedFeatures( chl );
for ( const auto &relation : relations )
{
if ( relation.referencingLayer() == chl )
{
const QList<QgsRelation::FieldPair> fieldPairs = relation.fieldPairs();
for ( const auto &fieldPair : fieldPairs )
{
for ( auto fid : fids )
{
chl->changeAttributeValue( fid, chl->fields().indexOf( fieldPair.referencingField() ), duplicatedFeature.attribute( fieldPair.referencedField() ) );
}
}
}
}

chl->commitChanges();
}

return duplicatedFeature;
Expand Down

1 comment on commit af01c81

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.