Skip to content
Permalink
Browse files

Put check registry into use

  • Loading branch information
m-kuhn committed Sep 26, 2018
1 parent abda865 commit dae3134b2371e9c002fc505916890cdaf232e563
@@ -1,6 +1,7 @@
#include "qgsgeometryvalidationmodel.h"

#include "qgsvectorlayer.h"
#include "qgssinglegeometrycheck.h"

#include <QIcon>

@@ -55,7 +56,7 @@ QVariant QgsGeometryValidationModel::data( const QModelIndex &index, int role )
if ( featureItem.errors.count() > 1 )
return tr( "%1: %n Errors", "", featureItem.errors.count() ).arg( featureTitle );
else if ( featureItem.errors.count() == 1 )
return tr( "%1: %2" ).arg( featureTitle, featureItem.errors.at( 0 ).what() );
return tr( "%1: %2" ).arg( featureTitle, featureItem.errors.at( 0 )->description() );
#if 0
else
return tr( "%1: No Errors" ).arg( featureTitle );
@@ -101,7 +102,7 @@ void QgsGeometryValidationModel::setCurrentLayer( QgsVectorLayer *currentLayer )
endResetModel();
}

void QgsGeometryValidationModel::onGeometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<QgsGeometry::Error> &errors )
void QgsGeometryValidationModel::onGeometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<std::shared_ptr<QgsSingleGeometryCheckError>> &errors )
{
auto &layerErrors = mErrorStorage[layer];

@@ -25,7 +25,7 @@ class QgsGeometryValidationModel : public QAbstractItemModel
void setCurrentLayer( QgsVectorLayer *currentLayer );

private slots:
void onGeometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<QgsGeometry::Error> &errors );
void onGeometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<std::shared_ptr<QgsSingleGeometryCheckError> > &errors );
void onGeometryCheckStarted( QgsVectorLayer *layer, QgsFeatureId fid );

private:
@@ -39,7 +39,7 @@ class QgsGeometryValidationModel : public QAbstractItemModel
{}

QgsFeatureId fid; // TODO INITIALIZE PROPERLY
QList<QgsGeometry::Error> errors;
QList<std::shared_ptr<QgsSingleGeometryCheckError>> errors;
};

int errorsForFeature( QgsVectorLayer *layer, QgsFeatureId fid );
@@ -17,19 +17,15 @@ email : matthias@opengis.ch
#include "qgsgeometryvalidationservice.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"

// TODO: Replace with registry
#include "qgsisvalidgeometrycheck.h"
#include "qgsgeometryoptions.h"
#include "qgsanalysis.h"
#include "qgsgeometrycheckregistry.h"
#include "qgsgeometrycheckfactory.h"

QgsGeometryValidationService::QgsGeometryValidationService( QgsProject *project )
: mProject( project )
{
connect( project, &QgsProject::layersAdded, this, &QgsGeometryValidationService::onLayersAdded );
// TODO: should not provide a nullptr context
mIsValidGeometryCheck = new QgsIsValidGeometryCheck( nullptr, QVariantMap() );
}

QgsGeometryValidationService::~QgsGeometryValidationService()
{
}

bool QgsGeometryValidationService::validationActive( QgsVectorLayer *layer, QgsFeatureId feature ) const
@@ -56,6 +52,8 @@ void QgsGeometryValidationService::onLayersAdded( const QList<QgsMapLayer *> &la
{
onFeatureDeleted( vectorLayer, fid );
} );

enableLayerChecks( vectorLayer );
}
}
}
@@ -78,6 +76,50 @@ void QgsGeometryValidationService::onFeatureDeleted( QgsVectorLayer *layer, QgsF
cancelChecks( layer, fid );
}

