17 #include <QApplication>
23 const QString &referencingLayerId,
const QString &referencingLayerName,
const QString &referencingLayerSource,
const QString &referencingLayerProviderKey,
24 const QString &referencedLayerId,
const QString &referencedLayerName,
const QString &referencedLayerSource,
const QString &referencedLayerProviderKey,
25 const QList<QgsRelation::FieldPair> &fieldPairs )
26 : mReferencingLayer( referencingLayerId, referencingLayerName, referencingLayerSource, referencingLayerProviderKey )
27 , mReferencedLayer( referencedLayerId, referencedLayerName, referencedLayerSource, referencedLayerProviderKey )
28 , mRelationId( relationId )
29 , mRelationName( relationName )
30 , mStrength( strength )
31 , mFieldPairs( fieldPairs )
38 relation.
setId( mRelationId );
39 relation.
setName( mRelationName );
53 for (
const auto &fp : std::as_const( mFieldPairs ) )
62 return mReferencingLayer;
67 return mReferencedLayer;
82 QDomElement relationElement = node.toElement();
84 if ( relationElement.tagName() != QLatin1String(
"relation" ) )
86 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( relationElement.tagName() ) );
90 const QDomNodeList fieldPairNodes { relationElement.elementsByTagName( QStringLiteral(
"fieldRef" ) ) };
91 for (
int j = 0; j < fieldPairNodes.length(); ++j )
93 const QDomElement fieldPairElement = fieldPairNodes.at( j ).toElement();
94 fieldPairs.push_back( { fieldPairElement.attribute( QStringLiteral(
"referencingField" ) ),
95 fieldPairElement.attribute( QStringLiteral(
"referencedField" ) )
102 return QgsWeakRelation { relationElement.attribute( QStringLiteral(
"id" ) ),
103 relationElement.attribute( QStringLiteral(
"name" ) ),
111 relationElement.attribute( QStringLiteral(
"layerId" ) ),
112 relationElement.attribute( QStringLiteral(
"layerName" ) ),
113 relationElement.attribute( QStringLiteral(
"dataSource" ) ),
114 relationElement.attribute( QStringLiteral(
"providerKey" ) ),
118 return QgsWeakRelation { relationElement.attribute( QStringLiteral(
"id" ) ),
119 relationElement.attribute( QStringLiteral(
"name" ) ),
122 relationElement.attribute( QStringLiteral(
"layerId" ) ),
123 relationElement.attribute( QStringLiteral(
"layerName" ) ),
124 relationElement.attribute( QStringLiteral(
"dataSource" ) ),
125 relationElement.attribute( QStringLiteral(
"providerKey" ) ),
135 return QgsWeakRelation( QString(), QString(), QgsRelation::RelationStrength::Association, QString(), QString(), QString(),
136 QString(), QString(), QString(), QString(), QString(), QList< QgsRelation::FieldPair >() );
150 QDomNodeList relationsNodeList = node.toElement().elementsByTagName( QStringLiteral(
"relation" ) );
151 QDomElement relationElement;
153 for (
int i = 0; i < relationsNodeList.size(); ++i )
155 relationElement = relationsNodeList.at( i ).toElement();
156 if ( relationElement.hasAttribute( QStringLiteral(
"id" ) ) && relationElement.attribute( QStringLiteral(
"id" ) ) == relation.
id() )
162 relationElement.setAttribute( QStringLiteral(
"layerId" ), relation.
referencedLayer()->
id() );
163 relationElement.setAttribute( QStringLiteral(
"layerName" ), relation.
referencedLayer()->
name() );
170 relationElement.setAttribute( QStringLiteral(
"layerId" ), relation.
referencingLayer()->
id() );
171 relationElement.setAttribute( QStringLiteral(
"layerName" ), relation.
referencingLayer()->
name() );
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() const
Gets a version of the internal layer definition that has sensitive bits removed (for example,...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
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 QgsProject * instance()
Returns the QgsProject singleton instance.
QgsPathResolver pathResolver() const
Returns path resolver object with considering whether the project uses absolute or relative paths and...
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 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.
RelationStrength
enum for the relation strength Association, Composition
QgsVectorLayer * referencingLayer
void setStrength(RelationStrength strength)
Set a strength for this relation.
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...
QgsVectorLayerRef referencedLayer() const
Returns a weak reference to the referenced layer.
QgsRelation::RelationStrength strength() const
Returns the strength of the relation.
WeakRelationType
Enum to distinguish if the layer is referenced or referencing.
@ Referencing
The layer is referencing.
@ Referenced
The layer is referenced.
QgsVectorLayerRef referencingLayer() const
Returns a weak reference to the referencing layer.
QgsRelation resolvedRelation(const QgsProject *project, QgsVectorLayerRef::MatchType matchType=QgsVectorLayerRef::MatchType::All) const
Resolves a weak relation in the given project returning a possibly invalid QgsRelation and without pe...
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.
QgsWeakRelation(const QString &relationId, const QString &relationName, const QgsRelation::RelationStrength strength, const QString &referencingLayerId, const QString &referencingLayerName, const QString &referencingLayerSource, const QString &referencingLayerProviderKey, const QString &referencedLayerId, const QString &referencedLayerName, const QString &referencedLayerSource, const QString &referencedLayerProviderKey, const QList< QgsRelation::FieldPair > &fieldPairs)
Creates a QgsWeakRelation.
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the list of field pairs.
MatchType
Flag for match type in weak resolution.
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.