65 mRelations.remove(
id );
71 mRelations.remove( relation.
id() );
77 return mRelations.value(
id );
86 if ( QString::compare( rel.
name(), name, Qt::CaseInsensitive ) == 0 )
103 return mRelations.values();
112 if ( fieldIdx != -2 )
114 bool containsField =
false;
119 containsField =
true;
124 if ( !containsField )
129 relations.append( rel );
140 return mRelations.values();
149 relations.append( rel );
156 void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
160 QDomNodeList nodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
163 QDomNode node = nodes.item( 0 );
164 QDomNodeList relationNodes = node.childNodes();
165 int relCount = relationNodes.count();
166 for (
int i = 0; i < relCount; ++i )
173 QgsDebugMsg( QStringLiteral(
"No relations data present in this document" ) );
180 void QgsRelationManager::writeProject( QDomDocument &doc )
182 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
185 QgsDebugMsg( QStringLiteral(
"Unable to find qgis element in project file" ) );
188 QDomNode qgisNode = nl.item( 0 );
190 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
191 qgisNode.appendChild( relationsNode );
195 relation.
writeXml( relationsNode, doc );
199 void QgsRelationManager::layersRemoved(
const QStringList &layers )
201 bool relationsChanged =
false;
202 Q_FOREACH (
const QString &layer, layers )
204 QMapIterator<QString, QgsRelation> it( mRelations );
206 while ( it.hasNext() )
210 if ( it.value().referencedLayerId() == layer
211 || it.value().referencingLayerId() == layer )
213 mRelations.remove( it.key() );
214 relationsChanged =
true;
218 if ( relationsChanged )
224 static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &
relation )
226 Q_FOREACH (
const QgsRelation &cur, existingRelations )
235 QList<QgsRelation> result;
240 if ( !hasRelationWithEqualDefinition( existingRelations, relation ) )
242 result.append( relation );
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...
void layersRemoved(const QStringList &layerIds)
Emitted after one or more layers were removed from the registry.
virtual QList< QgsRelation > discoverRelations(const QgsVectorLayer *self, const QList< QgsVectorLayer * > &layers) const
Discover the available relations with the given layers.
QgsVectorLayer referencingLayer
static QgsRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context)
Creates a relation from an XML structure.
QList< QgsRelation > relationsByName(const QString &name) const
Returns a list of relations with matching names.
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 writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
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.
QMap< QString, QgsRelation > relations() const
Gets access to the relations managed by this class.
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.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
QString referencingField() const
Gets the name of the referencing (child) field.
static QList< QgsRelation > discoverRelations(const QList< QgsRelation > &existingRelations, const QList< QgsVectorLayer * > &layers)
Discover all the relations available from the current layers.
Reads and writes project states.
QgsVectorLayer referencedLayer
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
void relationsLoaded()
This signal is emitted when the relations were loaded after reading a project.
QList< QgsRelation > referencedRelations(QgsVectorLayer *layer=nullptr) const
Gets all relations where this layer is the referenced part (i.e.
bool hasEqualDefinition(const QgsRelation &other) const
Compares the two QgsRelation, ignoring the name and the ID.
void writeProject(QDomDocument &)
Emitted when the project is being written.
void changed()
Emitted when relations are added or removed to the manager.
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
void removeRelation(const QString &id)
Remove a relation.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider.
QgsRelationManager(QgsProject *project=nullptr)
Constructor for QgsRelationManager.
Represents a vector layer which manages a vector based data sets.
void addRelation(const QgsRelation &relation)
Add a relation.