28using namespace Qt::StringLiterals;
32 setFlags(
flags() | QgsFieldFormatter::CanProvideAvailableValues );
37 return u
"RelationReference"_s;
42 if ( cache.isValid() )
44 return cache.value<QMap<QVariant, QString>>().value( value );
47 const QString fieldName = fieldIndex < layer->
fields().
size() ? layer->
fields().
at( fieldIndex ).
name() : QObject::tr(
"<unknown>" );
50 if ( !config.contains( u
"Relation"_s ) )
53 return value.toString();
56 const QString relationName = config[u
"Relation"_s].toString();
61 return value.toString();
64 if ( layer != referencingLayer )
66 QgsMessageLog::logMessage( QObject::tr(
"Layer %1, field %2: representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ).arg( layer->
name(), fieldName ) );
67 return value.toString();
70 if ( referencingFieldIdx != fieldIndex )
72 QgsMessageLog::logMessage( QObject::tr(
"Layer %1, field %2: representValue() with inconsistent fieldIndex parameter w.r.t relation referencingFieldIdx" ).arg( layer->
name(), fieldName ) );
73 return value.toString();
76 if ( !referencedLayer )
79 return value.toString();
85 attrs[ referencingFieldIdx ] = value;
91 return value.toString();
96 QString title = expr.
evaluate( &context ).toString();
100 title = feature.
attribute( referencedFieldIdx ).toString();
112 Q_UNUSED( fieldIndex )
113 QMap<QVariant, QString> cache;
115 const QString fieldName = fieldIndex < layer->
fields().
size() ? layer->
fields().
at( fieldIndex ).
name() : QObject::tr(
"<unknown>" );
118 if ( !config.contains( u
"Relation"_s ) )
123 const QString relationName = config[u
"Relation"_s].toString();
131 if ( layer != referencingLayer )
133 QgsMessageLog::logMessage( QObject::tr(
"Layer %1, field %2: representValue() with inconsistent layer parameter w.r.t relation referencingLayer" ).arg( layer->
name(), fieldName ) );
137 if ( !referencedLayer )
144 if ( referencedFieldIdx == -1 )
155 requiredAttributes << referencedFieldIdx;
158 auto iterator = referencedLayer->
getFeatures( request );
164 while ( iterator.nextFeature( feature ) )
167 QString title = expr.
evaluate( &context ).toString();
171 title = feature.
attribute( referencedFieldIdx ).toString();
174 cache.insert( feature.
attribute( referencedFieldIdx ), title );
177 return QVariant::fromValue<QMap<QVariant, QString>>( cache );
186 if ( config.value( u
"ReferencedLayerId"_s ).toString().isEmpty() )
191 const QList<QgsVectorLayerRef> result {{
193 config.value( u
"ReferencedLayerId"_s ).toString(),
194 config.value( u
"ReferencedLayerName"_s ).toString(),
195 config.value( u
"ReferencedLayerDataSource"_s ).toString(),
196 config.value( u
"ReferencedLayerProviderKey"_s ).toString() )
204 if (
auto *lProject = context.
project() )
206 const QgsVectorLayer *referencedLayer = lProject->relationManager()->relation( config[u
"Relation"_s].toString() ).referencedLayer();
207 if ( referencedLayer )
209 const int fieldIndex = lProject->relationManager()->relation( config[u
"Relation"_s].toString() ).referencedFields().first();
210 values = qgis::setToList( referencedLayer->
uniqueValues( fieldIndex, countLimit ) );
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
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.
Handles 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)
Fetch next feature and stores in f, returns true on success.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
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.
Q_INVOKABLE 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 size() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Q_INVOKABLE 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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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.
Represents a relationship between two vector layers.
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 dataset.
QString displayExpression
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
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