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();
92 QString title = expr.
evaluate( &context ).toString();
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 )
151 requiredAttributes << referencedFieldIdx;
154 auto iterator = referencedLayer->
getFeatures( request );
160 while ( iterator.nextFeature( feature ) )
163 QString title = expr.
evaluate( &context ).toString();
167 title = feature.
attribute( referencedFieldIdx ).toString();
170 cache.insert( feature.
attribute( referencedFieldIdx ), title );
173 return QVariant::fromValue<QMap<QVariant, QString>>( cache );
182 if ( config.value( QStringLiteral(
"ReferencedLayerId" ) ).toString().isEmpty() )
187 const QList<QgsVectorLayerRef> result {{
189 config.value( QStringLiteral(
"ReferencedLayerId" ) ).toString(),
190 config.value( QStringLiteral(
"ReferencedLayerName" ) ).toString(),
191 config.value( QStringLiteral(
"ReferencedLayerDataSource" ) ).toString(),
192 config.value( QStringLiteral(
"ReferencedLayerProviderKey" ) ).toString() )
200 if (
auto *lProject = context.
project() )
202 const QgsVectorLayer *referencedLayer = lProject->relationManager()->relation( config[QStringLiteral(
"Relation" )].toString() ).referencedLayer();
203 if ( referencedLayer )
205 const int fieldIndex = lProject->relationManager()->relation( config[QStringLiteral(
"Relation" )].toString() ).referencedFields().first();
206 values = qgis::setToList( referencedLayer->
uniqueValues( fieldIndex, countLimit ) );
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
QVariant evaluate()
Evaluate the feature and return the result.
QSet< int > referencedAttributeIndexes(const QgsFields &fields) const
Returns a list of field name indexes obtained from the provided fields.
bool nextFeature(QgsFeature &f)
This class wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
bool isValid() const
Returns the validity of this feature.
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
A context for field formatter containing information like the project.
QgsProject * project() const
Returns the project used in field formatter.
int count() const
Returns number of items.
int size() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
QgsRelationManager * relationManager
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
QgsFeatureRequest getReferencedFeatureRequest(const QgsAttributes &attributes) const
Creates a request to return the feature on the referenced (parent) layer which is referenced by the p...
QgsVectorLayer * referencedLayer
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
QgsVectorLayer * referencingLayer
Represents a vector layer which manages a vector based data sets.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QString displayExpression
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const FINAL
Calculates a list of unique values contained within an attribute in the layer.
QList< int > QgsAttributeList
_LayerRef< QgsVectorLayer > QgsVectorLayerRef