QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvectorlayereditbuffer.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayereditbuffer.h
3  ---------------------
4  begin : Dezember 2012
5  copyright : (C) 2012 by Martin Dobias
6  email : wonder dot sk at gmail dot com
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 #ifndef QGSVECTORLAYEREDITBUFFER_H
16 #define QGSVECTORLAYEREDITBUFFER_H
17 
18 #include "qgis_core.h"
19 #include <QList>
20 #include <QSet>
21 
22 #include "qgsfeature.h"
23 #include "qgsfields.h"
24 #include "qgsgeometry.h"
25 
26 class QgsVectorLayer;
28 
29 typedef QList<int> QgsAttributeList SIP_SKIP;
30 typedef QSet<int> QgsAttributeIds SIP_SKIP;
31 typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap;
32 
37 class CORE_EXPORT QgsVectorLayerEditBuffer : public QObject
38 {
39  Q_OBJECT
40  public:
42 
44  virtual bool isModified() const;
45 
51  virtual bool addFeature( QgsFeature &f );
52 
54  virtual bool addFeatures( QgsFeatureList &features );
55 
57  virtual bool deleteFeature( QgsFeatureId fid );
58 
60  virtual bool deleteFeatures( const QgsFeatureIds &fid );
61 
63  virtual bool changeGeometry( QgsFeatureId fid, const QgsGeometry &geom );
64 
66  virtual bool changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue = QVariant() );
67 
73  virtual bool changeAttributeValues( QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues );
74 
79  virtual bool addAttribute( const QgsField &field );
80 
82  virtual bool deleteAttribute( int attr );
83 
90  virtual bool renameAttribute( int attr, const QString &newName );
91 
107  virtual bool commitChanges( QStringList &commitErrors );
108 
110  virtual void rollBack();
111 
116  QgsFeatureMap addedFeatures() const { return mAddedFeatures; }
117 
124  QgsFeatureIds allAddedOrEditedFeatures() const;
125 
132  bool isFeatureAdded( QgsFeatureId id ) const { return mAddedFeatures.contains( id ); }
133 
138  QgsChangedAttributesMap changedAttributeValues() const { return mChangedAttributeValues; }
139 
146  bool isFeatureAttributesChanged( QgsFeatureId id ) const { return mChangedAttributeValues.contains( id ); }
147 
152  QgsAttributeList deletedAttributeIds() const { return mDeletedAttributeIds; }
153 
160  bool isAttributeDeleted( int index ) const { return mDeletedAttributeIds.contains( index ); }
161 
165  QList<QgsField> addedAttributes() const { return mAddedAttributes; }
166 
171  QgsGeometryMap changedGeometries() const { return mChangedGeometries; }
172 
179  bool isFeatureGeometryChanged( QgsFeatureId id ) const { return mChangedGeometries.contains( id ); }
180 
185  QgsFeatureIds deletedFeatureIds() const { return mDeletedFeatureIds; }
186 
193  bool isFeatureDeleted( QgsFeatureId id ) const { return mDeletedFeatureIds.contains( id ); }
194 
200  void updateFields( QgsFields &fields ) SIP_SKIP;
201 
206  QgsVectorLayerEditBufferGroup *editBufferGroup() const;
207 
212  void setEditBufferGroup( QgsVectorLayerEditBufferGroup *editBufferGroup );
213 
214  //QString dumpEditBuffer();
215 
216  protected slots:
217  void undoIndexChanged( int index );
218 
219  signals:
221  void layerModified();
222 
223  void featureAdded( QgsFeatureId fid );
224  void featureDeleted( QgsFeatureId fid );
225 
231  void geometryChanged( QgsFeatureId fid, const QgsGeometry &geom );
232 
233  void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant & );
234  void attributeAdded( int idx );
235  void attributeDeleted( int idx );
236 
243  void attributeRenamed( int idx, const QString &newName );
244 
246  void committedAttributesDeleted( const QString &layerId, const QgsAttributeList &deletedAttributes );
247  void committedAttributesAdded( const QString &layerId, const QList<QgsField> &addedAttributes );
248 
255  void committedAttributesRenamed( const QString &layerId, const QgsFieldNameMap &renamedAttributes );
256  void committedFeaturesAdded( const QString &layerId, const QgsFeatureList &addedFeatures );
257  void committedFeaturesRemoved( const QString &layerId, const QgsFeatureIds &deletedFeatureIds );
258  void committedAttributeValuesChanges( const QString &layerId, const QgsChangedAttributesMap &changedAttributesValues );
259  void committedGeometriesChanges( const QString &layerId, const QgsGeometryMap &changedGeometries );
260 
261  protected:
262 
264  QgsVectorLayerEditBuffer() = default;
265 
267  void updateFeatureGeometry( QgsFeature &f );
268 
270  void updateChangedAttributes( QgsFeature &f );
271 
273  void handleAttributeAdded( int index );
274 
276  void handleAttributeDeleted( int index );
277 
279  void updateAttributeMapIndex( QgsAttributeMap &attrs, int index, int offset ) const;
280 
281  void updateLayerFields();
282 
283  protected:
284  QgsVectorLayer *L = nullptr;
285 
294 
305 
312 
315 
318 
321 
323  QList<QgsField> mAddedAttributes;
324 
327 
330 
331  QgsVectorLayerEditBufferGroup *mEditBufferGroup = nullptr;
332 
333  friend class QgsGrassProvider; //GRASS provider totally abuses the edit buffer
334 
335  private:
336 
338 
344  bool commitChangesCheckGeometryTypeCompatibility( QStringList &commitErrors );
345 
352  bool commitChangesDeleteAttributes( bool &attributesDeleted, QStringList &commitErrors );
353 
360  bool commitChangesRenameAttributes( bool &attributesRenamed, QStringList &commitErrors );
361 
368  bool commitChangesAddAttributes( bool &attributesAdded, QStringList &commitErrors );
369 
376  bool commitChangesCheckAttributesModifications( const QgsFields oldFields, QStringList &commitErrors );
377 
384  bool commitChangesChangeAttributes( bool &attributesChanged, QStringList &commitErrors );
385 
392  bool commitChangesDeleteFeatures( bool &featuresDeleted, QStringList &commitErrors );
393 
400  bool commitChangesAddFeatures( bool &featuresAdded, QStringList &commitErrors );
401 };
402 
403 #endif // QGSVECTORLAYEREDITBUFFER_H
qgsfields.h
QgsVectorLayerEditBuffer::isFeatureAttributesChanged
bool isFeatureAttributesChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had an attribute changed but not committed.
Definition: qgsvectorlayereditbuffer.h:146
QgsAttributeIds
QSet< int > QgsAttributeIds
Definition: qgsvectordataprovider.h:39
QgsVectorLayerUndoCommandRenameAttribute
Undo command for renaming an existing attribute of a vector layer.
Definition: qgsvectorlayerundocommand.h:239
QgsVectorLayerEditBuffer::mAddedAttributes
QList< QgsField > mAddedAttributes
Added attributes fields which are not committed.
Definition: qgsvectorlayereditbuffer.h:323
QgsVectorLayerUndoCommandAddAttribute
Undo command for adding a new attribute to a vector layer.
Definition: qgsvectorlayerundocommand.h:180
QgsVectorLayerEditBuffer::mDeletedFeatureIds
QgsFeatureIds mDeletedFeatureIds
Deleted feature IDs which are not committed.
Definition: qgsvectorlayereditbuffer.h:311
QgsVectorLayerUndoPassthroughCommandChangeAttributes
Undo command for changing attributes' values from a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:222
QgsVectorLayerEditBufferGroup
The edit buffer group manages a group of edit buffers.
Definition: qgsvectorlayereditbuffergroup.h:38
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsVectorLayerEditBuffer::mDeletedAttributeIds
QgsAttributeList mDeletedAttributeIds
Deleted attributes fields which are not committed. The list is kept sorted.
Definition: qgsvectorlayereditbuffer.h:320
qgsfeature.h
QgsVectorLayerEditBuffer::isFeatureDeleted
bool isFeatureDeleted(QgsFeatureId id) const
Returns true if the specified feature ID has been deleted but not committed.
Definition: qgsvectorlayereditbuffer.h:193
QgsVectorLayerUndoCommandAddFeature
Undo command for adding a feature to a vector layer.
Definition: qgsvectorlayerundocommand.h:70
QgsVectorLayerUndoPassthroughCommandChangeGeometry
Undo command for changing feature geometry from a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:159
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:868
QgsVectorLayerEditBuffer::isAttributeDeleted
bool isAttributeDeleted(int index) const
Returns true if the specified attribute has been deleted but not committed.
Definition: qgsvectorlayereditbuffer.h:160
QgsVectorLayerUndoPassthroughCommand
Undo command for vector layer in transaction group mode.
Definition: qgsvectorlayerundopassthroughcommand.h:33
field
const QgsField & field
Definition: qgsfield.h:463
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
QgsVectorLayerUndoPassthroughCommandAddAttribute
Undo command for adding attri to a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:252
QgsVectorLayerEditBuffer::isFeatureGeometryChanged
bool isFeatureGeometryChanged(QgsFeatureId id) const
Returns true if the specified feature ID has had its geometry changed but not committed.
Definition: qgsvectorlayereditbuffer.h:179
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsVectorLayerEditBuffer::mAddedFeatures
QgsFeatureMap mAddedFeatures
New features which are not committed.
Definition: qgsvectorlayereditbuffer.h:314
QgsVectorLayerUndoPassthroughCommandAddFeatures
Undo command for adding a feature to a vector layer in transaction group mode.
Definition: qgsvectorlayerundopassthroughcommand.h:100
QgsVectorLayerEditBuffer::deletedAttributeIds
QgsAttributeList deletedAttributeIds() const
Returns a list of deleted attributes fields which are not committed.
Definition: qgsvectorlayereditbuffer.h:152
QgsVectorLayerEditBuffer::changedAttributeValues
QgsChangedAttributesMap changedAttributeValues() const
Returns a map of features with changed attributes values which are not committed.
Definition: qgsvectorlayereditbuffer.h:138
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsAttributeMap
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:38
QgsVectorLayerUndoPassthroughCommandDeleteFeatures
Undo command for deleting features from a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:132
QgsVectorLayerUndoCommandChangeGeometry
Undo command for modifying the geometry of a feature from a vector layer.
Definition: qgsvectorlayerundocommand.h:120
QgsVectorLayerUndoPassthroughCommandRenameAttribute
Undo command for deleting attri of a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:303
QgsFeatureMap
QMap< QgsFeatureId, QgsFeature > QgsFeatureMap
Definition: qgsvectorlayereditbuffer.h:31
QgsVectorLayerUndoCommandDeleteAttribute
Undo command for removing an existing attribute from a vector layer.
Definition: qgsvectorlayerundocommand.h:205
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsVectorLayerEditBuffer::deletedFeatureIds
QgsFeatureIds deletedFeatureIds() const
Returns a list of deleted feature IDs which are not committed.
Definition: qgsvectorlayereditbuffer.h:185
QgsVectorLayerUndoCommand
Base class for undo commands within a QgsVectorLayerEditBuffer.
Definition: qgsvectorlayerundocommand.h:40
QgsVectorLayerEditBuffer::isFeatureAdded
bool isFeatureAdded(QgsFeatureId id) const
Returns true if the specified feature ID has been added but not committed.
Definition: qgsvectorlayereditbuffer.h:132
QgsVectorLayerEditBuffer::mRenamedAttributes
QgsFieldNameMap mRenamedAttributes
Renamed attributes which are not committed.
Definition: qgsvectorlayereditbuffer.h:326
qgsgeometry.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayerUndoCommandDeleteFeature
Undo command for deleting a feature from a vector layer.
Definition: qgsvectorlayerundocommand.h:95
QgsVectorLayerEditBuffer
Definition: qgsvectorlayereditbuffer.h:37
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorLayerUndoPassthroughCommandUpdate
Undo command for running a specific sql query in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:331
QgsVectorLayerEditBuffer::changedGeometries
QgsGeometryMap changedGeometries() const
Returns a map of features with changed geometries which are not committed.
Definition: qgsvectorlayereditbuffer.h:171
QgsFieldNameMap
QMap< int, QString > QgsFieldNameMap
Definition: qgsattributes.h:44
QgsGeometryMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:877
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayerEditBuffer::mChangedGeometries
QgsGeometryMap mChangedGeometries
Changed geometries which are not committed.
Definition: qgsvectorlayereditbuffer.h:329
QgsVectorLayerUndoPassthroughCommandDeleteAttribute
Undo command for deleting attri of a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:277
QgsVectorLayerEditBuffer::addedAttributes
QList< QgsField > addedAttributes() const
Returns a list of added attributes fields which are not committed.
Definition: qgsvectorlayereditbuffer.h:165
QgsVectorLayerUndoCommandChangeAttribute
Undo command for modifying an attribute of a feature from a vector layer.
Definition: qgsvectorlayerundocommand.h:150
QgsVectorLayerUndoPassthroughCommandChangeAttribute
Undo command for changing attr value from a vector layer in transaction group.
Definition: qgsvectorlayerundopassthroughcommand.h:191
QgsVectorLayerEditBuffer::addedFeatures
QgsFeatureMap addedFeatures() const
Returns a map of new features which are not committed.
Definition: qgsvectorlayereditbuffer.h:116
QgsVectorLayerEditBuffer::mChangedAttributeValues
QgsChangedAttributesMap mChangedAttributeValues
Changed attributes values which are not committed.
Definition: qgsvectorlayereditbuffer.h:317
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50