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 )
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
A polymorphic relation consists of the same properties like a normal relation except for the referenc...
QList< QgsRelation > generateRelations() const
Returns a list of generated relations, based on the currently set referencedLayerIds()
static QgsPolymorphicRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext=QgsRelationContext())
Creates a relation from an XML structure.
QgsVectorLayer * referencingLayer
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
void layersRemoved(const QStringList &layerIds)
Emitted after one or more layers were removed from the registry.
void readProjectWithContext(const QDomDocument &, QgsReadWriteContext &context)
Emitted when a project is being read.
void writeProject(QDomDocument &)
Emitted when the project is being written.
void setDirty(bool b=true)
Flag the project as dirty (modified).
The class is used as a container of context for various read/write operations on other objects.
QList< QgsRelation > referencedRelations(const QgsVectorLayer *layer=nullptr) const
Gets all relations where this layer is the referenced part (i.e.
static QList< QgsRelation > discoverRelations(const QList< QgsRelation > &existingRelations, const QList< QgsVectorLayer * > &layers)
Discover all the relations available from the current layers.
QList< QgsRelation > relationsByName(const QString &name) const
Returns a list of relations with matching names.
void addPolymorphicRelation(const QgsPolymorphicRelation &polymorphicRelation)
Adds a new polymorphic relation.
QgsPolymorphicRelation polymorphicRelation(const QString &polymorphicRelationId) const
Returns the list of relations associated with a polymorphic relation.
void setPolymorphicRelations(const QList< QgsPolymorphicRelation > &relations)
Sets the specified polymorphic relations and removes any polymorphic relations currently set.
QMap< QString, QgsPolymorphicRelation > polymorphicRelations() const
Returns all the polymorphic relations.
QgsRelationManager(QgsProject *project=nullptr)
Constructor for QgsRelationManager.
QList< QgsRelation > referencingRelations(const QgsVectorLayer *layer=nullptr, int fieldIdx=-2) const
Gets all relations where the specified layer (and field) is the referencing part (i....
void clear()
Remove any relation managed by this class.
void changed()
Emitted when relations are added or removed to the manager.
QgsRelationContext context() const
Gets the relation context.
void updateRelationsStatus()
Updates relations status.
void removeRelation(const QString &id)
Remove a relation.
void setRelations(const QList< QgsRelation > &relations)
Will set the specified relations and remove any relation currently set.
void removePolymorphicRelation(const QString &polymorphicRelationId)
Removes an existing polymorphic relation and it's generated relations.
void addRelation(const QgsRelation &relation)
Add a relation.
QMap< QString, QgsRelation > relations() const
Gets access to the relations managed by this class.
void relationsLoaded()
Emitted when the relations were loaded after reading a project.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Defines a relation between matching fields of the two involved tables of a relation.
static QgsRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext=QgsRelationContext())
Creates a relation from an XML structure.
QgsVectorLayer * referencedLayer
@ Generated
A generated relation is a child of a polymorphic relation.
RelationType type() const
Returns the type of the relation.
QgsVectorLayer * referencingLayer
void writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
void updateRelationStatus()
Updates the validity status of this relation.
This is the base class for vector data providers.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
#define QgsDebugMsgLevel(str, level)