86 mRelations.remove(
id );
98 return mRelations.value(
id );
105 const auto constMRelations = mRelations;
108 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
125 return mRelations.values();
130 const auto constMRelations = mRelations;
133 if ( rel.referencingLayer() == layer )
135 if ( fieldIdx != -2 )
137 bool containsField =
false;
138 const auto constFieldPairs = rel.fieldPairs();
143 containsField =
true;
148 if ( !containsField )
164 return mRelations.values();
169 const auto constMRelations = mRelations;
172 if ( rel.referencedLayer() == layer )
181 void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
185 QDomNodeList nodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
190 QDomNode node = nodes.item( 0 );
191 QDomNodeList relationNodes = node.childNodes();
192 int relCount = relationNodes.count();
193 for (
int i = 0; i < relCount; ++i )
200 QgsDebugMsg( QStringLiteral(
"No relations data present in this document" ) );
207 void QgsRelationManager::writeProject( QDomDocument &doc )
209 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
212 QgsDebugMsg( QStringLiteral(
"Unable to find qgis element in project file" ) );
215 QDomNode qgisNode = nl.item( 0 );
217 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
218 qgisNode.appendChild( relationsNode );
220 const auto constMRelations = mRelations;
227 void QgsRelationManager::layersRemoved(
const QStringList &layers )
229 bool relationsChanged =
false;
230 const auto constLayers = layers;
231 for (
const QString &layer : constLayers )
233 QMapIterator<QString, QgsRelation> it( mRelations );
235 while ( it.hasNext() )
239 if ( it.value().referencedLayerId() == layer
240 || it.value().referencingLayerId() == layer )
242 mRelations.remove( it.key() );
243 relationsChanged =
true;
247 if ( relationsChanged )
253 static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &relation )
255 const auto constExistingRelations = existingRelations;
256 for (
const QgsRelation &cur : constExistingRelations )
258 if ( cur.hasEqualDefinition( relation ) )
return true;
265 QList<QgsRelation> result;
266 const auto constLayers = layers;
269 const auto constDiscoverRelations = layer->dataProvider()->discoverRelations( layer, layers );
272 if ( !hasRelationWithEqualDefinition( existingRelations,
relation ) )