QGIS API Documentation
3.26.3-Buenos Aires (65e4edfdad)
|
Go to the documentation of this file.
28 template <
class Key,
class T>
void mapToReversedLists(
const QMap< Key, T > &map, QList<Key> &ks, QList<T> &vs )
30 ks.reserve( map.size() );
31 vs.reserve( map.size() );
32 typename QMap<Key, T>::const_iterator i = map.constEnd();
33 while ( i-- != map.constBegin() )
36 vs.append( i.value() );
55 QgsDebugMsgLevel( QStringLiteral(
"undo index changed %1" ).arg( index ), 4 );
73 fields.
rename( renameIt.key(), renameIt.value() );
117 for ( QgsAttributeMap::const_iterator it = map.begin(); it != map.end(); ++it )
118 attrs[it.key()] = it.value();
152 for ( QgsFeatureList::iterator iter = features.begin(); iter != features.end(); ++iter )
167 QgsDebugMsg( QStringLiteral(
"Cannot delete features (missing DeleteFeature capability)" ) );
175 QgsDebugMsg( QStringLiteral(
"Cannot delete features (in the list of added features)" ) );
183 QgsDebugMsg( QStringLiteral(
"Cannot delete features (in the list of deleted features)" ) );
196 QgsDebugMsg( QStringLiteral(
"Cannot delete features (missing DeleteFeatures capability)" ) );
201 const auto constFids = fids;
233 for (
auto it = newValues.constBegin() ; it != newValues.constEnd(); ++it )
235 const int field = it.key();
236 const QVariant newValue = it.value();
239 if ( oldValues.contains(
field ) )
240 oldValue = oldValues[
field];
279 for (
const QgsField &updatedField : fields )
281 if ( updatedField.name() ==
field.
name() )
320 if ( newName.isEmpty() )
327 for (
const QgsField &updatedField : fields )
329 if ( updatedField.name() == newName )
340 commitErrors.clear();
352 success &= commitChangesCheckGeometryTypeCompatibility( commitErrors );
359 bool attributesChanged =
false;
362 bool attributesDeleted =
false;
363 success &= commitChangesDeleteAttributes( attributesDeleted, commitErrors );
364 attributesChanged |= attributesDeleted;
370 bool attributesRenamed =
false;
371 success &= commitChangesRenameAttributes( attributesRenamed, commitErrors );
372 attributesChanged |= attributesRenamed;
380 bool attributesAdded =
false;
381 success &= commitChangesAddAttributes( attributesAdded, commitErrors );
382 attributesChanged |= attributesAdded;
388 if ( success && attributesChanged )
389 success &= commitChangesCheckAttributesModifications( oldFields, commitErrors );
396 bool attributesChanged;
397 success &= commitChangesChangeAttributes( attributesChanged, commitErrors );
405 bool featuresDeleted;
406 success &= commitChangesDeleteFeatures( featuresDeleted, commitErrors );
415 success &= commitChangesAddFeatures( featuresAdded, commitErrors );
421 commitErrors << tr(
"\n Provider errors:" );
422 const auto constErrors = provider->
errors();
423 for ( QString e : constErrors )
425 commitErrors <<
" " + e.replace(
'\n', QLatin1String(
"\n " ) );
456 QString QgsVectorLayerEditBuffer::dumpEditBuffer()
461 msg +=
"CHANGED GEOMETRIES:\n";
465 msg += QString(
"- FID %1: %2" ).arg( it.key() ).arg( it.value().to );
486 attrs.insert( index, QVariant() );
487 featureIt->setAttributes( attrs );
493 std::sort( sortedRenamedIndices.begin(), sortedRenamedIndices.end(), std::greater< int >() );
494 const auto constSortedRenamedIndices = sortedRenamedIndices;
495 for (
int renameIndex : constSortedRenamedIndices )
497 if ( renameIndex >= index )
514 if ( attrMap.contains( index ) )
515 attrMap.remove( index );
526 attrs.remove( index );
527 featureIt->setAttributes( attrs );
533 std::sort( sortedRenamedIndices.begin(), sortedRenamedIndices.end() );
536 const auto constSortedRenamedIndices = sortedRenamedIndices;
537 for (
int renameIndex : constSortedRenamedIndices )
539 if ( renameIndex > index )
555 for ( QgsAttributeMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
557 int attrIndex = it.key();
558 updatedMap.insert( attrIndex < index ? attrIndex : attrIndex + offset, it.value() );
570 bool QgsVectorLayerEditBuffer::commitChangesCheckGeometryTypeCompatibility( QStringList &commitErrors )
581 if ( ( ! f.hasGeometry() ) ||
587 commitErrors << tr(
"ERROR: %n feature(s) not added - geometry type is not compatible with the current layer.",
"not added features count",
mAddedFeatures.size() );
595 commitErrors << tr(
"ERROR: %n feature(s) not added - provider doesn't support adding features.",
"not added features count",
mAddedFeatures.size() );
602 bool QgsVectorLayerEditBuffer::commitChangesDeleteAttributes(
bool &attributesDeleted, QStringList &commitErrors )
604 attributesDeleted =
false;
611 commitErrors << tr(
"SUCCESS: %n attribute(s) deleted.",
"deleted attributes count",
mDeletedAttributeIds.size() );
616 attributesDeleted =
true;
620 commitErrors << tr(
"ERROR: %n attribute(s) not deleted.",
"not deleted attributes count",
mDeletedAttributeIds.size() );
622 QString list =
"ERROR: Pending attribute deletes:";
624 for (
int idx : constMDeletedAttributeIds )
628 commitErrors << list;
636 bool QgsVectorLayerEditBuffer::commitChangesRenameAttributes(
bool &attributesRenamed, QStringList &commitErrors )
638 attributesRenamed =
false;
645 commitErrors << tr(
"SUCCESS: %n attribute(s) renamed.",
"renamed attributes count",
mRenamedAttributes.size() );
650 attributesRenamed =
true;
654 commitErrors << tr(
"ERROR: %n attribute(s) not renamed",
"not renamed attributes count",
mRenamedAttributes.size() );
661 bool QgsVectorLayerEditBuffer::commitChangesAddAttributes(
bool &attributesAdded, QStringList &commitErrors )
663 attributesAdded =
false;
670 commitErrors << tr(
"SUCCESS: %n attribute(s) added.",
"added attributes count",
mAddedAttributes.size() );
673 attributesAdded =
true;
677 commitErrors << tr(
"ERROR: %n new attribute(s) not added",
"not added attributes count",
mAddedAttributes.size() );
679 QString list =
"ERROR: Pending adds:";
681 for (
QgsField f : constMAddedAttributes )
683 list.append(
' ' + f.name() );
685 commitErrors << list;
693 bool QgsVectorLayerEditBuffer::commitChangesCheckAttributesModifications(
const QgsFields oldFields, QStringList &commitErrors )
700 commitErrors << tr(
"ERROR: the count of fields is incorrect after addition/removal of fields!" );
704 for (
int i = 0; i < std::min( oldFields.
count(), newFields.
count() ); ++i )
708 if ( oldField != newField )
711 << tr(
"ERROR: field with index %1 is not the same!" ).arg( i )
714 << QStringLiteral(
"%1: name=%2 type=%3 typeName=%4 len=%5 precision=%6" )
715 .arg( tr(
"expected field" ),
717 QVariant::typeToName( oldField.
type() ),
721 << QStringLiteral(
"%1: name=%2 type=%3 typeName=%4 len=%5 precision=%6" )
722 .arg( tr(
"retrieved field" ),
724 QVariant::typeToName( newField.
type() ),
735 bool QgsVectorLayerEditBuffer::commitChangesChangeAttributes(
bool &attributesChanged, QStringList &commitErrors )
737 attributesChanged =
false;
746 attributesChanged =
true;
765 commitErrors << tr(
"ERROR: %1 geometries not changed. Data provider '%2' does not have ChangeFeatures or ChangeGeometries capabilities",
"not changed geometries count" )
773 commitErrors << tr(
"SUCCESS: %n geometries were changed.",
"changed geometries count",
mChangedGeometries.size() );
774 attributesChanged =
true;
780 commitErrors << tr(
"ERROR: %n geometries not changed.",
"not changed geometries count",
mChangedGeometries.size() );
790 commitErrors << tr(
"ERROR: %1 attribute value change(s) not applied. Data provider '%2' does not have ChangeFeatures or ChangeAttributeValues capabilities",
"not changed attribute values count" )
798 commitErrors << tr(
"SUCCESS: %n attribute value(s) changed.",
"changed attribute values count",
mChangedAttributeValues.size() );
799 attributesChanged =
true;
805 commitErrors << tr(
"ERROR: %n attribute value change(s) not applied.",
"not changed attribute values count",
mChangedAttributeValues.size() );
807 QString list =
"ERROR: pending changes:";
813 for (
int idx : constKeys )
819 commitErrors << list;
828 bool QgsVectorLayerEditBuffer::commitChangesDeleteFeatures(
bool &featuresDeleted, QStringList &commitErrors )
830 featuresDeleted =
false;
837 commitErrors << tr(
"SUCCESS: %n feature(s) deleted.",
"deleted features count",
mDeletedFeatureIds.size() );
838 featuresDeleted =
true;
852 commitErrors << tr(
"ERROR: %n feature(s) not deleted.",
"not deleted features count",
mDeletedFeatureIds.size() );
854 QString list =
"ERROR: pending deletes:";
860 commitErrors << list;
868 bool QgsVectorLayerEditBuffer::commitChangesAddFeatures(
bool &featuresAdded, QStringList &commitErrors )
870 featuresAdded =
false;
877 QList<QgsFeatureId> ids;
885 for (
int i = 0; i < featuresToAdd.count(); ++i )
892 commitErrors << tr(
"SUCCESS: %n feature(s) added.",
"added features count", featuresToAdd.size() );
893 featuresAdded =
true;
897 for (
int i = 0; i < featuresToAdd.count(); ++i )
899 if ( featuresToAdd[i].
id() != ids[i] )
902 if (
L->mSelectedFeatureIds.contains( ids[i] ) )
904 L->mSelectedFeatureIds.remove( ids[i] );
905 L->mSelectedFeatureIds.insert( featuresToAdd[i].
id() );
916 commitErrors << tr(
"ERROR: %n feature(s) not added.",
"not added features count",
mAddedFeatures.size() );
918 QString list =
"ERROR: pending adds:";
923 for (
int i = 0; i <
L->
fields().size(); i++ )
925 list.append( QString(
" %1:%2" ).arg(
L->
fields().
at( i ).
name() ).arg( f.attributes()[i].toString() ) );
929 commitErrors << list;
936 commitErrors << tr(
"ERROR: %n feature(s) not added - provider doesn't support adding features.",
"not added features count",
mAddedFeatures.size() );
void mapToReversedLists(const QMap< Key, T > &map, QList< Key > &ks, QList< T > &vs)
populate two lists (ks, vs) from map - in reverse order
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
@ OriginEdit
Field has been temporarily added in editing mode (originIndex = index in the list of added attributes...
friend class QgsVectorLayerUndoCommandChangeAttribute
@ DeleteFeatures
Allows deletion of features.
QList< QgsField > mAddedAttributes
Added attributes fields which are not committed.
virtual bool deleteFeatures(const QgsFeatureIds &id)
Deletes one or more features from the provider.
bool hasErrors() const
Provider has errors to report.
virtual void updateExtents(bool force=false)
Update the extents for the layer.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
virtual bool isModified() const
Returns true if the provider has been modified since the last commit.
void updateFields()
Will regenerate the fields property of this layer by obtaining all fields from the dataProvider,...
friend class QgsVectorLayerUndoCommandAddFeature
@ DeleteAttributes
Allows deletion of attributes (fields)
QgsFeatureIds mDeletedFeatureIds
Deleted feature IDs which are not committed.
#define QgsDebugMsgLevel(str, level)
virtual bool addAttribute(const QgsField &field)
Adds an attribute field (but does not commit it) returns true if the field was added.
void committedGeometriesChanges(const QString &layerId, const QgsGeometryMap &changedGeometries)
friend class QgsVectorLayerUndoCommandAddAttribute
QgsFields fields() const override=0
Returns the fields associated with this data provider.
The edit buffer group manages a group of edit buffers.
virtual bool changeAttributeValues(const QgsChangedAttributesMap &attr_map)
Changes attribute values of existing features.
int count() const
Returns number of items.
Container of fields for a vector layer.
QgsAttributeList mDeletedAttributeIds
Deleted attributes fields which are not committed. The list is kept sorted.
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)
bool supportedType(const QgsField &field) const
check if provider supports type of field
QString typeName() const
Gets the field type.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
@ AddAttributes
Allows addition of new attributes (fields)
virtual bool doesStrictFeatureTypeCheck() const
Returns true if the provider is strict about the type of inserted features (e.g.
virtual bool addFeature(QgsFeature &f)
Adds a feature.
QgsVectorLayerEditBuffer()=default
Constructor for QgsVectorLayerEditBuffer.
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
void layerModified()
Emitted when modifications has been done on layer.
#define FID_TO_STRING(fid)
void updateChangedAttributes(QgsFeature &f)
Update feature with uncommitted attribute updates.
friend class QgsVectorLayerUndoCommandRenameAttribute
virtual bool deleteAttribute(int attr)
Deletes an attribute field (but does not commit it)
void committedAttributesAdded(const QString &layerId, const QList< QgsField > &addedAttributes)
@ ChangeGeometries
Allows modifications of geometries.
QString providerType() const
Returns the provider type (provider key) for this layer.
virtual bool changeGeometry(QgsFeatureId fid, const QgsGeometry &geom)
Change feature's geometry.
virtual bool changeFeatures(const QgsChangedAttributesMap &attr_map, const QgsGeometryMap &geometry_map)
Changes attribute values and geometries of existing features.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
QStringList errors() const
Gets recorded errors.
virtual Q_INVOKABLE QgsVectorDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities.
void committedFeaturesAdded(const QString &layerId, const QgsFeatureList &addedFeatures)
QgsFeatureMap mAddedFeatures
New features which are not committed.
virtual bool deleteFeature(QgsFeatureId fid)
Delete a feature from the layer (but does not commit it)
void featureDeleted(QgsFeatureId fid)
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
void committedAttributeValuesChanges(const QString &layerId, const QgsChangedAttributesMap &changedAttributesValues)
virtual bool changeGeometryValues(const QgsGeometryMap &geometry_map)
Changes geometries of existing features.
void featureAdded(QgsFeatureId fid)
void setEditBufferGroup(QgsVectorLayerEditBufferGroup *editBufferGroup)
Set the parent edit buffer group for this edit buffer.
virtual bool renameAttributes(const QgsFieldNameMap &renamedAttributes)
Renames existing attributes.
FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
QList< QgsFeature > QgsFeatureList
QMap< int, QVariant > QgsAttributeMap
void committedFeaturesRemoved(const QString &layerId, const QgsFeatureIds &deletedFeatureIds)
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
@ AddFeatures
Allows adding features.
void committedAttributesRenamed(const QString &layerId, const QgsFieldNameMap &renamedAttributes)
Emitted after committing an attribute rename.
virtual void rollBack()
Stop editing and discard the edits.
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QgsVectorLayerEditBufferGroup * editBufferGroup() const
Returns the parent edit buffer group for this edit buffer, or nullptr if not part of a group.
virtual bool renameAttribute(int attr, const QString &newName)
Renames an attribute field (but does not commit it)
void undoIndexChanged(int index)
QSet< QgsFeatureId > QgsFeatureIds
void clearErrors()
Clear recorded errors.
QgsGeometry convertToProviderType(const QgsGeometry &geom) const
Converts the geometry to the provider type if possible / necessary.
void committedAttributesDeleted(const QString &layerId, const QgsAttributeList &deletedAttributes)
Signals emitted after committing changes.
virtual bool deleteAttributes(const QgsAttributeIds &attributes)
Deletes existing attributes from the provider.
QgsFieldNameMap mRenamedAttributes
Renamed attributes which are not committed.
bool addFeatures(QgsFeatureList &flist, QgsFeatureSink::Flags flags=QgsFeatureSink::Flags()) override
Adds a list of features to the sink.
void handleAttributeDeleted(int index)
Update added and changed features after removal of an attribute.
static void matchAttributesToFields(QgsFeature &feature, const QgsFields &fields)
Matches the attributes in feature to the specified fields.
QgsVectorLayerEditBufferGroup * mEditBufferGroup
@ OriginExpression
Field is calculated from an expression.
A geometry is the spatial representation of a feature.
@ ChangeAttributeValues
Allows modification of attribute values.
Represents a vector layer which manages a vector based data sets.
virtual bool changeAttributeValue(QgsFeatureId fid, int field, const QVariant &newValue, const QVariant &oldValue=QVariant())
Changed an attribute value (but does not commit it)
QgsFeatureIds allAddedOrEditedFeatures() const
Returns a list of the features IDs for all newly added or edited features in the buffer.
virtual bool addAttributes(const QList< QgsField > &attributes)
Adds new attributes to the provider.
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
virtual bool commitChanges(QStringList &commitErrors)
Attempts to commit any changes to disk.
@ ChangeFeatures
Supports joint updates for attributes and geometry. Providers supporting this should still define Cha...
QUndoStack * undoStack()
Returns pointer to layer's undo stack.
QString storageType() const
Returns the permanent storage type for this layer as a friendly name.
@ RenameAttributes
Supports renaming attributes (fields). Since QGIS 2.16.
A vector of attributes. Mostly equal to QVector<QVariant>.
This is the base class for vector data providers.
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
friend class QgsVectorLayerUndoCommandChangeGeometry
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QgsGeometryMap mChangedGeometries
Changed geometries which are not committed.
virtual QString name() const =0
Returns a provider name.
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
virtual bool deleteFeatures(const QgsFeatureIds &fid)
Deletes a set of features from the layer (but does not commit it)
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
void remove(int fieldIdx)
Removes the field with the given index.
void updateFields(QgsFields &fields)
Updates fields.
friend class QgsVectorLayerUndoCommandDeleteFeature
virtual bool changeAttributeValues(QgsFeatureId fid, const QgsAttributeMap &newValues, const QgsAttributeMap &oldValues)
Changes values of attributes (but does not commit it).
void updateFeatureGeometry(QgsFeature &f)
Update feature with uncommitted geometry updates.
void handleAttributeAdded(int index)
Update added and changed features after addition of an attribute.
@ OriginJoin
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
virtual bool addFeatures(QgsFeatureList &features)
Insert a copy of the given features into the layer (but does not commit it)
QgsWkbTypes::Type wkbType() const override=0
Returns the geometry type which is returned by this layer.
QgsChangedAttributesMap mChangedAttributeValues
Changed attributes values which are not committed.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
friend class QgsVectorLayerUndoCommandDeleteAttribute
Encapsulate a field in an attribute table or data source.