20#include "moc_qgsserverparameters.cpp"
26#include <QNetworkReply>
27#include <QNetworkRequest>
35 , mDefaultValue( defaultValue )
46 return QVariant::typeToName(
mType );
53 QString cStr =
mValue.toString();
55 if ( !cStr.isEmpty() )
58 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
60 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
63 color = QColor( cStr );
73 QString value =
mValue.toString();
75 if ( value.isEmpty() && defaultValue )
85 return toString().split( delimiter, Qt::SkipEmptyParts );
92 list =
toString().split( delimiter, Qt::KeepEmptyParts );
101 QList<QgsGeometry> geoms;
103 const auto constStringList(
toStringList( delimiter ) );
104 for (
const auto &wkt : constStringList )
115 return QList<QgsGeometry>();
128 while ( pos < filter.size() )
130 if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
'<' )
133 int posEnd = filter.indexOf(
"Filter>)", pos );
136 posEnd = filter.size();
138 filters.append( filter.mid( pos + 1, posEnd - pos + 6 ) );
141 else if ( pos + 1 < filter.size() && filter[pos] ==
'(' && filter[pos + 1] ==
')' )
144 filters.append(
"" );
147 else if ( filter[pos] ==
'<' && pos + 7 < filter.size() && filter.mid( pos + 1, 6 ).compare( QLatin1String(
"Filter" ) ) == 0 )
150 filters.append( filter.mid( pos ) );
168 auto isOgcFilter = [filter]() {
169 return filter.contains( QStringLiteral(
"<Filter>" ) ) || filter.contains( QStringLiteral(
"()" ) );
172 while ( pos < filter.size() )
174 int posEnd = filter.indexOf(
';', pos );
176 if ( posEnd == pos + 1 )
178 if ( !isOgcFilter() )
179 filters.append( QString() );
184 if ( !isOgcFilter() )
185 filters.append( filter.mid( pos, posEnd - pos ) );
197 if ( !filter.isEmpty() && filter.back() ==
';' )
199 filters.append( QString() );
208 QList<QColor> colors;
210 const auto constStringList(
toStringList( delimiter ) );
211 for (
const auto &part : constStringList )
213 QString cStr( part );
214 if ( !cStr.isEmpty() )
217 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
219 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
222 const QColor color = QColor( cStr );
223 ok = color.isValid();
227 return QList<QColor>();
230 colors.append( color );
242 const auto constStringList(
toStringList( delimiter ) );
243 for (
const auto &part : constStringList )
245 const int val = part.toInt( &ok );
263 const auto constStringList(
toStringList( delimiter ) );
264 for (
const auto &part : constStringList )
266 const double val = part.toDouble( &ok );
270 return QList<double>();
284 if ( !
mValue.toString().isEmpty() )
288 if ( corners.size() == 4 )
292 for (
int i = 0; i < 4; i++ )
294 corners[i].replace(
' ',
'+' );
295 d[i] = corners[i].toDouble( &ok );
302 if ( d[0] > d[2] || d[1] > d[3] )
325 const QUrl url =
toUrl( ok );
331 QNetworkRequest request( url );
332 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
333 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
343 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( newReq.
errorMessage(), url.toString() ),
344 QStringLiteral(
"Server" )
351 ok = !reply.
content().isEmpty();
360 if ( !
mValue.toString().isEmpty() )
365 ok = ( !val.isEmpty() && val.isValid() );
374 if ( !
mValue.toString().isEmpty() )
376 val =
mValue.toInt( &ok );
386 if ( !
mValue.toString().isEmpty() )
399 if ( !
mValue.toString().isEmpty() )
401 val =
mValue.toDouble( &ok );
435 return QStringLiteral(
"VERSION" );
439 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
440 return metaEnum.valueToKey(
name );
446 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
452 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
459 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
484 mParameters[parameter.
mName] = parameter;
490 query.addQueryItem( key,
value );
496 QUrlQuery query = mUrlQuery;
498 if ( query.isEmpty() )
502 const auto constMap(
toMap().toStdMap() );
503 for (
const auto ¶m : constMap )
505 const QString
value = QUrl::toPercentEncoding( QString( param.second ) );
506 query.addQueryItem( param.first,
value );
527 if ( mParameters.contains( paramName ) )
529 mParameters.take( paramName );
553 if ( serviceValue.isEmpty() )
556 if (
request() == QLatin1String(
"GetMap" )
557 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
559 serviceValue =
"WMS";
570 for (
const auto ¶meter : mParameters.toStdMap() )
577 params[
"VERSION"] = parameter.second.mValue.toString();
582 params[paramName] = parameter.second.mValue.toString();
608 return mParameters[name].mValue;
614 QUrlQuery cleanQuery( query );
615 cleanQuery.setQuery( query.query().replace(
'+', QLatin1String(
"%20" ) ) );
618 const auto constQueryItems( cleanQuery.queryItems( QUrl::FullyDecoded ) );
619 for (
const auto &item : constQueryItems )
624 mParameters[name].mValue = item.second;
625 if ( !mParameters[name].isValid() )
627 mParameters[name].raiseError();
630 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
633 mParameters[name].mValue = item.second;
634 if ( !mParameters[name].isValid() )
636 mParameters[name].raiseError();
Exception thrown in case of malformed request.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
A geometry is the spatial representation of a feature.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
bool isGeosValid(Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const
Checks validity of the geometry using GEOS.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
A rectangle specified with double values.
Q_INVOKABLE QString toString(int precision=16) const
Returns a string representation of form xmin,ymin : xmax,ymax Coordinates will be truncated to the sp...
Definition of a parameter with basic conversion methods.
QList< QgsGeometry > toGeomList(bool &ok, char delimiter=',') const
Converts the parameter into a list of geometries.
QString loadUrl(bool &ok) const
Loads the data associated to the parameter converted into an url.
QUrl toUrl(bool &ok) const
Converts the parameter into an url.
QString toString(bool defaultValue=false) const
Converts the parameter into a string.
bool toBool() const
Converts the parameter into a boolean.
QgsServerParameterDefinition(const QMetaType::Type type=QMetaType::Type::QString, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
QList< double > toDoubleList(bool &ok, char delimiter=',') const
Converts the parameter into a list of doubles.
QStringList toStringList(char delimiter=',', bool skipEmptyParts=true) const
Converts the parameter into a list of strings.
virtual bool isValid() const
Returns true if the parameter is valid, false otherwise.
QString typeName() const
Returns the type of the parameter as a string.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
QStringList toExpressionList() const
Converts the parameter into a list of QGIS expressions.
int toInt(bool &ok) const
Converts the parameter into an integer.
QList< int > toIntList(bool &ok, char delimiter=',') const
Converts the parameter into a list of integers.
QColor toColor(bool &ok) const
Converts the parameter into a color.
double toDouble(bool &ok) const
Converts the parameter into a double.
QgsRectangle toRectangle(bool &ok) const
Converts the parameter into a rectangle.
QList< QColor > toColorList(bool &ok, char delimiter=',') const
Converts the parameter into a list of colors.
QStringList toOgcFilterList() const
Converts the parameter into a list of OGC filters.
Parameter common to all services (WMS, WFS, ...)
QgsServerParameter::Name mName
QgsServerParameter(const QgsServerParameter::Name name=QgsServerParameter::UNKNOWN, const QMetaType::Type type=QMetaType::Type::QString, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameter.
Name
Parameter's name common to all services.
void raiseError() const
Raises an error in case of an invalid conversion.
static QString name(const QgsServerParameter::Name name)
Converts a parameter's name into its string representation.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
QgsServerParameters()
Constructor.
void add(const QString &key, const QString &value)
Adds a parameter.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
virtual QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
void clear()
Removes all parameters.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
virtual bool loadParameter(const QString &name, const QString &value)
Loads a parameter with a specific value.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
QMap< QString, QString > mUnmanagedParameters
void load(const QUrlQuery &query)
Loads new parameters.
void remove(const QString &key)
Removes a parameter.
virtual QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
QString value(const QString &key) const
Returns the value of a parameter.
Contains utility functions for working with QVariants and QVariant types.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.