QGIS API Documentation
3.14.0-Pi (9f7028fd23)
|
Go to the documentation of this file.
24 #include <QNetworkReply>
25 #include <QNetworkRequest>
32 const QVariant defaultValue )
34 , mDefaultValue( defaultValue )
40 return QVariant::typeToName(
mType );
47 QString cStr =
mValue.toString();
49 if ( !cStr.isEmpty() )
52 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
54 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
57 color = QColor( cStr );
67 QString value =
mValue.toString();
69 if ( value.isEmpty() && defaultValue )
79 return toString().split( delimiter, QString::SkipEmptyParts );
86 list =
toString().split( delimiter, QString::KeepEmptyParts );
95 QList<QgsGeometry> geoms;
108 return QList<QgsGeometry>();
118 QList<QColor> colors;
122 QString cStr( part );
123 if ( !cStr.isEmpty() )
126 if ( cStr.startsWith( QLatin1String(
"0x" ), Qt::CaseInsensitive ) )
128 cStr.replace( 0, 2, QStringLiteral(
"#" ) );
131 const QColor color = QColor( cStr );
132 ok = color.isValid();
136 return QList<QColor>();
139 colors.append( color );
153 const int val = part.toInt( &ok );
173 const double val = part.toDouble( &ok );
177 return QList<double>();
191 if ( !
mValue.toString().isEmpty() )
193 QStringList corners =
mValue.toString().split(
',' );
195 if ( corners.size() == 4 )
199 for (
int i = 0; i < 4; i++ )
201 corners[i].replace(
' ',
'+' );
202 d[i] = corners[i].toDouble( &ok );
209 if ( d[0] > d[2] || d[1] > d[3] )
232 QUrl url =
toUrl( ok );
244 QObject::tr(
"Request started [url: %1]" ).arg( url.toString() ),
245 QStringLiteral(
"Server" ) );
246 QNetworkRequest request( url );
247 request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
248 request.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
true );
252 loop.exec( QEventLoop::ExcludeUserInputEvents );
254 QNetworkReply *reply = fetcher.
reply();
259 QObject::tr(
"Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
260 QStringLiteral(
"Server" ) );
264 QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
265 if ( !status.isNull() && status.toInt() >= 400 )
268 if ( reply->error() != QNetworkReply::NoError )
271 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
272 QStringLiteral(
"Server" ) );
274 QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
276 QObject::tr(
"Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
277 QStringLiteral(
"Server" ) );
281 if ( reply->error() != QNetworkReply::NoError )
285 QObject::tr(
"Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
286 QStringLiteral(
"Server" ) );
291 QObject::tr(
"Request finished [url: %1]" ).arg( url.toString() ),
292 QStringLiteral(
"Server" ) );
295 ok = ( !content.isEmpty() );
304 if ( !
mValue.toString().isEmpty() )
309 ok = ( !val.isEmpty() && val.isValid() );
318 if ( !
mValue.toString().isEmpty() )
320 val =
mValue.toInt( &ok );
330 if ( !
mValue.toString().isEmpty() )
343 if ( !
mValue.toString().isEmpty() )
345 val =
mValue.toDouble( &ok );
365 const QVariant::Type type,
const QVariant defaultValue )
375 return QStringLiteral(
"VERSION" );
379 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
380 return metaEnum.valueToKey(
name );
386 if (
name.compare( QLatin1String(
"VERSION" ) ) == 0 )
392 const QMetaEnum metaEnum( QMetaEnum::fromType<QgsServerParameter::Name>() );
399 const QString msg = QString(
"%1 ('%2') cannot be converted into %3" ).arg(
name(
mName ),
mValue.toString(),
typeName() );
424 mParameters[ parameter.
mName ] = parameter;
430 query.addQueryItem( key,
value );
436 QUrlQuery query = mUrlQuery;
438 if ( query.isEmpty() )
442 for (
auto param :
toMap().toStdMap() )
444 query.addQueryItem( param.first, param.second );
465 if ( mParameters.contains( paramName ) )
467 mParameters.take( paramName );
491 if ( serviceValue.isEmpty() )
494 if (
request() == QLatin1String(
"GetMap" ) \
495 ||
request() == QLatin1String(
"GetFeatureInfo" ) )
497 serviceValue =
"WMS";
508 for (
const auto ¶meter : mParameters.toStdMap() )
510 if ( parameter.second.mValue.isNull() )
515 params[
"VERSION"] = parameter.second.mValue.toString();
520 params[paramName] = parameter.second.mValue.toString();
546 return mParameters[name].mValue;
552 QUrlQuery cleanQuery( query );
553 cleanQuery.setQuery( query.query().replace(
'+', QStringLiteral(
"%20" ) ) );
556 for (
const auto &item : cleanQuery.queryItems( QUrl::FullyDecoded ) )
561 mParameters[name].mValue = item.second;
562 if ( ! mParameters[name].isValid() )
564 mParameters[name].raiseError();
567 else if ( item.first.compare( QLatin1String(
"VERSION" ), Qt::CaseInsensitive ) == 0 )
570 mParameters[name].mValue = item.second;
571 if ( ! mParameters[name].isValid() )
573 mParameters[name].raiseError();
bool toBool() const
Converts the parameter into a boolean.
Parameter common to all services (WMS, WFS, ...)
QgsServerParameters()
Constructor.
static void raiseError(const QString &msg)
Raises an exception in case of an invalid parameters.
QMap< QString, QString > toMap() const
Returns all parameters in a map.
QgsServerParameter::Name mName
QString request() const
Returns REQUEST parameter as a string or an empty string if not defined.
void load(const QUrlQuery &query)
Loads new parameters.
QString value(const QString &key) const
Returns the value of a parameter.
double toDouble(bool &ok) const
Converts the parameter into a double.
QStringList toStringList(char delimiter=',', bool skipEmptyParts=true) const
Converts the parameter into a list of strings.
QString loadUrl(bool &ok) const
Loads the data associated to the parameter converted into an url.
void remove(const QString &key)
Removes a parameter.
QString version() const
Returns VERSION parameter as a string or an empty string if not defined.
QMap< QString, QString > mUnmanagedParameters
void add(const QString &key, const QString &value)
Adds a parameter.
HTTP network content fetcher. A simple method for fetching remote HTTP content and converting the con...
QUrl toUrl(bool &ok) const
Converts the parameter into an url.
void fetchContent(const QUrl &url, const QString &authcfg=QString())
Fetches content from a remote URL and handles redirects.
Definition of a parameter with basic conversion methods.
Name
Parameter's name common to all services.
QNetworkReply * reply()
Returns a reference to the network reply.
QString toString(bool defaultValue=false) const
Converts the parameter into a string.
QString service() const
Returns SERVICE parameter as a string or an empty string if not defined.
QColor toColor(bool &ok) const
Converts the parameter into a color.
virtual bool loadParameter(const QString &name, const QString &value)
Loads a parameter with a specific value.
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
QString map() const
Returns MAP parameter as a string or an empty string if not defined.
QgsServerParameterDefinition(const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameterDefinition.
int toInt(bool &ok) const
Converts the parameter into an integer.
virtual bool isValid() const
Returns true if the parameter is valid, false otherwise.
QString contentAsString() const
Returns the fetched content as a string.
void finished()
Emitted when content has loaded.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
static QString name(const QgsServerParameter::Name name)
Converts a parameter's name into its string representation.
QUrlQuery urlQuery() const
Returns a url query with underlying parameters.
QgsServerParameters provides an interface to retrieve and manipulate global parameters received from ...
void raiseError() const
Raises an error in case of an invalid conversion.
QString typeName() const
Returns the type of the parameter as a string.
bool isGeosValid(QgsGeometry::ValidityFlags flags=QgsGeometry::ValidityFlags()) const
Checks validity of the geometry using GEOS.
QList< QgsGeometry > toGeomList(bool &ok, char delimiter=',') const
Converts the parameter into a list of geometries.
QList< int > toIntList(bool &ok, char delimiter=',') const
Converts the parameter into a list of integers.
QString fileName() const
Returns FILE_NAME parameter as a string or an empty string if not defined.
Exception thrown in case of malformed request.
QList< double > toDoubleList(bool &ok, char delimiter=',') const
Converts the parameter into a list of doubles.
void clear()
Removes all parameters.
QgsServerParameter(const QgsServerParameter::Name name=QgsServerParameter::UNKNOWN, const QVariant::Type type=QVariant::String, const QVariant defaultValue=QVariant(""))
Constructor for QgsServerParameter.
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.