Skip to content
Permalink
Browse files

get availableValues over the fieldFormatter

  • Loading branch information
signedav committed Dec 16, 2019
1 parent f2dbb2e commit 6baaae5340115d5eadb62b5c0fb9da85e271c769
@@ -40,6 +40,9 @@ Default constructor of field formatter for a relation reference field.



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



};

@@ -87,6 +87,15 @@ make use of a cache if present.
%End


virtual QList<QVariant> availableValues( const QVariantMap &config, int countLimit ) const;
%Docstring
Returns a list of the values that would be possible to select with this widget type
On a RelationReference that would be the parents ids or on ValueMap all the configured keys
according to the settings in the ``config``

.. versionadded:: 3.12
%End


};

@@ -174,3 +174,16 @@ QList<QgsVectorLayerRef> QgsRelationReferenceFieldFormatter::layerDependencies(
}};
return result;
}

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

QgsVectorLayer *referencedLayer = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ).referencedLayer();
if ( referencedLayer )
{
int layerFieldIndex = QgsProject::instance()->relationManager()->relation( config[QStringLiteral( "Relation" )].toString() ).referencedFields().first();
values = referencedLayer->uniqueValues( layerFieldIndex, countLimit ).toList();
}
return values;
}
@@ -46,6 +46,8 @@ class CORE_EXPORT QgsRelationReferenceFieldFormatter : public QgsFieldFormatter

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

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

//friend class TestQgsRelationReferenceFieldFormatter;

};
@@ -68,3 +68,11 @@ QList<QgsVectorLayerRef> QgsFieldFormatter::layerDependencies( const QVariantMap
Q_UNUSED( config )
return QList<QgsVectorLayerRef>();
}

QList<QVariant> QgsFieldFormatter::availableValues( const QVariantMap &config, int countLimit ) const
{
Q_UNUSED( config )
Q_UNUSED( countLimit )

return QList<QVariant>();
}
@@ -112,6 +112,14 @@ class CORE_EXPORT QgsFieldFormatter
*/
virtual QList< QgsVectorLayerRef > layerDependencies( const QVariantMap &config ) const SIP_SKIP;

/**
* Returns a list of the values that would be possible to select with this widget type
* On a RelationReference that would be the parents ids or on ValueMap all the configured keys
* according to the settings in the \a config
* \since QGIS 3.12
*/
virtual QList<QVariant> availableValues( const QVariantMap &config, int countLimit ) const;


};

@@ -248,8 +248,8 @@ void QgsExpressionBuilderWidget::currentChanged( const QModelIndex &index, const
if ( fieldIndex != -1 )
{
const QgsEditorWidgetSetup setup = fields.at( fieldIndex ).editorWidgetSetup();
cbxRelatedLayerValues->setVisible( setup.config().contains( QStringLiteral( "Relation" ) ) );
cbxRelatedLayerValues->setChecked( true );
cbxValuesInUse->setVisible( setup.config().contains( QStringLiteral( "Relation" ) ) );
cbxValuesInUse->setChecked( false );
}
}
mValueGroupBox->setVisible( isField );
@@ -478,21 +478,15 @@ void QgsExpressionBuilderWidget::fillFieldValues( const QString &fieldName, int
const QgsEditorWidgetSetup setup = fields.at( fieldIndex ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

const QgsVectorLayer *layer = mLayer;
int layerFieldIndex = fieldIndex;

// request for values of the referenced layer on a relation reference widget field
if ( cbxRelatedLayerValues->isChecked() && setup.config().contains( QStringLiteral( "Relation" ) ) )
QList<QVariant> values;
if ( cbxValuesInUse->isVisible() && !cbxValuesInUse->isChecked() )
{
QgsVectorLayer *referencedLayer = mProject->relationManager()->relation( setup.config()[QStringLiteral( "Relation" )].toString() ).referencedLayer();
if ( referencedLayer )
{
layer = referencedLayer;
layerFieldIndex = mProject->relationManager()->relation( setup.config()[QStringLiteral( "Relation" )].toString() ).referencedFields().first();
}
values = formatter->availableValues( setup.config(), countLimit );
}
else
{
values = mLayer->uniqueValues( fieldIndex, countLimit ).toList();
}

QList<QVariant> values = layer->uniqueValues( layerFieldIndex, countLimit ).toList();
std::sort( values.begin(), values.end() );

mValuesModel->clear();
@@ -579,9 +579,12 @@
</widget>
</item>
<item row="2" column="0" colspan="2">
<widget class="QCheckBox" name="cbxRelatedLayerValues">
<widget class="QCheckBox" name="cbxValuesInUse">
<property name="toolTip">
<string>Only show values used in this layer and not all the possible values you could choose.</string>
</property>
<property name="text">
<string>Show values of referenced layer</string>
<string>Only show values in use</string>
</property>
</widget>
</item>

0 comments on commit 6baaae5

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