Skip to content

Commit

Permalink
Merge pull request #8721 from signedav/json-for-valuerelations
Browse files Browse the repository at this point in the history
Storing Value Releation multi-selection as JSON
  • Loading branch information
m-kuhn committed Jan 8, 2019
2 parents 3732cb7 + 6e91349 commit 15ddb27
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Expand Up @@ -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() )
Expand All @@ -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 ) )
Expand Down
29 changes: 27 additions & 2 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -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
for ( const QString &s : qgis::as_const( selection ) )
{
vl << s;
}
v = vl;
}
else
{
//store as hstore string
v = selection.join( ',' ).prepend( '{' ).append( '}' );
}
}

if ( mLineEdit )
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 15ddb27

Please sign in to comment.