Skip to content

Commit

Permalink
Fixes in the QgsPolymorphicRelation
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Jan 12, 2021
1 parent 7feb073 commit 89c07ff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
10 changes: 5 additions & 5 deletions python/core/auto_generated/qgspolymorphicrelation.sip.in
Expand Up @@ -207,11 +207,6 @@ Sets the ``expression`` to identify the parent layer
QString referencedLayerExpression() const;
%Docstring
Returns the expression to identify the parent layer
%End

QgsExpressionContext getLayerContext() const;
%Docstring
Provide layer metadata as variables in an expression context
%End

void setReferencedLayerIds( const QStringList &childRelationIds );
Expand All @@ -227,6 +222,11 @@ Returns a list of layer ids to be used as potential referenced layers
QList<QgsRelation> getGeneratedRelations() const;
%Docstring
Returns a list of generated relations, based on the currently set :py:func:`~QgsPolymorphicRelation.referencedLayerIds`
%End

QString layerRepresentation( const QgsVectorLayer *layer ) const;
%Docstring
Returns layer representation as evaluated string
%End

};
Expand Down
32 changes: 20 additions & 12 deletions src/core/qgspolymorphicrelation.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgspolymorphicrelation_p.h"
#include "qgsexpressioncontextutils.h"

QgsPolymorphicRelation::QgsPolymorphicRelation()
: d( new QgsPolymorphicRelationPrivate() )
Expand Down Expand Up @@ -225,20 +226,21 @@ bool QgsPolymorphicRelation::hasEqualDefinition( const QgsPolymorphicRelation &o

void QgsPolymorphicRelation::updateRelationStatus()
{
const QMap<QString, QgsMapLayer *> &mapLayers = mContext.project()->mapLayers();

d->mValid = true;
d->mReferencingLayer = mapLayers.contains( d->mReferencingLayerId )
? qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] )
: nullptr;
d->mReferencedLayersMap.clear();

if ( d->mRelationId.isEmpty() )
{
QgsDebugMsg( QStringLiteral( "Invalid relation: no ID" ) );
d->mValid = false;
return;
}

const QMap<QString, QgsMapLayer *> &mapLayers = mContext.project()->mapLayers();

d->mValid = true;
d->mReferencingLayer = qobject_cast<QgsVectorLayer *>( mapLayers[d->mReferencingLayerId] );
// TODO probably this map is not needed??
d->mReferencedLayersMap.clear();

if ( !d->mReferencingLayer )
{
QgsDebugMsgLevel( QStringLiteral( "Invalid relation: referencing layer does not exist. ID: %1" ).arg( d->mReferencingLayerId ), 4 );
Expand Down Expand Up @@ -355,11 +357,6 @@ QStringList QgsPolymorphicRelation::referencedLayerIds() const
return d->mReferencedLayerIds;
}

QgsExpressionContext QgsPolymorphicRelation::getLayerContext() const
{
return QgsExpressionContext();
}

QList<QgsRelation> QgsPolymorphicRelation::getGeneratedRelations() const
{
QList<QgsRelation> relations;
Expand Down Expand Up @@ -391,3 +388,14 @@ QList<QgsRelation> QgsPolymorphicRelation::getGeneratedRelations() const

return relations;
}

QString QgsPolymorphicRelation::layerRepresentation( const QgsVectorLayer *layer ) const
{
if ( !layer || !layer->isValid() )
return QString();

QgsExpressionContext context = layer->createExpressionContext();
QgsExpression expr( d->mReferencedLayerExpression );

return expr.evaluate( &context ).toString();
}
10 changes: 5 additions & 5 deletions src/core/qgspolymorphicrelation.h
Expand Up @@ -254,11 +254,6 @@ class CORE_EXPORT QgsPolymorphicRelation
*/
QString referencedLayerExpression() const;

/**
* Provide layer metadata as variables in an expression context
*/
QgsExpressionContext getLayerContext() const;

/**
* Sets a list of layer ids to be used as potential referenced layers
*/
Expand All @@ -274,6 +269,11 @@ class CORE_EXPORT QgsPolymorphicRelation
*/
QList<QgsRelation> getGeneratedRelations() const;

/**
* Returns layer representation as evaluated string
*/
QString layerRepresentation( const QgsVectorLayer *layer ) const;

private:

mutable QExplicitlySharedDataPointer<QgsPolymorphicRelationPrivate> d;
Expand Down

0 comments on commit 89c07ff

Please sign in to comment.