Skip to content

Commit

Permalink
Small cleanups in WMS renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Jul 31, 2017
1 parent 5c82a85 commit 3ebad9a
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 84 deletions.
4 changes: 1 addition & 3 deletions src/core/qgsprojectversion.cpp
Expand Up @@ -69,9 +69,7 @@ bool QgsProjectVersion::operator!=( const QgsProjectVersion &other ) const

bool QgsProjectVersion::operator>=( const QgsProjectVersion &other ) const
{
return ( ( mMajor >= other.mMajor ) ||
( ( mMajor == other.mMajor ) && ( mMinor >= other.mMinor ) ) ||
( ( mMajor == other.mMajor ) && ( mMinor == other.mMinor ) && ( mSub >= other.mSub ) ) );
return ( *this == other ) || ( *this > other );
}

bool QgsProjectVersion::operator>( const QgsProjectVersion &other ) const
Expand Down
152 changes: 134 additions & 18 deletions src/server/services/wms/qgswmsparameters.cpp
Expand Up @@ -23,6 +23,11 @@ namespace QgsWms
{
QgsWmsParameters::QgsWmsParameters()
{
// Available version number
mVersions.append( QgsProjectVersion( 1, 1, 1 ) );
mVersions.append( QgsProjectVersion( 1, 3, 0 ) );

// WMS parameters definition
const Parameter pBoxSpace = { ParameterName::BOXSPACE,
QVariant::Double,
QVariant( 2.0 ),
Expand Down Expand Up @@ -184,6 +189,13 @@ namespace QgsWms
};
save( pCRS );

const Parameter pSRS = { ParameterName::SRS,
QVariant::String,
QVariant( "" ),
QVariant()
};
save( pSRS );

const Parameter pFormat = { ParameterName::FORMAT,
QVariant::String,
QVariant( "" ),
Expand Down Expand Up @@ -393,6 +405,27 @@ namespace QgsWms
QVariant()
};
save( pWmsPrecision );

const Parameter pTransparent = { ParameterName::TRANSPARENT,
QVariant::Bool,
QVariant( false ),
QVariant()
};
save( pTransparent );

const Parameter pBgColor = { ParameterName::BGCOLOR,
QVariant::String,
QVariant( "white" ),
QVariant()
};
save( pBgColor );

const Parameter pDpi = { ParameterName::DPI,
QVariant::Int,
QVariant( -1 ),
QVariant()
};
save( pDpi );
}

QgsWmsParameters::QgsWmsParameters( const QgsServerRequest::Parameters &parameters )
Expand Down Expand Up @@ -438,6 +471,9 @@ namespace QgsWms
log( " - " + name + " : " + value );
}
}

if ( !version().isEmpty() )
log( " - VERSION : " + version() );
}

void QgsWmsParameters::save( const Parameter &parameter )
Expand Down Expand Up @@ -490,7 +526,25 @@ namespace QgsWms

QString QgsWmsParameters::crs() const
{
return value( ParameterName::CRS ).toString();
QString rs;
QString srs = value( ParameterName::SRS ).toString();
QString crs = value( ParameterName::CRS ).toString();

// both SRS/CRS are supported but there's a priority according to the
// specified version when both are defined in the request
if ( !srs.isEmpty() && crs.isEmpty() )
rs = srs;
else if ( srs.isEmpty() && !crs.isEmpty() )
rs = crs;
else if ( !srs.isEmpty() && !crs.isEmpty() )
{
if ( versionAsNumber() >= QgsProjectVersion( 1, 3, 0 ) )
rs = crs;
else
rs = srs;
}

return rs;
}

QString QgsWmsParameters::bbox() const
Expand Down Expand Up @@ -552,6 +606,39 @@ namespace QgsWms
return toInt( ParameterName::WIDTH );
}

QString QgsWmsParameters::dpi() const
{
return value( ParameterName::DPI ).toString();
}

int QgsWmsParameters::dpiAsInt() const
{
return toInt( ParameterName::DPI );
}

QString QgsWmsParameters::version() const
{
// VERSION parameter is not managed with other parameters because
// there's a conflict with qgis VERSION defined in qgsconfig.h
if ( mRequestParameters.contains( "VERSION" ) )
return mRequestParameters["VERSION"];
else
return QString();
}

QgsProjectVersion QgsWmsParameters::versionAsNumber() const
{
QString vStr = version();
QgsProjectVersion version;

if ( vStr.isEmpty() )
version = QgsProjectVersion( 1, 3, 0 ); // default value
else if ( mVersions.contains( QgsProjectVersion( vStr ) ) )
version = QgsProjectVersion( vStr );

return version;
}

double QgsWmsParameters::toDouble( ParameterName p ) const
{
double val = defaultValue( p ).toDouble();
Expand Down Expand Up @@ -605,6 +692,31 @@ namespace QgsWms
return val;
}

QColor QgsWmsParameters::toColor( ParameterName p ) const
{
QColor c = defaultValue( p ).value<QColor>();

if ( !value( p ).toString().isEmpty() )
{
// support hexadecimal notation to define colors
QString cStr = value( p ).toString();
if ( cStr.startsWith( "0x", Qt::CaseInsensitive ) )
cStr.replace( 0, 2, "#" );

c = QColor( cStr );

if ( !c.isValid() )
{
QString val = value( p ).toString();
QString n = name( p );
QString msg = n + " ('" + val + "') cannot be converted into a color";
raiseError( msg );
}
}

return c;
}

