Skip to content

Commit

Permalink
use enum in the validation result struct, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Mar 30, 2020
1 parent f2cbe04 commit 4334a99
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,20 @@



class QgsAbstractBaseValidator

class QgsAbstractMetadataBaseValidator
{
%Docstring
Abstract base class for validators.
Abstract base class for metadata validators.

.. versionadded:: 3.14
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayermetadatavalidator.h"
%End
public:


virtual ~QgsAbstractBaseValidator();

struct ValidationResult
{

Expand All @@ -40,26 +38,10 @@ Constructor for ValidationResult.

QString note;
};
};



class QgsAbstractMetadataBaseValidator : QgsAbstractBaseValidator
{
%Docstring
Abstract base class for metadata validators.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayermetadatavalidator.h"
%End
public:

virtual ~QgsAbstractMetadataBaseValidator();

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const = 0;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const = 0;
%Docstring
Validates a ``metadata`` object, and returns ``True`` if the
metadata is considered valid.
Expand Down Expand Up @@ -89,7 +71,7 @@ A validator for the native base QGIS metadata schema definition.
Constructor for QgsNativeMetadataBaseValidator.
%End

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const;


};
Expand All @@ -114,7 +96,7 @@ A validator for the native QGIS layer metadata schema definition.
Constructor for QgsNativeMetadataValidator.
%End

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const;


};
Expand All @@ -138,7 +120,7 @@ A validator for the native QGIS project metadata schema definition.
Constructor for QgsNativeProjectMetadataValidator.
%End

virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results /Out/ ) const;


};
Expand Down
38 changes: 35 additions & 3 deletions python/core/auto_generated/qgsprojectservervalidator.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@



class QgsProjectServerValidator : QgsAbstractBaseValidator
class QgsProjectServerValidator
{
%Docstring
Project server validator.
Expand All @@ -25,10 +25,42 @@ Project server validator.

QgsProjectServerValidator();
%Docstring
Constructor for :py:class:`QgsNativeMetadataBaseValidator`.
Constructor for QgsProjectServerValidator.
%End

bool validate( QgsLayerTree *layerTree, QList< QgsAbstractBaseValidator::ValidationResult > &results /Out/ ) const;
enum ValidationError
{
DuplicatedNames,
ShortNames,
Encoding
};

static QString displayValidationError( QgsProjectServerValidator::ValidationError error );

struct ValidationResult
{

ValidationResult( const QgsProjectServerValidator::ValidationError error, const QVariant &identifier );
%Docstring
Constructor for ValidationResult.
%End

QgsProjectServerValidator::ValidationError error;

QVariant identifier;
};

bool validate( QgsLayerTree *layerTree, QList< QgsProjectServerValidator::ValidationResult > &results /Out/ ) const;
%Docstring
Validates a layer tree 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 layerTree: input layer tree

:return: - bool
- results: results of the validation
%End

};

Expand Down
14 changes: 5 additions & 9 deletions src/app/qgsprojectproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1906,21 +1906,17 @@ void QgsProjectProperties::pbnWCSLayersDeselectAll_clicked()

void QgsProjectProperties::pbnLaunchOWSChecker_clicked()
{
QList<QgsAbstractBaseValidator::ValidationResult> validationResults;
QList<QgsProjectServerValidator::ValidationResult> validationResults;
QgsProjectServerValidator validator;
bool results = validator.validate( QgisApp::instance()->layerTreeView()->layerTreeModel()->rootGroup(), validationResults );
bool results = validator.validate( QgsProject::instance()->layerTreeRoot(), validationResults );

QString errors;
if ( !results )
{
for ( const QgsAbstractBaseValidator::ValidationResult &result : qgis::as_const( validationResults ) )
for ( const QgsProjectServerValidator::ValidationResult &result : qgis::as_const( validationResults ) )
{
errors += QLatin1String( "<b>" ) % result.section % QLatin1String( " :</b> " );
if ( ! result.identifier.isNull() )
{
errors += QLatin1String( " " ) % result.identifier.toString();
}
errors += QLatin1String( " : " ) % result.note % QLatin1String( "<br />" );
errors += QLatin1String( "<b>" ) % QgsProjectServerValidator::displayValidationError( result.error ) % QLatin1String( " :</b> " );
errors += result.identifier.toString();
}
}
else
Expand Down
4 changes: 2 additions & 2 deletions src/core/metadata/qgslayermetadatavalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// QgsNativeMetadataBaseValidator
//

bool QgsNativeMetadataBaseValidator::validate( const QgsAbstractMetadataBase *metadata, QList<QgsAbstractBaseValidator::ValidationResult> &results ) const
bool QgsNativeMetadataBaseValidator::validate( const QgsAbstractMetadataBase *metadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
{
results.clear();
if ( !metadata )
Expand Down Expand Up @@ -185,7 +185,7 @@ bool QgsNativeMetadataValidator::validate( const QgsAbstractMetadataBase *baseMe
// QgsNativeProjectMetadataValidator
//

bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractBaseValidator::ValidationResult> &results ) const
bool QgsNativeProjectMetadataValidator::validate( const QgsAbstractMetadataBase *baseMetadata, QList<QgsAbstractMetadataBaseValidator::ValidationResult> &results ) const
{
results.clear();

Expand Down
35 changes: 10 additions & 25 deletions src/core/metadata/qgslayermetadatavalidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ class QgsLayerMetadata;

/**
* \ingroup core
* \class QgsAbstractBaseValidator
* \brief Abstract base class for validators.
* \since QGIS 3.14
* \class QgsAbstractMetadataBaseValidator
* \brief Abstract base class for metadata validators.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsAbstractBaseValidator
{
public:

class CORE_EXPORT QgsAbstractMetadataBaseValidator
{

virtual ~QgsAbstractBaseValidator() = default;
public:

/**
* Contains the parameters describing a metadata validation
Expand Down Expand Up @@ -69,20 +68,6 @@ class CORE_EXPORT QgsAbstractBaseValidator
//! The reason behind the validation failure.
QString note;
};
};


/**
* \ingroup core
* \class QgsAbstractMetadataBaseValidator
* \brief Abstract base class for metadata validators.
* \since QGIS 3.0
*/

class CORE_EXPORT QgsAbstractMetadataBaseValidator : public QgsAbstractBaseValidator
{

public:

virtual ~QgsAbstractMetadataBaseValidator() = default;

Expand All @@ -93,7 +78,7 @@ class CORE_EXPORT QgsAbstractMetadataBaseValidator : public QgsAbstractBaseValid
* items describing why the validation failed and what needs to be rectified
* to fix the metadata.
*/
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const = 0;
virtual bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const = 0;

};

Expand All @@ -114,7 +99,7 @@ class CORE_EXPORT QgsNativeMetadataBaseValidator : public QgsAbstractMetadataBas
*/
QgsNativeMetadataBaseValidator() = default;

bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const override;

};

Expand All @@ -136,7 +121,7 @@ class CORE_EXPORT QgsNativeMetadataValidator : public QgsNativeMetadataBaseValid
*/
QgsNativeMetadataValidator() = default;

bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const override;

};

Expand All @@ -157,7 +142,7 @@ class CORE_EXPORT QgsNativeProjectMetadataValidator : public QgsNativeMetadataBa
*/
QgsNativeProjectMetadataValidator() = default;

bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractBaseValidator::ValidationResult > &results SIP_OUT ) const override;
bool validate( const QgsAbstractMetadataBase *metadata, QList< QgsAbstractMetadataBaseValidator::ValidationResult > &results SIP_OUT ) const override;

};

