-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
Fixes #21986 plus: - fix multiple string keys with commas in value relation widget - more robust JSON and array (un)marshalling - uniform array representation in value relation widgets - lot of test coverage - automatic QVariant type conversions in JSON utils
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#include "qgsvaluerelationfieldformatter.h" | ||
#include "qgsattributeform.h" | ||
#include "qgsattributes.h" | ||
#include "qgsjsonutils.h" | ||
|
||
#include <QHeaderView> | ||
#include <QComboBox> | ||
|
@@ -33,6 +34,10 @@ | |
#include <QStringListModel> | ||
#include <QCompleter> | ||
|
||
#include <nlohmann/json.hpp> | ||
using json = nlohmann::json; | ||
|
||
|
||
QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent ) | ||
: QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent ) | ||
{ | ||
|
@@ -70,21 +75,26 @@ QVariant QgsValueRelationWidgetWrapper::value() const | |
} | ||
} | ||
|
||
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 ) ) | ||
{ | ||
QVariantList vl; | ||
//store as QVariantList because it's json | ||
for ( const QString &s : qgis::as_const( selection ) ) | ||
// Convert to proper type | ||
const QVariant::Type type { fkType() }; | ||
switch ( type ) | ||
{ | ||
vl << s; | ||
case QVariant::Type::Int: | ||
vl.push_back( s.toInt() ); | ||
break; | ||
case QVariant::Type::LongLong: | ||
vl.push_back( s.toLongLong() ); | ||
break; | ||
default: | ||
vl.push_back( s ); | ||
break; | ||
} | ||
v = vl; | ||
} | ||
else | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
elpaso
Author
Contributor
|
||
{ | ||
//store as hstore string | ||
v = selection.join( ',' ).prepend( '{' ).append( '}' ); | ||
} | ||
v = vl; | ||
} | ||
|
||
if ( mLineEdit ) | ||
|
@@ -289,6 +299,21 @@ int QgsValueRelationWidgetWrapper::columnCount() const | |
return std::max( 1, config( QStringLiteral( "NofColumns" ) ).toInt() ); | ||
} | ||
|
||
QVariant::Type QgsValueRelationWidgetWrapper::fkType() const | ||
{ | ||
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( config().value( QStringLiteral( "Layer" ) ).toString() ); | ||
if ( layer ) | ||
{ | ||
QgsFields fields = layer->fields(); | ||
int idx { fields.lookupField( config().value( QStringLiteral( "Key" ) ).toString() ) }; | ||
if ( idx >= 0 ) | ||
{ | ||
return fields.at( idx ).type(); | ||
} | ||
} | ||
return QVariant::Type::Invalid; | ||
} | ||
|
||
void QgsValueRelationWidgetWrapper::populate( ) | ||
{ | ||
// Initialize, note that signals are blocked, to avoid double signals on new features | ||
|
Hi @elpaso
Sorry for commenting such an old PR. But what is the reason to remove this else-clause? Do you remember?
I guess it needs to convert somewhere the list into a string in hstore-format to be able to use it (if JSON not used). But honestly I'm confused...