Skip to content
Permalink
Browse files

using of json fields for value relation widget multi selection

if the type of field is QVariant::Map - means a json source (eg. GPKG or Postgres (storing not yet implemented)) - it stores as json list
  • Loading branch information
signedav committed Dec 20, 2018
1 parent 54a5fae commit 8fd47c9fc3051b1c4657a129f5101c21d930c68b
@@ -39,9 +39,6 @@ QString QgsValueRelationFieldFormatter::id() const

QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
{
Q_UNUSED( layer )
Q_UNUSED( fieldIndex )

ValueRelationCache vrCache;

if ( cache.isValid() )
@@ -55,7 +52,18 @@ QString QgsValueRelationFieldFormatter::representValue( QgsVectorLayer *layer, i

if ( config.value( QStringLiteral( "AllowMulti" ) ).toBool() )
{
QStringList keyList = valueToStringList( value );
QStringList keyList;

if ( layer->fields().at( fieldIndex ).type() == QVariant::Map )
{
//because of json it's stored as QVariantList
keyList = value.toStringList();
}
else
{
keyList = valueToStringList( value );
}

QStringList valueList;

for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( vrCache ) )
@@ -69,7 +69,22 @@ QVariant QgsValueRelationWidgetWrapper::value() const
}
}
}
v = selection.join( QStringLiteral( "," ) ).prepend( '{' ).append( '}' );

if ( layer()->fields().at( fieldIdx() ).type() == QVariant::Map )
{
QVariantList vl;
//store as QVariantList because it's json
foreach ( QString s, selection )
{
vl << s;
}
v = vl;
}
else
{
//store as hstore string
v = selection.join( QStringLiteral( "," ) ).prepend( '{' ).append( '}' );
}
}

if ( mLineEdit )
@@ -149,7 +164,17 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
{
if ( mTableWidget )
{
QStringList checkList( QgsValueRelationFieldFormatter::valueToStringList( value ) );
QStringList checkList;

if ( layer()->fields().at( fieldIdx() ).type() == QVariant::Map )
{
//because of json it's stored as QVariantList
checkList = value.toStringList();
}
else
{
checkList = QgsValueRelationFieldFormatter::valueToStringList( value );
}

QTableWidgetItem *lastChangedItem = nullptr;

0 comments on commit 8fd47c9

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