Skip to content

Commit de966e2

Browse files
committed
Fix relations in offline editing
1 parent 86c0a16 commit de966e2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/core/qgsofflineediting.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ void QgsOfflineEditing::synchronize()
254254

255255
// copy style
256256
copySymbology( offlineLayer, remoteLayer );
257+
updateRelations( offlineLayer, remoteLayer );
257258

258259
// apply layer edit log
259260
QString qgisLayerId = layer->id();
@@ -611,6 +612,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit
611612
}
612613
}
613614

615+
updateRelations( layer, newLayer );
614616
// copy features
615617
newLayer->startEditing();
616618
QgsFeature f;
@@ -898,6 +900,29 @@ void QgsOfflineEditing::copySymbology( QgsVectorLayer* sourceLayer, QgsVectorLay
898900
}
899901
}
900902

903+
void QgsOfflineEditing::updateRelations( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer )
904+
{
905+
QgsRelationManager* relationManager = QgsProject::instance()->relationManager();
906+
QList<QgsRelation> relations;
907+
relations = relationManager->referencedRelations( sourceLayer );
908+
909+
Q_FOREACH ( QgsRelation relation, relations )
910+
{
911+
relationManager->removeRelation( relation );
912+
relation.setReferencedLayer( targetLayer->id() );
913+
relationManager->addRelation( relation );
914+
}
915+
916+
relations = relationManager->referencingRelations( sourceLayer );
917+
918+
Q_FOREACH ( QgsRelation relation, relations )
919+
{
920+
relationManager->removeRelation( relation );
921+
relation.setReferencingLayer( targetLayer->id() );
922+
relationManager->addRelation( relation );
923+
}
924+
}
925+
901926
// NOTE: use this to map column indices in case the remote geometry column is not last
902927
QMap<int, int> QgsOfflineEditing::attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer )
903928
{

src/core/qgsofflineediting.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ class CORE_EXPORT QgsOfflineEditing : public QObject
108108
void applyGeometryChanges( QgsVectorLayer* remoteLayer, sqlite3* db, int layerId, int commitNo );
109109
void updateFidLookup( QgsVectorLayer* remoteLayer, sqlite3* db, int layerId );
110110
void copySymbology( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer );
111+
112+
/**
113+
* Updates all relations that reference or are referenced by the source layer to the targetLayer.
114+
*/
115+
void updateRelations( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer );
111116
QMap<int, int> attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer );
112117

113118
void showWarning( const QString& message );

0 commit comments

Comments
 (0)