Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
move the QGIS Server validator from the project properties to its own…
… class
- Loading branch information
Showing
9 changed files
with
413 additions
and
87 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
python/core/auto_generated/qgsprojectservervalidator.sip.in
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsprojectservervalidator.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ | ||
|
||
|
||
|
||
|
||
|
||
class QgsProjectServerValidator | ||
{ | ||
%Docstring | ||
Project server validator. | ||
|
||
.. versionadded:: 3.14 | ||
%End | ||
|
||
%TypeHeaderCode | ||
#include "qgsprojectservervalidator.h" | ||
%End | ||
public: | ||
|
||
QgsProjectServerValidator(); | ||
%Docstring | ||
Constructor for QgsProjectServerValidator. | ||
%End | ||
|
||
enum ValidationError | ||
{ | ||
DuplicatedNames, | ||
ShortNames, | ||
Encoding | ||
}; | ||
|
||
static QString displayValidationError( QgsProjectServerValidator::ValidationError error ); | ||
%Docstring | ||
Returns a human readable string for a given error. | ||
|
||
:param error: the error. | ||
|
||
:return: the human readable error. | ||
%End | ||
|
||
struct ValidationResult | ||
{ | ||
|
||
ValidationResult( const QgsProjectServerValidator::ValidationError error, const QVariant &identifier ); | ||
%Docstring | ||
Constructor for ValidationResult. | ||
%End | ||
|
||
QgsProjectServerValidator::ValidationError error; | ||
|
||
QVariant identifier; | ||
}; | ||
|
||
static bool validate( QgsProject *project, QList< QgsProjectServerValidator::ValidationResult > &results /Out/ ); | ||
%Docstring | ||
Validates a project to avoid some problems on QGIS Server, and returns ``True`` if it's considered valid. | ||
If validation fails, the ``results`` list will be filled with a list of | ||
items describing why the validation failed and what needs to be rectified | ||
|
||
:param project: input project to check | ||
|
||
:return: - bool | ||
- results: results of the validation | ||
%End | ||
|
||
}; | ||
|
||
/************************************************************************ | ||
* This file has been generated automatically from * | ||
* * | ||
* src/core/qgsprojectservervalidator.h * | ||
* * | ||
* Do not edit manually ! Edit header and run scripts/sipify.pl again * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/*************************************************************************** | ||
qgsprojectservervalidator.cpp | ||
--------------------------- | ||
begin : March 2020 | ||
copyright : (C) 2020 by Etienne Trimaille | ||
email : etienne dot trimaille at gmail dot com | ||
***************************************************************************/ | ||
|
||
/*************************************************************************** | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the GNU General Public License as published by * | ||
* the Free Software Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
***************************************************************************/ | ||
|
||
|
||
#include "qgsapplication.h" | ||
#include "qgslayertreelayer.h" | ||
#include "qgsprojectservervalidator.h" | ||
#include "qgsvectorlayer.h" | ||
|
||
|
||
QString QgsProjectServerValidator::displayValidationError( QgsProjectServerValidator::ValidationError error ) | ||
{ | ||
switch ( error ) | ||
{ | ||
case QgsProjectServerValidator::Encoding: | ||
return QObject::tr( "Encoding is not set properly. A non 'System' encoding is required" ); | ||
case QgsProjectServerValidator::ShortNames: | ||
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" ); | ||
case QgsProjectServerValidator::DuplicatedNames: | ||
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 needs to be unique" ); | ||
} | ||
return QString(); | ||
} | ||
|
||
void QgsProjectServerValidator::browseLayerTree( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages ) | ||
{ | ||
QList< QgsLayerTreeNode * > treeGroupChildren = treeGroup->children(); | ||
for ( int i = 0; i < treeGroupChildren.size(); ++i ) | ||
{ | ||
QgsLayerTreeNode *treeNode = treeGroupChildren.at( i ); | ||
if ( treeNode->nodeType() == QgsLayerTreeNode::NodeGroup ) | ||
{ | ||
QgsLayerTreeGroup *treeGroupChild = static_cast<QgsLayerTreeGroup *>( treeNode ); | ||
QString shortName = treeGroupChild->customProperty( QStringLiteral( "wmsShortName" ) ).toString(); | ||
if ( shortName.isEmpty() ) | ||
owsNames << treeGroupChild->name(); | ||
else | ||
owsNames << shortName; | ||
browseLayerTree( treeGroupChild, owsNames, encodingMessages ); | ||
} | ||
else | ||
{ | ||
QgsLayerTreeLayer *treeLayer = static_cast<QgsLayerTreeLayer *>( treeNode ); | ||
QgsMapLayer *layer = treeLayer->layer(); | ||
if ( layer ) | ||
{ | ||
QString shortName = layer->shortName(); | ||
if ( shortName.isEmpty() ) | ||
owsNames << layer->name(); | ||
else | ||
owsNames << shortName; | ||
|
||
if ( layer->type() == QgsMapLayerType::VectorLayer ) | ||
{ | ||
QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( layer ); | ||
if ( vl->dataProvider()->encoding() == QLatin1String( "System" ) ) | ||
encodingMessages << layer->name(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
bool QgsProjectServerValidator::validate( QgsProject *project, QList<QgsProjectServerValidator::ValidationResult> &results ) | ||
{ | ||
results.clear(); | ||
bool result = true; | ||
|
||
if ( !project ) | ||
return false; | ||
|
||
if ( !project->layerTreeRoot() ) | ||
return false; | ||
|
||
QStringList owsNames, encodingMessages; | ||
browseLayerTree( project->layerTreeRoot(), owsNames, encodingMessages ); | ||
|
||
QStringList duplicateNames, regExpMessages; | ||
QRegExp snRegExp = QgsApplication::shortNameRegExp(); | ||
const auto constOwsNames = owsNames; | ||
for ( const QString &name : constOwsNames ) | ||
{ | ||
if ( !snRegExp.exactMatch( name ) ) | ||
{ | ||
regExpMessages << name; | ||
} | ||
|
||
if ( duplicateNames.contains( name ) ) | ||
{ | ||
continue; | ||
} | ||
|
||
if ( owsNames.count( name ) > 1 ) | ||
{ | ||
duplicateNames << name; | ||
} | ||
} | ||
|
||
if ( !duplicateNames.empty() ) | ||
{ | ||
result = false; | ||
results << ValidationResult( QgsProjectServerValidator::DuplicatedNames, duplicateNames.join( QStringLiteral( ", " ) ) ); | ||
} | ||
|
||
if ( !regExpMessages.empty() ) | ||
{ | ||
result = false; | ||
results << ValidationResult( QgsProjectServerValidator::ShortNames, regExpMessages.join( QStringLiteral( ", " ) ) ); | ||
} | ||
|
||
if ( !encodingMessages.empty() ) | ||
{ | ||
result = false; | ||
results << ValidationResult( QgsProjectServerValidator::Encoding, encodingMessages.join( QStringLiteral( ", " ) ) ); | ||
} | ||
|
||
return result; | ||
} |
Oops, something went wrong.