Skip to content
Permalink
Browse files

beautyfications and clean up

  • Loading branch information
signedav committed Dec 20, 2018
1 parent 03222c8 commit 9ff514b9be2c238df132fd893b9ee0c8eb71f48c
@@ -128,6 +128,6 @@ QgsField QgsAddAttrDialog::field() const
mLength->value(),
mPrec->value(),
mCommentEdit->text(),
( QVariant::Type ) mTypeBox->currentData( Qt::UserRole ).toInt() == QVariant::Map ? QVariant::String : QVariant::Invalid
static_cast<QVariant::Type>( mTypeBox->currentData( Qt::UserRole ).toInt() ) == QVariant::Map ? QVariant::String : QVariant::Invalid
);
}
@@ -254,7 +254,7 @@ QString QgsField::displayString( const QVariant &v ) const
if ( ok )
return QLocale().toString( converted );
}
else if ( d->typeName == QLatin1String( "json" ) || d->typeName == QLatin1String( "jsonb" ) || d->typeName == QLatin1String( "JSON" ) )
else if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String( "jsonb" ) )
{
QJsonDocument doc = QJsonDocument::fromVariant( v );
return QString::fromUtf8( doc.toJson().data() );
@@ -44,5 +44,5 @@ QgsEditorConfigWidget *QgsListWidgetFactory::configWidget( QgsVectorLayer *vl, i
unsigned int QgsListWidgetFactory::fieldScore( const QgsVectorLayer *vl, int fieldIdx ) const
{
const QgsField field = vl->fields().field( fieldIdx );
return ( field.type() == QVariant::List || field.type() == QVariant::StringList || field.typeName() == QLatin1String( "json" ) || field.typeName() == QLatin1String( "jsonb" ) || field.typeName() == QLatin1String( "JSON" ) ) && field.subType() != QVariant::Invalid ? 20 : 0;
return ( field.type() == QVariant::List || field.type() == QVariant::StringList || field.typeName().compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || field.typeName() == QLatin1String( "jsonb" ) ) && field.subType() != QVariant::Invalid ? 20 : 0;
}
@@ -476,8 +476,10 @@ QgsOgrProvider::QgsOgrProvider( QString const &uri, const ProviderOptions &optio
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer)" ), QStringLiteral( "integer" ), QVariant::Int, 0, nMaxIntLen )
<< QgsVectorDataProvider::NativeType( tr( "Whole number (integer 64 bit)" ), QStringLiteral( "integer64" ), QVariant::LongLong, 0, nMaxInt64Len )
<< QgsVectorDataProvider::NativeType( tr( "Decimal number (real)" ), QStringLiteral( "double" ), QVariant::Double, 0, nMaxDoubleLen, 0, nMaxDoublePrec )
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 )
<< QgsVectorDataProvider::NativeType( tr( "Map (JSON)" ), QStringLiteral( "JSON" ), QVariant::Map, 0, 0, 0, 0, QVariant::String );
<< QgsVectorDataProvider::NativeType( tr( "Text (string)" ), QStringLiteral( "string" ), QVariant::String, 0, 65535 );

if ( mGDALDriverName == QLatin1String( "GPKG" ) )
nativeTypes << QgsVectorDataProvider::NativeType( tr( "Map (JSON)" ), QStringLiteral( "JSON" ), QVariant::Map, 0, 0, 0, 0, QVariant::String );

bool supportsDate = true;
bool supportsTime = mGDALDriverName != QLatin1String( "ESRI Shapefile" ) && mGDALDriverName != QLatin1String( "GPKG" );
@@ -1053,7 +1055,6 @@ void QgsOgrProvider::loadFields()
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
ogrSubType = OFSTJSON;
QgsDebugMsg( QStringLiteral( "JSON Field found" ) );
varType = QVariant::Map;
varSubType = QVariant::String;
}
@@ -1407,6 +1408,16 @@ OGRGeometryH QgsOgrProvider::ConvertGeometryIfNecessary( OGRGeometryH hGeom )
return OGR_G_ForceTo( hGeom, layerGeomType, nullptr );
}

QString QgsOgrProvider::jsonStringValue( const QVariant &value )
{
QString stringValue = QString::fromUtf8( QJsonDocument::fromVariant( value.toMap() ).toJson().data() );
if ( stringValue == QStringLiteral( "{\n}\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( value.toList() ).toJson().data() );
if ( stringValue == QStringLiteral( "[\n]\n" ) && !value.toString().isEmpty() )
stringValue = value.toString();
return stringValue;
}

bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )
{
bool returnValue = true;
@@ -1536,13 +1547,7 @@ bool QgsOgrProvider::addFeaturePrivate( QgsFeature &f, Flags flags )

#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
if ( OGR_Fld_GetSubType( fldDef ) == OFSTJSON )
{
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( attrVal.toMap() ).toJson().data() );
if ( stringValue == QStringLiteral( "{\n}\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( attrVal.toList() ).toJson().data() );
if ( stringValue == QStringLiteral( "[\n]\n" ) && !attrVal.toString().isEmpty() )
stringValue = attrVal.toString();
}
stringValue = jsonStringValue( attrVal );
else
{
stringValue = attrVal.toString();
@@ -2131,17 +2136,9 @@ bool QgsOgrProvider::changeAttributeValues( const QgsChangedAttributesMap &attr_
QString stringValue;
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(2,4,0)
if ( OGR_Fld_GetSubType( fd ) == OFSTJSON )
{
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toMap() ).toJson().data() );
if ( stringValue == QStringLiteral( "{\n}\n" ) )
stringValue = QString::fromUtf8( QJsonDocument::fromVariant( it2->toList() ).toJson().data() );
if ( stringValue == QStringLiteral( "[\n]\n" ) && !it2->toString().isEmpty() )
stringValue = it2->toString();
}
stringValue = jsonStringValue( it2.value() );
else
{
stringValue = it2->toString();
}
#else
stringValue = it2->toString();
#endif
@@ -284,6 +284,9 @@ class QgsOgrProvider : public QgsVectorDataProvider

mutable QStringList mSubLayerList;

//! convert from json QVariant to QString
QString jsonStringValue( const QVariant &value );

bool addFeaturePrivate( QgsFeature &f, QgsFeatureSink::Flags flags );
//! Deletes one feature
bool deleteFeature( QgsFeatureId id );

0 comments on commit 9ff514b

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