77 mRelations.remove(
id );
83 mRelations.remove( relation.
id() );
89 return mRelations.value(
id );
96 const auto constMRelations = mRelations;
99 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
116 return mRelations.values();
121 const auto constMRelations = mRelations;
124 if ( rel.referencingLayer() == layer )
126 if ( fieldIdx != -2 )
128 bool containsField =
false;
129 const auto constFieldPairs = rel.fieldPairs();
134 containsField =
true;
139 if ( !containsField )
144 relations.append( rel );
155 return mRelations.values();
160 const auto constMRelations = mRelations;
163 if ( rel.referencedLayer() == layer )
165 relations.append( rel );
172 void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
176 QDomNodeList nodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
179 QDomNode node = nodes.item( 0 );
180 QDomNodeList relationNodes = node.childNodes();
181 int relCount = relationNodes.count();
182 for (
int i = 0; i < relCount; ++i )
189 QgsDebugMsg( QStringLiteral(
"No relations data present in this document" ) );
196 void QgsRelationManager::writeProject( QDomDocument &doc )
198 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
201 QgsDebugMsg( QStringLiteral(
"Unable to find qgis element in project file" ) );
204 QDomNode qgisNode = nl.item( 0 );
206 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
207 qgisNode.appendChild( relationsNode );
209 const auto constMRelations = mRelations;
216 void QgsRelationManager::layersRemoved(
const QStringList &layers )
218 bool relationsChanged =
false;
219 const auto constLayers = layers;
220 for (
const QString &layer : constLayers )
222 QMapIterator<QString, QgsRelation> it( mRelations );
224 while ( it.hasNext() )
228 if ( it.value().referencedLayerId() == layer
229 || it.value().referencingLayerId() == layer )
231 mRelations.remove( it.key() );
232 relationsChanged =
true;
236 if ( relationsChanged )
242 static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &
relation )
244 const auto constExistingRelations = existingRelations;
245 for (
const QgsRelation &cur : constExistingRelations )
247 if ( cur.hasEqualDefinition( relation ) )
return true;
254 QList<QgsRelation> result;
255 const auto constLayers = layers;
258 const auto constDiscoverRelations = layer->dataProvider()->discoverRelations( layer, layers );
259 for (
const QgsRelation &relation : constDiscoverRelations )
261 if ( !hasRelationWithEqualDefinition( existingRelations, relation ) )
263 result.append( relation );
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
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 > referencingRelations(const QgsVectorLayer *layer=nullptr, int fieldIdx=-2) const
Gets all relations where the specified layer (and field) is the referencing part (i.e.
void layersRemoved(const QStringList &layerIds)
Emitted after one or more layers were removed from the registry.
QgsVectorLayer referencingLayer
static QgsRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context)
Creates a relation from an XML structure.
void updateRelationsStatus()
Updates relations status.
void clear()
Remove any relation managed by this class.
void setRelations(const QList< QgsRelation > &relations)
Will set the specified relations and remove any relation currently set.
void readProjectWithContext(const QDomDocument &, QgsReadWriteContext &context)
Emitted when a project is being read.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Defines a relation between matching fields of the two involved tables of a relation.
QList< QgsRelation > relationsByName(const QString &name) const
Returns a list of relations with matching names.
QMap< QString, QgsRelation > relations() const
Gets access to the relations managed by this class.
Reads and writes project states.
QgsVectorLayer referencedLayer
QList< QgsRelation > referencedRelations(QgsVectorLayer *layer=nullptr) const
Gets all relations where this layer is the referenced part (i.e.
void relationsLoaded()
Emitted when the relations were loaded after reading a project.
void writeProject(QDomDocument &)
Emitted when the project is being written.
void changed()
Emitted when relations are added or removed to the manager.
static QList< QgsRelation > discoverRelations(const QList< QgsRelation > &existingRelations, const QList< QgsVectorLayer *> &layers)
Discover all the relations available from the current layers.
void removeRelation(const QString &id)
Remove a relation.
QgsRelationManager(QgsProject *project=nullptr)
Constructor for QgsRelationManager.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
void writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
void updateRelationStatus()
Updates the validity status of this relation.
void addRelation(const QgsRelation &relation)
Add a relation.