Skip to content

Commit 11e80b1

Browse files
committedFeb 26, 2019
Replace string by enum for format
1 parent 81f0c5c commit 11e80b1

File tree

5 files changed

+57
-37
lines changed

5 files changed

+57
-37
lines changed
 

‎src/server/services/wms/qgswmsgetprint.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,26 @@
2525
namespace QgsWms
2626
{
2727
void writeGetPrint( QgsServerInterface *serverIface, const QgsProject *project,
28-
const QString &version, const QgsServerRequest &request,
28+
const QString &, const QgsServerRequest &request,
2929
QgsServerResponse &response )
3030
{
31-
QgsServerRequest::Parameters params = request.parameters();
32-
33-
Q_UNUSED( version );
34-
35-
QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
31+
const QgsWmsParameters wmsParameters( QUrlQuery( request.url() ) );
3632
QgsRenderer renderer( serverIface, project, wmsParameters );
3733

38-
QString format = params.value( "FORMAT" );
34+
const QgsWmsParameters::Format format = wmsParameters.format();
3935
QString contentType;
4036

4137
// GetPrint supports svg/png/pdf
42-
if ( format.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
43-
format.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
38+
if ( format == QgsWmsParameters::PNG )
4439
{
45-
format = "png";
4640
contentType = "image/png";
4741
}
48-
else if ( format.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
49-
format.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
50-
format.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
42+
else if ( format == QgsWmsParameters::SVG )
5143
{
52-
format = "svg";
5344
contentType = "image/svg+xml";
5445
}
55-
else if ( format.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
56-
format.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
46+
else if ( format == QgsWmsParameters::PDF )
5747
{
58-
format = "pdf";
5948
contentType = "application/pdf";
6049
}
6150
else
@@ -65,11 +54,7 @@ namespace QgsWms
6554
}
6655

6756
response.setHeader( QStringLiteral( "Content-Type" ), contentType );
68-
response.write( renderer.getPrint( format ) );
57+
response.write( renderer.getPrint() );
6958
}
7059

7160
} // namespace QgsWms
72-
73-
74-
75-

‎src/server/services/wms/qgswmsparameters.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,18 +716,39 @@ namespace QgsWms
716716
return mWmsParameters[ QgsWmsParameter::FORMAT ].toString();
717717
}
718718

719-
QgsWmsParameters::Format QgsWmsParameters::format() const
719+
QString QgsWmsParameters::formatAsString( const QgsWmsParameters::Format format )
720720
{
721-
QString fStr = formatAsString();
721+
const QMetaEnum metaEnum( QMetaEnum::fromType<QgsWmsParameters::Format>() );
722+
return metaEnum.valueToKey( format );
723+
}
722724

723-
if ( fStr.isEmpty() )
724-
return Format::NONE;
725+
QgsWmsParameters::Format QgsWmsParameters::format() const
726+
{
727+
const QString fStr = formatAsString();
725728

726-
Format f = Format::PNG;
727-
if ( fStr.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0
728-
|| fStr.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0
729-
|| fStr.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
729+
Format f = Format::NONE;
730+
if ( fStr.compare( QLatin1String( "image/png" ), Qt::CaseInsensitive ) == 0 ||
731+
fStr.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 )
732+
{
733+
f = Format::PNG;
734+
}
735+
else if ( fStr.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0
736+
|| fStr.compare( QLatin1String( "jpeg" ), Qt::CaseInsensitive ) == 0
737+
|| fStr.compare( QLatin1String( "image/jpeg" ), Qt::CaseInsensitive ) == 0 )
738+
{
730739
f = Format::JPG;
740+
}
741+
else if ( fStr.compare( QLatin1String( "image/svg" ), Qt::CaseInsensitive ) == 0 ||
742+
fStr.compare( QLatin1String( "image/svg+xml" ), Qt::CaseInsensitive ) == 0 ||
743+
fStr.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
744+
{
745+
f = Format::SVG;
746+
}
747+
else if ( fStr.compare( QLatin1String( "application/pdf" ), Qt::CaseInsensitive ) == 0 ||
748+
fStr.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
749+
{
750+
f = Format::PDF;
751+
}
731752

732753
return f;
733754
}

‎src/server/services/wms/qgswmsparameters.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,15 @@ namespace QgsWms
306306
NONE,
307307
JPG,
308308
PNG,
309+
SVG,
310+
PDF,
309311
TEXT,
310312
XML,
311313
HTML,
312314
GML,
313315
JSON
314316
};
317+
Q_ENUM( Format )
315318

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

510+
/**
511+
* Returns format parameter as a string.
512+
* \returns format parameter as a string.
513+
* \since QGIS 3.6
514+
*/
515+
static QString formatAsString( Format format );
516+
507517
/**
508518
* Returns format. If the FORMAT parameter is not used, then the
509519
* default value is PNG.

‎src/server/services/wms/qgswmsrenderer.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ namespace QgsWms
275275
}
276276

277277

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

466466
// Get the temporary output file
467-
QTemporaryFile tempOutputFile( QDir::tempPath() + '/' + QStringLiteral( "XXXXXX.%1" ).arg( formatString.toLower() ) );
467+
const QgsWmsParameters::Format format = mWmsParameters.format();
468+
const QString extension = mWmsParameters.formatAsString( format ).toLower();
469+
470+
QTemporaryFile tempOutputFile( QDir::tempPath() + '/' + QStringLiteral( "XXXXXX.%1" ).arg( extension ) );
468471
if ( !tempOutputFile.open() )
469472
{
470473
throw QgsServerException( QStringLiteral( "Could not open temporary file for the GetPrint request." ) );
471474

472475
}
473476

474477
QString exportError;
475-
if ( formatString.compare( QLatin1String( "svg" ), Qt::CaseInsensitive ) == 0 )
478+
if ( format == QgsWmsParameters::SVG )
476479
{
477480
// Settings for the layout exporter
478481
QgsLayoutExporter::SvgExportSettings exportSettings;
@@ -501,10 +504,11 @@ namespace QgsWms
501504
exporter.exportToSvg( tempOutputFile.fileName(), exportSettings );
502505
}
503506
}
504-
else if ( formatString.compare( QLatin1String( "png" ), Qt::CaseInsensitive ) == 0 || formatString.compare( QLatin1String( "jpg" ), Qt::CaseInsensitive ) == 0 )
507+
else if ( format == QgsWmsParameters::PNG )
505508
{
506509
// Settings for the layout exporter
507510
QgsLayoutExporter::ImageExportSettings exportSettings;
511+
508512
// Get the dpi from input or use the default
509513
double dpi( layout->renderContext().dpi( ) );
510514
if ( !mWmsParameters.dpi().isEmpty() )
@@ -540,7 +544,7 @@ namespace QgsWms
540544
exporter.exportToImage( tempOutputFile.fileName(), exportSettings );
541545
}
542546
}
543-
else if ( formatString.compare( QLatin1String( "pdf" ), Qt::CaseInsensitive ) == 0 )
547+
else if ( format == QgsWmsParameters::PDF )
544548
{
545549
// Settings for the layout exporter
546550
QgsLayoutExporter::PdfExportSettings exportSettings;
@@ -571,7 +575,7 @@ namespace QgsWms
571575
else //unknown format
572576
{
573577
throw QgsBadRequestException( QStringLiteral( "InvalidFormat" ),
574-
QStringLiteral( "Output format '%1' is not supported in the GetPrint request" ).arg( formatString ) );
578+
QStringLiteral( "Output format '%1' is not supported in the GetPrint request" ).arg( mWmsParameters.formatAsString() ) );
575579
}
576580

577581
if ( atlas )

‎src/server/services/wms/qgswmsrenderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace QgsWms
100100
* Returns printed page as binary
101101
\param formatString out: format of the print output (e.g. pdf, svg, png, ...)
102102
\returns printed page as binary or 0 in case of error*/
103-
QByteArray getPrint( const QString &formatString );
103+
QByteArray getPrint();
104104

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

0 commit comments

Comments
 (0)
Please sign in to comment.