26 : d( new QgsRelationPrivate() )
31 : d( new QgsRelationPrivate() )
40 , mContext( other.mContext )
47 mContext = other.mContext;
53 QDomElement elem = node.toElement();
55 if ( elem.tagName() != QLatin1String(
"relation" ) )
57 QgsLogger::warning( QApplication::translate(
"QgsRelation",
"Cannot create relation. Unexpected tag '%1'" ).arg( elem.tagName() ) );
63 QString
referencedLayerId = elem.attribute( QStringLiteral(
"referencedLayer" ) );
64 QString
id = elem.attribute( QStringLiteral(
"id" ) );
66 QString
strength = elem.attribute( QStringLiteral(
"strength" ) );
68 QMap<QString, QgsMapLayer *> mapLayers = relationContext.
project()->
mapLayers();
92 relation.d->mReferencingLayer = qobject_cast<QgsVectorLayer *>(
referencingLayer );
94 relation.d->mReferencedLayer = qobject_cast<QgsVectorLayer *>(
referencedLayer );
95 relation.d->mRelationId =
id;
96 relation.d->mRelationName =
name;
97 if (
strength == QLatin1String(
"Composition" ) )
99 relation.d->mRelationStrength = RelationStrength::Composition;
103 relation.d->mRelationStrength = RelationStrength::Association;
106 QDomNodeList references = elem.elementsByTagName( QStringLiteral(
"fieldRef" ) );
107 for (
int i = 0; i < references.size(); ++i )
109 QDomElement refEl = references.at( i ).toElement();
111 QString referencingField = refEl.attribute( QStringLiteral(
"referencingField" ) );
112 QString referencedField = refEl.attribute( QStringLiteral(
"referencedField" ) );
114 relation.
addFieldPair( referencingField, referencedField );
124 QDomElement elem = doc.createElement( QStringLiteral(
"relation" ) );
125 elem.setAttribute( QStringLiteral(
"id" ), d->mRelationId );
126 elem.setAttribute( QStringLiteral(
"name" ), d->mRelationName );
127 elem.setAttribute( QStringLiteral(
"referencingLayer" ), d->mReferencingLayerId );
128 elem.setAttribute( QStringLiteral(
"referencedLayer" ), d->mReferencedLayerId );
129 if ( d->mRelationStrength == RelationStrength::Composition )
131 elem.setAttribute( QStringLiteral(
"strength" ), QStringLiteral(
"Composition" ) );
135 elem.setAttribute( QStringLiteral(
"strength" ), QStringLiteral(
"Association" ) );
138 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
140 QDomElement referenceElem = doc.createElement( QStringLiteral(
"fieldRef" ) );
141 referenceElem.setAttribute( QStringLiteral(
"referencingField" ), pair.first );
142 referenceElem.setAttribute( QStringLiteral(
"referencedField" ), pair.second );
143 elem.appendChild( referenceElem );
146 node.appendChild( elem );
160 d->mRelationName =
name;
173 d->mReferencingLayerId =
id;
181 d->mReferencedLayerId =
id;
189 d->mFieldPairs <<
FieldPair( referencingField, referencedField );
196 d->mFieldPairs << fieldPair;
208 QgsDebugMsg( QStringLiteral(
"Filter conditions: '%1'" ).arg( filter ) );
217 QStringList conditions;
219 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
221 QVariant val( feature.
attribute( pair.referencedField() ) );
225 return conditions.join( QStringLiteral(
" AND " ) );
230 QStringList conditions;
232 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
240 QgsDebugMsg( QStringLiteral(
"Filter conditions: '%1'" ).arg( conditions.join(
" AND " ) ) );
257 d->mReferencedLayer->getFeatures( request ).nextFeature( f );
263 return d->mRelationName;
268 return d->mRelationStrength;
273 return d->mRelationId;
278 d->mRelationId = QStringLiteral(
"%1_%2_%3_%4" )
280 d->mFieldPairs.at( 0 ).referencingField(),
282 d->mFieldPairs.at( 0 ).referencedField() );
288 return d->mReferencingLayerId;
293 return d->mReferencingLayer;
298 return d->mReferencedLayerId;
303 return d->mReferencedLayer;
308 return d->mFieldPairs;
315 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
317 attrs << d->mReferencedLayer->fields().lookupField( pair.second );
326 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
328 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
336 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayer.isNull() && d->mReferencingLayer.data()->isValid() && d->mReferencedLayer.data()->isValid();
341 return d->mReferencedLayerId == other.d->mReferencedLayerId && d->mReferencingLayerId == other.d->mReferencingLayerId && d->mFieldPairs == other.d->mFieldPairs;
346 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
348 if ( pair.first == referencingField )
356 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
358 if ( pair.second == referencedField )
366 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.
project()->
mapLayers();
368 d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] );
369 d->mReferencedLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencedLayerId] );
373 if ( d->mRelationId.isEmpty() )
375 QgsDebugMsg( QStringLiteral(
"Invalid relation: no ID" ) );
380 if ( !d->mReferencedLayer )
382 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer does not exist. ID: %1" ).arg( d->mReferencedLayerId ), 4 );
385 else if ( !d->mReferencingLayer )
387 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referencing layer does not exist. ID: %2" ).arg( d->mReferencingLayerId ), 4 );
392 if ( d->mFieldPairs.count() < 1 )
394 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: no pair of field is specified." ), 4 );
398 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
400 if ( -1 == d->mReferencingLayer->fields().lookupField( pair.first ) )
402 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() ) );
406 else if ( -1 == d->mReferencedLayer->fields().lookupField( pair.second ) )
408 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referencedg layer %2" ).arg( pair.second, d->mReferencedLayer->name() ) );