QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsvectorlayereditpassthrough.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayereditpassthrough.cpp
3  ---------------------
4  begin : Jan 12 2015
5  copyright : (C) 2015 by Sandro Mani
6  email : manisandro 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 
17 #include "qgsvectorlayer.h"
18 #include "qgsvectordataprovider.h"
20 #include "qgstransaction.h"
21 
23  : mModified( false )
24 {
25  L = layer;
26 }
27 
29 {
30  return mModified;
31 }
32 
33 bool QgsVectorLayerEditPassthrough::modify( QgsVectorLayerUndoPassthroughCommand *cmd )
34 {
35  L->undoStack()->push( cmd ); // push takes ownership -> no need for cmd to be a smart ptr
36  if ( cmd->hasError() )
37  return false;
38 
39  if ( !mModified )
40  {
41  mModified = true;
42  emit layerModified();
43  }
44 
45  return true;
46 }
47 
49 {
51  if ( !modify( cmd ) ) // modify takes owneship -> no need for cmd to be a smart ptr
52  return false;
53 
54  const QgsFeatureList features = cmd->features();
55  f = features.at( features.count() - 1 );
56  return true;
57 }
58 
60 {
62  if ( !modify( cmd ) ) // modify takes owneship -> no need for cmd to be a smart ptr
63  return false;
64 
65  features = cmd->features();
66  return true;
67 }
68 
70 {
71  return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, QgsFeatureIds() << fid ) );
72 }
73 
75 {
76  return modify( new QgsVectorLayerUndoPassthroughCommandDeleteFeatures( this, fids ) );
77 }
78 
80 {
81  return modify( new QgsVectorLayerUndoPassthroughCommandChangeGeometry( this, fid, geom ) );
82 }
83 
84 bool QgsVectorLayerEditPassthrough::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &/*oldValue*/ )
85 {
86  return modify( new QgsVectorLayerUndoPassthroughCommandChangeAttribute( this, fid, field, newValue ) );
87 }
88 
90 {
91  return modify( new QgsVectorLayerUndoPassthroughCommandChangeAttributes( this, fid, newValues, oldValues ) );
92 }
93 
95 {
96  return modify( new QgsVectorLayerUndoPassthroughCommandAddAttribute( this, field ) );
97 }
98 
100 {
101  return modify( new QgsVectorLayerUndoPassthroughCommandDeleteAttribute( this, attr ) );
102 }
103 
104 bool QgsVectorLayerEditPassthrough::renameAttribute( int attr, const QString &newName )
105 {
106  return modify( new QgsVectorLayerUndoPassthroughCommandRenameAttribute( this, attr, newName ) );
107 }
108 
109 bool QgsVectorLayerEditPassthrough::commitChanges( QStringList & /*commitErrors*/ )
110 {
111  mModified = false;
112  return true;
113 }
114 
116 {
117  mModified = false;
118 }
119 
120 bool QgsVectorLayerEditPassthrough::update( QgsTransaction *tr, const QString &sql, const QString &name )
121 {
122  return modify( new QgsVectorLayerUndoPassthroughCommandUpdate( this, tr, sql, name ) );
123 }
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandRenameAttribute
friend class QgsVectorLayerUndoPassthroughCommandRenameAttribute
Definition: qgsvectorlayereditbuffer.h:303
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandDeleteAttribute
friend class QgsVectorLayerUndoPassthroughCommandDeleteAttribute
Definition: qgsvectorlayereditbuffer.h:302
QgsVectorLayerEditPassthrough::deleteAttribute
bool deleteAttribute(int attr) override
Deletes an attribute field (but does not commit it)
Definition: qgsvectorlayereditpassthrough.cpp:99
QgsVectorLayerEditPassthrough::changeAttributeValue
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant()) override
Changed an attribute value (but does not commit it)
Definition: qgsvectorlayereditpassthrough.cpp:84
QgsVectorLayerEditPassthrough::addFeature
bool addFeature(QgsFeature &f) override
Adds a feature.
Definition: qgsvectorlayereditpassthrough.cpp:48
QgsVectorLayerEditPassthrough::addFeatures
bool addFeatures(QgsFeatureList &features) override
Insert a copy of the given features into the layer (but does not commit it)
Definition: qgsvectorlayereditpassthrough.cpp:59
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandChangeGeometry
friend class QgsVectorLayerUndoPassthroughCommandChangeGeometry
Definition: qgsvectorlayereditbuffer.h:298
QgsVectorLayerUndoPassthroughCommandAddFeatures::features
QgsFeatureList features() const
List of features (added feaures can be modified by default values from database)
Definition: qgsvectorlayerundopassthroughcommand.h:117
QgsTransaction
This class allows including a set of layers in a database-side transaction, provided the layer data p...
Definition: qgstransaction.h:56
QgsVectorLayerUndoPassthroughCommand
Undo command for vector layer in transaction group mode.
Definition: qgsvectorlayerundopassthroughcommand.h:33
field
const QgsField & field
Definition: qgsfield.h:463
QgsVectorLayerEditBuffer::layerModified
void layerModified()
Emitted when modifications has been done on layer.
QgsVectorLayerUndoPassthroughCommand::hasError
bool hasError() const
Returns error status.
Definition: qgsvectorlayerundopassthroughcommand.h:48
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandChangeAttribute
friend class QgsVectorLayerUndoPassthroughCommandChangeAttribute
Definition: qgsvectorlayereditbuffer.h:299
qgsvectorlayerundopassthroughcommand.h
QgsVectorLayerEditPassthrough::addAttribute
bool addAttribute(const QgsField &field) override
Adds an attribute field (but does not commit it) returns true if the field was added.
Definition: qgsvectorlayereditpassthrough.cpp:94
QgsVectorLayerEditPassthrough::deleteFeatures
bool deleteFeatures(const QgsFeatureIds &fids) override
Deletes a set of features from the layer (but does not commit it)
Definition: qgsvectorlayereditpassthrough.cpp:74
QgsVectorLayerUndoPassthroughCommandAddFeatures
Undo command for adding a feature to a vector layer in transaction group mode.
Definition: qgsvectorlayerundopassthroughcommand.h:100
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandAddAttribute
friend class QgsVectorLayerUndoPassthroughCommandAddAttribute
Definition: qgsvectorlayereditbuffer.h:301
QgsVectorLayerEditPassthrough::QgsVectorLayerEditPassthrough
QgsVectorLayerEditPassthrough(QgsVectorLayer *layer)
Definition: qgsvectorlayereditpassthrough.cpp:22
qgsvectordataprovider.h
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:882
QgsAttributeMap
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:38
QgsVectorLayerEditPassthrough::update
bool update(QgsTransaction *transaction, const QString &sql, const QString &name)
Update underlying data with a SQL query embedded in a transaction.
Definition: qgsvectorlayereditpassthrough.cpp:120
QgsVectorLayerEditPassthrough::rollBack
void rollBack() override
Stop editing and discard the edits.
Definition: qgsvectorlayereditpassthrough.cpp:115
QgsVectorLayerEditPassthrough::changeAttributeValues
bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues) override
Changes values of attributes (but does not commit it).
Definition: qgsvectorlayereditpassthrough.cpp:89
QgsVectorLayerEditBuffer::L
QgsVectorLayer * L
Definition: qgsvectorlayereditbuffer.h:284
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandUpdate
friend class QgsVectorLayerUndoPassthroughCommandUpdate
Definition: qgsvectorlayereditbuffer.h:304
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
qgsvectorlayer.h
qgstransaction.h
QgsVectorLayerEditPassthrough::renameAttribute
bool renameAttribute(int attr, const QString &newName) override
Renames an attribute field (but does not commit it)
Definition: qgsvectorlayereditpassthrough.cpp:104
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsVectorLayerEditPassthrough::isModified
bool isModified() const override
Returns true if the provider has been modified since the last commit.
Definition: qgsvectorlayereditpassthrough.cpp:28
QgsMapLayer::undoStack
QUndoStack * undoStack()
Returns pointer to layer's undo stack.
Definition: qgsmaplayer.cpp:1961
QgsVectorLayerEditPassthrough::deleteFeature
bool deleteFeature(QgsFeatureId fid) override
Delete a feature from the layer (but does not commit it)
Definition: qgsvectorlayereditpassthrough.cpp:69
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandAddFeatures
friend class QgsVectorLayerUndoPassthroughCommandAddFeatures
Definition: qgsvectorlayereditbuffer.h:296
QgsVectorLayerEditPassthrough::changeGeometry
bool changeGeometry(QgsFeatureId fid, const QgsGeometry &geom) override
Change feature's geometry.
Definition: qgsvectorlayereditpassthrough.cpp:79
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandChangeAttributes
friend class QgsVectorLayerUndoPassthroughCommandChangeAttributes
Definition: qgsvectorlayereditbuffer.h:300
QgsVectorLayerEditBuffer::QgsVectorLayerUndoPassthroughCommandDeleteFeatures
friend class QgsVectorLayerUndoPassthroughCommandDeleteFeatures
Definition: qgsvectorlayereditbuffer.h:297
QgsVectorLayerEditPassthrough::commitChanges
bool commitChanges(QStringList &commitErrors) override
Attempts to commit any changes to disk.
Definition: qgsvectorlayereditpassthrough.cpp:109
qgsvectorlayereditpassthrough.h
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