85 mRelations.remove(
id );
97 return mRelations.value(
id );
104 for (
const QgsRelation &rel : std::as_const( mRelations ) )
106 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
123 return mRelations.values();
128 for (
const QgsRelation &rel : std::as_const( mRelations ) )
130 if ( rel.referencingLayer() == layer )
132 if ( fieldIdx != -2 )
134 bool containsField =
false;
135 const auto constFieldPairs = rel.fieldPairs();
140 containsField =
true;
145 if ( !containsField )
161 return mRelations.values();
166 for (
const QgsRelation &rel : std::as_const( mRelations ) )
168 if ( rel.referencedLayer() == layer )
177 void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
180 mPolymorphicRelations.clear();
182 QDomNodeList relationNodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
183 if ( relationNodes.count() )
187 QDomNode node = relationNodes.item( 0 );
188 QDomNodeList relationNodes = node.childNodes();
189 int relCount = relationNodes.count();
190 for (
int i = 0; i < relCount; ++i )
197 QgsDebugMsg( QStringLiteral(
"No relations data present in this document" ) );
200 QDomNodeList polymorphicRelationNodes = doc.elementsByTagName( QStringLiteral(
"polymorphicRelations" ) );
201 if ( polymorphicRelationNodes.count() )
205 QDomNode node = polymorphicRelationNodes.item( 0 );
206 QDomNodeList relationNodes = node.childNodes();
207 int relCount = relationNodes.count();
208 for (
int i = 0; i < relCount; ++i )
215 QgsDebugMsgLevel( QStringLiteral(
"No polymorphic relations data present in this document" ), 3 );
222 void QgsRelationManager::writeProject( QDomDocument &doc )
224 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
227 QgsDebugMsg( QStringLiteral(
"Unable to find qgis element in project file" ) );
230 QDomNode qgisNode = nl.item( 0 );
232 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
233 qgisNode.appendChild( relationsNode );
245 QDomElement polymorphicRelationsNode = doc.createElement( QStringLiteral(
"polymorphicRelations" ) );
246 qgisNode.appendChild( polymorphicRelationsNode );
254 void QgsRelationManager::layersRemoved(
const QStringList &layers )
256 bool relationsChanged =
false;
257 for (
const QString &layer : std::as_const( layers ) )
259 QMapIterator<QString, QgsRelation> it( mRelations );
261 while ( it.hasNext() )
265 if ( it.value().referencedLayerId() == layer
266 || it.value().referencingLayerId() == layer )
268 mRelations.remove( it.key() );
269 relationsChanged =
true;
273 if ( relationsChanged )
279 static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &relation )
281 for (
const QgsRelation &cur : std::as_const( existingRelations ) )
283 if ( cur.hasEqualDefinition( relation ) )
return true;
290 QList<QgsRelation> result;
295 const auto constDiscoverRelations = provider->discoverRelations( layer, layers );
298 if ( !hasRelationWithEqualDefinition( existingRelations,
relation ) )
310 return mPolymorphicRelations;
315 return mPolymorphicRelations.value( polymorphicRelationId );
326 for (
const QgsRelation &generatedRelation : generatedRelations )
334 const QList<QgsRelation> generatedRelations =
relation.generateRelations();
335 for (
const QgsRelation &generatedRelation : generatedRelations )