Skip to content

Commit

Permalink
available values in value relations
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Dec 16, 2019
1 parent 54f4efb commit 5e6e756
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Expand Up @@ -128,6 +128,9 @@ Returns the (possibly NULL) layer from the widget's ``config`` and ``project``
.. versionadded:: 3.8
%End


virtual QList<QVariant> availableValues( const QVariantMap &config, int countLimit ) const;

};


Expand Down
18 changes: 18 additions & 0 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -40,6 +40,11 @@ bool orderByValueLessThan( const QgsValueRelationFieldFormatter::ValueRelationIt
return qgsVariantLessThan( p1.value, p2.value );
}

QgsValueRelationFieldFormatter::QgsValueRelationFieldFormatter()
{
setFlags( flags() | QgsFieldFormatter::CanProvideAvailableValues );
}

QString QgsValueRelationFieldFormatter::id() const
{
return QStringLiteral( "ValueRelation" );
Expand Down Expand Up @@ -183,6 +188,19 @@ QList<QgsVectorLayerRef> QgsValueRelationFieldFormatter::layerDependencies( cons
return result;
}

QList<QVariant> QgsValueRelationFieldFormatter::availableValues( const QVariantMap &config, int countLimit ) const
{
QList<QVariant> values;

const QgsVectorLayer *referencedLayer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( config[QStringLiteral( "Layer" )].toString() ) );
if ( referencedLayer )
{
int fieldIndex = referencedLayer->fields().indexOf( config.value( QStringLiteral( "Key" ) ).toString() );
values = referencedLayer->uniqueValues( fieldIndex, countLimit ).toList();
}
return values;
}

QStringList QgsValueRelationFieldFormatter::valueToStringList( const QVariant &value )
{
QStringList checkList;
Expand Down
4 changes: 3 additions & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.h
Expand Up @@ -56,7 +56,7 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
/**
* Constructor for QgsValueRelationFieldFormatter.
*/
QgsValueRelationFieldFormatter() = default;
QgsValueRelationFieldFormatter();

QString id() const override;
QString representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const override;
Expand Down Expand Up @@ -126,6 +126,8 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
static QgsVectorLayer *resolveLayer( const QVariantMap &config, const QgsProject *project );

QList<QgsVectorLayerRef> layerDependencies( const QVariantMap &config ) const override SIP_SKIP;

QList<QVariant> availableValues( const QVariantMap &config, int countLimit ) const override;
};

Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )
Expand Down

0 comments on commit 5e6e756

Please sign in to comment.