Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix issue #7169
If removing multiple attributes, remove the indices in descending order, so it does not get confused
  • Loading branch information
m-kuhn authored and jef-n committed Mar 27, 2013
1 parent 6596f48 commit 0d3a387
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 40 deletions.
42 changes: 2 additions & 40 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -493,23 +493,13 @@ void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
QgsAttributeTableModel* masterModel = mMainView->masterModel();

mLayer->beginEditCommand( tr( "Deleted attribute" ) );
bool deleted = false;
QList<int>::const_iterator it = attributes.constBegin();
for ( ; it != attributes.constEnd(); ++it )
{
if ( mLayer->deleteAttribute( *it ) )
{
deleted = true;
}
}

if ( deleted )
if ( mLayer->deleteAttributes( attributes ) )
{
mLayer->endEditCommand();
}
else
{
QMessageBox::critical( 0, tr( "Attribute Error" ), tr( "The attribute(s) could not be deleted" ) );
QgisApp::instance()->messageBar()->pushMessage( tr( "Attribute error" ), tr( "The attribute(s) could not be deleted" ), QgsMessageBar::WARNING, QgisApp::instance()->messageTimeout() );
mLayer->destroyEditCommand();
}
// update model - a field has been added or updated
Expand All @@ -518,34 +508,6 @@ void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
}
}





























void QgsAttributeTableDialog::filterQueryChanged( const QString& query )
{
QString str;
Expand Down
17 changes: 17 additions & 0 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -2980,6 +2980,23 @@ bool QgsVectorLayer::deleteAttribute( int index )
return mEditBuffer->deleteAttribute( index );
}

bool QgsVectorLayer::deleteAttributes( QList<int> attrs )
{
bool deleted = false;

qSort( attrs.begin(), attrs.end(), qGreater<int>() );

foreach( int attr, attrs )
{
if ( deleteAttribute( attr ) )
{
deleted = true;
}
}

return deleted;
}

bool QgsVectorLayer::deleteFeature( QgsFeatureId fid )
{
if ( !mEditBuffer )
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -667,6 +667,15 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** delete an attribute field (but does not commit it) */
bool deleteAttribute( int attr );

/**
* Deletes a list of attribute fields (but does not commit it)
*
* @param attrs the indices of the attributes to delete
* @return true if at least one attribute has been deleted
*
*/
bool deleteAttributes( QList<int> attrs );

/** Insert a copy of the given features into the layer (but does not commit it) */
bool addFeatures( QgsFeatureList features, bool makeSelected = true );

Expand Down

0 comments on commit 0d3a387

Please sign in to comment.