QStringList QgsWmsParameters::toStringList( ParameterName name, char delimiter ) const
{
return value( name ).toString().split( delimiter, QString::SkipEmptyParts );
Expand Down Expand Up @@ -795,6 +907,16 @@ namespace QgsWms
return toBool( ParameterName::RULELABEL );
}

QString QgsWmsParameters::transparent() const
{
return value( ParameterName::TRANSPARENT ).toString();
}

bool QgsWmsParameters::transparentAsBool() const
{
return toBool( ParameterName::TRANSPARENT );
}

QString QgsWmsParameters::scale() const
{
return value( ParameterName::SCALE ).toString();
Expand Down Expand Up @@ -962,23 +1084,7 @@ namespace QgsWms

QColor QgsWmsParameters::layerFontColorAsColor() const
{
ParameterName p = ParameterName::LAYERFONTCOLOR;
QColor c = defaultValue( p ).value<QColor>();

if ( !layerFontColor().isEmpty() )
{
c = QColor( layerFontColor() );

if ( !c.isValid() )
{
QString val = value( p ).toString();
QString n = name( p );
QString msg = n + " ('" + val + "') cannot be converted into a color";
raiseError( msg );
}
}

return c;
return toColor( ParameterName::LAYERFONTCOLOR );
}

QString QgsWmsParameters::itemFontSize() const
Expand Down Expand Up @@ -1301,6 +1407,16 @@ namespace QgsWms
return params;
}

QString QgsWmsParameters::backgroundColor() const
{
return value( ParameterName::BGCOLOR ).toString();
}

QColor QgsWmsParameters::backgroundColorAsColor() const
{
return toColor( ParameterName::BGCOLOR );
}

QString QgsWmsParameters::name( ParameterName name ) const
{
const QMetaEnum metaEnum( QMetaEnum::fromType<ParameterName>() );
Expand Down
61 changes: 59 additions & 2 deletions src/server/services/wms/qgswmsparameters.h
Expand Up @@ -22,11 +22,13 @@
#include <QObject>
#include <QMetaEnum>
#include <QColor>

#include "qgsrectangle.h"
#include "qgswmsserviceexception.h"
#include "qgsserverrequest.h"
#include "qgslegendsettings.h"
#include "qgsgeometry.h"
#include "qgsprojectversion.h"

/** QgsWmsParameters provides an interface to retrieve and manipulate WMS
* parameters received from the client.
Expand Down Expand Up @@ -66,7 +68,7 @@ namespace QgsWms
{
BOXSPACE,
CRS, // instead of SRS for WMS 1.3.0
// SRS, // for WMS 1.1.1
SRS, // for WMS 1.1.1
WIDTH,
HEIGHT,
BBOX,
Expand Down Expand Up @@ -117,7 +119,10 @@ namespace QgsWms
HIGHLIGHT_LABELCOLOR,
HIGHLIGHT_LABELBUFFERCOLOR,
HIGHLIGHT_LABELBUFFERSIZE,
WMS_PRECISION
WMS_PRECISION,
TRANSPARENT,
BGCOLOR,
DPI
};
Q_ENUM( ParameterName )

Expand Down Expand Up @@ -189,6 +194,17 @@ namespace QgsWms
*/
int heightAsInt() const;

/** Returns VERSION parameter as a string or an empty string if not
* defined.
* \returns version
*/
QString version() const;

/** Returns VERSION parameter if defined or its default value.
* \returns version
*/
QgsProjectVersion versionAsNumber() const;

/** Returns BBOX if defined or an empty string.
* \returns bbox parameter
*/
Expand Down Expand Up @@ -712,6 +728,45 @@ namespace QgsWms
*/
int wmsPrecisionAsInt() const;

/** Returns TRANSPARENT parameter or an empty string if not defined.
* \returns TRANSPARENT parameter
*/
QString transparent() const;

/** Returns TRANSPARENT parameter as a bool or its default value if not
* defined. An exception is raised if TRANSPARENT is defined and cannot
* be converted.
* \returns transparent parameter
* \throws QgsBadRequestException
*/
bool transparentAsBool() const;

/** Returns BGCOLOR parameter or an empty string if not defined.
* \returns BGCOLOR parameter
*/
QString backgroundColor() const;

/** Returns BGCOLOR parameter as a QColor or its default value if not
* defined. An exception is raised if BGCOLOR is defined and cannot
* be converted.
* \returns background color parameter
* \throws QgsBadRequestException
*/
QColor backgroundColorAsColor() const;

/** Returns DPI parameter or an empty string if not defined.
* \returns DPI parameter
*/
QString dpi() const;

/** Returns DPI parameter as an int or its default value if not
* defined. An exception is raised if DPI is defined and cannot
* be converted.
* \returns dpi parameter
* \throws QgsBadRequestException
*/
int dpiAsInt() const;

private:
QString name( ParameterName name ) const;
void raiseError( ParameterName name ) const;
Expand All @@ -724,13 +779,15 @@ namespace QgsWms
double toDouble( ParameterName name ) const;
bool toBool( ParameterName name ) const;
int toInt( ParameterName name ) const;
QColor toColor( ParameterName name ) const;
QStringList toStringList( ParameterName name, char delimiter = ',' ) const;
QList<int> toIntList( QStringList l, ParameterName name ) const;
QList<float> toFloatList( QStringList l, ParameterName name ) const;
QList<QColor> toColorList( QStringList l, ParameterName name ) const;

QgsServerRequest::Parameters mRequestParameters;
QMap<ParameterName, Parameter> mParameters;
QList<QgsProjectVersion> mVersions;
};
}

Expand Down

0 comments on commit 3ebad9a

Please sign in to comment.