Skip to content

Commit

Permalink
Put check registry into use
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 15, 2018
1 parent abda865 commit dae3134
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/app/qgsgeometryvalidationmodel.cpp
@@ -1,6 +1,7 @@
#include "qgsgeometryvalidationmodel.h"

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

#include <QIcon>

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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];

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsgeometryvalidationmodel.h
Expand Up @@ -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:
Expand All @@ -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 );
Expand Down
77 changes: 67 additions & 10 deletions src/app/qgsgeometryvalidationservice.cpp
Expand Up @@ -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
Expand All @@ -56,6 +52,8 @@ void QgsGeometryValidationService::onLayersAdded( const QList<QgsMapLayer *> &la
{
onFeatureDeleted( vectorLayer, fid );
} );

enableLayerChecks( vectorLayer );
}
}
}
Expand All @@ -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 )
{

Expand All @@ -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 );
}
18 changes: 10 additions & 8 deletions src/app/qgsgeometryvalidationservice.h
Expand Up @@ -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;



/**
Expand All @@ -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
Expand All @@ -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 );
Expand All @@ -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.