25#include <QRegularExpression>
28using namespace Qt::StringLiterals;
35 return QObject::tr(
"Encoding is not correctly set. A non 'System' encoding is required" );
37 return QObject::tr(
"Layer short name is not valid. It must start with an unaccented alphabetical letter, followed by any alphanumeric letters, dot, dash or underscore" );
39 return QObject::tr(
"One or more layers or groups have the same name or short name. Both the 'name' and 'short name' for layers and groups must be unique" );
42 "The project root name (either the project short name or project title) is not valid. It must start with an unaccented alphabetical letter, followed by any alphanumeric letters, dot, dash or "
46 return QObject::tr(
"The project root name (either the project short name or project title) is already used by a layer or a group" );
48 return QObject::tr(
"Use only maptip for HTML GetFeatureInfo response is enabled but the HTML maptip is empty" );
56 const QString shortName = node->serverProperties()->shortName();
57 return shortName.isEmpty() ? node->name() : shortName;
60void QgsProjectServerValidator::browseLayerTree(
QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages, QStringList &layerNames, QStringList &maptipTemplates )
62 const QList< QgsLayerTreeNode * > treeGroupChildren = treeGroup->
children();
63 for (
int i = 0; i < treeGroupChildren.size(); ++i )
65 QgsLayerTreeNode *treeNode = treeGroupChildren.at( i );
68 QgsLayerTreeGroup *treeGroupChild =
static_cast<QgsLayerTreeGroup *
>( treeNode );
70 browseLayerTree( treeGroupChild, owsNames, encodingMessages, layerNames, maptipTemplates );
74 QgsLayerTreeLayer *treeLayer =
static_cast<QgsLayerTreeLayer *
>( treeNode );
75 QgsMapLayer *layer = treeLayer->
layer();
81 QgsVectorLayer *vl =
static_cast<QgsVectorLayer *
>( layer );
83 encodingMessages << layer->
name();
85 layerNames << treeLayer->
name();
92bool QgsProjectServerValidator::isOnlyMaptipEnabled(
QgsProject *project )
94 return project->
readBoolEntry( u
"WMSHTMLFeatureInfoUseOnlyMaptip"_s, QString(),
false );
108 QStringList owsNames, encodingMessages, layerNames, maptipTemplates;
109 browseLayerTree( project->
layerTreeRoot(), owsNames, encodingMessages, layerNames, maptipTemplates );
111 QStringList duplicateNames, regExpMessages;
113 const auto constOwsNames = owsNames;
114 for (
const QString &name : constOwsNames )
116 if ( !snRegExp.match( name ).hasMatch() )
118 regExpMessages << name;
121 if ( duplicateNames.contains( name ) )
126 if ( owsNames.count( name ) > 1 )
128 duplicateNames << name;
132 if ( !duplicateNames.empty() )
138 if ( !regExpMessages.empty() )
144 if ( !encodingMessages.empty() )
151 QString rootLayerName = project->
readEntry( u
"WMSRootName"_s, u
"/"_s,
"" );
152 if ( rootLayerName.isEmpty() && !project->
title().isEmpty() )
154 rootLayerName = project->
title();
156 if ( !rootLayerName.isEmpty() )
158 if ( owsNames.count( rootLayerName ) >= 1 )
164 if ( !snRegExp.match( rootLayerName ).hasMatch() )
170 if ( isOnlyMaptipEnabled( project ) )
172 QStringList emptyLayers;
173 for (
int i = 0; i < maptipTemplates.size(); ++i )
175 if ( maptipTemplates[i].trimmed().isEmpty() )
176 emptyLayers << layerNames[i];
179 if ( !emptyLayers.isEmpty() )
182 QString details = emptyLayers.join(
", "_L1 ).toHtmlEscaped();
static QRegularExpression shortNameRegularExpression()
Returns the short name regular expression for line edit validator.
Layer tree group node serves as a container for layers and further groups.
QString name() const override
Returns the layer's name.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
@ NodeGroup
Container of other groups and layers.
QList< QgsLayerTreeNode * > children()
Gets list of children of the node. Children are owned by the parent.
NodeType nodeType() const
Find out about type of the node. It is usually shorter to use convenience functions from QgsLayerTree...
static QString displayValidationError(QgsProjectServerValidator::ValidationError error)
Returns a human readable string for a given error.
ValidationError
Errors that might be raised by the validation process.
@ LayerEncoding
Encoding is not correctly set on a vector layer.
@ ProjectRootNameConflict
The project root name is already used by a layer or a group.
@ ProjectShortName
The project short name is not valid.
@ DuplicatedNames
A duplicated layer/group name in the layer tree.
@ OnlyMaptipTrueButEmptyMaptip
Use only maptip for HTML GetFeatureInfo response is enabled but HTML maptip is empty.
@ LayerShortName
Layer/group short name is not valid.
static bool validate(QgsProject *project, QList< QgsProjectServerValidator::ValidationResult > &results)
Validates a project to detect problems on QGIS Server, and returns true if it's considered valid.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=nullptr) const
Reads a boolean from the specified scope and key.
QString encoding() const
Returns the encoding which is used for accessing data.
QgsVectorDataProvider * dataProvider() final
Returns the layer's data provider, it may be nullptr.
QString getShortName(T *node)
helper method to retrieve a layer or layer tree group short name
Contains the parameters describing a project validation failure.