QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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"
19 
21  : mModified( false )
22 {
23  L = layer;
24 }
25 
27 {
28  return mModified;
29 }
30 
32 {
33 
34  QgsFeatureList fl;
35  fl << f;
36  if ( L->dataProvider()->addFeatures( fl ) )
37  {
38  f = fl.first();
39  emit featureAdded( f.id() );
40  mModified = true;
41  return true;
42  }
43  return false;
44 }
45 
47 {
48  if ( L->dataProvider()->addFeatures( features ) )
49  {
50  Q_FOREACH ( const QgsFeature& f, features )
51  {
52  emit featureAdded( f.id() );
53  }
54  mModified = true;
55  return true;
56  }
57  return false;
58 }
59 
61 {
62  if ( L->dataProvider()->deleteFeatures( QgsFeatureIds() << fid ) )
63  {
64  emit featureDeleted( fid );
65  mModified = true;
66  return true;
67  }
68  return false;
69 }
70 
72 {
73  if ( L->dataProvider()->deleteFeatures( fids ) )
74  {
75  Q_FOREACH ( QgsFeatureId fid, fids )
76  emit featureDeleted( fid );
77 
78  mModified = true;
79  return true;
80  }
81  return false;
82 }
83 
85 {
86  QgsGeometryMap geomMap;
87  geomMap.insert( fid, *geom );
88  if ( L->dataProvider()->changeGeometryValues( geomMap ) )
89  {
90  emit geometryChanged( fid, *geom );
91  mModified = true;
92  return true;
93  }
94  return false;
95 }
96 
97 bool QgsVectorLayerEditPassthrough::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &/*oldValue*/ )
98 {
99  QgsAttributeMap map;
100  map.insert( field, newValue );
101  QgsChangedAttributesMap attribMap;
102  attribMap.insert( fid, map );
103  if ( L->dataProvider()->changeAttributeValues( attribMap ) )
104  {
105  emit attributeValueChanged( fid, field, newValue );
106  mModified = true;
107  return true;
108  }
109  return false;
110 }
111 
113 {
114  Q_UNUSED( oldValues );
115  bool result = false;
116 
117  QgsChangedAttributesMap attribMap;
118  attribMap.insert( fid, newValues );
119 
120  if ( L->dataProvider()->changeAttributeValues( attribMap ) )
121  {
122  result = true;
123  QgsAttributeMap::const_iterator it;
124  for ( it = newValues.constBegin(); it != newValues.constEnd(); ++it )
125  {
126  emit attributeValueChanged( fid, it.key(), it.value() );
127  }
128  }
129 
130  return result;
131 }
132 
134 {
135  if ( L->dataProvider()->addAttributes( QList<QgsField>() << field ) )
136  {
137  emit attributeAdded( L->dataProvider()->fieldNameIndex( field.name() ) );
138  mModified = true;
139  return true;
140  }
141  return false;
142 }
143 
145 {
146  if ( L->dataProvider()->deleteAttributes( QgsAttributeIds() << attr ) )
147  {
148  mModified = true;
149  L->editFormConfig()->removeWidgetConfig( attr );
150  emit attributeDeleted( attr );
151  mModified = true;
152  return true;
153  }
154  return false;
155 }
156 
158 {
159  QgsFieldNameMap map;
160  map[ attr ] = newName;
161  if ( L->dataProvider()->renameAttributes( map ) )
162  {
163  mModified = true;
164  emit attributeRenamed( attr, newName );
165  return true;
166  }
167  return false;
168 }
169 
171 {
172  mModified = false;
173  return true;
174 }
175 
177 {
178  mModified = false;
179 }
QString name
Definition: qgsfield.h:52
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes.
bool removeWidgetConfig(int fieldIdx)
Remove the configuration for the editor widget used to represent the field at the given index...
bool deleteAttribute(int attr) override
Delete an attribute field (but does not commit it)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:439
void attributeDeleted(int idx)
bool addFeature(QgsFeature &f) override
Adds a feature.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features.
const_iterator constBegin() const
virtual bool renameAttributes(const QgsFieldNameMap &renamedAttributes)
Renames existing attributes.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant()) override
Changed an attribute value (but does not commit it)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
virtual bool addFeatures(QgsFeatureList &flist)
Adds a list of features.
bool addFeatures(QgsFeatureList &features) override
Insert a copy of the given features into the layer (but does not commit it)
QgsEditFormConfig * editFormConfig() const
Get the configuration of the form used to represent this vector layer.
void featureAdded(QgsFeatureId fid)
QSet< int > QgsAttributeIds
void attributeAdded(int idx)
void geometryChanged(QgsFeatureId fid, QgsGeometry &geom)
bool deleteFeatures(const QgsFeatureIds &fids) override
Deletes a set of features from the layer (but does not commit it)
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
QgsVectorLayer * layer()
Returns the layer associated with the undo command.
const_iterator constEnd() const
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
void rollBack() override
Stop editing and discard the edits.
bool addAttribute(const QgsField &field) override
Add an attribute field (but does not commit it) returns true if the field was added.
T & first()
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:44
virtual bool changeGeometryValues(const QgsGeometryMap &geometry_map)
Changes geometries of existing features.
void attributeRenamed(int idx, const QString &newName)
Emitted when an attribute has been renamed.
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
bool isModified() const override
Returns true if the provider has been modified since the last commit.
bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues) override
Changes values of attributes (but does not commit it).
QgsVectorLayerEditPassthrough(QgsVectorLayer *layer)
bool deleteFeature(QgsFeatureId fid) override
Delete a feature from the layer (but does not commit it)
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &)
bool changeGeometry(QgsFeatureId fid, QgsGeometry *geom) override
Change feature&#39;s geometry.
virtual bool deleteAttributes(const QgsAttributeIds &attributes)
Deletes existing attributes.
qint64 QgsFeatureId
Definition: qgsfeature.h:31
iterator insert(const Key &key, const T &value)
bool renameAttribute(int attr, const QString &newName) override
Renames an attribute field (but does not commit it)
QgsVectorDataProvider * dataProvider()
Returns the data provider.
Represents a vector layer which manages a vector based data sets.
void featureDeleted(QgsFeatureId fid)
bool commitChanges(QStringList &commitErrors) override
Attempts to commit any changes to disk.