Skip to content

Commit

Permalink
Added a QgsRelation::polymorphicRelation() method
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Jan 12, 2021
1 parent 81600fd commit 41e1e61
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/qgsrelation.sip.in
Expand Up @@ -326,6 +326,13 @@ Sets the parent polymorphic relation id.
%Docstring
Returns the parent polymorphic relation id. If the relation is a normal relation, a null string is returned.

.. versionadded:: 3.18
%End

QgsPolymorphicRelation polymorphicRelation() const;
%Docstring
Returns the parent polymorphic relation. If the relation is a normal relation, an invalid polymorphic relation is returned.

.. versionadded:: 3.18
%End

Expand Down
14 changes: 10 additions & 4 deletions src/core/qgsrelation.cpp
Expand Up @@ -206,11 +206,10 @@ QString QgsRelation::getRelatedFeaturesFilter( const QgsFeature &feature ) const

if ( ! d->mPolymorphicRelationId.isEmpty() )
{
QgsPolymorphicRelation polymorphicRelation = mContext.project()->relationManager()->polymorphicRelation( d->mPolymorphicRelationId );

if ( polymorphicRelation.isValid() )
QgsPolymorphicRelation polyRel = polymorphicRelation();
if ( polyRel.isValid() )
{
conditions << QgsExpression::createFieldEqualityExpression( polymorphicRelation.referencedLayerField(), polymorphicRelation.layerRepresentation( referencedLayer() ) );
conditions << QgsExpression::createFieldEqualityExpression( polyRel.referencedLayerField(), polyRel.layerRepresentation( referencedLayer() ) );
}
else
{
Expand Down Expand Up @@ -429,3 +428,10 @@ QString QgsRelation::polymorphicRelationId() const
return d->mPolymorphicRelationId;
}

QgsPolymorphicRelation QgsRelation::polymorphicRelation() const
{
if ( ! mContext.project() || ! mContext.project()->relationManager() )
return QgsPolymorphicRelation();

return mContext.project()->relationManager()->polymorphicRelation( d->mPolymorphicRelationId );
}
9 changes: 8 additions & 1 deletion src/core/qgsrelation.h
Expand Up @@ -33,7 +33,7 @@ class QgsFeatureRequest;
class QgsAttributes;
class QgsVectorLayer;
class QgsRelationPrivate;
class QgsExpressionContext;
class QgsPolymorphicRelation;

/**
* \ingroup core
Expand All @@ -49,6 +49,7 @@ class CORE_EXPORT QgsRelation
Q_PROPERTY( QString name READ name WRITE setName )
Q_PROPERTY( bool isValid READ isValid )
Q_PROPERTY( QString polymorphicRelationId READ polymorphicRelationId WRITE setPolymorphicRelationId )
Q_PROPERTY( QgsPolymorphicRelation polymorphicRelation READ polymorphicRelation )

public:

Expand Down Expand Up @@ -395,6 +396,12 @@ class CORE_EXPORT QgsRelation
*/
QString polymorphicRelationId() const;

/**
* Returns the parent polymorphic relation. If the relation is a normal relation, an invalid polymorphic relation is returned.
* \since QGIS 3.18
*/
QgsPolymorphicRelation polymorphicRelation() const;

private:

mutable QExplicitlySharedDataPointer<QgsRelationPrivate> d;
Expand Down

0 comments on commit 41e1e61

Please sign in to comment.