Skip to content

Commit

Permalink
get availableValues over the fieldFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
signedav committed Dec 16, 2019
1 parent f2dbb2e commit 6baaae5
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 17 deletions.
Expand Up @@ -40,6 +40,9 @@ Default constructor of field formatter for a relation reference field.



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



};

Expand Down
9 changes: 9 additions & 0 deletions python/core/auto_generated/qgsfieldformatter.sip.in
Expand Up @@ -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


};

Expand Down
13 changes: 13 additions & 0 deletions src/core/fieldformatter/qgsrelationreferencefieldformatter.cpp
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/core/fieldformatter/qgsrelationreferencefieldformatter.h
Expand Up @@ -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;

};
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsfieldformatter.cpp
Expand Up @@ -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>();
}
8 changes: 8 additions & 0 deletions src/core/qgsfieldformatter.h
Expand Up @@ -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;


};

Expand Down
24 changes: 9 additions & 15 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions src/ui/qgsexpressionbuilder.ui
Expand Up @@ -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>
Expand Down

0 comments on commit 6baaae5

Please sign in to comment.