Skip to content

Commit

Permalink
using of json fields for value relation widget multi selection
Browse files Browse the repository at this point in the history
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 Jan 7, 2019
1 parent 54a5fae commit 8fd47c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
foreach ( QString s, selection )
{
vl << s;
}
v = vl;
}
else
{
//store as hstore string
v = selection.join( QStringLiteral( "," ) ).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 8fd47c9

Please sign in to comment.