23#include <QApplication>
25#include "moc_qgspolymorphicrelation.cpp"
28 : d( new QgsPolymorphicRelationPrivate() )
33 : d( new QgsPolymorphicRelationPrivate() )
42 , mContext( other.mContext )
47 : d( std::move( other.d ) )
48 , mContext( std::move( other.mContext ) )
58 mContext = other.mContext;
67 d = std::move( other.d );
68 mContext = std::move( other.mContext );
75 QDomElement elem = node.toElement();
77 if ( elem.tagName() != QLatin1String(
"relation" ) )
79 QgsLogger::warning( QApplication::translate(
"QgsPolymorphicRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
87 QString
id = elem.attribute( QStringLiteral(
"id" ) );
88 QString
name = elem.attribute( QStringLiteral(
"name" ) );
89 QString relationStrength = elem.attribute( QStringLiteral(
"relationStrength" ) );
90 QStringList
referencedLayerIds = elem.attribute( QStringLiteral(
"referencedLayerIds" ) ).split(
"," );
92 QMap<QString, QgsMapLayer *> mapLayers = relationContext.
project()->
mapLayers();
95 relation.d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[
referencingLayerId] );
99 relation.d->mRelationId =
id;
100 relation.d->mRelationName =
name;
103 QDomNodeList references = elem.elementsByTagName( QStringLiteral(
"fieldRef" ) );
104 for (
int i = 0; i < references.size(); ++i )
106 QDomElement refEl = references.at( i ).toElement();
108 QString referencingField = refEl.attribute( QStringLiteral(
"referencingField" ) );
109 QString referencedField = refEl.attribute( QStringLiteral(
"referencedField" ) );
111 relation.
addFieldPair( referencingField, referencedField );
121 QDomElement elem = doc.createElement( QStringLiteral(
"relation" ) );
122 elem.setAttribute( QStringLiteral(
"id" ), d->mRelationId );
123 elem.setAttribute( QStringLiteral(
"name" ), d->mRelationName );
124 elem.setAttribute( QStringLiteral(
"referencingLayer" ), d->mReferencingLayerId );
125 elem.setAttribute( QStringLiteral(
"referencedLayerField" ), d->mReferencedLayerField );
126 elem.setAttribute( QStringLiteral(
"referencedLayerExpression" ), d->mReferencedLayerExpression );
127 elem.setAttribute( QStringLiteral(
"referencedLayerIds" ), d->mReferencedLayerIds.join(
"," ) );
131 for (
const QString &layerId : std::as_const( d->mReferencedLayerIds ) )
132 Q_ASSERT( ! layerId.contains(
"," ) );
136 QDomElement referenceElem = doc.createElement( QStringLiteral(
"fieldRef" ) );
137 referenceElem.setAttribute( QStringLiteral(
"referencingField" ), pair.first );
138 referenceElem.setAttribute( QStringLiteral(
"referencedField" ), pair.second );
139 elem.appendChild( referenceElem );
142 node.appendChild( elem );
147 if ( d->mRelationId ==
id )
159 d->mReferencingLayerId =
id;
174 d->mFieldPairs << fieldPair;
180 return d->mRelationId;
185 d->mRelationId = QStringLiteral(
"%1_%2_%3_%4" )
187 d->mFieldPairs.at( 0 ).referencingField(),
189 d->mFieldPairs.at( 0 ).referencedField() );
195 return d->mReferencingLayerId;
200 return d->mReferencingLayer;
205 return d->mFieldPairs;
212 if ( d->mReferencedLayerIds.contains( layerId ) )
234 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
242 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayerField.isNull() && d->mReferencingLayer.data()->isValid() && !d->mReferencedLayerExpression.isNull();
247 return d->mReferencedLayerField == other.d->mReferencedLayerField
248 && d->mReferencedLayerExpression == other.d->mReferencedLayerExpression
249 && d->mReferencingLayerId == other.d->mReferencingLayerId
250 && d->mFieldPairs == other.d->mFieldPairs;
255 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.project()->mapLayers();
258 d->mReferencingLayer = mapLayers.contains( d->mReferencingLayerId )
259 ? qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] )
261 d->mReferencedLayersMap.clear();
263 if ( d->mRelationId.isEmpty() )
265 QgsDebugError( QStringLiteral(
"Invalid relation: no ID" ) );
270 if ( !d->mReferencingLayer )
272 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referencing layer does not exist. ID: %1" ).arg( d->mReferencingLayerId ), 4 );
277 if ( d->mReferencingLayer->fields().lookupField( d->mReferencedLayerField ) == -1 )
279 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer field \"%1\" does not exist in layer with ID: %2" ).arg( d->mReferencedLayerField, d->mReferencingLayerId ), 4 );
284 if ( d->mReferencedLayerExpression.trimmed().isNull() )
286 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer expression \"%1\" is missing" ).arg( d->mReferencedLayerExpression ), 4 );
294 d->mReferencedLayersMap.insert( referencedLayerId, qobject_cast<QgsVectorLayer *>( mapLayers[referencedLayerId] ) );
296 if ( !d->mReferencedLayersMap[referencedLayerId] || !d->mReferencedLayersMap[referencedLayerId]->isValid() )
298 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer does not exist. ID: %1" ).arg( d->mReferencedLayersMap[referencedLayerId]->id() ), 4 );
304 if ( d->mFieldPairs.count() == 0 )
306 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: no pair of field is specified." ), 4 );
313 if ( d->mReferencingLayer->fields().lookupField( pair.first ) == -1 )
315 QgsDebugError( QStringLiteral(
"Invalid relation: field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() ) );
322 if ( d->mReferencedLayersMap[referencedLayerId]->fields().lookupField( pair.second ) == -1 )
324 QgsDebugError( QStringLiteral(
"Invalid relation: field %1 does not exist in referenced layer %2" ).arg( pair.second, d->mReferencedLayersMap[referencedLayerId]->name() ) );
334 if ( d->mRelationName ==
name && !
name.isEmpty() )
338 d->mRelationName =
name;
344 if ( d->mRelationName.isEmpty() )
345 return QObject::tr(
"Polymorphic relations for \"%1\"" ).arg( d->mReferencingLayer ? d->mReferencingLayer->name() : QStringLiteral(
"<NO LAYER>" ) );
347 return d->mRelationName;
359 return d->mReferencedLayerField;
371 return d->mReferencedLayerExpression;
383 return d->mReferencedLayerIds;
388 return d->mRelationStrength;
394 d->mRelationStrength = relationStrength;
400 QList<QgsRelation> relations;
410 const QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->
name();
412 relation.
setId( QStringLiteral(
"%1_%2" ).arg( d->mRelationId, referencedLayerId ) );
415 relation.
setName( QStringLiteral(
"Generated for \"%1\"" ).arg( referencedLayerName ) );
419 const QList<QgsRelation::FieldPair> constFieldPairs =
fieldPairs();
426 relations << relation;
432QString QgsPolymorphicRelation::upgradeGeneratedRelationId(
const QString &oldRelationId )
const
440 const QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->name();
441 if ( oldRelationId == QStringLiteral(
"%1_%2" ).arg( d->mRelationId, referencedLayerName ) )
443 return QStringLiteral(
"%1_%2" ).arg( d->mRelationId, referencedLayerId );
452 if ( !layer || !layer->
isValid() )
458 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...
Handles parsing and evaluation of expressions (formerly called "search strings").
QVariant evaluate()
Evaluate the feature and return the result.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static void warning(const QString &msg)
Goes to qWarning.
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.
A container for the context for various read/write operations on objects.
const QgsProject * project() const
Gets the associated project.
Defines a relation between matching fields of the two involved tables of a relation.
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.
void setPolymorphicRelationId(const QString &polymorphicRelationId)
Sets the parent polymorphic relation id.
void setStrength(Qgis::RelationshipStrength strength)
Set a strength for this relation.
Q_INVOKABLE 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 dataset.
QgsExpressionContext createExpressionContext() const final
This method needs to be reimplemented in all classes which implement this interface and return an exp...
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
QList< int > QgsAttributeList
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)