Skip to content
Permalink
Browse files

Merge pull request #6615 from elpaso/bugfix-16967-multi-in-value-rela…

…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 685adbf7e19c04392bb91557d669de3d92c68b8b
@@ -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
};

@@ -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 ) )
@@ -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;
}
@@ -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 )
@@ -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 )
{
@@ -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):

0 comments on commit 685adbf

Please sign in to comment.
You can’t perform that action at this time.