Expand Down
38 changes: 26 additions & 12 deletions src/core/qgsprojectservervalidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
#include "qgsvectorlayer.h"


QString QgsProjectServerValidator::displayValidationError( QgsProjectServerValidator::ValidationError error )
{
switch ( error )
{
case QgsProjectServerValidator::Encoding:
return QObject::tr( "Encoding is not set properly. It shouldn't be 'System'" );
case QgsProjectServerValidator::ShortNames:
return QObject::tr( "Layer short name needs to be updated" );
case QgsProjectServerValidator::DuplicatedNames:
return QObject::tr( "Layers/groups have the same name or short name" );
}
return QString();
}

void QgsProjectServerValidator::browseLayerTree( QgsLayerTreeGroup *treeGroup, QStringList &owsNames, QStringList &encodingMessages )
{
QList< QgsLayerTreeNode * > treeGroupChildren = treeGroup->children();
Expand All @@ -41,27 +55,27 @@ void QgsProjectServerValidator::browseLayerTree( QgsLayerTreeGroup *treeGroup, Q
else
{
QgsLayerTreeLayer *treeLayer = static_cast<QgsLayerTreeLayer *>( treeNode );
QgsMapLayer *l = treeLayer->layer();
if ( l )
QgsMapLayer *layer = treeLayer->layer();
if ( layer )
{
QString shortName = l->shortName();
QString shortName = layer->shortName();
if ( shortName.isEmpty() )
owsNames << l->name();
owsNames << layer->name();
else
owsNames << shortName;
if ( l->type() == QgsMapLayerType::VectorLayer )

if ( layer->type() == QgsMapLayerType::VectorLayer )
{
QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( l );
QgsVectorLayer *vl = static_cast<QgsVectorLayer *>( layer );
if ( vl->dataProvider()->encoding() == QLatin1String( "System" ) )
encodingMessages << l->name();
encodingMessages << layer->name();
}
}
}
}
}


bool QgsProjectServerValidator::validate( QgsLayerTree *layerTree, QList<QgsAbstractBaseValidator::ValidationResult> &results ) const
bool QgsProjectServerValidator::validate( QgsLayerTree *layerTree, QList<QgsProjectServerValidator::ValidationResult> &results ) const
{
results.clear();
bool result = true;
Expand Down Expand Up @@ -96,19 +110,19 @@ bool QgsProjectServerValidator::validate( QgsLayerTree *layerTree, QList<QgsAbst
if ( !duplicateNames.empty() )
{
result = false;
results << ValidationResult( QObject::tr( "Duplicated names" ), QObject::tr( "Layers/groups have the same name or short name." ), duplicateNames.join( QStringLiteral( ", " ) ) );
results << ValidationResult( QgsProjectServerValidator::DuplicatedNames, duplicateNames.join( QStringLiteral( ", " ) ) );
}

if ( !regExpMessages.empty() )
{
result = false;
results << ValidationResult( QObject::tr( "Short names" ), QObject::tr( "Layers short names have to be updated." ), regExpMessages.join( QStringLiteral( ", " ) ) );
results << ValidationResult( QgsProjectServerValidator::ShortNames, regExpMessages.join( QStringLiteral( ", " ) ) );
}

if ( !encodingMessages.empty() )
{
result = false;
results << ValidationResult( QObject::tr( "Encoding" ), QObject::tr( "Encoding is not set." ), encodingMessages.join( QStringLiteral( ", " ) ) );
results << ValidationResult( QgsProjectServerValidator::Encoding, encodingMessages.join( QStringLiteral( ", " ) ) );
}

return result;
Expand Down
Loading

0 comments on commit 4334a99

Please sign in to comment.