void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )
{
// TODO: finish all ongoing checks
qDeleteAll( mSingleFeatureChecks.value( layer ) );

// TODO: ownership and lifetime of the context!!
auto context = new QgsGeometryCheckContext( 1, mProject->crs(), mProject->transformContext() );
QList<QgsGeometryCheck *> layerChecks;

QgsGeometryCheckRegistry *checkRegistry = QgsAnalysis::instance()->geometryCheckRegistry();

const QStringList activeChecks = layer->geometryOptions()->geometryChecks();

const QList<QgsGeometryCheckFactory *> singleCheckFactories = checkRegistry->geometryCheckFactories( layer, QgsGeometryCheck::SingleGeometryCheck );

for ( QgsGeometryCheckFactory *factory : singleCheckFactories )
{
const QString checkId = factory->id();
if ( activeChecks.contains( checkId ) )
{
const QVariantMap checkConfiguration = layer->geometryOptions()->checkConfiguration( checkId );
layerChecks.append( factory->createGeometryCheck( context, checkConfiguration ) );
}
}

QList<QgsSingleGeometryCheck *> singleGeometryChecks;
for ( QgsGeometryCheck *check : qgis::as_const( layerChecks ) )
{
Q_ASSERT( dynamic_cast<QgsSingleGeometryCheck *>( check ) );
singleGeometryChecks.append( dynamic_cast<QgsSingleGeometryCheck *>( check ) );
}

mSingleFeatureChecks.insert( layer, singleGeometryChecks );

#if 0
const QList<QgsGeometryCheckFactory *> topologyCheckFactories = checkRegistry->geometryCheckFactories( layer, QgsGeometryCheck::SingleLayerTopologyCheck );

for ( const QString &check : activeChecks )
{
checkRegistry->geometryCheckFactories( layer, QgsGeometryCheck::SingleLayerTopologyCheck );
}
#endif
}

void QgsGeometryValidationService::cancelChecks( QgsVectorLayer *layer, QgsFeatureId fid )
{

@@ -88,7 +130,22 @@ void QgsGeometryValidationService::processFeature( QgsVectorLayer *layer, QgsFea
emit geometryCheckStarted( layer, fid );

QgsFeature feature = layer->getFeature( fid );

const auto &checks = mSingleFeatureChecks.value( layer );

// The errors are going to be sent out via a signal. We cannot keep ownership in here (or can we?)
// nor can we be sure that a single slot is connected to the signal. So make it a shared_ptr.
QList<std::shared_ptr<QgsSingleGeometryCheckError>> allErrors;
for ( QgsSingleGeometryCheck *check : checks )
{
const auto errors = check->processGeometry( feature.geometry() );

for ( auto error : errors )
allErrors.append( std::shared_ptr<QgsSingleGeometryCheckError>( error ) );
}

emit geometryCheckCompleted( layer, fid, allErrors );
// TODO: this is a bit hardcore
const auto errors = mIsValidGeometryCheck->processGeometry( feature.geometry() );
// const auto errors = mIsValidGeometryCheck->processGeometry( feature.geometry() );
// emit geometryCheckCompleted( layer, fid, errors );
}
@@ -24,8 +24,10 @@ email : matthias@opengis.ch
class QgsProject;
class QgsMapLayer;
class QgsVectorLayer;
// TODO: Should be retrieved from registry!!
class QgsIsValidGeometryCheck;
class QgsGeometryCheck;
class QgsSingleGeometryCheck;
class QgsSingleGeometryCheckError;



/**
@@ -52,7 +54,7 @@ class QgsGeometryValidationService : public QObject
typedef QList<FeatureError> FeatureErrors;

QgsGeometryValidationService( QgsProject *project );
~QgsGeometryValidationService();
~QgsGeometryValidationService() = default;

/**
* Returns if a validation is active for the specified \a feature on
@@ -62,7 +64,7 @@ class QgsGeometryValidationService : public QObject

signals:
void geometryCheckStarted( QgsVectorLayer *layer, QgsFeatureId fid );
void geometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<QgsGeometry::Error> &errors );
void geometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<std::shared_ptr<QgsSingleGeometryCheckError>> &errors );

private slots:
void onLayersAdded( const QList<QgsMapLayer *> &layers );
@@ -71,15 +73,15 @@ class QgsGeometryValidationService : public QObject
void onFeatureDeleted( QgsVectorLayer *layer, QgsFeatureId fid );

private:
void enableLayerChecks( QgsVectorLayer *layer );

void cancelChecks( QgsVectorLayer *layer, QgsFeatureId fid );

void processFeature( QgsVectorLayer *layer, QgsFeatureId fid );

QgsIsValidGeometryCheck *mIsValidGeometryCheck;

QgsProject *mProject;
QgsProject *mProject = nullptr;

QMap<QgsVectorLayer *, bool> mActiveChecks;
QHash<QgsVectorLayer *, QList< QgsSingleGeometryCheck * > > mSingleFeatureChecks;
};

#endif // QGSGEOMETRYVALIDATIONSERVICE_H

0 comments on commit dae3134

Please sign in to comment.
You can’t perform that action at this time.