Skip to content

Commit 41e1e61

Browse files
committedJan 12, 2021
Added a QgsRelation::polymorphicRelation() method
1 parent 81600fd commit 41e1e61

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed
 

‎python/core/auto_generated/qgsrelation.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,13 @@ Sets the parent polymorphic relation id.
326326
%Docstring
327327
Returns the parent polymorphic relation id. If the relation is a normal relation, a null string is returned.
328328

329+
.. versionadded:: 3.18
330+
%End
331+
332+
QgsPolymorphicRelation polymorphicRelation() const;
333+
%Docstring
334+
Returns the parent polymorphic relation. If the relation is a normal relation, an invalid polymorphic relation is returned.
335+
329336
.. versionadded:: 3.18
330337
%End
331338

‎src/core/qgsrelation.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,10 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature &feature ) const
206206

207207
if ( ! d->mPolymorphicRelationId.isEmpty() )
208208
{
209-
QgsPolymorphicRelation polymorphicRelation = mContext.project()->relationManager()->polymorphicRelation( d->mPolymorphicRelationId );
210-
211-
if ( polymorphicRelation.isValid() )
209+
QgsPolymorphicRelation polyRel = polymorphicRelation();
210+
if ( polyRel.isValid() )
212211
{
213-
conditions << QgsExpression::createFieldEqualityExpression( polymorphicRelation.referencedLayerField(), polymorphicRelation.layerRepresentation( referencedLayer() ) );
212+
conditions << QgsExpression::createFieldEqualityExpression( polyRel.referencedLayerField(), polyRel.layerRepresentation( referencedLayer() ) );
214213
}
215214
else
216215
{
@@ -429,3 +428,10 @@ QString QgsRelation::polymorphicRelationId() const
429428
return d->mPolymorphicRelationId;
430429
}
431430

431+
QgsPolymorphicRelation QgsRelation::polymorphicRelation() const
432+
{
433+
if ( ! mContext.project() || ! mContext.project()->relationManager() )
434+
return QgsPolymorphicRelation();
435+
436+
return mContext.project()->relationManager()->polymorphicRelation( d->mPolymorphicRelationId );
437+
}

‎src/core/qgsrelation.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class QgsFeatureRequest;
3333
class QgsAttributes;
3434
class QgsVectorLayer;
3535
class QgsRelationPrivate;
36-
class QgsExpressionContext;
36+
class QgsPolymorphicRelation;
3737

3838
/**
3939
* \ingroup core
@@ -49,6 +49,7 @@ class CORE_EXPORT QgsRelation
4949
Q_PROPERTY( QString name READ name WRITE setName )
5050
Q_PROPERTY( bool isValid READ isValid )
5151
Q_PROPERTY( QString polymorphicRelationId READ polymorphicRelationId WRITE setPolymorphicRelationId )
52+
Q_PROPERTY( QgsPolymorphicRelation polymorphicRelation READ polymorphicRelation )
5253

5354
public:
5455

@@ -395,6 +396,12 @@ class CORE_EXPORT QgsRelation
395396
*/
396397
QString polymorphicRelationId() const;
397398

399+
/**
400+
* Returns the parent polymorphic relation. If the relation is a normal relation, an invalid polymorphic relation is returned.
401+
* \since QGIS 3.18
402+
*/
403+
QgsPolymorphicRelation polymorphicRelation() const;
404+
398405
private:
399406

400407
mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;

0 commit comments

Comments
 (0)
Please sign in to comment.