QGIS API Documentation  2.14.0-Essen
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  if ( L->dataProvider()->addAttributes( QList<QgsField>() << field ) )
115  {
116  emit attributeAdded( L->dataProvider()->fieldNameIndex( field.name() ) );
117  mModified = true;
118  return true;
119  }
120  return false;
121 }
122 
124 {
125  if ( L->dataProvider()->deleteAttributes( QgsAttributeIds() << attr ) )
126  {
127  mModified = true;
128  L->editFormConfig()->removeWidgetConfig( attr );
129  emit attributeDeleted( attr );
130  mModified = true;
131  return true;
132  }
133  return false;
134 }
135 
137 {
138  mModified = false;
139  return true;
140 }
141 
143 {
144  mModified = false;
145 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
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:433
void attributeDeleted(int idx)
bool addFeature(QgsFeature &f) override
Adds a feature.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features.
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
int fieldNameIndex(const QString &fieldName) const
Returns the index of a field name or -1 if the field does not exist.
QgsEditFormConfig * editFormConfig() const
Get the configuration of the form used to represent this vector layer.
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)
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)
QString name() const
Gets the name of the field.
Definition: qgsfield.cpp:84
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
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.
bool isModified() const override
Returns true if the provider has been modified since the last commit.
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)
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.