28 return QStringLiteral(
"RelationReference" );
33 if ( cache.isValid() )
35 return cache.value<QMap<QVariant, QString>>().value( value );
39 if ( !config.contains( QStringLiteral(
"Relation" ) ) )
42 return value.toString();
48 return value.toString();
51 if ( layer != referencingLayer )
53 QgsMessageLog::logMessage( QObject::tr(
"representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ) );
54 return value.toString();
57 if ( referencingFieldIdx != fieldIndex )
59 QgsMessageLog::logMessage( QObject::tr(
"representValue() with inconsistent fieldIndex parameter w.r.t relation referencingFieldIdx" ) );
60 return value.toString();
63 if ( !referencedLayer )
66 return value.toString();
72 attrs[ referencingFieldIdx ] = value;
78 return value.toString();
82 context.setFeature( feature );
83 QString title = expr.evaluate( &context ).toString();
84 if ( expr.hasEvalError() )
87 title = feature.
attribute( referencedFieldIdx ).toString();
99 Q_UNUSED( fieldIndex )
100 QMap<QVariant, QString> cache;
103 if ( !config.contains( QStringLiteral(
"Relation" ) ) )
115 if ( layer != referencingLayer )
117 QgsMessageLog::logMessage( QObject::tr(
"representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ) );
121 if ( !referencedLayer )
127 if ( referencedFieldIdx == -1 )
137 QgsAttributeList requiredAttributes = expr.referencedAttributeIndexes( referencedLayer->
fields() ).toList();
138 requiredAttributes << referencedFieldIdx;
141 auto iterator = referencedLayer->
getFeatures( request );
145 expr.prepare( &context );
147 while ( iterator.nextFeature( feature ) )
150 QString title = expr.evaluate( &context ).toString();
152 if ( expr.hasEvalError() )
155 title = feature.
attribute( referencedFieldIdx ).toString();
158 cache.insert( feature.
attribute( referencedFieldIdx ), title );
161 return QVariant::fromValue<QMap<QVariant, QString>>( cache );
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
bool isValid() const
Returns the validity of this feature.
Class for parsing and evaluation of expressions (formerly called "search strings").
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsVectorLayer referencingLayer
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
int count() const
Returns number of items.
QgsFeatureRequest getReferencedFeatureRequest(const QgsAttributes &attributes) const
Creates a request to return the feature on the referenced (parent) layer which is referenced by the p...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsVectorLayer referencedLayer
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QgsRelationManager relationManager
QString displayExpression
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QList< int > QgsAttributeList
bool nextFeature(QgsFeature &f)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.