23#include "moc_qgsrelationmanager.cpp"
69 mProject->setDirty(
true );
77 for (
auto relationIt = mRelations.begin(); relationIt != mRelations.end(); ++relationIt )
79 relationIt->updateRelationStatus();
86 mRelations.remove(
id );
98 if ( !mRelations.contains(
id ) )
102 const QList<QString> keys = mPolymorphicRelations.keys();
103 for (
const QString &key : keys )
105 if (
id.startsWith( key ) )
107 return mRelations.value( mPolymorphicRelations[key].upgradeGeneratedRelationId(
id ) );
112 return mRelations.value(
id );
119 for (
const QgsRelation &rel : std::as_const( mRelations ) )
121 if ( QString::compare( rel.name(), name, Qt::CaseInsensitive ) == 0 )
138 return mRelations.values();
143 for (
const QgsRelation &rel : std::as_const( mRelations ) )
145 if ( rel.referencingLayer() == layer )
147 if ( fieldIdx != -2 )
149 bool containsField =
false;
150 const auto constFieldPairs = rel.fieldPairs();
155 containsField =
true;
160 if ( !containsField )
176 return mRelations.values();
181 for (
const QgsRelation &rel : std::as_const( mRelations ) )
183 if ( rel.referencedLayer() == layer )
192void QgsRelationManager::readProject(
const QDomDocument &doc,
QgsReadWriteContext &context )
195 mPolymorphicRelations.clear();
197 QDomNodeList relationNodes = doc.elementsByTagName( QStringLiteral(
"relations" ) );
198 if ( relationNodes.count() )
202 QDomNode node = relationNodes.item( 0 );
203 QDomNodeList relationNodes = node.childNodes();
204 int relCount = relationNodes.count();
205 for (
int i = 0; i < relCount; ++i )
212 QgsDebugMsgLevel( QStringLiteral(
"No relations data present in this document" ), 2 );
215 QDomNodeList polymorphicRelationNodes = doc.elementsByTagName( QStringLiteral(
"polymorphicRelations" ) );
216 if ( polymorphicRelationNodes.count() )
218 QgsRelationContext relcontext( mProject );
220 QDomNode node = polymorphicRelationNodes.item( 0 );
221 QDomNodeList relationNodes = node.childNodes();
222 int relCount = relationNodes.count();
223 for (
int i = 0; i < relCount; ++i )
230 QgsDebugMsgLevel( QStringLiteral(
"No polymorphic relations data present in this document" ), 3 );
237void QgsRelationManager::writeProject( QDomDocument &doc )
239 QDomNodeList nl = doc.elementsByTagName( QStringLiteral(
"qgis" ) );
242 QgsDebugError( QStringLiteral(
"Unable to find qgis element in project file" ) );
245 QDomNode qgisNode = nl.item( 0 );
247 QDomElement relationsNode = doc.createElement( QStringLiteral(
"relations" ) );
248 qgisNode.appendChild( relationsNode );
250 for (
const QgsRelation &
relation : std::as_const( mRelations ) )
262 relation.writeXml( relationsNode, doc );
265 QDomElement polymorphicRelationsNode = doc.createElement( QStringLiteral(
"polymorphicRelations" ) );
266 qgisNode.appendChild( polymorphicRelationsNode );
268 for (
const QgsPolymorphicRelation &
relation : std::as_const( mPolymorphicRelations ) )
270 relation.writeXml( polymorphicRelationsNode, doc );
274void QgsRelationManager::layersRemoved(
const QStringList &layers )
276 bool relationsChanged =
false;
277 for (
const QString &layer : std::as_const( layers ) )
279 QMapIterator<QString, QgsRelation> it( mRelations );
281 while ( it.hasNext() )
285 if ( it.value().referencedLayerId() == layer
286 || it.value().referencingLayerId() == layer )
288 mRelations.remove( it.key() );
289 relationsChanged =
true;
293 if ( relationsChanged )
299static bool hasRelationWithEqualDefinition(
const QList<QgsRelation> &existingRelations,
const QgsRelation &relation )
301 for (
const QgsRelation &cur : std::as_const( existingRelations ) )
303 if ( cur.hasEqualDefinition( relation ) )
return true;
310 QList<QgsRelation> result;
315 const auto constDiscoverRelations = provider->discoverRelations( layer, layers );
318 if ( !hasRelationWithEqualDefinition( existingRelations,
relation ) )
330 return mPolymorphicRelations;
335 return mPolymorphicRelations.value( polymorphicRelationId );
346 for (
const QgsRelation &generatedRelation : generatedRelations )
354 const QList<QgsRelation> generatedRelations =
relation.generateRelations();
355 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 relation where the referenced (parent) layer is calculated based on fields from the referencing (ch...
static QgsPolymorphicRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext=QgsRelationContext())
Creates a relation from an XML structure.
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 writeProject(QDomDocument &document)
Emitted when the project is being written.
A container for the context for various read/write operations on 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.
Q_INVOKABLE 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.
Q_INVOKABLE 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.
Base class for vector data providers.
Represents a vector layer which manages a vector based dataset.
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)