QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 : std::as_const( features ) )
96  selectedFeatureIds.insert( feature.id() );
97  lyr->selectByIds( selectedFeatureIds );
98  }
99  } );
100  }
101 #endif
102 
103  for ( const QgsFeature &feature : std::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 }
QgsVectorLayer::addFeatures
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a list of features to the sink.
Definition: qgsvectorlayer.cpp:3658
QgsVectorLayerFeaturePool::QgsVectorLayerFeaturePool
QgsVectorLayerFeaturePool(QgsVectorLayer *layer)
Creates a new feature pool for layer.
Definition: qgsvectorlayerfeaturepool.cpp:22
QgsVectorLayer::selectByIds
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
Definition: qgsvectorlayer.cpp:572
QgsVectorLayerFeaturePool::addFeatures
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
Definition: qgsvectorlayerfeaturepool.cpp:68
qgsfeaturerequest.h
QgsVectorLayer::featureDeleted
void featureDeleted(QgsFeatureId fid)
Emitted when a feature has been deleted.
QgsFeaturePool::getFeature
bool getFeature(QgsFeatureId id, QgsFeature &feature)
Retrieves the feature with the specified id into feature.
Definition: qgsfeaturepool.cpp:41
QgsVectorLayerFeaturePool::updateFeature
void updateFeature(QgsFeature &feature) override
Updates a feature in this pool.
Definition: qgsvectorlayerfeaturepool.cpp:109
QgsFeature::id
QgsFeatureId id
Definition: qgsfeature.h:68
QgsFeaturePool::isFeatureCached
bool isFeatureCached(QgsFeatureId fid)
Checks if the feature fid is cached.
Definition: qgsfeaturepool.cpp:160
QgsVectorLayer::selectedFeatureIds
const Q_INVOKABLE QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
Definition: qgsvectorlayer.cpp:3621
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsFeaturePool::removeFeature
void removeFeature(const QgsFeatureId featureId)
Removes a feature from the cache and the spatial index.
Definition: qgsfeaturepool.cpp:142
QgsVectorLayerFeaturePool::addFeature
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a single feature to the sink.
Definition: qgsvectorlayerfeaturepool.cpp:30
qgsvectorlayer.h
QgsFeaturePool::layer
QgsVectorLayer * layer() const
Gets a pointer to the underlying layer.
Definition: qgsfeaturepool.cpp:109
qgsthreadingutils.h
QgsVectorLayer::deleteFeatures
bool deleteFeatures(const QgsFeatureIds &fids, DeleteContext *context=nullptr)
Deletes a set of features from the layer (but does not commit it)
Definition: qgsvectorlayer.cpp:3420
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsThreadingUtils::runOnMainThread
static bool runOnMainThread(const Func &func, QgsFeedback *feedback=nullptr)
Guarantees that func is executed on the main thread.
Definition: qgsthreadingutils.h:56
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorLayer::geometryChanged
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayer::updateFeature
bool updateFeature(QgsFeature &feature, bool skipDefaultValues=false)
Updates an existing feature in the layer, replacing the attributes and geometry for the feature with ...
Definition: qgsvectorlayer.cpp:1096
QgsVectorLayerFeaturePool::deleteFeature
void deleteFeature(QgsFeatureId fid) override
Removes a feature from this pool.
Definition: qgsvectorlayerfeaturepool.cpp:123
QgsVectorLayer::addFeature
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a single feature to the sink.
Definition: qgsvectorlayer.cpp:1070
qgsvectorlayerfeaturepool.h
QgsFeaturePool
A feature pool is based on a vector layer and caches features.
Definition: qgsfeaturepool.h:37
QgsFeaturePool::refreshCache
void refreshCache(const QgsFeature &feature)
Changes a feature in the cache and the spatial index.
Definition: qgsfeaturepool.cpp:131
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsFeaturePool::insertFeature
void insertFeature(const QgsFeature &feature, bool skipLock=false)
Inserts a feature into the cache and the spatial index.
Definition: qgsfeaturepool.cpp:121