17#include <QApplication>
26 const QString &referencingLayerId,
const QString &referencingLayerName,
const QString &referencingLayerSource,
const QString &referencingLayerProviderKey,
27 const QString &referencedLayerId,
const QString &referencedLayerName,
const QString &referencedLayerSource,
const QString &referencedLayerProviderKey )
28 : mReferencingLayer( referencingLayerId, referencingLayerName, referencingLayerSource, referencingLayerProviderKey )
29 , mReferencedLayer( referencedLayerId, referencedLayerName, referencedLayerSource, referencedLayerProviderKey )
30 , mRelationId( relationId )
31 , mRelationName( relationName )
32 , mStrength( strength )
38 QList< QgsRelation > res;
40 switch ( mCardinality )
48 relation.
setId( mRelationId );
49 relation.
setName( mRelationName );
64 for (
int i = 0 ; i < std::min( mReferencingLayerFields.size(), mReferencedLayerFields.size() ); ++i )
66 relation.
addFieldPair( mReferencingLayerFields.at( i ), mReferencedLayerFields.at( i ) );
69 res.push_back( relation );
77 relationLeft.
setId( mRelationId + QStringLiteral(
"_forward" ) );
78 relationLeft.
setName( mRelationName + QStringLiteral(
"_forward" ) );
94 for (
int i = 0 ; i < std::min( mMappingReferencedLayerFields.size(), mReferencedLayerFields.size() ); ++i )
96 relationLeft.
addFieldPair( mMappingReferencedLayerFields.at( i ), mReferencedLayerFields.at( i ) );
99 res.push_back( relationLeft );
102 relationRight.
setId( mRelationId + QStringLiteral(
"_backward" ) );
103 relationRight.
setName( mRelationName + QStringLiteral(
"_backward" ) );
117 for (
int i = 0 ; i < std::min( mMappingReferencingLayerFields.size(), mReferencingLayerFields.size() ); ++i )
119 relationRight.
addFieldPair( mMappingReferencingLayerFields.at( i ), mReferencingLayerFields.at( i ) );
122 res.push_back( relationRight );
132 return mReferencingLayer;
137 return mReferencingLayer.
source;
147 mReferencingLayer.
source = sourceUri;
148 mReferencingLayer.
provider = provider;
155 return metadata->decodeUri( mReferencingLayer.
source ).value( QStringLiteral(
"layerName" ) ).toString();
162 return mReferencedLayer;
167 return mReferencedLayer.
source;
179 return metadata->decodeUri( mReferencedLayer.
source ).value( QStringLiteral(
"layerName" ) ).toString();
186 mReferencedLayer.
source = sourceUri;
187 mReferencedLayer.
provider = provider;
192 return mMappingTable;
197 mMappingTable = table;
202 return mMappingTable.
source;
214 return metadata->decodeUri( mMappingTable.
source ).value( QStringLiteral(
"layerName" ) ).toString();
221 mMappingTable.
source = sourceUri;
232 QDomElement relationElement = node.toElement();
234 if ( relationElement.tagName() != QLatin1String(
"relation" ) )
236 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( relationElement.tagName() ) );
239 QStringList referencingFields;
240 QStringList referencedFields;
241 const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral(
"fieldRef" ) ) };
242 for (
int j = 0; j < fieldPairNodes.length(); ++j )
244 const QDomElement fieldPairElement = fieldPairNodes.at( j ).toElement();
245 referencingFields.push_back( fieldPairElement.attribute( QStringLiteral(
"referencingField" ) ) );
246 referencedFields.push_back( fieldPairElement.attribute( QStringLiteral(
"referencedField" ) ) );
253 QgsWeakRelation rel{ relationElement.attribute( QStringLiteral(
"id" ) ),
254 relationElement.attribute( QStringLiteral(
"name" ) ),
262 relationElement.attribute( QStringLiteral(
"layerId" ) ),
263 relationElement.attribute( QStringLiteral(
"layerName" ) ),
264 relationElement.attribute( QStringLiteral(
"dataSource" ) ),
265 relationElement.attribute( QStringLiteral(
"providerKey" ) )
267 rel.setReferencedLayerFields( referencedFields );
268 rel.setReferencingLayerFields( referencingFields );
274 QgsWeakRelation rel{ relationElement.attribute( QStringLiteral(
"id" ) ),
275 relationElement.attribute( QStringLiteral(
"name" ) ),
278 relationElement.attribute( QStringLiteral(
"layerId" ) ),
279 relationElement.attribute( QStringLiteral(
"layerName" ) ),
280 relationElement.attribute( QStringLiteral(
"dataSource" ) ),
281 relationElement.attribute( QStringLiteral(
"providerKey" ) ),
288 rel.setReferencedLayerFields( referencedFields );
289 rel.setReferencingLayerFields( referencingFields );
308 resolver = project->pathResolver();
311 QDomNodeList relationsNodeList = node.toElement().elementsByTagName( QStringLiteral(
"relation" ) );
312 QDomElement relationElement;
314 for (
int i = 0; i < relationsNodeList.size(); ++i )
316 relationElement = relationsNodeList.at( i ).toElement();
317 if ( relationElement.hasAttribute( QStringLiteral(
"id" ) ) && relationElement.attribute( QStringLiteral(
"id" ) ) == relation.
id() )
323 relationElement.setAttribute( QStringLiteral(
"layerId" ), relation.
referencedLayer()->
id() );
324 relationElement.setAttribute( QStringLiteral(
"layerName" ), relation.
referencedLayer()->
name() );
331 relationElement.setAttribute( QStringLiteral(
"layerId" ), relation.
referencingLayer()->
id() );
332 relationElement.setAttribute( QStringLiteral(
"layerName" ), relation.
referencingLayer()->
name() );
RelationshipStrength
Relationship strength.
@ ManyToMany
Many to many relationship.
@ ManyToOne
Many to one relationship.
@ OneToOne
One to one relationship.
@ OneToMany
One to many relationship.
static void warning(const QString &msg)
Goes to qWarning.
Base class for all map layer types.
QString providerType() const
Returns the provider type (provider key) for this layer.
QString publicSource(bool hidePassword=false) const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QgsProject * project() const
Returns the parent project if this map layer is added to a project.
Resolves relative paths into absolute paths and vice versa.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
static QgsProviderRegistry * instance(const QString &pluginPath=QString())
Means of accessing canonical single instance.
Represents a relationship between two vector layers.
void setId(const QString &id)
Set an id for this relation.
void setReferencedLayer(const QString &id)
Set the referenced (parent) layer id.
QgsVectorLayer * referencedLayer
void setStrength(Qgis::RelationshipStrength strength)
Set a strength for this relation.
void addFieldPair(const QString &referencingField, const QString &referencedField)
Add a field pair which is part of this relation The first element of each pair are the field names of...
void setReferencingLayer(const QString &id)
Set the referencing (child) layer id.
QgsVectorLayer * referencingLayer
void setName(const QString &name)
Set a name for this relation.
void writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
Represents a vector layer which manages a vector based data sets.
The QgsWeakRelation class represent a QgsRelation with possibly unresolved layers or unmatched fields...
void setReferencedLayer(const QString &sourceUri, const QString &provider)
Sets the source for the referenced (or "parent" / "left") layer, by sourceUri and provider ID.
QgsVectorLayerRef referencedLayer() const
Returns a weak reference to the referenced (or "parent" / "left") layer.
QList< QgsRelation > resolvedRelations(const QgsProject *project, QgsVectorLayerRef::MatchType matchType=QgsVectorLayerRef::MatchType::All) const
Resolves a weak relation in the given project returning a list of possibly invalid QgsRelations and w...
void setMappingTable(const QgsVectorLayerRef &table)
Sets a weak reference to the mapping table, which forms the middle table in many-to-many relationship...
WeakRelationType
Enum to distinguish if the layer is referenced or referencing.
@ Referencing
The layer is referencing (or the "child" / "right" layer in the relationship)
@ Referenced
The layer is referenced (or the "parent" / "left" left in the relationship)
QString referencedLayerProvider() const
Returns the provider ID for the referenced (or "parent" / "left") layer.
QgsWeakRelation()
Default constructor for an invalid relation.
QString mappingTableProvider() const
Returns the provider ID for the mapping table, which forms the middle table in many-to-many relations...
QString mappingTableName() const
Returns the layer name of the mapping table, which forms the middle table in many-to-many relationshi...
QString mappingTableSource() const
Returns the source URI for the mapping table, which forms the middle table in many-to-many relationsh...
QString referencingLayerProvider() const
Returns the provider ID for the referencing (or "child" / "right") layer.
QgsVectorLayerRef referencingLayer() const
Returns a weak reference to the referencing (or "child" / "right") layer.
QString referencedLayerName() const
Returns the layer name of the referenced (or "parent" / "left") layer.
static void writeXml(const QgsVectorLayer *layer, WeakRelationType type, const QgsRelation &relation, QDomNode &node, QDomDocument &doc)
Writes a weak relation infoto an XML structure.
static QgsWeakRelation readXml(const QgsVectorLayer *layer, WeakRelationType type, const QDomNode &node, const QgsPathResolver resolver)
Returns a weak relation for the given layer.
void setReferencingLayer(const QString &sourceUri, const QString &provider)
Sets the source for the referencing (or "child" / "right") layer, by sourceUri and provider ID.
QString referencedLayerSource() const
Returns the source URI for the referenced (or "parent" / "left") layer.
QString referencingLayerSource() const
Returns the source URI for the referencing (or "child" / "right") layer.
QgsVectorLayerRef mappingTable() const
Returns a weak reference to the mapping table, which forms the middle table in many-to-many relations...
QString referencingLayerName() const
Returns the layer name of the referencing (or "child" / "right") layer.
Qgis::RelationshipStrength strength() const
Returns the strength of the relation.
TYPE * resolveWeakly(const QgsProject *project, MatchType matchType=MatchType::All)
Resolves the map layer by attempting to find a matching layer in a project using a weak match.
MatchType
Flag for match type in weak resolution.
QString source
Weak reference to layer public source.
QString provider
Weak reference to layer provider.