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 relation.d->mRelationStrength = qgsEnumKeyToValue<QgsRelation::RelationStrength>(
strength, RelationStrength::Association );
99 QDomNodeList references = elem.elementsByTagName( QStringLiteral(
"fieldRef" ) );
100 for (
int i = 0; i < references.size(); ++i )
102 QDomElement refEl = references.at( i ).toElement();
104 QString referencingField = refEl.attribute( QStringLiteral(
"referencingField" ) );
105 QString referencedField = refEl.attribute( QStringLiteral(
"referencedField" ) );
107 relation.
addFieldPair( referencingField, referencedField );
117 QDomElement elem = doc.createElement( QStringLiteral(
"relation" ) );
118 elem.setAttribute( QStringLiteral(
"id" ), d->mRelationId );
119 elem.setAttribute( QStringLiteral(
"name" ), d->mRelationName );
120 elem.setAttribute( QStringLiteral(
"referencingLayer" ), d->mReferencingLayerId );
121 elem.setAttribute( QStringLiteral(
"referencedLayer" ), d->mReferencedLayerId );
122 elem.setAttribute( QStringLiteral(
"strength" ), qgsEnumValueToKey<RelationStrength>( d->mRelationStrength ) );
124 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
126 QDomElement referenceElem = doc.createElement( QStringLiteral(
"fieldRef" ) );
127 referenceElem.setAttribute( QStringLiteral(
"referencingField" ), pair.first );
128 referenceElem.setAttribute( QStringLiteral(
"referencedField" ), pair.second );
129 elem.appendChild( referenceElem );
132 node.appendChild( elem );
146 d->mRelationName =
name;
159 d->mReferencingLayerId =
id;
167 d->mReferencedLayerId =
id;
175 d->mFieldPairs <<
FieldPair( referencingField, referencedField );
182 d->mFieldPairs << fieldPair;
194 QgsDebugMsgLevel( QStringLiteral(
"Filter conditions: '%1'" ).arg( filter ), 2 );
203 QStringList conditions;
205 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
207 QVariant val( feature.
attribute( pair.referencedField() ) );
211 return conditions.join( QLatin1String(
" AND " ) );
216 QStringList conditions;
218 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
226 QgsDebugMsgLevel( QStringLiteral(
"Filter conditions: '%1'" ).arg( conditions.join(
" AND " ) ), 2 );
243 d->mReferencedLayer->getFeatures( request ).nextFeature( f );
249 return d->mRelationName;
254 return d->mRelationStrength;
259 return d->mRelationId;
264 d->mRelationId = QStringLiteral(
"%1_%2_%3_%4" )
266 d->mFieldPairs.at( 0 ).referencingField(),
268 d->mFieldPairs.at( 0 ).referencedField() );
274 return d->mReferencingLayerId;
279 return d->mReferencingLayer;
284 return d->mReferencedLayerId;
289 return d->mReferencedLayer;
294 return d->mFieldPairs;
301 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
303 attrs << d->mReferencedLayer->fields().lookupField( pair.second );
312 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
314 attrs << d->mReferencingLayer->fields().lookupField( pair.first );
322 return d->mValid && !d->mReferencingLayer.isNull() && !d->mReferencedLayer.isNull() && d->mReferencingLayer.data()->isValid() && d->mReferencedLayer.data()->isValid();
327 return d->mReferencedLayerId == other.d->mReferencedLayerId && d->mReferencingLayerId == other.d->mReferencingLayerId && d->mFieldPairs == other.d->mFieldPairs;
332 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
334 if ( pair.first == referencingField )
342 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
344 if ( pair.second == referencedField )
352 const QMap<QString, QgsMapLayer *> &mapLayers = mContext.
project()->
mapLayers();
354 d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] );
355 d->mReferencedLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencedLayerId] );
359 if ( d->mRelationId.isEmpty() )
361 QgsDebugMsg( QStringLiteral(
"Invalid relation: no ID" ) );
366 if ( !d->mReferencedLayer )
368 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referenced layer does not exist. ID: %1" ).arg( d->mReferencedLayerId ), 4 );
371 else if ( !d->mReferencingLayer )
373 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: referencing layer does not exist. ID: %2" ).arg( d->mReferencingLayerId ), 4 );
378 if ( d->mFieldPairs.count() < 1 )
380 QgsDebugMsgLevel( QStringLiteral(
"Invalid relation: no pair of field is specified." ), 4 );
384 for (
const FieldPair &pair : qgis::as_const( d->mFieldPairs ) )
386 if ( -1 == d->mReferencingLayer->fields().lookupField( pair.first ) )
388 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referencing layer %2" ).arg( pair.first, d->mReferencingLayer->name() ) );
392 else if ( -1 == d->mReferencedLayer->fields().lookupField( pair.second ) )
394 QgsDebugMsg( QStringLiteral(
"Invalid relation: field %1 does not exist in referencedg layer %2" ).arg( pair.second, d->mReferencedLayer->name() ) );