Skip to content

Commit

Permalink
Handle layer deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Oct 15, 2018
1 parent d0d08cc commit 4d40d2f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/app/qgsgeometryvalidationservice.cpp
Expand Up @@ -81,6 +81,12 @@ void QgsGeometryValidationService::onLayersAdded( const QList<QgsMapLayer *> &la
enableLayerChecks( vectorLayer );
}, Qt::UniqueConnection );

connect( vectorLayer, &QgsVectorLayer::destroyed, this, [vectorLayer, this]()
{
cleanupLayerChecks( vectorLayer );
mLayerChecks.remove( vectorLayer );
});

enableLayerChecks( vectorLayer );
}
}
Expand All @@ -100,8 +106,8 @@ void QgsGeometryValidationService::onGeometryChanged( QgsVectorLayer *layer, Qgs
Q_UNUSED( geometry )
// It would be nice to use the geometry here for the tests.
// But:
// 1. other codepaths to the checks also have no geometry (feature added / feature deleted)
// 2. and looking it up from the edit buffer (in memory) is really fast.
// 1. other codepaths to the checks also have no geometry (feature added / feature deleted)
// 2. and looking it up from the edit buffer (in memory) is really fast.
// so in short: it's still a good idea, but not as important as on first thought.

if ( !mLayerChecks[layer].topologyChecks.empty() )
Expand Down Expand Up @@ -135,9 +141,9 @@ void QgsGeometryValidationService::onBeforeCommitChanges( QgsVectorLayer *layer
}
}

void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )
void QgsGeometryValidationService::cleanupLayerChecks(QgsVectorLayer* layer)
{
if ( layer->geometryOptions()->geometryChecks().empty() && !mLayerChecks.contains( layer ) )
if ( !mLayerChecks.contains( layer ) )
return;

VectorLayerCheckInformation &checkInformation = mLayerChecks[layer];
Expand All @@ -146,7 +152,17 @@ void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )

qDeleteAll( checkInformation.singleFeatureChecks );
qDeleteAll( checkInformation.topologyChecks );
delete checkInformation.context;
checkInformation.context.reset();
}

void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )
{
if ( layer->geometryOptions()->geometryChecks().empty() && !mLayerChecks.contains( layer ) )
return;

VectorLayerCheckInformation &checkInformation = mLayerChecks[layer];

cleanupLayerChecks( layer );

if ( layer->geometryOptions()->geometryChecks().empty() )
{
Expand Down Expand Up @@ -174,7 +190,7 @@ void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )
if ( activeChecks.contains( checkId ) )
{
const QVariantMap checkConfiguration = layer->geometryOptions()->checkConfiguration( checkId );
layerChecks.append( factory->createGeometryCheck( checkInformation.context, checkConfiguration ) );
layerChecks.append( factory->createGeometryCheck( checkInformation.context.get(), checkConfiguration ) );
}
}

Expand All @@ -197,7 +213,7 @@ void QgsGeometryValidationService::enableLayerChecks( QgsVectorLayer *layer )
if ( activeChecks.contains( checkId ) )
{
const QVariantMap checkConfiguration = layer->geometryOptions()->checkConfiguration( checkId );
topologyChecks.append( factory->createGeometryCheck( checkInformation.context, checkConfiguration ) );
topologyChecks.append( factory->createGeometryCheck( checkInformation.context.get(), checkConfiguration ) );
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/app/qgsgeometryvalidationservice.h
Expand Up @@ -22,6 +22,7 @@ email : matthias@opengis.ch
#include <QReadWriteLock>

#include "qgsfeature.h"
#include "qgsgeometrycheckcontext.h"

class QgsProject;
class QgsMapLayer;
Expand All @@ -32,7 +33,6 @@ class QgsSingleGeometryCheckError;
class QgsGeometryCheckError;
class QgsFeedback;
class QgsFeaturePool;
struct QgsGeometryCheckContext;

/**
* This service connects to all layers in a project and triggers validation
Expand Down Expand Up @@ -69,6 +69,7 @@ class QgsGeometryValidationService : public QObject
void geometryCheckCompleted( QgsVectorLayer *layer, QgsFeatureId fid, const QList<std::shared_ptr<QgsSingleGeometryCheckError>> &errors );
void topologyChecksUpdated( QgsVectorLayer *layer, const QList<std::shared_ptr<QgsGeometryCheckError> > &errors );
void topologyChecksCleared( QgsVectorLayer *layer );
void topologyErrorUpdated( QgsVectorLayer *layer, QgsGeometryCheckError *error );

void warning( const QString &message );

Expand All @@ -80,6 +81,7 @@ class QgsGeometryValidationService : public QObject
void onBeforeCommitChanges( QgsVectorLayer *layer );

private:
void cleanupLayerChecks( QgsVectorLayer *layer );
void enableLayerChecks( QgsVectorLayer *layer );

void cancelTopologyCheck( QgsVectorLayer *layer );
Expand All @@ -98,7 +100,7 @@ class QgsGeometryValidationService : public QObject
QList<QgsFeedback *> topologyCheckFeedbacks; // will be deleted when topologyCheckFutureWatcher is delteed
QList<std::shared_ptr<QgsGeometryCheckError>> topologyCheckErrors;
QList<QMetaObject::Connection> connections;
QgsGeometryCheckContext *context = nullptr;
std::shared_ptr<QgsGeometryCheckContext> context;
};

QReadWriteLock mTopologyCheckLock;
Expand Down

0 comments on commit 4d40d2f

Please sign in to comment.