QGIS API Documentation  3.6.0-Noosa (5873452)
qgsvectorlayerfeaturepool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayerfeaturepool.h
3  --------------------------------------
4 Date : 18.9.2018
5 Copyright : (C) 2018 by Matthias Kuhn
6 email : [email protected]
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgsthreadingutils.h"
18 
19 #include "qgsfeaturerequest.h"
20 #include "qgsvectorlayer.h"
21 
23  : QObject()
24  , QgsFeaturePool( layer )
25 {
26  connect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayerFeaturePool::onFeatureDeleted );
27  connect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayerFeaturePool::onGeometryChanged );
28 }
29 
31 {
32  Q_UNUSED( flags );
33 
34  bool res = false;
35 
36  auto addFeatureSynchronized = [ this, &feature, &res ]()
37  {
38  QgsVectorLayer *lyr = layer();
39  if ( lyr )
40  res = lyr->addFeature( feature );
41  };
42 
43  QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
44 
45  if ( !res )
46  return false;
47 
48 #if 0
49  if ( mSelectedOnly )
50  {
51  QgsThreadingUtils::runOnMainThread( [ this, feature ]()
52  {
53  QgsVectorLayer *lyr = layer();
54  if ( lyr )
55  {
56  QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
57  selectedFeatureIds.insert( feature.id() );
58  lyr->selectByIds( selectedFeatureIds );
59  }
60  } );
61  }
62 #endif
63  insertFeature( feature );
64 
65  return res;
66 }
67 
68 bool QgsVectorLayerFeaturePool::addFeatures( QgsFeatureList &features, QgsFeatureSink::Flags flags )
69 {
70  Q_UNUSED( flags );
71 
72  bool res = false;
73 
74  auto addFeatureSynchronized = [ this, &features, &res ]()
75  {
76  QgsVectorLayer *lyr = layer();
77  if ( lyr )
78  res = lyr->addFeatures( features );
79  };
80 
81  QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
82 
83  if ( !res )
84  return false;
85 
86 #if 0
87  if ( mSelectedOnly )
88  {
89  QgsThreadingUtils::runOnMainThread( [ this, features ]()
90  {
91  QgsVectorLayer *lyr = layer();
92  if ( lyr )
93  {
94  QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
95  for ( const QgsFeature &feature : qgis::as_const( features ) )
96  selectedFeatureIds.insert( feature.id() );
97  lyr->selectByIds( selectedFeatureIds );
98  }
99  } );
100  }
101 #endif
102 
103  for ( const QgsFeature &feature : qgis::as_const( features ) )
104  insertFeature( feature );
105 
106  return res;
107 }
108 
110 {
111  QgsThreadingUtils::runOnMainThread( [this, &feature]()
112  {
113  QgsVectorLayer *lyr = layer();
114  if ( lyr )
115  {
116  lyr->updateFeature( feature );
117  }
118  } );
119 
120  refreshCache( feature );
121 }
122 
124 {
125  removeFeature( fid );
127  {
128  QgsVectorLayer *lyr = layer();
129  if ( lyr )
130  {
131  lyr->deleteFeatures( QgsFeatureIds() << fid );
132  }
133  } );
134 }
135 
136 void QgsVectorLayerFeaturePool::onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geometry )
137 {
138  Q_UNUSED( geometry )
139 
140  if ( isFeatureCached( fid ) )
141  {
142  QgsFeature feature;
143  getFeature( fid, feature );
144  refreshCache( feature );
145  }
146 }
147 
148 void QgsVectorLayerFeaturePool::onFeatureDeleted( QgsFeatureId fid )
149 {
150  deleteFeature( fid );
151 }
bool isFeatureCached(QgsFeatureId fid)
Checks if the feature fid is cached.
QgsFeatureId id
Definition: qgsfeature.h:64
void deleteFeature(QgsFeatureId fid) override
Removes a feature from this pool.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
bool getFeature(QgsFeatureId id, QgsFeature &feature, QgsFeedback *feedback=nullptr)
Retrieves the feature with the specified id into feature.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:571
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
bool deleteFeatures(const QgsFeatureIds &fids)
Deletes a set of features from the layer (but does not commit it)
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
void selectByIds(const QgsFeatureIds &ids, SelectBehavior behavior=SetSelection)
Select matching features using a list of feature IDs.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
bool updateFeature(QgsFeature &feature, bool skipDefaultValues=false)
Updates an existing feature in the layer, replacing the attributes and geometry for the feature with ...
QgsVectorLayer * layer() const
Gets a pointer to the underlying layer.
void insertFeature(const QgsFeature &feature)
Inserts a feature into the cache and the spatial index.
void updateFeature(QgsFeature &feature) override
Updates a feature in this pool.
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
void refreshCache(const QgsFeature &feature)
Changes a feature in the cache and the spatial index.
QgsVectorLayerFeaturePool(QgsVectorLayer *layer)
Creates a new feature pool for layer.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) FINAL
Adds a list of features to the sink.
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Is emitted whenever a geometry change is done in the edit buffer.
static bool runOnMainThread(const Func &func, QgsFeedback *feedback=nullptr)
Guarantees that func is executed on the main thread.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=nullptr) override
Adds a list of features to the sink.
A feature pool is based on a vector layer and caches features.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a single feature to the sink.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) FINAL
Adds a single feature to the sink.
Represents a vector layer which manages a vector based data sets.
void removeFeature(const QgsFeatureId featureId)
Removes a feature from the cache and the spatial index.