33 return QStringLiteral(
"RelationReference" );
38 if ( cache.isValid() )
40 return cache.value<QMap<QVariant, QString>>().value( value );
43 const QString fieldName = fieldIndex < layer->
fields().
size() ? layer->
fields().
at( fieldIndex ).
name() : QObject::tr(
"<unknown>" );
46 if ( !config.contains( QStringLiteral(
"Relation" ) ) )
49 return value.toString();
52 const QString relationName = config[QStringLiteral(
"Relation" )].toString();
57 return value.toString();
60 if ( layer != referencingLayer )
62 QgsMessageLog::logMessage( QObject::tr(
"Layer %1, field %2: representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ).arg( layer->
name(), fieldName ) );
63 return value.toString();
66 if ( referencingFieldIdx != fieldIndex )
68 QgsMessageLog::logMessage( QObject::tr(
"Layer %1, field %2: representValue() with inconsistent fieldIndex parameter w.r.t relation referencingFieldIdx" ).arg( layer->
name(), fieldName ) );
69 return value.toString();
72 if ( !referencedLayer )
75 return value.toString();
81 attrs[ referencingFieldIdx ] = value;
87 return value.toString();
91 context.setFeature( feature );
92 QString title = expr.evaluate( &context ).toString();
93 if ( expr.hasEvalError() )
96 title = feature.
attribute( referencedFieldIdx ).toString();
108 Q_UNUSED( fieldIndex )
109 QMap<QVariant, QString> cache;
111 const QString fieldName = fieldIndex < layer->
fields().
size() ? layer->
fields().
at( fieldIndex ).
name() : QObject::tr(
"<unknown>" );
114 if ( !config.contains( QStringLiteral(
"Relation" ) ) )
119 const QString relationName = config[QStringLiteral(
"Relation" )].toString();
127 if ( layer != referencingLayer )
129 QgsMessageLog::logMessage( QObject::tr(
"Layer %1, field %2: representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ).arg( layer->
name(), fieldName ) );
133 if ( !referencedLayer )
140 if ( referencedFieldIdx == -1 )
150 QgsAttributeList requiredAttributes = expr.referencedAttributeIndexes( referencedLayer->
fields() ).toList();
151 requiredAttributes << referencedFieldIdx;
154 auto iterator = referencedLayer->
getFeatures( request );
158 expr.prepare( &context );
160 while ( iterator.nextFeature( feature ) )
163 QString title = expr.evaluate( &context ).toString();
165 if ( expr.hasEvalError() )
167 title = feature.
attribute( referencedFieldIdx ).toString();
170 cache.insert( feature.
attribute( referencedFieldIdx ), title );
173 return QVariant::fromValue<QMap<QVariant, QString>>( cache );
180 const QList<QgsVectorLayerRef> result {{
182 config.value( QStringLiteral(
"ReferencedLayerId" ) ).toString(),
183 config.value( QStringLiteral(
"ReferencedLayerName" ) ).toString(),
184 config.value( QStringLiteral(
"ReferencedLayerDataSource" ) ).toString(),
185 config.value( QStringLiteral(
"ReferencedLayerProviderKey" ) ).toString() )
196 if ( referencedLayer )
199 values = referencedLayer->
uniqueValues( fieldIndex, countLimit ).toList();
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").
int size() const
Returns number of items.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
A context for field formatter containing information like the project.
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.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QgsAttributeList referencedFields() const
Returns a list of attributes used to form the referenced fields (most likely primary key) on the refe...
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...
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const FINAL
Calculates a list of unique values contained within an attribute in the layer.
static QgsProject * instance()
Returns the QgsProject singleton instance.
_LayerRef< QgsVectorLayer > QgsVectorLayerRef
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.
QgsProject * project() const
Returns the project used in field formatter.
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.