QGIS API Documentation  2.99.0-Master (bdf46d7)
qgsvectorlayereditbuffer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsvectorlayereditbuffer.cpp
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  ***************************************************************************/
16 
17 #include "qgsgeometry.h"
18 #include "qgslogger.h"
20 #include "qgsvectordataprovider.h"
21 #include "qgsvectorlayer.h"
22 
24 template <class Key, class T> void mapToReversedLists( const QMap< Key, T > &map, QList<Key> &ks, QList<T> &vs )
25 {
26  ks.reserve( map.size() );
27  vs.reserve( map.size() );
28  typename QMap<Key, T>::const_iterator i = map.constEnd();
29  while ( i-- != map.constBegin() )
30  {
31  ks.append( i.key() );
32  vs.append( i.value() );
33  }
34 }
35 
36 
38  : L( layer )
39 {
40  connect( L->undoStack(), &QUndoStack::indexChanged, this, &QgsVectorLayerEditBuffer::undoIndexChanged ); // TODO[MD]: queued?
41 }
42 
44 {
45  return !L->undoStack()->isClean();
46 }
47 
48 
50 {
51  QgsDebugMsgLevel( QString( "undo index changed %1" ).arg( index ), 4 );
52  Q_UNUSED( index );
53  emit layerModified();
54 }
55 
56 
58 {
59  // delete attributes from the higher indices to lower indices
60  for ( int i = mDeletedAttributeIds.count() - 1; i >= 0; --i )
61  {
62  fields.remove( mDeletedAttributeIds.at( i ) );
63  }
64  // add new fields
65  for ( int i = 0; i < mAddedAttributes.count(); ++i )
66  {
67  fields.append( mAddedAttributes.at( i ), QgsFields::OriginEdit, i );
68  }
69  // rename fields
70  QgsFieldNameMap::const_iterator renameIt = mRenamedAttributes.constBegin();
71  for ( ; renameIt != mRenamedAttributes.constEnd(); ++renameIt )
72  {
73  fields[ renameIt.key()].setName( renameIt.value() );
74  }
75 }
76 
77 
79 {
80  if ( mChangedGeometries.contains( f.id() ) )
82 }
83 
84 
86 {
87  QgsAttributes attrs = f.attributes();
88 
89  // remove all attributes that will disappear - from higher indices to lower
90  for ( int idx = mDeletedAttributeIds.count() - 1; idx >= 0; --idx )
91  {
92  attrs.remove( mDeletedAttributeIds[idx] );
93  }
94 
95  // adjust size to accommodate added attributes
96  attrs.resize( attrs.count() + mAddedAttributes.count() );
97 
98  // update changed attributes
99  if ( mChangedAttributeValues.contains( f.id() ) )
100  {
101  const QgsAttributeMap &map = mChangedAttributeValues[f.id()];
102  for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
103  attrs[it.key()] = it.value();
104  }
105 
106  f.setAttributes( attrs );
107 }
108 
109 
110 
111 
113 {
115  {
116  return false;
117  }
118  if ( L->mFields.count() != f.attributes().count() )
119  return false;
120 
121  // TODO: check correct geometry type
122 
123  L->undoStack()->push( new QgsVectorLayerUndoCommandAddFeature( this, f ) );
124  return true;
125 }
126 
127 
129 {
131  return false;
132 
133  bool result = true;
134  for ( QgsFeatureList::iterator iter = features.begin(); iter != features.end(); ++iter )
135  {
136  result = result && addFeature( *iter );
137  }
138 
139  L->updateExtents();
140  return result;
141 }
142 
143 
144 
146 {
148  {
149  QgsDebugMsg( "Cannot delete features (missing DeleteFeature capability)" );
150  return false;
151  }
152 
153  if ( FID_IS_NEW( fid ) )
154  {
155  if ( !mAddedFeatures.contains( fid ) )
156  {
157  QgsDebugMsg( "Cannot delete features (in the list of added features)" );
158  return false;
159  }
160  }
161  else // existing feature
162  {
163  if ( mDeletedFeatureIds.contains( fid ) )
164  {
165  QgsDebugMsg( "Cannot delete features (in the list of deleted features)" );
166  return false;
167  }
168  }
169 
170  L->undoStack()->push( new QgsVectorLayerUndoCommandDeleteFeature( this, fid ) );
171  return true;
172 }
173 
175 {
177  {
178  QgsDebugMsg( "Cannot delete features (missing DeleteFeatures capability)" );
179  return false;
180  }
181 
182  bool ok = true;
183  Q_FOREACH ( QgsFeatureId fid, fids )
184  ok = deleteFeature( fid ) && ok;
185 
186  return ok;
187 }
188 
189 
191 {
192  if ( !L->hasGeometryType() )
193  {
194  return false;
195  }
196 
197  if ( FID_IS_NEW( fid ) )
198  {
199  if ( !mAddedFeatures.contains( fid ) )
200  return false;
201  }
203  return false;
204 
205  // TODO: check compatible geometry
206 
207  L->undoStack()->push( new QgsVectorLayerUndoCommandChangeGeometry( this, fid, geom ) );
208  return true;
209 }
210 
211 
212 bool QgsVectorLayerEditBuffer::changeAttributeValue( QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue )
213 {
214  if ( FID_IS_NEW( fid ) )
215  {
216  if ( !mAddedFeatures.contains( fid ) )
217  return false;
218  }
220  {
221  return false;
222  }
223 
224  if ( field < 0 || field >= L->fields().count() ||
225  L->fields().fieldOrigin( field ) == QgsFields::OriginJoin ||
227  return false;
228 
229  L->undoStack()->push( new QgsVectorLayerUndoCommandChangeAttribute( this, fid, field, newValue, oldValue ) );
230  return true;
231 }
232 
233 
235 {
237  return false;
238 
239  if ( field.name().isEmpty() )
240  return false;
241 
242  Q_FOREACH ( const QgsField &updatedField, L->fields() )
243  {
244  if ( updatedField.name() == field.name() )
245  return false;
246  }
247 
248  if ( !L->dataProvider()->supportedType( field ) )
249  return false;
250 
251  L->undoStack()->push( new QgsVectorLayerUndoCommandAddAttribute( this, field ) );
252  return true;
253 }
254 
255 
257 {
259  return false;
260 
261  if ( index < 0 || index >= L->fields().count() )
262  return false;
263 
264  // find out source of the field
265  QgsFields::FieldOrigin origin = L->fields().fieldOrigin( index );
266  int originIndex = L->fields().fieldOriginIndex( index );
267 
268  if ( origin == QgsFields::OriginProvider && mDeletedAttributeIds.contains( originIndex ) )
269  return false;
270 
271  if ( origin == QgsFields::OriginJoin )
272  return false;
273 
274  L->undoStack()->push( new QgsVectorLayerUndoCommandDeleteAttribute( this, index ) );
275  return true;
276 }
277 
278 bool QgsVectorLayerEditBuffer::renameAttribute( int index, const QString &newName )
279 {
281  return false;
282 
283  if ( newName.isEmpty() )
284  return false;
285 
286  if ( index < 0 || index >= L->fields().count() )
287  return false;
288 
289  Q_FOREACH ( const QgsField &updatedField, L->fields() )
290  {
291  if ( updatedField.name() == newName )
292  return false;
293  }
294 
295  L->undoStack()->push( new QgsVectorLayerUndoCommandRenameAttribute( this, index, newName ) );
296  return true;
297 }
298 
299 
300 bool QgsVectorLayerEditBuffer::commitChanges( QStringList &commitErrors )
301 {
302  QgsVectorDataProvider *provider = L->dataProvider();
303  commitErrors.clear();
304 
305  int cap = provider->capabilities();
306  bool success = true;
307 
308  // geometry updates attribute updates
309  // yes no => changeGeometryValues
310  // no yes => changeAttributeValues
311  // yes yes => changeFeatures
312 
313  //
314  // update geometries
315  //
316  if ( !mChangedGeometries.isEmpty() && ( ( cap & QgsVectorDataProvider::ChangeFeatures ) == 0 || mChangedAttributeValues.isEmpty() ) )
317  {
318  if ( provider->changeGeometryValues( mChangedGeometries ) )
319  {
320  commitErrors << tr( "SUCCESS: %n geometries were changed.", "changed geometries count", mChangedGeometries.size() );
321 
323  mChangedGeometries.clear();
324  }
325  else
326  {
327  commitErrors << tr( "ERROR: %n geometries not changed.", "not changed geometries count", mChangedGeometries.size() );
328  success = false;
329  }
330  }
331 
332  QgsFields oldFields = L->fields();
333 
334  //
335  // delete attributes
336  //
337  bool attributesChanged = false;
338  if ( !mDeletedAttributeIds.isEmpty() )
339  {
340  if ( ( cap & QgsVectorDataProvider::DeleteAttributes ) && provider->deleteAttributes( mDeletedAttributeIds.toSet() ) )
341  {
342  commitErrors << tr( "SUCCESS: %n attribute(s) deleted.", "deleted attributes count", mDeletedAttributeIds.size() );
343 
345 
346  mDeletedAttributeIds.clear();
347  attributesChanged = true;
348  }
349  else
350  {
351  commitErrors << tr( "ERROR: %n attribute(s) not deleted.", "not deleted attributes count", mDeletedAttributeIds.size() );
352 #if 0
353  QString list = "ERROR: Pending attribute deletes:";
354  Q_FOREACH ( int idx, mDeletedAttributeIds )
355  {
356  list.append( ' ' + L->pendingFields().at( idx ).name() );
357  }
358  commitErrors << list;
359 #endif
360  success = false;
361  }
362  }
363 
364  //
365  // add attributes
366  //
367  if ( !mAddedAttributes.isEmpty() )
368  {
370  {
371  commitErrors << tr( "SUCCESS: %n attribute(s) added.", "added attributes count", mAddedAttributes.size() );
372 
374 
375  mAddedAttributes.clear();
376  attributesChanged = true;
377  }
378  else
379  {
380  commitErrors << tr( "ERROR: %n new attribute(s) not added", "not added attributes count", mAddedAttributes.size() );
381 #if 0
382  QString list = "ERROR: Pending adds:";
383  Q_FOREACH ( QgsField f, mAddedAttributes )
384  {
385  list.append( ' ' + f.name() );
386  }
387  commitErrors << list;
388 #endif
389  success = false;
390  }
391  }
392 
393  // rename attributes
394  if ( !mRenamedAttributes.isEmpty() )
395  {
397  {
398  commitErrors << tr( "SUCCESS: %n attribute(s) renamed.", "renamed attributes count", mRenamedAttributes.size() );
399 
401 
402  mRenamedAttributes.clear();
403  attributesChanged = true;
404  }
405  else
406  {
407  commitErrors << tr( "ERROR: %n attribute(s) not renamed", "not renamed attributes count", mRenamedAttributes.size() );
408  success = false;
409  }
410  }
411 
412  //
413  // check that addition/removal went as expected
414  //
415  bool attributeChangesOk = true;
416  if ( attributesChanged )
417  {
418  L->updateFields();
419  QgsFields newFields = L->fields();
420 
421  if ( oldFields.count() != newFields.count() )
422  {
423  commitErrors << tr( "ERROR: the count of fields is incorrect after addition/removal of fields!" );
424  attributeChangesOk = false; // don't try attribute updates - they'll fail.
425  }
426 
427  for ( int i = 0; i < qMin( oldFields.count(), newFields.count() ); ++i )
428  {
429  QgsField oldField = oldFields.at( i );
430  QgsField newField = newFields.at( i );
431  if ( attributeChangesOk && oldField != newField )
432  {
433  commitErrors
434  << tr( "ERROR: field with index %1 is not the same!" ).arg( i )
435  << tr( "Provider: %1" ).arg( L->providerType() )
436  << tr( "Storage: %1" ).arg( L->storageType() )
437  << QStringLiteral( "%1: name=%2 type=%3 typeName=%4 len=%5 precision=%6" )
438  .arg( tr( "expected field" ),
439  oldField.name(),
440  QVariant::typeToName( oldField.type() ),
441  oldField.typeName() )
442  .arg( oldField.length() )
443  .arg( oldField.precision() )
444  << QStringLiteral( "%1: name=%2 type=%3 typeName=%4 len=%5 precision=%6" )
445  .arg( tr( "retrieved field" ),
446  newField.name(),
447  QVariant::typeToName( newField.type() ),
448  newField.typeName() )
449  .arg( newField.length() )
450  .arg( newField.precision() );
451  attributeChangesOk = false; // don't try attribute updates - they'll fail.
452  }
453  }
454  }
455 
456  if ( attributeChangesOk )
457  {
459  {
461 
463  {
464  commitErrors << tr( "SUCCESS: %1 attribute value(s) and %2 geometries changed." ).arg( mChangedAttributeValues.size(), mChangedGeometries.size() );
466  mChangedAttributeValues.clear();
467 
469  mChangedGeometries.clear();
470  }
471  else
472  {
473  success = false;
474  }
475  }
476  else
477  {
478  //
479  // change attributes
480  //
481  if ( !mChangedAttributeValues.isEmpty() && ( ( cap & QgsVectorDataProvider::ChangeFeatures ) == 0 || mChangedGeometries.isEmpty() ) )
482  {
484  {
485  commitErrors << tr( "SUCCESS: %n attribute value(s) changed.", "changed attribute values count", mChangedAttributeValues.size() );
486 
488  mChangedAttributeValues.clear();
489  }
490  else
491  {
492  commitErrors << tr( "ERROR: %n attribute value change(s) not applied.", "not changed attribute values count", mChangedAttributeValues.size() );
493 #if 0
494  QString list = "ERROR: pending changes:";
495  Q_FOREACH ( QgsFeatureId id, mChangedAttributeValues.keys() )
496  {
497  list.append( "\n " + FID_TO_STRING( id ) + '[' );
498  Q_FOREACH ( int idx, mChangedAttributeValues[ id ].keys() )
499  {
500  list.append( QString( " %1:%2" ).arg( L->pendingFields().at( idx ).name() ).arg( mChangedAttributeValues[id][idx].toString() ) );
501  }
502  list.append( " ]" );
503  }
504  commitErrors << list;
505 #endif
506  success = false;
507  }
508  }
509  }
510 
511  //
512  // delete features
513  //
514  if ( success && !mDeletedFeatureIds.isEmpty() )
515  {
517  {
518  commitErrors << tr( "SUCCESS: %n feature(s) deleted.", "deleted features count", mDeletedFeatureIds.size() );
519  // TODO[MD]: we should not need this here
520  Q_FOREACH ( QgsFeatureId id, mDeletedFeatureIds )
521  {
522  mChangedAttributeValues.remove( id );
523  mChangedGeometries.remove( id );
524  }
525 
527 
528  mDeletedFeatureIds.clear();
529  }
530  else
531  {
532  commitErrors << tr( "ERROR: %n feature(s) not deleted.", "not deleted features count", mDeletedFeatureIds.size() );
533 #if 0
534  QString list = "ERROR: pending deletes:";
535  Q_FOREACH ( QgsFeatureId id, mDeletedFeatureIds )
536  {
537  list.append( ' ' + FID_TO_STRING( id ) );
538  }
539  commitErrors << list;
540 #endif
541  success = false;
542  }
543  }
544 
545  //
546  // add features
547  //
548  if ( success && !mAddedFeatures.isEmpty() )
549  {
551  {
552  QList<QgsFeatureId> ids;
553  QgsFeatureList featuresToAdd;
554  // get the list of added features in reversed order
555  // this will preserve the order how they have been added e.g. (-1, -2, -3) while in the map they are ordered (-3, -2, -1)
556  mapToReversedLists( mAddedFeatures, ids, featuresToAdd );
557 
558  if ( provider->addFeatures( featuresToAdd ) )
559  {
560  commitErrors << tr( "SUCCESS: %n feature(s) added.", "added features count", featuresToAdd.size() );
561 
562  emit committedFeaturesAdded( L->id(), featuresToAdd );
563 
564  // notify everyone that the features with temporary ids were updated with permanent ids
565  for ( int i = 0; i < featuresToAdd.count(); ++i )
566  {
567  if ( featuresToAdd[i].id() != ids[i] )
568  {
569  //update selection
570  if ( L->mSelectedFeatureIds.contains( ids[i] ) )
571  {
572  L->mSelectedFeatureIds.remove( ids[i] );
573  L->mSelectedFeatureIds.insert( featuresToAdd[i].id() );
574  }
575  emit featureDeleted( ids[i] );
576  emit featureAdded( featuresToAdd[i].id() );
577  }
578  }
579 
580  mAddedFeatures.clear();
581  }
582  else
583  {
584  commitErrors << tr( "ERROR: %n feature(s) not added.", "not added features count", mAddedFeatures.size() );
585 #if 0
586  QString list = "ERROR: pending adds:";
587  Q_FOREACH ( QgsFeature f, mAddedFeatures )
588  {
589  list.append( ' ' + FID_TO_STRING( f.id() ) + '[' );
590  for ( int i = 0; i < L->pendingFields().size(); i++ )
591  {
592  list.append( QString( " %1:%2" ).arg( L->pendingFields().at( i ).name() ).arg( f.attributes()[i].toString() ) );
593  }
594  list.append( " ]" );
595  }
596  commitErrors << list;
597 #endif
598  success = false;
599  }
600  }
601  else
602  {
603  commitErrors << tr( "ERROR: %n feature(s) not added - provider doesn't support adding features.", "not added features count", mAddedFeatures.size() );
604  success = false;
605  }
606  }
607  }
608  else
609  {
610  success = false;
611  }
612 
613  if ( !success && provider->hasErrors() )
614  {
615  commitErrors << tr( "\n Provider errors:" );
616  Q_FOREACH ( QString e, provider->errors() )
617  {
618  commitErrors << " " + e.replace( '\n', QLatin1String( "\n " ) );
619  }
620  provider->clearErrors();
621  }
622 
623  return success;
624 }
625 
626 
628 {
629  if ( !isModified() )
630  return;
631 
632  // limit canvas redraws to one by jumping to beginning of stack
633  // see QgsUndoWidget::indexChanged
634  L->undoStack()->setIndex( 0 );
635 
636  Q_ASSERT( mAddedAttributes.isEmpty() );
637  Q_ASSERT( mDeletedAttributeIds.isEmpty() );
638  Q_ASSERT( mChangedAttributeValues.isEmpty() );
639  Q_ASSERT( mChangedGeometries.isEmpty() );
640  Q_ASSERT( mAddedFeatures.isEmpty() );
641 }
642 
643 #if 0
644 QString QgsVectorLayerEditBuffer::dumpEditBuffer()
645 {
646  QString msg;
647  if ( !mChangedGeometries.isEmpty() )
648  {
649  msg += "CHANGED GEOMETRIES:\n";
650  for ( QgsGeometryMap::const_iterator it = mChangedGeometries.begin(); it != mChangedGeometries.end(); ++it )
651  {
652  // QgsFeatureId, QgsGeometry
653  msg += QString( "- FID %1: %2" ).arg( it.key() ).arg( it.value().to );
654  }
655  }
656  return msg;
657 }
658 #endif
659 
661 {
662  // go through the changed attributes map and adapt indices
663  QgsChangedAttributesMap::iterator it = mChangedAttributeValues.begin();
664  for ( ; it != mChangedAttributeValues.end(); ++it )
665  {
666  updateAttributeMapIndex( it.value(), index, + 1 );
667  }
668 
669  // go through added features and adapt attributes
670  QgsFeatureMap::iterator featureIt = mAddedFeatures.begin();
671  for ( ; featureIt != mAddedFeatures.end(); ++featureIt )
672  {
673  QgsAttributes attrs = featureIt->attributes();
674  attrs.insert( index, QVariant() );
675  featureIt->setAttributes( attrs );
676  }
677 
678  // go through renamed attributes and adapt
679  QList< int > sortedRenamedIndices = mRenamedAttributes.keys();
680  //sort keys
681  std::sort( sortedRenamedIndices.begin(), sortedRenamedIndices.end(), std::greater< int >() );
682  Q_FOREACH ( int renameIndex, sortedRenamedIndices )
683  {
684  if ( renameIndex >= index )
685  {
686  mRenamedAttributes[ renameIndex + 1 ] = mRenamedAttributes.value( renameIndex );
687  }
688  }
689  //remove last
690  mRenamedAttributes.remove( index );
691 }
692 
694 {
695  // go through the changed attributes map and adapt indices
696  QgsChangedAttributesMap::iterator it = mChangedAttributeValues.begin();
697  for ( ; it != mChangedAttributeValues.end(); ++it )
698  {
699  QgsAttributeMap &attrMap = it.value();
700  // remove the attribute
701  if ( attrMap.contains( index ) )
702  attrMap.remove( index );
703 
704  // update attribute indices
705  updateAttributeMapIndex( attrMap, index, -1 );
706  }
707 
708  // go through added features and adapt attributes
709  QgsFeatureMap::iterator featureIt = mAddedFeatures.begin();
710  for ( ; featureIt != mAddedFeatures.end(); ++featureIt )
711  {
712  QgsAttributes attrs = featureIt->attributes();
713  attrs.remove( index );
714  featureIt->setAttributes( attrs );
715  }
716 
717  // go through rename attributes and adapt
718  QList< int > sortedRenamedIndices = mRenamedAttributes.keys();
719  //sort keys
720  std::sort( sortedRenamedIndices.begin(), sortedRenamedIndices.end() );
721  int last = -1;
722  mRenamedAttributes.remove( index );
723  Q_FOREACH ( int renameIndex, sortedRenamedIndices )
724  {
725  if ( renameIndex > index )
726  {
727  mRenamedAttributes.insert( renameIndex - 1, mRenamedAttributes.value( renameIndex ) );
728  last = renameIndex;
729  }
730  }
731  //remove last
732  if ( last > -1 )
733  mRenamedAttributes.remove( last );
734 }
735 
736 
737 
739 {
740  QgsAttributeMap updatedMap;
741  for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
742  {
743  int attrIndex = it.key();
744  updatedMap.insert( attrIndex < index ? attrIndex : attrIndex + offset, it.value() );
745  }
746  map = updatedMap;
747 }
748 
749 
750 
752 {
753  L->updateFields();
754 }
void updateFields()
Assembles mUpdatedFields considering provider fields, joined fields and added fields.
void updateChangedAttributes(QgsFeature &f)
Update feature with uncommitted attribute updates.
QgsFeatureId id
Definition: qgsfeature.h:70
static unsigned index
void handleAttributeDeleted(int index)
Update added and changed features after removal of an attribute.
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
Definition: qgsfields.h:47
virtual bool renameAttribute(int attr, const QString &newName)
Renames an attribute field (but does not commit it)
virtual bool addAttribute(const QgsField &field)
Add an attribute field (but does not commit it) returns true if the field was added.
int size() const
Return number of items.
Definition: qgsfields.cpp:120
FieldOrigin fieldOrigin(int fieldIdx) const
Get field&#39;s origin (value from an enumeration)
Definition: qgsfields.cpp:161
virtual Capabilities capabilities() const
Returns flags containing the supported capabilities.
void committedAttributesDeleted(const QString &layerId, const QgsAttributeList &deletedAttributes)
Signals emitted after committing changes.
void mapToReversedLists(const QMap< Key, T > &map, QList< Key > &ks, QList< T > &vs)
populate two lists (ks, vs) from map - in reverse order
QString name
Definition: qgsfield.h:53
int precision
Definition: qgsfield.h:51
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes to the provider.
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
virtual bool addFeature(QgsFeature &f)
Adds a feature.
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
Definition: qgsfields.h:48
void committedAttributesAdded(const QString &layerId, const QList< QgsField > &addedAttributes)
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:517
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:522
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features from the provider.
virtual bool renameAttributes(const QgsFieldNameMap &renamedAttributes)
Renames existing attributes.
#define FID_TO_STRING(fid)
Definition: qgsfeature.h:52
friend class QgsVectorLayerUndoCommandChangeGeometry
Container of fields for a vector layer.
Definition: qgsfields.h:39
virtual void rollBack()
Stop editing and discard the edits.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:127
friend class QgsVectorLayerUndoCommandAddAttribute
QgsChangedAttributesMap mChangedAttributeValues
Changed attributes values which are not committed.
void updateFeatureGeometry(QgsFeature &f)
Update feature with uncommitted geometry updates.
Allows deletion of attributes (fields)
Field comes from the underlying data provider of the vector layer (originIndex = index in provider&#39;s ...
Definition: qgsfields.h:46
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:61
QgsFieldNameMap mRenamedAttributes
Renamed attributes which are not committed.
friend class QgsVectorLayerUndoCommandRenameAttribute
virtual bool addFeatures(QgsFeatureList &flist) override
Adds a list of features to the sink.
int count() const
Return number of items.
Definition: qgsfields.cpp:115
friend class QgsVectorLayerUndoCommandDeleteAttribute
virtual void updateExtents()
Update the extents for the layer.
bool supportedType(const QgsField &field) const
check if provider supports type of field
QgsFields fields() const
Returns the list of fields of this layer.
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:135
int length
Definition: qgsfield.h:50
int fieldOriginIndex(int fieldIdx) const
Get field&#39;s origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:169
bool hasGeometryType() const
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
void featureAdded(QgsFeatureId fid)
void committedGeometriesChanges(const QString &layerId, const QgsGeometryMap &changedGeometries)
QString id() const
Returns the layer&#39;s unique ID, which is used to access this layer from QgsProject.
Allows addition of new attributes (fields)
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:37
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:104
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
virtual bool changeFeatures(const QgsChangedAttributesMap &attr_map, const QgsGeometryMap &geometry_map)
Changes attribute values and geometries of existing features.
QMap< int, QVariant > QgsAttributeMap
Definition: qgsattributes.h:39
QgsGeometryMap mChangedGeometries
Changed geometries which are not committed.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
QStringList errors() const
Get recorded errors.
virtual bool changeGeometry(QgsFeatureId fid, const QgsGeometry &geom)
Change feature&#39;s geometry.
void handleAttributeAdded(int index)
Update added and changed features after addition of an attribute.
Allows modifications of geometries.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
Definition: qgsfields.cpp:59
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
void clearErrors()
Clear recorded errors.
QgsFeatureIds mDeletedFeatureIds
Deleted feature IDs which are not committed.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:45
virtual bool changeGeometryValues(const QgsGeometryMap &geometry_map)
Changes geometries of existing features.
void updateAttributeMapIndex(QgsAttributeMap &attrs, int index, int offset) const
Updates an index in an attribute map to a new value (for updates of changed attributes) ...
void remove(int fieldIdx)
Remove a field with the given index.
Definition: qgsfields.cpp:83
virtual bool commitChanges(QStringList &commitErrors)
Attempts to commit any changes to disk.
virtual bool deleteAttribute(int attr)
Delete an attribute field (but does not commit it)
friend class QgsVectorLayerUndoCommandDeleteFeature
Supports joint updates for attributes and geometry Providers supporting this should still define Chan...
QList< QgsField > mAddedAttributes
Added attributes fields which are not committed.
void committedAttributeValuesChanges(const QString &layerId, const QgsChangedAttributesMap &changedAttributesValues)
void committedAttributesRenamed(const QString &layerId, const QgsFieldNameMap &renamedAttributes)
Emitted after committing an attribute rename.
bool hasErrors() const
Provider has errors to report.
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:137
QgsVectorDataProvider * dataProvider() override
Returns the layer&#39;s data provider.
virtual bool deleteAttributes(const QgsAttributeIds &attributes)
Deletes existing attributes from the provider.
virtual bool deleteFeature(QgsFeatureId fid)
Delete a feature from the layer (but does not commit it)
qint64 QgsFeatureId
Definition: qgsfeature.h:37
QgsFields pendingFields() const
Returns the list of fields of this layer.
virtual bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant())
Changed an attribute value (but does not commit it)
#define FID_IS_NEW(fid)
Definition: qgsfeature.h:50
QUndoStack * undoStack()
Return pointer to layer&#39;s undo stack.
void committedFeaturesRemoved(const QString &layerId, const QgsFeatureIds &deletedFeatureIds)
QString providerType() const
Return the provider type for this layer.
void committedFeaturesAdded(const QString &layerId, const QgsFeatureList &addedFeatures)
This is the base class for vector data providers.
QgsFeatureMap mAddedFeatures
New features which are not committed.
A vector of attributes.
Definition: qgsattributes.h:57
void updateFields(QgsFields &fields)
void layerModified()
This signal is emitted when modifications has been done on layer.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:94
Field is calculated from an expression.
Definition: qgsfields.h:49
QgsAttributeList mDeletedAttributeIds
Deleted attributes fields which are not committed. The list is kept sorted.
Allows modification of attribute values.
void featureDeleted(QgsFeatureId fid)
QgsAttributes attributes
Definition: qgsfeature.h:71
Supports renaming attributes (fields). Added in QGIS 2.16.
friend class QgsVectorLayerUndoCommandChangeAttribute