Skip to content

Commit

Permalink
Replace string by enum for format
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Feb 26, 2019
1 parent 81f0c5c commit 11e80b1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 37 deletions.
29 changes: 7 additions & 22 deletions src/server/services/wms/qgswmsgetprint.cpp
Expand Up @@ -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
Expand All @@ -65,11 +54,7 @@ namespace QgsWms
}

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

} // namespace QgsWms




37 changes: 29 additions & 8 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions src/server/services/wms/qgswmsparameters.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 10 additions & 6 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() )
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wms/qgswmsrenderer.h
Expand Up @@ -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.
Expand Down

0 comments on commit 11e80b1

Please sign in to comment.