75 for (
auto relationIt = mRelations.begin(); relationIt != mRelations.end(); ++relationIt )
77 relationIt->updateRelationStatus();
84 mRelations.remove(
id );
96 if ( !mRelations.contains(
id ) )
100 const QList<QString> keys = mPolymorphicRelations.keys();
101 for (
const QString &key : keys )
103 if (
id.startsWith( key ) )
105 return mRelations.value( mPolymorphicRelations[key].upgradeGeneratedRelationId(
id ) );
110 return mRelations.value(
id );
117 for (
const QgsRelation &rel : std::as_const( mRelations ) )
119 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
136 return mRelations.values();
141 for (
const QgsRelation &rel : std::as_const( mRelations ) )
143 if ( rel.referencingLayer() == layer )
145 if ( fieldIdx != -2 )
147 bool containsField =
false;
148 const auto constFieldPairs = rel.fieldPairs();
153 containsField =
true;
158 if ( !containsField )
174 return mRelations.values();
179 for (
const QgsRelation &rel : std::as_const( mRelations ) )
181 if ( rel.referencedLayer() == layer )
190void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
193 mPolymorphicRelations.clear();
195 QDomNodeList relationNodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
196 if ( relationNodes.count() )
200 QDomNode node = relationNodes.item( 0 );
201 QDomNodeList relationNodes = node.childNodes();
202 int relCount = relationNodes.count();
203 for (
int i = 0; i < relCount; ++i )
210 QgsDebugMsgLevel( QStringLiteral(
"No relations data present in this document" ), 2 );
213 QDomNodeList polymorphicRelationNodes = doc.elementsByTagName( QStringLiteral(
"polymorphicRelations" ) );
214 if ( polymorphicRelationNodes.count() )
218 QDomNode node = polymorphicRelationNodes.item( 0 );
219 QDomNodeList relationNodes = node.childNodes();
220 int relCount = relationNodes.count();
221 for (
int i = 0; i < relCount; ++i )
228 QgsDebugMsgLevel( QStringLiteral(
"No polymorphic relations data present in this document" ), 3 );
235void QgsRelationManager::writeProject( QDomDocument &doc )
237 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
240 QgsDebugError( QStringLiteral(
"Unable to find qgis element in project file" ) );
243 QDomNode qgisNode = nl.item( 0 );
245 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
246 qgisNode.appendChild( relationsNode );
263 QDomElement polymorphicRelationsNode = doc.createElement( QStringLiteral(
"polymorphicRelations" ) );
264 qgisNode.appendChild( polymorphicRelationsNode );
272void QgsRelationManager::layersRemoved(
const QStringList &layers )
274 bool relationsChanged =
false;
275 for (
const QString &layer : std::as_const( layers ) )
277 QMapIterator<QString, QgsRelation> it( mRelations );
279 while ( it.hasNext() )
283 if ( it.value().referencedLayerId() == layer
284 || it.value().referencingLayerId() == layer )
286 mRelations.remove( it.key() );
287 relationsChanged =
true;
291 if ( relationsChanged )
297static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &relation )
299 for (
const QgsRelation &cur : std::as_const( existingRelations ) )
301 if ( cur.hasEqualDefinition( relation ) )
return true;
308 QList<QgsRelation> result;
313 const auto constDiscoverRelations = provider->discoverRelations( layer, layers );
316 if ( !hasRelationWithEqualDefinition( existingRelations,
relation ) )
328 return mPolymorphicRelations;
333 return mPolymorphicRelations.value( polymorphicRelationId );
344 for (
const QgsRelation &generatedRelation : generatedRelations )
352 const QList<QgsRelation> generatedRelations =
relation.generateRelations();
353 for (
const QgsRelation &generatedRelation : generatedRelations )
@ Generated
A generated relation is a child of a polymorphic relation.
@ Normal
A normal relation.
Q_INVOKABLE 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 &document, QgsReadWriteContext &context)
Emitted when a project is being read.
void setDirty(bool b=true)
Flag the project as dirty (modified).
void writeProject(QDomDocument &document)
Emitted when the project is being written.
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.
Represents a relationship between two vector layers.
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.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)