QGIS API Documentation 3.41.0-Master (af5edcb665c)
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 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
69{
70 Q_UNUSED( flags )
71
72 bool res = false;
73
74 auto addFeatureSynchronized = [this, &features, &res]() {
75 QgsVectorLayer *lyr = layer();
76 if ( lyr )
77 res = lyr->addFeatures( features );
78 };
79
80 QgsThreadingUtils::runOnMainThread( addFeatureSynchronized );
81
82 if ( !res )
83 return false;
84
85#if 0
86 if ( mSelectedOnly )
87 {
88 QgsThreadingUtils::runOnMainThread( [ this, features ]()
89 {
90 QgsVectorLayer *lyr = layer();
91 if ( lyr )
92 {
93 QgsFeatureIds selectedFeatureIds = lyr->selectedFeatureIds();
94 for ( const QgsFeature &feature : std::as_const( features ) )
95 selectedFeatureIds.insert( feature.id() );
96 lyr->selectByIds( selectedFeatureIds );
97 }
98 } );
99 }
100#endif
101
102 for ( const QgsFeature &feature : std::as_const( features ) )
103 insertFeature( feature );
104
105 return res;
106}
107
109{
110 QgsFeature origFeature;
111 getFeature( feature.id(), origFeature );
112
113 QgsThreadingUtils::runOnMainThread( [this, &feature]() {
114 QgsVectorLayer *lyr = layer();
115 if ( lyr )
116 {
117 lyr->updateFeature( feature );
118 }
119 } );
120
121 refreshCache( feature, origFeature );
122}
123
125{
126 removeFeature( fid );
128 QgsVectorLayer *lyr = layer();
129 if ( lyr )
130 {
131 lyr->deleteFeatures( QgsFeatureIds() << fid );
132 }
133 } );
134}
135
136void QgsVectorLayerFeaturePool::onGeometryChanged( QgsFeatureId fid, const QgsGeometry &geometry )
137{
138 QgsFeature feature;
139 QgsFeature origFeature;
140 getFeature( fid, origFeature );
141 feature = origFeature;
142 feature.setGeometry( geometry );
143 refreshCache( feature, origFeature );
144}
145
146void QgsVectorLayerFeaturePool::onFeatureDeleted( QgsFeatureId fid )
147{
148 deleteFeature( fid );
149}
A feature pool is based on a vector layer and caches features.
void refreshCache(QgsFeature feature, const QgsFeature &origFeature)
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.
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
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
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