Skip to content
Permalink
Browse files

Replace string by enum for format

  • Loading branch information
pblottiere committed Feb 26, 2019
1 parent 81f0c5c commit 11e80b1b6fa157cdbe8c5b3a5cab1de5fe01c3b4
@@ -25,37 +25,26 @@
namespace QgsWms
{
void writeGetPrint( QgsServerInterface *serverIface, const QgsProject *project,
const QString &version, const QgsServerRequest &request,
const QString &, const QgsServerRequest &request,
QgsServerResponse &response )
{
QgsServerRequest::Parameters params = request.parameters();

Q_UNUSED( version );

QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
const QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
QgsRenderer renderer( serverIface, project, wmsParameters );

QString format = params.value( "FORMAT" );
const QgsWmsParameters::Format format = wmsParameters.format();
QString contentType;

// GetPrint supports svg/png/pdf
if ( format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
if ( format == QgsWmsParameters::PNG )
{
format = "png";
contentType = "image/png";
}
else if ( format.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::SVG )
{
format = "svg";
contentType = "image/svg+xml";
}
else if ( format.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
format.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::PDF )
{
format = "pdf";
contentType = "application/pdf";
}
else
@@ -65,11 +54,7 @@ namespace QgsWms
}

response.setHeader( QStringLiteral( "Content-Type" ), contentType );
response.write( renderer.getPrint( format ) );
response.write( renderer.getPrint() );
}

} // namespace QgsWms




@@ -716,18 +716,39 @@ namespace QgsWms
return mWmsParameters[ QgsWmsParameter::FORMAT ].toString();
}

QgsWmsParameters::Format QgsWmsParameters::format() const
QString QgsWmsParameters::formatAsString( const QgsWmsParameters::Format format )
{
QString fStr = formatAsString();
const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWmsParameters::Format>() );
return metaEnum.valueToKey( format );
}

if ( fStr.isEmpty() )
return Format::NONE;
QgsWmsParameters::Format QgsWmsParameters::format() const
{
const QString fStr = formatAsString();

Format f = Format::PNG;
if ( fStr.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
Format f = Format::NONE;
if ( fStr.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::PNG;
}
else if ( fStr.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0
|| fStr.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::JPG;
}
else if ( fStr.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::SVG;
}
else if ( fStr.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
fStr.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
{
f = Format::PDF;
}

return f;
}
@@ -306,12 +306,15 @@ namespace QgsWms
NONE,
JPG,
PNG,
SVG,
PDF,
TEXT,
XML,
HTML,
GML,
JSON
};
Q_ENUM( Format )

/**
* Constructor for WMS parameters with specific values.
@@ -504,6 +507,13 @@ namespace QgsWms
*/
QString formatAsString() const;

/**
* Returns format parameter as a string.
* \returns format parameter as a string.
* \since QGIS 3.6
*/
static QString formatAsString( Format format );

/**
* Returns format. If the FORMAT parameter is not used, then the
* default value is PNG.
@@ -275,7 +275,7 @@ namespace QgsWms
}


QByteArray QgsRenderer::getPrint( const QString &formatString )
QByteArray QgsRenderer::getPrint()
{
//GetPrint request needs a template parameter
QString templateName = mWmsParameters.composerTemplate();
@@ -464,15 +464,18 @@ namespace QgsWms
configurePrintLayout( layout.get(), mapSettings, atlas );

// Get the temporary output file
QTemporaryFile tempOutputFile( QDir::tempPath() + '/' + QStringLiteral( "XXXXXX.%1" ).arg( formatString.toLower() ) );
const QgsWmsParameters::Format format = mWmsParameters.format();
const QString extension = mWmsParameters.formatAsString( format ).toLower();

QTemporaryFile tempOutputFile( QDir::tempPath() + '/' + QStringLiteral( "XXXXXX.%1" ).arg( extension ) );
if ( !tempOutputFile.open() )
{
throw QgsServerException( QStringLiteral( "Could not open temporary file for the GetPrint request." ) );

}

QString exportError;
if ( formatString.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
if ( format == QgsWmsParameters::SVG )
{
// Settings for the layout exporter
QgsLayoutExporter::SvgExportSettings exportSettings;
@@ -501,10 +504,11 @@ namespace QgsWms
exporter.exportToSvg( tempOutputFile.fileName(), exportSettings );
}
}
else if ( formatString.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 || formatString.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::PNG )
{
// Settings for the layout exporter
QgsLayoutExporter::ImageExportSettings exportSettings;

// Get the dpi from input or use the default
double dpi( layout->renderContext().dpi( ) );
if ( !mWmsParameters.dpi().isEmpty() )
@@ -540,7 +544,7 @@ namespace QgsWms
exporter.exportToImage( tempOutputFile.fileName(), exportSettings );
}
}
else if ( formatString.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
else if ( format == QgsWmsParameters::PDF )
{
// Settings for the layout exporter
QgsLayoutExporter::PdfExportSettings exportSettings;
@@ -571,7 +575,7 @@ namespace QgsWms
else //unknown format
{
throw QgsBadRequestException( QStringLiteral( "InvalidFormat" ),
QStringLiteral( "Output format '%1' is not supported in the GetPrint request" ).arg( formatString ) );
QStringLiteral( "Output format '%1' is not supported in the GetPrint request" ).arg( mWmsParameters.formatAsString() ) );
}

if ( atlas )
@@ -100,7 +100,7 @@ namespace QgsWms
* Returns printed page as binary
\param formatString out: format of the print output (e.g. pdf, svg, png, ...)
\returns printed page as binary or 0 in case of error*/
QByteArray getPrint( const QString &formatString );
QByteArray getPrint();

/**
* Creates an xml document that describes the result of the getFeatureInfo request.

0 comments on commit 11e80b1

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