Skip to content

Commit

Permalink
Move layer resolver into a static method
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jun 10, 2019
1 parent dcc779a commit f9b50b5
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 47 deletions.
Expand Up @@ -121,6 +121,15 @@ Check whether the ``feature`` has all values required by the ``expression``
.. versionadded:: 3.2
%End

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

.. versionadded:: 3.8
%End



};


Expand Down
Expand Up @@ -78,7 +78,7 @@ Access the widget managed by this wrapper
%End


virtual void setConfig( const QVariantMap &config );
void setConfig( const QVariantMap &config );
%Docstring
Will set the config of this wrapper to the specified config.

Expand Down
25 changes: 24 additions & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -118,7 +118,7 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
{
ValueRelationCache cache;

QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() );
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( resolveLayer( config ) );

if ( !layer )
return cache;
Expand Down Expand Up @@ -273,3 +273,26 @@ bool QgsValueRelationFieldFormatter::expressionIsUsable( const QString &expressi
return false;
return true;
}

QString QgsValueRelationFieldFormatter::resolveLayer( const QVariantMap &config )
{
if ( QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() ) )
{
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 QgsVectorLayer *vl { qobject_cast<const QgsVectorLayer *>( l ) };
if ( vl && vl->name() == name )
{
return vl->id();
}
}
}
return QString();
}

8 changes: 8 additions & 0 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.h
Expand Up @@ -119,6 +119,14 @@ 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
* \since QGIS 3.8
*/
static QString resolveLayer( const QVariantMap &config );



};

Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/editorwidgets/core/qgswidgetwrapper.h
Expand Up @@ -117,7 +117,7 @@ class GUI_EXPORT QgsWidgetWrapper : public QObject
*
* \param config The config for this wrapper
*/
virtual void setConfig( const QVariantMap &config );
void setConfig( const QVariantMap &config );

/**
* Set the context in which this widget is shown
Expand Down
20 changes: 2 additions & 18 deletions src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsvectorlayer.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgsexpressioncontextutils.h"
#include "qgsvaluerelationfieldformatter.h"

QgsValueRelationConfigDlg::QgsValueRelationConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
: QgsEditorConfigWidget( vl, fieldIdx, parent )
Expand Down Expand Up @@ -70,24 +71,7 @@ QVariantMap QgsValueRelationConfigDlg::config()

void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
{
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() );
// If layer is null, let's try to match it against name and provider
if ( ! lyr )
{
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
if ( ! name.isEmpty() )
{
for ( const auto &l : QgsProject::instance()->mapLayers( true ) )
{
QgsVectorLayer *vl { qobject_cast<QgsVectorLayer *>( l ) };
if ( vl && vl->name() == name )
{
lyr = vl;
break;
}
}
}
}
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QgsValueRelationFieldFormatter::resolveLayer( config ) );
mLayerName->setLayer( lyr );
mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );
Expand Down
24 changes: 1 addition & 23 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -302,7 +302,7 @@ int QgsValueRelationWidgetWrapper::columnCount() const

QVariant::Type QgsValueRelationWidgetWrapper::fkType() const
{
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config().value( QStringLiteral( "Layer" ) ).toString() );
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QgsValueRelationFieldFormatter::resolveLayer( config() ) );
if ( layer )
{
QgsFields fields = layer->fields();
Expand Down Expand Up @@ -435,25 +435,3 @@ void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
else
QgsEditorWidgetWrapper::setEnabled( enabled );
}

void QgsValueRelationWidgetWrapper::setConfig( const QVariantMap &config )
{
QVariantMap cfg { config };
if ( ! QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() ) )
{
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
if ( ! name.isEmpty() )
{
for ( const auto &l : QgsProject::instance()->mapLayers( true ) )
{
QgsVectorLayer *vl { qobject_cast<QgsVectorLayer *>( l ) };
if ( vl && vl->name() == name )
{
cfg[ QStringLiteral( "Layer" ) ] = vl->id();
break;
}
}
}
}
QgsWidgetWrapper::setConfig( cfg );
}
3 changes: 0 additions & 3 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h
Expand Up @@ -73,9 +73,6 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper

void setEnabled( bool enabled ) override;

void setConfig( const QVariantMap &config ) override;


protected:
QWidget *createWidget( QWidget *parent ) override;
void initWidget( QWidget *editor ) override;
Expand Down

0 comments on commit f9b50b5

Please sign in to comment.