QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
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 );
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 = 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.
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.
QgsAttributeList referencedFields() const
Returns a list of attributes used to form the referenced fields (most likely primary key) on the refe...
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.
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.
QVariant attribute(const QString &name) const
Lookup attribute value from 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.
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).
_LayerRef< QgsVectorLayer > QgsVectorLayerRef
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
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.