QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 }
A feature pool is based on a vector layer and caches features.
void refreshCache(const QgsFeature &feature)
Changes a feature in the cache and the spatial index.
void insertFeature(const QgsFeature &feature, bool skipLock=false)
Inserts a feature into the cache and the spatial index.
bool isFeatureCached(QgsFeatureId fid)
Checks if the feature fid is cached.
void removeFeature(const QgsFeatureId featureId)
Removes a feature from the cache and the spatial index.
QgsVectorLayer * layer() const
Gets a pointer to the underlying layer.
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:56
Q_GADGET QgsFeatureId id
Definition: qgsfeature.h:64
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
static bool runOnMainThread(const Func &func, QgsFeedback *feedback=nullptr)
Guarantees that func is executed on the main thread.
void deleteFeature(QgsFeatureId fid) override
Removes a feature from this pool.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
QgsVectorLayerFeaturePool(QgsVectorLayer *layer)
Creates a new feature pool for layer.
void updateFeature(QgsFeature &feature) override
Updates a feature in this pool.
Represents a vector layer which manages a vector based data sets.
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a list of features to the sink.
bool deleteFeatures(const QgsFeatureIds &fids, DeleteContext *context=nullptr)
Deletes a set of features from the layer (but does not commit it)
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
bool updateFeature(QgsFeature &feature, bool skipDefaultValues=false)
Updates an existing feature in the layer, replacing the attributes and geometry for the feature with ...
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, QgsVectorLayer::SelectBehavior behavior=QgsVectorLayer::SetSelection)
Selects matching features using a list of feature IDs.
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:614
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28