Skip to content
Permalink
Browse files

Implement QgsFeatureSink in QgsFeaturePool

  • Loading branch information
m-kuhn committed Sep 6, 2018
1 parent d361d9b commit e41680a9f55e240649823f8b93453cc82ee569ab
@@ -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:
@@ -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.
@@ -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 )
{
@@ -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 )
@@ -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;

0 comments on commit e41680a

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