22#include <QApplication>
25 : d( new QgsPolymorphicRelationPrivate() )
30 : d( new QgsPolymorphicRelationPrivate() )
39 , mContext( other.mContext )
46 mContext = other.mContext;
53 QDomElement elem = node.toElement();
55 if ( elem.tagName() != QLatin1String(
"relation" ) )
57 QgsLogger::warning( QApplication::translate(
"QgsPolymorphicRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
65 QString
id = elem.attribute( QStringLiteral(
"id" ) );
66 QString
name = elem.attribute( QStringLiteral(
"name" ) );
67 QString relationStrength = elem.attribute( QStringLiteral(
"relationStrength" ) );
68 QStringList
referencedLayerIds = elem.attribute( QStringLiteral(
"referencedLayerIds" ) ).split(
"," );
70 QMap<QString, QgsMapLayer *> mapLayers = relationContext.
project()->
mapLayers();
73 relation.d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[
referencingLayerId] );
77 relation.d->mRelationId =
id;
78 relation.d->mRelationName =
name;
81 QDomNodeList references = elem.elementsByTagName( QStringLiteral(
"fieldRef" ) );
82 for (
int i = 0; i < references.size(); ++i )
84 QDomElement refEl = references.at( i ).toElement();
86 QString referencingField = refEl.attribute( QStringLiteral(
"referencingField" ) );
87 QString referencedField = refEl.attribute( QStringLiteral(
"referencedField" ) );
89 relation.
addFieldPair( referencingField, referencedField );
99 QDomElement elem = doc.createElement( QStringLiteral(
"relation" ) );
100 elem.setAttribute( QStringLiteral(
"id" ), d->mRelationId );
101 elem.setAttribute( QStringLiteral(
"name" ), d->mRelationName );
102 elem.setAttribute( QStringLiteral(
"referencingLayer" ), d->mReferencingLayerId );
103 elem.setAttribute( QStringLiteral(
"referencedLayerField" ), d->mReferencedLayerField );
104 elem.setAttribute( QStringLiteral(
"referencedLayerExpression" ), d->mReferencedLayerExpression );
105 elem.setAttribute( QStringLiteral(
"referencedLayerIds" ), d->mReferencedLayerIds.join(
"," ) );
106 elem.setAttribute( QStringLiteral(
"relationStrength" ), qgsEnumValueToKey<Qgis::RelationshipStrength>( d->mRelationStrength ) );
109 for (
const QString &layerId : std::as_const( d->mReferencedLayerIds ) )
110 Q_ASSERT( ! layerId.contains(
"," ) );
114 QDomElement referenceElem = doc.createElement( QStringLiteral(
"fieldRef" ) );
115 referenceElem.setAttribute( QStringLiteral(
"referencingField" ), pair.first );
116 referenceElem.setAttribute( QStringLiteral(
"referencedField" ), pair.second );
117 elem.appendChild( referenceElem );
120 node.appendChild( elem );
125 if ( d->mRelationId ==
id )
137 d->mReferencingLayerId =
id;
152 d->mFieldPairs << fieldPair;
158 return d->mRelationId;
163 d->mRelationId = QStringLiteral(
"%1_%2_%3_%4" )
165 d->mFieldPairs.at( 0 ).referencingField(),
167 d->mFieldPairs.at( 0 ).referencedField() );
173 return d->mReferencingLayerId;
178 return d->mReferencingLayer;
183 return d->mFieldPairs;
190 if ( d->mReferencedLayerIds.contains( layerId ) )
212 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
220 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayerField.isNull() && d->mReferencingLayer.data()->isValid() && !d->mReferencedLayerExpression.isNull();
225 return d->mReferencedLayerField == other.d->mReferencedLayerField
226 && d->mReferencedLayerExpression == other.d->mReferencedLayerExpression
227 && d->mReferencingLayerId == other.d->mReferencingLayerId
228 && d->mFieldPairs == other.d->mFieldPairs;
233 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.
project()->
mapLayers();
236 d->mReferencingLayer = mapLayers.contains( d->mReferencingLayerId )
237 ? qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] )
239 d->mReferencedLayersMap.clear();
241 if ( d->mRelationId.isEmpty() )
243 QgsDebugMsg( QStringLiteral(
"Invalid relation: no ID" ) );
248 if ( !d->mReferencingLayer )
250 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referencing layer does not exist. ID: %1" ).arg( d->mReferencingLayerId ), 4 );
255 if ( d->mReferencingLayer->fields().lookupField( d->mReferencedLayerField ) == -1 )
257 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer field \"%1\" does not exist in layer with ID: %2" ).arg( d->mReferencedLayerField, d->mReferencingLayerId ), 4 );
262 if ( d->mReferencedLayerExpression.trimmed().isNull() )
264 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer expression \"%1\" is missing" ).arg( d->mReferencedLayerExpression ), 4 );
272 d->mReferencedLayersMap.insert( referencedLayerId, qobject_cast<QgsVectorLayer *>( mapLayers[referencedLayerId] ) );
274 if ( !d->mReferencedLayersMap[referencedLayerId] || !d->mReferencedLayersMap[referencedLayerId]->isValid() )
276 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer does not exist. ID: %1" ).arg( d->mReferencedLayersMap[referencedLayerId]->id() ), 4 );
282 if ( d->mFieldPairs.count() == 0 )
284 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: no pair of field is specified." ), 4 );
291 if ( d->mReferencingLayer->fields().lookupField( pair.first ) == -1 )
293 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() ) );
300 if ( d->mReferencedLayersMap[referencedLayerId]->fields().lookupField( pair.second ) == -1 )
302 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayersMap[referencedLayerId]->name() ) );
312 if ( d->mRelationName ==
name && !
name.isEmpty() )
316 d->mRelationName =
name;
322 if ( d->mRelationName.isEmpty() )
323 return QObject::tr(
"Polymorphic relations for \"%1\"" ).arg( d->mReferencingLayer ? d->mReferencingLayer->name() : QStringLiteral(
"<NO LAYER>" ) );
325 return d->mRelationName;
337 return d->mReferencedLayerField;
349 return d->mReferencedLayerExpression;
361 return d->mReferencedLayerIds;
366 return d->mRelationStrength;
372 d->mRelationStrength = relationStrength;
378 QList<QgsRelation> relations;
388 QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->name();
390 relation.
setId( QStringLiteral(
"%1_%2" ).arg( d->mRelationId, referencedLayerName ) );
393 relation.
setName( QStringLiteral(
"Generated for \"%1\"" ).arg( referencedLayerName ) );
397 const QList<QgsRelation::FieldPair> constFieldPairs =
fieldPairs();
404 relations << relation;
412 if ( !layer || !layer->
isValid() )
418 return expr.
evaluate( &context ).toString();
RelationshipStrength
Relationship strength.
@ Association
Loose relation, related elements are not part of the parent and a parent copy will not copy any child...
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...
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.
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()
void setRelationStrength(Qgis::RelationshipStrength relationStrength)
Sets the relation strength for all the generated normal relations.
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.
Qgis::RelationshipStrength strength() const
Returns the relation strength for all the generated normal relations.
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 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.
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)