Fixes in the QgsPolymorphicRelation

This commit is contained in:
Ivan Ivanov 2021-01-12 10:07:19 +02:00
parent 7feb0730f8
commit 89c07ff262
3 changed files with 30 additions and 22 deletions

View File

@ -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 );
@ -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
};

View File

@ -21,6 +21,7 @@
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgspolymorphicrelation_p.h"
#include "qgsexpressioncontextutils.h"
QgsPolymorphicRelation::QgsPolymorphicRelation()
: d( new QgsPolymorphicRelationPrivate() )
@ -225,6 +226,14 @@ 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" ) );
@ -232,13 +241,6 @@ void QgsPolymorphicRelation::updateRelationStatus()
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 );
@ -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;
@ -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();
}

View File

@ -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
*/
@ -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;