Skip to content

Commit f9b50b5

Browse files
committedJun 10, 2019
Move layer resolver into a static method
1 parent dcc779a commit f9b50b5

File tree

8 files changed

+46
-47
lines changed

8 files changed

+46
-47
lines changed
 

‎python/core/auto_generated/fieldformatter/qgsvaluerelationfieldformatter.sip.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ Check whether the ``feature`` has all values required by the ``expression``
121121
.. versionadded:: 3.2
122122
%End
123123

124+
static QString resolveLayer( const QVariantMap &config );
125+
%Docstring
126+
Returns the (possibly empty) layer ID from the widget ``config``
127+
128+
.. versionadded:: 3.8
129+
%End
130+
131+
132+
124133
};
125134

126135

‎python/gui/auto_generated/editorwidgets/core/qgswidgetwrapper.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Access the widget managed by this wrapper
7878
%End
7979

8080

81-
virtual void setConfig( const QVariantMap &config );
81+
void setConfig( const QVariantMap &config );
8282
%Docstring
8383
Will set the config of this wrapper to the specified config.
8484

‎src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte
118118
{
119119
ValueRelationCache cache;
120120

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

123123
if ( !layer )
124124
return cache;
@@ -273,3 +273,26 @@ bool QgsValueRelationFieldFormatter::expressionIsUsable( const QString &expressi
273273
return false;
274274
return true;
275275
}
276+
277+
QString QgsValueRelationFieldFormatter::resolveLayer( const QVariantMap &config )
278+
{
279+
if ( QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() ) )
280+
{
281+
return config.value( QStringLiteral( "Layer" ) ).toString();
282+
}
283+
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
284+
if ( ! name.isEmpty() )
285+
{
286+
const auto constLayers { QgsProject::instance()->mapLayers( true ) };
287+
for ( const QgsMapLayer *l : constLayers )
288+
{
289+
const QgsVectorLayer *vl { qobject_cast<const QgsVectorLayer *>( l ) };
290+
if ( vl && vl->name() == name )
291+
{
292+
return vl->id();
293+
}
294+
}
295+
}
296+
return QString();
297+
}
298+

‎src/core/fieldformatter/qgsvaluerelationfieldformatter.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
119119
*/
120120
static bool expressionIsUsable( const QString &expression, const QgsFeature &feature );
121121

122+
/**
123+
* Returns the (possibly empty) layer ID from the widget \a config
124+
* \since QGIS 3.8
125+
*/
126+
static QString resolveLayer( const QVariantMap &config );
127+
128+
129+
122130
};
123131

124132
Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )

‎src/gui/editorwidgets/core/qgswidgetwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class GUI_EXPORT QgsWidgetWrapper : public QObject
117117
*
118118
* \param config The config for this wrapper
119119
*/
120-
virtual void setConfig( const QVariantMap &config );
120+
void setConfig( const QVariantMap &config );
121121

122122
/**
123123
* Set the context in which this widget is shown

‎src/gui/editorwidgets/qgsvaluerelationconfigdlg.cpp

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgsvectorlayer.h"
1919
#include "qgsexpressionbuilderdialog.h"
2020
#include "qgsexpressioncontextutils.h"
21+
#include "qgsvaluerelationfieldformatter.h"
2122

2223
QgsValueRelationConfigDlg::QgsValueRelationConfigDlg( QgsVectorLayer *vl, int fieldIdx, QWidget *parent )
2324
: QgsEditorConfigWidget( vl, fieldIdx, parent )
@@ -70,24 +71,7 @@ QVariantMap QgsValueRelationConfigDlg::config()
7071

7172
void QgsValueRelationConfigDlg::setConfig( const QVariantMap &config )
7273
{
73-
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() );
74-
// If layer is null, let's try to match it against name and provider
75-
if ( ! lyr )
76-
{
77-
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
78-
if ( ! name.isEmpty() )
79-
{
80-
for ( const auto &l : QgsProject::instance()->mapLayers( true ) )
81-
{
82-
QgsVectorLayer *vl { qobject_cast<QgsVectorLayer *>( l ) };
83-
if ( vl && vl->name() == name )
84-
{
85-
lyr = vl;
86-
break;
87-
}
88-
}
89-
}
90-
}
74+
QgsVectorLayer *lyr = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QgsValueRelationFieldFormatter::resolveLayer( config ) );
9175
mLayerName->setLayer( lyr );
9276
mKeyColumn->setField( config.value( QStringLiteral( "Key" ) ).toString() );
9377
mValueColumn->setField( config.value( QStringLiteral( "Value" ) ).toString() );

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ int QgsValueRelationWidgetWrapper::columnCount() const
302302

303303
QVariant::Type QgsValueRelationWidgetWrapper::fkType() const
304304
{
305-
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config().value( QStringLiteral( "Layer" ) ).toString() );
305+
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( QgsValueRelationFieldFormatter::resolveLayer( config() ) );
306306
if ( layer )
307307
{
308308
QgsFields fields = layer->fields();
@@ -435,25 +435,3 @@ void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
435435
else
436436
QgsEditorWidgetWrapper::setEnabled( enabled );
437437
}
438-
439-
void QgsValueRelationWidgetWrapper::setConfig( const QVariantMap &config )
440-
{
441-
QVariantMap cfg { config };
442-
if ( ! QgsProject::instance()->mapLayer<QgsVectorLayer *>( config.value( QStringLiteral( "Layer" ) ).toString() ) )
443-
{
444-
const auto name { config.value( QStringLiteral( "LayerName" ) ).toString() };
445-
if ( ! name.isEmpty() )
446-
{
447-
for ( const auto &l : QgsProject::instance()->mapLayers( true ) )
448-
{
449-
QgsVectorLayer *vl { qobject_cast<QgsVectorLayer *>( l ) };
450-
if ( vl && vl->name() == name )
451-
{
452-
cfg[ QStringLiteral( "Layer" ) ] = vl->id();
453-
break;
454-
}
455-
}
456-
}
457-
}
458-
QgsWidgetWrapper::setConfig( cfg );
459-
}

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
7373

7474
void setEnabled( bool enabled ) override;
7575

76-
void setConfig( const QVariantMap &config ) override;
77-
78-
7976
protected:
8077
QWidget *createWidget( QWidget *parent ) override;
8178
void initWidget( QWidget *editor ) override;

0 commit comments

Comments
 (0)
Please sign in to comment.