62 mRelations.remove(
id );
68 mRelations.remove( relation.
id() );
74 return mRelations.value(
id );
83 if ( QString::compare( rel.
name(), name, Qt::CaseInsensitive ) == 0 )
100 return mRelations.values();
109 if ( fieldIdx != -2 )
111 bool containsField =
false;
116 containsField =
true;
121 if ( !containsField )
126 relations.append( rel );
137 return mRelations.values();
146 relations.append( rel );
153 void QgsRelationManager::readProject(
const QDomDocument &doc )
157 QDomNodeList nodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
160 QDomNode node = nodes.item( 0 );
161 QDomNodeList relationNodes = node.childNodes();
162 int relCount = relationNodes.count();
163 for (
int i = 0; i < relCount; ++i )
170 QgsDebugMsg(
"No relations data present in this document" );
177 void QgsRelationManager::writeProject( QDomDocument &doc )
179 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
182 QgsDebugMsg(
"Unable to find qgis element in project file" );
185 QDomNode qgisNode = nl.item( 0 );
187 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
188 qgisNode.appendChild( relationsNode );
192 relation.
writeXml( relationsNode, doc );
196 void QgsRelationManager::layersRemoved(
const QStringList &layers )
198 bool relationsChanged =
false;
199 Q_FOREACH (
const QString &layer, layers )
201 QMapIterator<QString, QgsRelation> it( mRelations );
203 while ( it.hasNext() )
207 if ( it.value().referencedLayerId() == layer
208 || it.value().referencingLayerId() == layer )
210 mRelations.remove( it.key() );
211 relationsChanged =
true;
215 if ( relationsChanged )
221 static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &
relation )
223 Q_FOREACH (
const QgsRelation &cur, existingRelations )
232 QList<QgsRelation> result;
237 if ( !hasRelationWithEqualDefinition( existingRelations, relation ) )
239 result.append( relation );
int lookupField(const QString &fieldName) const
Look up field's index from the field name.
void setDirty(bool b=true)
Flag the project as dirty (modified).
QList< QgsRelation > referencingRelations(const QgsVectorLayer *layer=nullptr, int fieldIdx=-2) const
Get 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
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.
Defines a relation between matching fields of the two involved tables of a relation.
QgsFields fields() const override
Returns the list of fields of this layer.
QList< QgsRelation > relationsByName(const QString &name) const
Returns a list of relations with matching names.
QMap< QString, QgsRelation > relations() const
Get access to the relations managed by this class.
void readProject(const QDomDocument &)
emitted when project is being read
static QgsRelation createFromXml(const QDomNode &node)
Creates a relation from an XML structure.
Reads and writes project states.
QgsVectorLayer referencedLayer
QList< QgsRelation > referencedRelations(QgsVectorLayer *layer=nullptr) const
Get 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.
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 relationsLoaded()
This signal is emitted when the relations were loaded after reading a project.
void writeProject(QDomDocument &)
emitted when 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.
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
void removeRelation(const QString &id)
Remove a relation.
QgsRelationManager(QgsProject *project=nullptr)
Constructor for QgsRelationManager.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Get access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
QString referencingField() const
Get the name of the referencing (child) field.
void writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
void addRelation(const QgsRelation &relation)
Add a relation.
virtual QList< QgsRelation > discoverRelations(const QgsVectorLayer *self, const QList< QgsVectorLayer *> &layers) const
Discover the available relations with the given layers.