Skip to content

Commit

Permalink
Merge pull request #6615 from elpaso/bugfix-16967-multi-in-value-rela…
Browse files Browse the repository at this point in the history
…tion

[bugfix] value relation widget with Allow multiple selection doesn't resolve
  • Loading branch information
luipir committed Mar 15, 2018
2 parents d771991 + 4f05ed1 commit 685adbf
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
11 changes: 11 additions & 0 deletions python/core/fieldformatter/qgsvaluerelationfieldformatter.sip.in
Expand Up @@ -63,6 +63,17 @@ This can be used to keep the value map in the local memory
if doing multiple lookups in a loop.

.. versionadded:: 3.0
%End

static QStringList valueToStringList( const QVariant &value );
%Docstring
Utility to convert an array or a string representation of and array ``value`` to a string list

:param value: The value to be converted

:return: A string list

.. versionadded:: 3.2
%End
};

Expand Down
22 changes: 21 additions & 1 deletion src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -54,7 +54,7 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer *layer, i

if ( config.value( QStringLiteral( "AllowMulti" ) ).toBool() )
{
QStringList keyList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
QStringList keyList = valueToStringList( value );
QStringList valueList;

for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( vrCache ) )
Expand Down Expand Up @@ -142,3 +142,23 @@ QgsValueRelationFieldFormatter::ValueRelationCache QgsValueRelationFieldFormatte

return cache;
}

QStringList QgsValueRelationFieldFormatter::valueToStringList( const QVariant &value )
{
QStringList checkList;
if ( value.type() == QVariant::StringList )
checkList = value.toStringList();
else if ( value.type() == QVariant::String )
checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
else if ( value.type() == QVariant::List )
{
QVariantList valuesList( value.toList( ) );
for ( const QVariant &listItem : qgis::as_const( valuesList ) )
{
QString v( listItem.toString( ) );
if ( ! v.isEmpty() )
checkList.append( v );
}
}
return checkList;
}
9 changes: 9 additions & 0 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.h
Expand Up @@ -70,6 +70,15 @@ class CORE_EXPORT QgsValueRelationFieldFormatter : public QgsFieldFormatter
* \since QGIS 3.0
*/
static QgsValueRelationFieldFormatter::ValueRelationCache createCache( const QVariantMap &config );

/**
* Utility to convert an array or a string representation of and array \a value to a string list
*
* \param value The value to be converted
* \return A string list
* \since QGIS 3.2
*/
static QStringList valueToStringList( const QVariant &value );
};

Q_DECLARE_METATYPE( QgsValueRelationFieldFormatter::ValueRelationCache )
Expand Down
6 changes: 1 addition & 5 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -151,11 +151,7 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
{
if ( mListWidget )
{
QStringList checkList;
if ( value.type() == QVariant::StringList )
checkList = value.toStringList();
else if ( value.type() == QVariant::String )
checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( ',' );
QStringList checkList( QgsValueRelationFieldFormatter::valueToStringList( value ) );

for ( int i = 0; i < mListWidget->count(); ++i )
{
Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_qgsfieldformatters.py
Expand Up @@ -107,6 +107,16 @@ def test_representValue(self):

QgsProject.instance().removeMapLayer(second_layer.id())

def test_valueToStringList(self):

def _test(a, b):
self.assertEqual(QgsValueRelationFieldFormatter.valueToStringList(a), b)

_test([1, 2, 3], ["1", "2", "3"])
_test("{1,2,3}", ["1", "2", "3"])
_test(['1', '2', '3'], ["1", "2", "3"])
_test('not an array', ['not an array'])


class TestQgsRelationReferenceFieldFormatter(unittest.TestCase):

Expand Down

0 comments on commit 685adbf

Please sign in to comment.