27 : d( new QgsPolymorphicRelationPrivate() )
32 : d( new QgsPolymorphicRelationPrivate() )
41 , mContext( other.mContext )
48 mContext = other.mContext;
55 QDomElement elem = node.toElement();
57 if ( elem.tagName() != QLatin1String(
"relation" ) )
59 QgsLogger::warning( QApplication::translate(
"QgsPolymorphicRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
67 QString
id = elem.attribute( QStringLiteral(
"id" ) );
68 QString
name = elem.attribute( QStringLiteral(
"name" ) );
69 QString relationStrength = elem.attribute( QStringLiteral(
"relationStrength" ) );
70 QStringList
referencedLayerIds = elem.attribute( QStringLiteral(
"referencedLayerIds" ) ).split(
"," );
72 QMap<QString, QgsMapLayer *> mapLayers = relationContext.
project()->
mapLayers();
75 relation.d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[
referencingLayerId] );
79 relation.d->mRelationId =
id;
80 relation.d->mRelationName =
name;
81 relation.d->mRelationStrength = qgsEnumKeyToValue<QgsRelation::RelationStrength>( relationStrength, QgsRelation::RelationStrength::Association );
83 QDomNodeList references = elem.elementsByTagName( QStringLiteral(
"fieldRef" ) );
84 for (
int i = 0; i < references.size(); ++i )
86 QDomElement refEl = references.at( i ).toElement();
88 QString referencingField = refEl.attribute( QStringLiteral(
"referencingField" ) );
89 QString referencedField = refEl.attribute( QStringLiteral(
"referencedField" ) );
91 relation.
addFieldPair( referencingField, referencedField );
101 QDomElement elem = doc.createElement( QStringLiteral(
"relation" ) );
102 elem.setAttribute( QStringLiteral(
"id" ), d->mRelationId );
103 elem.setAttribute( QStringLiteral(
"name" ), d->mRelationName );
104 elem.setAttribute( QStringLiteral(
"referencingLayer" ), d->mReferencingLayerId );
105 elem.setAttribute( QStringLiteral(
"referencedLayerField" ), d->mReferencedLayerField );
106 elem.setAttribute( QStringLiteral(
"referencedLayerExpression" ), d->mReferencedLayerExpression );
107 elem.setAttribute( QStringLiteral(
"referencedLayerIds" ), d->mReferencedLayerIds.join(
"," ) );
108 elem.setAttribute( QStringLiteral(
"relationStrength" ), qgsEnumValueToKey<QgsRelation::RelationStrength>( d->mRelationStrength ) );
111 for (
const QString &layerId : qgis::as_const( d->mReferencedLayerIds ) )
112 Q_ASSERT( ! layerId.contains(
"," ) );
116 QDomElement referenceElem = doc.createElement( QStringLiteral(
"fieldRef" ) );
117 referenceElem.setAttribute( QStringLiteral(
"referencingField" ), pair.first );
118 referenceElem.setAttribute( QStringLiteral(
"referencedField" ), pair.second );
119 elem.appendChild( referenceElem );
122 node.appendChild( elem );
127 if ( d->mRelationId ==
id )
139 d->mReferencingLayerId =
id;
154 d->mFieldPairs << fieldPair;
160 return d->mRelationId;
165 d->mRelationId = QStringLiteral(
"%1_%2_%3_%4" )
167 d->mFieldPairs.at( 0 ).referencingField(),
169 d->mFieldPairs.at( 0 ).referencedField() );
175 return d->mReferencingLayerId;
180 return d->mReferencingLayer;
185 return d->mFieldPairs;
192 if ( d->mReferencedLayerIds.contains( layerId ) )
214 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
222 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayerField.isNull() && d->mReferencingLayer.data()->isValid() && !d->mReferencedLayerExpression.isNull();
227 return d->mReferencedLayerField == other.d->mReferencedLayerField
228 && d->mReferencedLayerExpression == other.d->mReferencedLayerExpression
229 && d->mReferencingLayerId == other.d->mReferencingLayerId
230 && d->mFieldPairs == other.d->mFieldPairs;
235 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.
project()->
mapLayers();
238 d->mReferencingLayer = mapLayers.contains( d->mReferencingLayerId )
239 ? qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] )
241 d->mReferencedLayersMap.clear();
243 if ( d->mRelationId.isEmpty() )
245 QgsDebugMsg( QStringLiteral(
"Invalid relation: no ID" ) );
250 if ( !d->mReferencingLayer )
252 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referencing layer does not exist. ID: %1" ).arg( d->mReferencingLayerId ), 4 );
257 if ( d->mReferencingLayer->fields().lookupField( d->mReferencedLayerField ) == -1 )
259 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer field \"%1\" does not exist in layer with ID: %2" ).arg( d->mReferencedLayerField, d->mReferencingLayerId ), 4 );
264 if ( d->mReferencedLayerExpression.trimmed().isNull() )
266 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer expression \"%1\" is missing" ).arg( d->mReferencedLayerExpression ), 4 );
274 d->mReferencedLayersMap.insert( referencedLayerId, qobject_cast<QgsVectorLayer *>( mapLayers[referencedLayerId] ) );
276 if ( !d->mReferencedLayersMap[referencedLayerId] || !d->mReferencedLayersMap[referencedLayerId]->isValid() )
278 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer does not exist. ID: %1" ).arg( d->mReferencedLayersMap[referencedLayerId]->id() ), 4 );
284 if ( d->mFieldPairs.count() == 0 )
286 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: no pair of field is specified." ), 4 );
293 if ( d->mReferencingLayer->fields().lookupField( pair.first ) == -1 )
295 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() ) );
302 if ( d->mReferencedLayersMap[referencedLayerId]->fields().lookupField( pair.second ) == -1 )
304 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayersMap[referencedLayerId]->name() ) );
314 if ( d->mRelationName ==
name && !
name.isEmpty() )
318 d->mRelationName =
name;
324 if ( d->mRelationName.isEmpty() )
325 return QObject::tr(
"Polymorphic relations for \"%1\"" ).arg( d->mReferencingLayer ? d->mReferencingLayer->name() : QStringLiteral(
"<NO LAYER>" ) );
327 return d->mRelationName;
339 return d->mReferencedLayerField;
351 return d->mReferencedLayerExpression;
363 return d->mReferencedLayerIds;
368 return d->mRelationStrength;
374 d->mRelationStrength = relationStrength;
380 QList<QgsRelation> relations;
390 QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->name();
394 relation.
setName( QStringLiteral(
"Generated for \"%1\"" ).arg( referencedLayerName ) );
395 relation.
setId( QStringLiteral(
"%1_%2" ).arg( d->mRelationId, referencedLayerName ) );
399 const QList<QgsRelation::FieldPair> constFieldPairs =
fieldPairs();
406 relations << relation;
414 if ( !layer || !layer->
isValid() )
420 return expr.
evaluate( &context ).toString();
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
QVariant evaluate()
Evaluate the feature and return the result.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static void warning(const QString &msg)
Goes to qWarning.
A polymorphic relation consists of the same properties like a normal relation except for the referenc...
void setRelationStrength(QgsRelation::RelationStrength relationStrength)
Sets the relation strength for all the generated normal relations.
QgsAttributeList referencedFields(const QString &layerId) const
Returns a list of attributes used to form the referenced fields (most likely primary key) on the refe...
QString layerRepresentation(const QgsVectorLayer *layer) const
Returns layer representation as evaluated string.
QList< QgsRelation > generateRelations() const
Returns a list of generated relations, based on the currently set referencedLayerIds()
void setReferencedLayerIds(const QStringList &childRelationIds)
Sets a list of layer ids to be used as potential referenced layers.
QString referencedLayerExpression
void setReferencedLayerExpression(const QString &expression)
Sets the expression to identify the parent layer.
void setReferencedLayerField(const QString &referencedLayerField)
Sets the field in the referencing layer where the referenced layer identifier is stored.
void generateId()
Generate a (project-wide) unique id for this relation.
void setReferencingLayer(const QString &id)
Set the referencing (child) layer 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 writeXml(QDomNode &node, QDomDocument &doc) const
Writes a relation to an XML structure.
QgsRelation::RelationStrength strength() const
Returns the relation strength for all the generated normal relations.
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...
static QgsPolymorphicRelation createFromXml(const QDomNode &node, QgsReadWriteContext &context, const QgsRelationContext &relationContext=QgsRelationContext())
Creates a relation from an XML structure.
~QgsPolymorphicRelation()
bool hasEqualDefinition(const QgsPolymorphicRelation &other) const
Compares the two QgsRelation, ignoring the name and the ID.
QString referencingLayerId() const
Access the referencing (child) layer's id This is the layer which has the field(s) which point to ano...
void updateRelationStatus()
Updates the validity status of this relation.
QgsVectorLayer * referencingLayer
QStringList referencedLayerIds() const
Returns a list of layer ids to be used as potential referenced layers.
QgsPolymorphicRelation()
Default constructor.
QgsAttributeList referencingFields() const
Returns a list of attributes used to form the referencing fields (foreign key) on the referencing (ch...
QString referencedLayerField
void setName(const QString &name)
Set a name for this relation.
void setId(const QString &id)
Set an id for this relation.
QgsPolymorphicRelation & operator=(const QgsPolymorphicRelation &other)
Copies a relation.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
The class is used as a container of context for various read/write operations on other objects.
const QgsProject * project() const
Gets the associated project.
Defines a relation between matching fields of the two involved tables of a relation.
void setId(const QString &id)
Set an id for this relation.
void setReferencedLayer(const QString &id)
Set the referenced (parent) layer id.
void setPolymorphicRelationId(const QString &polymorphicRelationId)
Sets the parent polymorphic relation id.
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
void setStrength(RelationStrength strength)
Set a strength for this relation.
void setName(const QString &name)
Set a name for this relation.
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QgsExpressionContext createExpressionContext() const FINAL
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QList< int > QgsAttributeList
#define QgsDebugMsgLevel(str, level)