17#include "moc_qgsrelationmanager.cpp"
76 for (
auto relationIt = mRelations.begin(); relationIt != mRelations.end(); ++relationIt )
78 relationIt->updateRelationStatus();
85 mRelations.remove(
id );
97 if ( !mRelations.contains(
id ) )
101 const QList<QString> keys = mPolymorphicRelations.keys();
102 for (
const QString &key : keys )
104 if (
id.startsWith( key ) )
106 return mRelations.value( mPolymorphicRelations[key].upgradeGeneratedRelationId(
id ) );
111 return mRelations.value(
id );
118 for (
const QgsRelation &rel : std::as_const( mRelations ) )
120 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
137 return mRelations.values();
142 for (
const QgsRelation &rel : std::as_const( mRelations ) )
144 if ( rel.referencingLayer() == layer )
146 if ( fieldIdx != -2 )
148 bool containsField =
false;
149 const auto constFieldPairs = rel.fieldPairs();
154 containsField =
true;
159 if ( !containsField )
175 return mRelations.values();
180 for (
const QgsRelation &rel : std::as_const( mRelations ) )
182 if ( rel.referencedLayer() == layer )
191void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
194 mPolymorphicRelations.clear();
196 QDomNodeList relationNodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
197 if ( relationNodes.count() )
201 QDomNode node = relationNodes.item( 0 );
202 QDomNodeList relationNodes = node.childNodes();
203 int relCount = relationNodes.count();
204 for (
int i = 0; i < relCount; ++i )
211 QgsDebugMsgLevel( QStringLiteral(
"No relations data present in this document" ), 2 );
214 QDomNodeList polymorphicRelationNodes = doc.elementsByTagName( QStringLiteral(
"polymorphicRelations" ) );
215 if ( polymorphicRelationNodes.count() )
219 QDomNode node = polymorphicRelationNodes.item( 0 );
220 QDomNodeList relationNodes = node.childNodes();
221 int relCount = relationNodes.count();
222 for (
int i = 0; i < relCount; ++i )
229 QgsDebugMsgLevel( QStringLiteral(
"No polymorphic relations data present in this document" ), 3 );
236void QgsRelationManager::writeProject( QDomDocument &doc )
238 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
241 QgsDebugError( QStringLiteral(
"Unable to find qgis element in project file" ) );
244 QDomNode qgisNode = nl.item( 0 );
246 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
247 qgisNode.appendChild( relationsNode );
264 QDomElement polymorphicRelationsNode = doc.createElement( QStringLiteral(
"polymorphicRelations" ) );
265 qgisNode.appendChild( polymorphicRelationsNode );
273void QgsRelationManager::layersRemoved(
const QStringList &layers )
275 bool relationsChanged =
false;
276 for (
const QString &layer : std::as_const( layers ) )
278 QMapIterator<QString, QgsRelation> it( mRelations );
280 while ( it.hasNext() )
284 if ( it.value().referencedLayerId() == layer
285 || it.value().referencingLayerId() == layer )
287 mRelations.remove( it.key() );
288 relationsChanged =
true;
292 if ( relationsChanged )
298static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &relation )
300 for (
const QgsRelation &cur : std::as_const( existingRelations ) )
302 if ( cur.hasEqualDefinition( relation ) )
return true;
309 QList<QgsRelation> result;
314 const auto constDiscoverRelations = provider->discoverRelations( layer, layers );
317 if ( !hasRelationWithEqualDefinition( existingRelations,
relation ) )
329 return mPolymorphicRelations;
334 return mPolymorphicRelations.value( polymorphicRelationId );
345 for (
const QgsRelation &generatedRelation : generatedRelations )
353 const QList<QgsRelation> generatedRelations =
relation.generateRelations();
354 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)