Skip to content
Permalink
Browse files

available values in value relations

  • Loading branch information
signedav committed Dec 16, 2019
1 parent 54f4efb commit 5e6e75662f2c5959bfe2fc6bbe1760cfbc1fc8bb
@@ -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;

};


@@ -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" );
@@ -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;
@@ -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;
@@ -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 )

0 comments on commit 5e6e756

Please sign in to comment.
You can’t perform that action at this time.