QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsvectorlayerfeaturepool.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectorlayerfeaturepool.h
3 --------------------------------------
4Date : 18.9.2018
5Copyright : (C) 2018 by Matthias Kuhn
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 "moc_qgsvectorlayerfeaturepool.cpp"
18#include "qgsthreadingutils.h"
19
20#include "qgsfeaturerequest.h"
21#include "qgsvectorlayer.h"
22
24 : QObject()
25 , QgsFeaturePool( layer )
26{
27 connect( layer, &QgsVectorLayer::featureDeleted, this, &QgsVectorLayerFeaturePool::onFeatureDeleted );
28 connect( layer, &QgsVectorLayer::geometryChanged, this, &QgsVectorLayerFeaturePool::onGeometryChanged );
29}
30
32{
33 Q_UNUSED( flags )
34
35 bool res = false;
36
37 auto addFeatureSynchronized = [ this, &feature, &res ]()
38 {
39 QgsVectorLayer *lyr = layer();
40 if ( lyr )
41 res = lyr->addFeature( feature );
42 };
43
44 QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
45
46 if ( !res )
47 return false;
48
49#if 0
50 if ( mSelectedOnly )
51 {
52 QgsThreadingUtils::runOnMainThread( [ this, feature ]()
53 {
54 QgsVectorLayer *lyr = layer();
55 if ( lyr )
56 {
57 QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
58 selectedFeatureIds.insert( feature.id() );
59 lyr->selectByIds( selectedFeatureIds );
60 }
61 } );
62 }
63#endif
64 insertFeature( feature );
65
66 return res;
67}
68
70{
71 Q_UNUSED( flags )
72
73 bool res = false;
74
75 auto addFeatureSynchronized = [ this, &features, &res ]()
76 {
77 QgsVectorLayer *lyr = layer();
78 if ( lyr )
79 res = lyr->addFeatures( features );
80 };
81
82 QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
83
84 if ( !res )
85 return false;
86
87#if 0
88 if ( mSelectedOnly )
89 {
90 QgsThreadingUtils::runOnMainThread( [ this, features ]()
91 {
92 QgsVectorLayer *lyr = layer();
93 if ( lyr )
94 {
95 QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
96 for ( const QgsFeature &feature : std::as_const( features ) )
97 selectedFeatureIds.insert( feature.id() );
98 lyr->selectByIds( selectedFeatureIds );
99 }
100 } );
101 }
102#endif
103
104 for ( const QgsFeature &feature : std::as_const( features ) )
105 insertFeature( feature );
106
107 return res;
108}
109
111{
112 QgsThreadingUtils::runOnMainThread( [this, &feature]()
113 {
114 QgsVectorLayer *lyr = layer();
115 if ( lyr )
116 {
117 lyr->updateFeature( feature );
118 }
119 } );
120
121 refreshCache( feature );
122}
123
125{
126 removeFeature( fid );
128 {
129 QgsVectorLayer *lyr = layer();
130 if ( lyr )
131 {
132 lyr->deleteFeatures( QgsFeatureIds() << fid );
133 }
134 } );
135}
136
137void QgsVectorLayerFeaturePool::onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geometry )
138{
139 Q_UNUSED( geometry )
140
141 if ( isFeatureCached( fid ) )
142 {
143 QgsFeature feature;
144 getFeature( fid, feature );
145 refreshCache( feature );
146 }
147}
148
149void QgsVectorLayerFeaturePool::onFeatureDeleted( QgsFeatureId fid )
150{
151 deleteFeature( fid );
152}
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.
QFlags< Flag > Flags
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
QgsFeatureId id
Definition qgsfeature.h:66
A geometry is the spatial representation of a feature.
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.
Q_INVOKABLE bool deleteFeatures(const QgsFeatureIds &fids, QgsVectorLayer::DeleteContext *context=nullptr)
Deletes a set of features from the layer (but does not commit it)
bool addFeatures(QgsFeatureList &features, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) FINAL
Adds a list of features to the sink.
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 ...
Q_INVOKABLE void selectByIds(const QgsFeatureIds &ids, Qgis::SelectBehavior behavior=Qgis::SelectBehavior::SetSelection)
Selects matching features using a list of feature IDs.
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.
void geometryChanged(QgsFeatureId fid, const QgsGeometry &geometry)
Emitted whenever a geometry change is done in the edit buffer.
QList< QgsFeature > QgsFeatureList
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features