Skip to content

Commit

Permalink
Implement QgsFeatureSink in QgsFeaturePool
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 6, 2018
1 parent d361d9b commit e41680a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
12 changes: 4 additions & 8 deletions src/analysis/vector/geometry_checker/qgsfeaturepool.h
Expand Up @@ -21,18 +21,20 @@

#include <QCache>
#include <QMutex>
#include <QPointer>

#include "qgis_analysis.h"
#include "qgsfeature.h"
#include "qgsspatialindex.h"
#include "qgsvectorlayer.h"
#include "qgsfeaturesink.h"

class QgsVectorLayer;

/**
* \ingroup analysis
* A feature pool is based on a vector layer and caches features.
*/
class ANALYSIS_EXPORT QgsFeaturePool
class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink
{

public:
Expand All @@ -46,12 +48,6 @@ class ANALYSIS_EXPORT QgsFeaturePool
*/
bool get( QgsFeatureId id, QgsFeature &feature );

/**
* Adds a feature to this pool.
* Implementations will add the feature to the layer or to the data provider.
*/
virtual void addFeature( QgsFeature &feature ) = 0;

/**
* Updates a feature in this pool.
* Implementations will update the feature on the layer or on the data provider.
Expand Down
Expand Up @@ -62,20 +62,26 @@ QgsVectorDataProviderFeaturePool::QgsVectorDataProviderFeaturePool( QgsVectorLay
setFeatureIds( featureIds );
}

void QgsVectorDataProviderFeaturePool::addFeature( QgsFeature &feature )
bool QgsVectorDataProviderFeaturePool::addFeature( QgsFeature &feature, Flags flags )
{
Q_UNUSED( flags );
QgsFeatureList features;
features.append( feature );

auto addFeatureSynchronized = [ this, &features ]()
bool res = false;

auto addFeatureSynchronized = [ this, &features, &res ]()
{
QgsVectorLayer *lyr = layer();
if ( lyr )
lyr->dataProvider()->addFeatures( features );
res = lyr->dataProvider()->addFeatures( features );
};

runOnMainThread( addFeatureSynchronized );

if ( !res )
return false;

feature.setId( features.front().id() );
if ( mSelectedOnly )
{
Expand All @@ -92,6 +98,47 @@ void QgsVectorDataProviderFeaturePool::addFeature( QgsFeature &feature )
}

insertFeature( feature );

return res;
}

bool QgsVectorDataProviderFeaturePool::addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags )
{
Q_UNUSED( flags );

bool res = false;

auto addFeatureSynchronized = [ this, &features, &res ]()
{
QgsVectorLayer *lyr = layer();
if ( lyr )
res = lyr->dataProvider()->addFeatures( features );
};

runOnMainThread( addFeatureSynchronized );

if ( !res )
return false;

if ( mSelectedOnly )
{
runOnMainThread( [ this, features ]()
{
QgsVectorLayer *lyr = layer();
if ( lyr )
{
QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
for ( const QgsFeature &feature : qgis::as_const( features ) )
selectedFeatureIds.insert( feature.id() );
lyr->selectByIds( selectedFeatureIds );
}
} );
}

for ( const QgsFeature &feature : qgis::as_const( features ) )
insertFeature( feature );

return res;
}

void QgsVectorDataProviderFeaturePool::updateFeature( QgsFeature &feature )
Expand Down
Expand Up @@ -32,9 +32,10 @@ class ANALYSIS_EXPORT QgsVectorDataProviderFeaturePool : public QgsFeaturePool
public:
QgsVectorDataProviderFeaturePool( QgsVectorLayer *layer, double layerToMapUnits, const QgsCoordinateTransform &layerToMapTransform, bool selectedOnly = false );

void addFeature( QgsFeature &feature );
void updateFeature( QgsFeature &feature );
void deleteFeature( QgsFeatureId fid );
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
void updateFeature( QgsFeature &feature ) override;
void deleteFeature( QgsFeatureId fid ) override;

private:
bool mSelectedOnly = false;
Expand Down

0 comments on commit e41680a

Please sign in to comment.