75 for (
auto relationIt = mRelations.begin(); relationIt != mRelations.end(); ++relationIt )
77 relationIt->updateRelationStatus();
84 mRelations.remove(
id );
96 return mRelations.value(
id );
103 for (
const QgsRelation &rel : std::as_const( mRelations ) )
105 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
122 return mRelations.values();
127 for (
const QgsRelation &rel : std::as_const( mRelations ) )
129 if ( rel.referencingLayer() == layer )
131 if ( fieldIdx != -2 )
133 bool containsField =
false;
134 const auto constFieldPairs = rel.fieldPairs();
139 containsField =
true;
144 if ( !containsField )
160 return mRelations.values();
165 for (
const QgsRelation &rel : std::as_const( mRelations ) )
167 if ( rel.referencedLayer() == layer )
176void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
179 mPolymorphicRelations.clear();
181 QDomNodeList relationNodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
182 if ( relationNodes.count() )
186 QDomNode node = relationNodes.item( 0 );
187 QDomNodeList relationNodes = node.childNodes();
188 int relCount = relationNodes.count();
189 for (
int i = 0; i < relCount; ++i )
196 QgsDebugMsgLevel( QStringLiteral(
"No relations data present in this document" ), 2 );
199 QDomNodeList polymorphicRelationNodes = doc.elementsByTagName( QStringLiteral(
"polymorphicRelations" ) );
200 if ( polymorphicRelationNodes.count() )
204 QDomNode node = polymorphicRelationNodes.item( 0 );
205 QDomNodeList relationNodes = node.childNodes();
206 int relCount = relationNodes.count();
207 for (
int i = 0; i < relCount; ++i )
214 QgsDebugMsgLevel( QStringLiteral(
"No polymorphic relations data present in this document" ), 3 );
221void QgsRelationManager::writeProject( QDomDocument &doc )
223 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
226 QgsDebugError( QStringLiteral(
"Unable to find qgis element in project file" ) );
229 QDomNode qgisNode = nl.item( 0 );
231 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
232 qgisNode.appendChild( relationsNode );
249 QDomElement polymorphicRelationsNode = doc.createElement( QStringLiteral(
"polymorphicRelations" ) );
250 qgisNode.appendChild( polymorphicRelationsNode );
258void QgsRelationManager::layersRemoved(
const QStringList &layers )
260 bool relationsChanged =
false;
261 for (
const QString &layer : std::as_const( layers ) )
263 QMapIterator<QString, QgsRelation> it( mRelations );
265 while ( it.hasNext() )
269 if ( it.value().referencedLayerId() == layer
270 || it.value().referencingLayerId() == layer )
272 mRelations.remove( it.key() );
273 relationsChanged =
true;
277 if ( relationsChanged )
283static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &relation )
285 for (
const QgsRelation &cur : std::as_const( existingRelations ) )
287 if ( cur.hasEqualDefinition( relation ) )
return true;
294 QList<QgsRelation> result;
299 const auto constDiscoverRelations = provider->discoverRelations( layer, layers );
302 if ( !hasRelationWithEqualDefinition( existingRelations,
relation ) )
314 return mPolymorphicRelations;
319 return mPolymorphicRelations.value( polymorphicRelationId );
330 for (
const QgsRelation &generatedRelation : generatedRelations )
338 const QList<QgsRelation> generatedRelations =
relation.generateRelations();
339 for (
const QgsRelation &generatedRelation : generatedRelations )
@ Generated
A generated relation is a child of a polymorphic relation.
@ Normal
A normal relation.
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
Qgis::RelationshipType type() const
Returns the type of the relation.
QgsVectorLayer * referencingLayer
void writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
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)
#define QgsDebugError(str)