QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
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 ) );
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const FINAL
Queries the layer for features specified in request.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsRelationManager relationManager
int count() const
Returns number of items.
QSet< int > referencedAttributeIndexes(const QgsFields &fields) const
Returns a list of field name indexes obtained from the provided fields.
A context for field formatter containing information like the project.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsRelation relation(const QString &id) const
Gets access to a relation by its id.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
QList< int > QgsAttributeList
QgsVectorLayer referencingLayer
QgsFields fields() const FINAL
Returns the list of fields of this layer.
This class wraps a request for features to a vector layer (or directly its vector data provider).
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
bool isValid() const
Returns the validity of this feature.
QgsVectorLayer referencedLayer
QSet< QVariant > uniqueValues(int fieldIndex, int limit=-1) const FINAL
Calculates a list of unique values contained within an attribute in the layer.
int size() const
Returns number of items.
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).
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
QgsFeatureRequest getReferencedFeatureRequest(const QgsAttributes &attributes) const
Creates a request to return the feature on the referenced (parent) layer which is referenced by the p...
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
QVariant evaluate()
Evaluate the feature and return the result.
QList< QgsRelation::FieldPair > fieldPairs() const
Returns the field pairs which form this relation The first element of each pair are the field names o...
bool nextFeature(QgsFeature &f)
QgsProject * project() const
Returns the project used in field formatter.
Represents a vector layer which manages a vector based data sets.
_LayerRef< QgsVectorLayer > QgsVectorLayerRef
A vector of attributes. Mostly equal to QVector<QVariant>.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
Class for parsing and evaluation of expressions (formerly called "search strings")....
QString displayExpression
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.