Skip to content

Commit

Permalink
ResolveLayer returns a layer instance instead of an ID
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jun 10, 2019
1 parent f9b50b5 commit a295701
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ Check whether the ``feature`` has all values required by the ``expression``
.. versionadded:: 3.2
%End

static QString resolveLayer( const QVariantMap &config );
static QgsVectorLayer *resolveLayer( const QVariantMap &config );
%Docstring
Returns the (possibly empty) layer ID from the widget ``config``
Returns the (possibly NULL) layer from the widget's ``config``

.. versionadded:: 3.8
%End
Expand Down
27 changes: 13 additions & 14 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
{
ValueRelationCache cache;

QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( resolveLayer( config ) );
const QgsVectorLayer *layer = resolveLayer( config );

if ( !layer )
return cache;
Expand Down Expand Up @@ -274,25 +274,24 @@ bool QgsValueRelationFieldFormatter::expressionIsUsable( const QString &expressi
return true;
}

QString QgsValueRelationFieldFormatter::resolveLayer( const QVariantMap &config )
QgsVectorLayer *QgsValueRelationFieldFormatter::resolveLayer( const QVariantMap &config )
{
if ( QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() ) )
auto layer { QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() ) };
if ( ! layer )
{
return config.value( QStringLiteral( "Layer" ) ).toString();
}
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
if ( ! name.isEmpty() )
{
const auto constLayers { QgsProject::instance()->mapLayers( true ) };
for ( const QgsMapLayer *l : constLayers )
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
if ( ! name.isEmpty() )
{
const QgsVectorLayer *vl { qobject_cast<const QgsVectorLayer *>( l ) };
if ( vl && vl->name() == name )
for ( QgsMapLayer *l : QgsProject::instance()->mapLayers( true ) )
{
return vl->id();
QgsVectorLayer *vl { qobject_cast<QgsVectorLayer *>( l ) };
if ( vl && vl->name() == name )
{
layer = vl;
}
}
}
}
return QString();
return layer;
}

4 changes: 2 additions & 2 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
static bool expressionIsUsable( const QString &expression, const QgsFeature &feature );

/**
* Returns the (possibly empty) layer ID from the widget \a config
* Returns the (possibly NULL) layer from the widget's \a config
* \since QGIS 3.8
*/
static QString resolveLayer( const QVariantMap &config );
static QgsVectorLayer *resolveLayer( const QVariantMap &config );



Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ QVariantMap QgsValueRelationConfigDlg::config()

void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
{
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QgsValueRelationFieldFormatter::resolveLayer( config ) );
QgsVectorLayer *lyr = QgsValueRelationFieldFormatter::resolveLayer( config );
mLayerName->setLayer( lyr );
mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ int QgsValueRelationWidgetWrapper::columnCount() const

QVariant::Type QgsValueRelationWidgetWrapper::fkType() const
{
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QgsValueRelationFieldFormatter::resolveLayer( config() ) );
const QgsVectorLayer *layer = QgsValueRelationFieldFormatter::resolveLayer( config() );
if ( layer )
{
QgsFields fields = layer->fields();
Expand Down

0 comments on commit a295701

Please sign in to comment.