Skip to content
Permalink
Browse files

Address review

  • Loading branch information
nirvn authored and nyalldawson committed Sep 13, 2020
1 parent b898ec2 commit 31b2b89c4eb991dd8e23d1f190414c849d5bc326
@@ -482,7 +482,7 @@ The ``context`` must have valid sprite definitions and images set via :py:func:`
prior to conversion.
%End

static QString retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &size, QString &spriteProperty, QString &spriteSizeProperty );
static QString retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize, QString &spriteProperty, QString &spriteSizeProperty );
%Docstring
Retrieves the sprite image with the specified ``name``, taken from the specified ``context`` as a base64 encoded value

@@ -1996,30 +1996,39 @@ QImage QgsMapBoxGlStyleConverter::retrieveSprite( const QString &name, QgsMapBox
return sprite;
}

QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &size, QString &spriteProperty, QString &spriteSizeProperty )
QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize, QString &spriteProperty, QString &spriteSizeProperty )
{
QString returnPath;
QString spritePath;

auto prepareSprite = [context]( const QImage & sprite, QString & path, QSize & size )
{
if ( !sprite.isNull() )
{
size = sprite.size() * context.pixelSizeConversionFactor();
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();
path = QString( encoded );
path.prepend( QLatin1String( "base64:" ) );
}
else
{
path.clear();
size = QSize();
}
};

switch ( value.type() )
{
case QVariant::String:
{
spriteProperty.clear();
spriteSizeProperty.clear();
const QImage sprite = retrieveSprite( value.toString(), context );
if ( !sprite.isNull() )
{
size = sprite.size() * context.pixelSizeConversionFactor();
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

QString path( encoded );
path.prepend( QLatin1String( "base64:" ) );
returnPath = path;
}
prepareSprite( sprite, spritePath, spriteSize );
break;
}

@@ -2030,52 +2039,24 @@ QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value
break;

QString path;
QSize size;
QImage sprite;

sprite = retrieveSprite( stops.value( 0 ).toList().value( 1 ).toString(), context );
if ( !sprite.isNull() )
{
size = sprite.size() * context.pixelSizeConversionFactor();
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

path = QString( encoded );
path.prepend( QLatin1String( "base64:" ) );
returnPath = path;
}
prepareSprite( sprite, spritePath, spriteSize );

spriteProperty = QStringLiteral( "CASE WHEN @zoom_level < %1 THEN '%2'" )
.arg( stops.value( 0 ).toList().value( 0 ).toString() )
.arg( path );
.arg( spritePath );
spriteSizeProperty = QStringLiteral( "CASE WHEN @zoom_level < %1 THEN %2" )
.arg( stops.value( 0 ).toList().value( 0 ).toString() )
.arg( size.width() );
.arg( spriteSize.width() );

QSize s;
for ( int i = 0; i < stops.size() - 1; ++i )
{
;
sprite = retrieveSprite( stops.value( 0 ).toList().value( 1 ).toString(), context );
if ( !sprite.isNull() )
{
s = sprite.size() * context.pixelSizeConversionFactor();
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

path = QString( encoded );
path.prepend( QLatin1String( "base64:" ) );
}
else
{
path.clear();
}
prepareSprite( sprite, path, size );

spriteProperty += QStringLiteral( " WHEN @zoom_level >= %1 AND @zoom_level < %2 "
"THEN '%3'" )
@@ -2086,22 +2067,10 @@ QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value
"THEN %3" )
.arg( stops.value( i ).toList().value( 0 ).toString(),
stops.value( i + 1 ).toList().value( 0 ).toString() )
.arg( s.width() );
.arg( size.width() );
}
sprite = retrieveSprite( stops.last().toList().value( 1 ).toString(), context );
if ( !sprite.isNull() )
{
s = sprite.size() * context.pixelSizeConversionFactor();
QByteArray blob;
QBuffer buffer( &blob );
buffer.open( QIODevice::WriteOnly );
sprite.save( &buffer, "PNG" );
buffer.close();
QByteArray encoded = blob.toBase64();

path = QString( encoded );
path.prepend( QLatin1String( "base64:" ) );
}
prepareSprite( sprite, path, size );

spriteProperty += QStringLiteral( " WHEN @zoom_level >= %1 "
"THEN '%2' END" )
@@ -2110,7 +2079,7 @@ QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value
spriteSizeProperty += QStringLiteral( " WHEN @zoom_level >= %1 "
"THEN %2 END" )
.arg( stops.last().toList().value( 0 ).toString() )
.arg( s.width() );
.arg( size.width() );
break;
}

@@ -2119,7 +2088,7 @@ QString QgsMapBoxGlStyleConverter::retrieveSpriteAsBase64( const QVariant &value
break;
}

return returnPath;
return spritePath;
}

QString QgsMapBoxGlStyleConverter::parseValue( const QVariant &value, QgsMapBoxGlStyleConversionContext &context )
@@ -473,7 +473,7 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
* The \a context must have valid sprite definitions and images set via QgsMapBoxGlStyleConversionContext::setSprites()
* prior to conversion.
*/
static QString retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &size, QString &spriteProperty, QString &spriteSizeProperty );
static QString retrieveSpriteAsBase64( const QVariant &value, QgsMapBoxGlStyleConversionContext &context, QSize &spriteSize, QString &spriteProperty, QString &spriteSizeProperty );

private:

0 comments on commit 31b2b89

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