@@ -17,19 +17,15 @@ email : matthias@opengis.ch
17
17
#include " qgsgeometryvalidationservice.h"
18
18
#include " qgsproject.h"
19
19
#include " qgsvectorlayer.h"
20
-
21
- // TODO: Replace with registry
22
- #include " qgsisvalidgeometrycheck.h"
20
+ #include " qgsgeometryoptions.h"
21
+ #include " qgsanalysis.h"
22
+ #include " qgsgeometrycheckregistry.h"
23
+ #include " qgsgeometrycheckfactory.h"
23
24
24
25
QgsGeometryValidationService::QgsGeometryValidationService ( QgsProject *project )
26
+ : mProject( project )
25
27
{
26
28
connect ( project, &QgsProject::layersAdded, this , &QgsGeometryValidationService::onLayersAdded );
27
- // TODO: should not provide a nullptr context
28
- mIsValidGeometryCheck = new QgsIsValidGeometryCheck ( nullptr , QVariantMap () );
29
- }
30
-
31
- QgsGeometryValidationService::~QgsGeometryValidationService ()
32
- {
33
29
}
34
30
35
31
bool QgsGeometryValidationService::validationActive ( QgsVectorLayer *layer, QgsFeatureId feature ) const
@@ -56,6 +52,8 @@ void QgsGeometryValidationService::onLayersAdded( const QList<QgsMapLayer *> &la
56
52
{
57
53
onFeatureDeleted ( vectorLayer, fid );
58
54
} );
55
+
56
+ enableLayerChecks ( vectorLayer );
59
57
}
60
58
}
61
59
}
@@ -78,6 +76,50 @@ void QgsGeometryValidationService::onFeatureDeleted( QgsVectorLayer *layer, QgsF
78
76
cancelChecks ( layer, fid );
79
77
}
80
78
79
+ void QgsGeometryValidationService::enableLayerChecks ( QgsVectorLayer *layer )
80
+ {
81
+ // TODO: finish all ongoing checks
82
+ qDeleteAll ( mSingleFeatureChecks .value ( layer ) );
83
+
84
+ // TODO: ownership and lifetime of the context!!
85
+ auto context = new QgsGeometryCheckContext ( 1 , mProject ->crs (), mProject ->transformContext () );
86
+ QList<QgsGeometryCheck *> layerChecks;
87
+
88
+ QgsGeometryCheckRegistry *checkRegistry = QgsAnalysis::instance ()->geometryCheckRegistry ();
89
+
90
+ const QStringList activeChecks = layer->geometryOptions ()->geometryChecks ();
91
+
92
+ const QList<QgsGeometryCheckFactory *> singleCheckFactories = checkRegistry->geometryCheckFactories ( layer, QgsGeometryCheck::SingleGeometryCheck );
93
+
94
+ for ( QgsGeometryCheckFactory *factory : singleCheckFactories )
95
+ {
96
+ const QString checkId = factory->id ();
97
+ if ( activeChecks.contains ( checkId ) )
98
+ {
99
+ const QVariantMap checkConfiguration = layer->geometryOptions ()->checkConfiguration ( checkId );
100
+ layerChecks.append ( factory->createGeometryCheck ( context, checkConfiguration ) );
101
+ }
102
+ }
103
+
104
+ QList<QgsSingleGeometryCheck *> singleGeometryChecks;
105
+ for ( QgsGeometryCheck *check : qgis::as_const ( layerChecks ) )
106
+ {
107
+ Q_ASSERT ( dynamic_cast <QgsSingleGeometryCheck *>( check ) );
108
+ singleGeometryChecks.append ( dynamic_cast <QgsSingleGeometryCheck *>( check ) );
109
+ }
110
+
111
+ mSingleFeatureChecks .insert ( layer, singleGeometryChecks );
112
+
113
+ #if 0
114
+ const QList<QgsGeometryCheckFactory *> topologyCheckFactories = checkRegistry->geometryCheckFactories( layer, QgsGeometryCheck::SingleLayerTopologyCheck );
115
+
116
+ for ( const QString &check : activeChecks )
117
+ {
118
+ checkRegistry->geometryCheckFactories( layer, QgsGeometryCheck::SingleLayerTopologyCheck );
119
+ }
120
+ #endif
121
+ }
122
+
81
123
void QgsGeometryValidationService::cancelChecks ( QgsVectorLayer *layer, QgsFeatureId fid )
82
124
{
83
125
@@ -88,7 +130,22 @@ void QgsGeometryValidationService::processFeature( QgsVectorLayer *layer, QgsFea
88
130
emit geometryCheckStarted ( layer, fid );
89
131
90
132
QgsFeature feature = layer->getFeature ( fid );
133
+
134
+ const auto &checks = mSingleFeatureChecks .value ( layer );
135
+
136
+ // The errors are going to be sent out via a signal. We cannot keep ownership in here (or can we?)
137
+ // nor can we be sure that a single slot is connected to the signal. So make it a shared_ptr.
138
+ QList<std::shared_ptr<QgsSingleGeometryCheckError>> allErrors;
139
+ for ( QgsSingleGeometryCheck *check : checks )
140
+ {
141
+ const auto errors = check->processGeometry ( feature.geometry () );
142
+
143
+ for ( auto error : errors )
144
+ allErrors.append ( std::shared_ptr<QgsSingleGeometryCheckError>( error ) );
145
+ }
146
+
147
+ emit geometryCheckCompleted ( layer, fid, allErrors );
91
148
// TODO: this is a bit hardcore
92
- const auto errors = mIsValidGeometryCheck ->processGeometry ( feature.geometry () );
149
+ // const auto errors = mIsValidGeometryCheck->processGeometry( feature.geometry() );
93
150
// emit geometryCheckCompleted( layer, fid, errors );
94
151
}
0 commit comments