Skip to content

Commit e41680a

Browse files
committedSep 6, 2018
Implement QgsFeatureSink in QgsFeaturePool
1 parent d361d9b commit e41680a

File tree

3 files changed

+58
-14
lines changed

3 files changed

+58
-14
lines changed
 

‎src/analysis/vector/geometry_checker/qgsfeaturepool.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121

2222
#include <QCache>
2323
#include <QMutex>
24+
#include <QPointer>
25+
2426
#include "qgis_analysis.h"
2527
#include "qgsfeature.h"
2628
#include "qgsspatialindex.h"
27-
#include "qgsvectorlayer.h"
29+
#include "qgsfeaturesink.h"
2830

2931
class QgsVectorLayer;
3032

3133
/**
3234
* \ingroup analysis
3335
* A feature pool is based on a vector layer and caches features.
3436
*/
35-
class ANALYSIS_EXPORT QgsFeaturePool
37+
class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink
3638
{
3739

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

49-
/**
50-
* Adds a feature to this pool.
51-
* Implementations will add the feature to the layer or to the data provider.
52-
*/
53-
virtual void addFeature( QgsFeature &feature ) = 0;
54-
5551
/**
5652
* Updates a feature in this pool.
5753
* Implementations will update the feature on the layer or on the data provider.

‎src/analysis/vector/geometry_checker/qgsvectordataproviderfeaturepool.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,26 @@ QgsVectorDataProviderFeaturePool::QgsVectorDataProviderFeaturePool( QgsVectorLay
6262
setFeatureIds( featureIds );
6363
}
6464

65-
void QgsVectorDataProviderFeaturePool::addFeature( QgsFeature &feature )
65+
bool QgsVectorDataProviderFeaturePool::addFeature( QgsFeature &feature, Flags flags )
6666
{
67+
Q_UNUSED( flags );
6768
QgsFeatureList features;
6869
features.append( feature );
6970

70-
auto addFeatureSynchronized = [ this, &features ]()
71+
bool res = false;
72+
73+
auto addFeatureSynchronized = [ this, &features, &res ]()
7174
{
7275
QgsVectorLayer *lyr = layer();
7376
if ( lyr )
74-
lyr->dataProvider()->addFeatures( features );
77+
res = lyr->dataProvider()->addFeatures( features );
7578
};
7679

7780
runOnMainThread( addFeatureSynchronized );
7881

82+
if ( !res )
83+
return false;
84+
7985
feature.setId( features.front().id() );
8086
if ( mSelectedOnly )
8187
{
@@ -92,6 +98,47 @@ void QgsVectorDataProviderFeaturePool::addFeature( QgsFeature &feature )
9298
}
9399

94100
insertFeature( feature );
101+
102+
return res;
103+
}
104+
105+
bool QgsVectorDataProviderFeaturePool::addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags )
106+
{
107+
Q_UNUSED( flags );
108+
109+
bool res = false;
110+
111+
auto addFeatureSynchronized = [ this, &features, &res ]()
112+
{
113+
QgsVectorLayer *lyr = layer();
114+
if ( lyr )
115+
res = lyr->dataProvider()->addFeatures( features );
116+
};
117+
118+
runOnMainThread( addFeatureSynchronized );
119+
120+
if ( !res )
121+
return false;
122+
123+
if ( mSelectedOnly )
124+
{
125+
runOnMainThread( [ this, features ]()
126+
{
127+
QgsVectorLayer *lyr = layer();
128+
if ( lyr )
129+
{
130+
QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
131+
for ( const QgsFeature &feature : qgis::as_const( features ) )
132+
selectedFeatureIds.insert( feature.id() );
133+
lyr->selectByIds( selectedFeatureIds );
134+
}
135+
} );
136+
}
137+
138+
for ( const QgsFeature &feature : qgis::as_const( features ) )
139+
insertFeature( feature );
140+
141+
return res;
95142
}
96143

97144
void QgsVectorDataProviderFeaturePool::updateFeature( QgsFeature &feature )

‎src/analysis/vector/geometry_checker/qgsvectordataproviderfeaturepool.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ class ANALYSIS_EXPORT QgsVectorDataProviderFeaturePool : public QgsFeaturePool
3232
public:
3333
QgsVectorDataProviderFeaturePool( QgsVectorLayer *layer, double layerToMapUnits, const QgsCoordinateTransform &layerToMapTransform, bool selectedOnly = false );
3434

35-
void addFeature( QgsFeature &feature );
36-
void updateFeature( QgsFeature &feature );
37-
void deleteFeature( QgsFeatureId fid );
35+
bool addFeature( QgsFeature &feature, QgsFeatureSink::Flags flags = nullptr ) override;
36+
bool addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags = nullptr ) override;
37+
void updateFeature( QgsFeature &feature ) override;
38+
void deleteFeature( QgsFeatureId fid ) override;
3839

3940
private:
4041
bool mSelectedOnly = false;

0 commit comments

Comments
 (0)
Please sign in to comment.