Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added listAllFeatureIds and check that features exist when setting se…
…lection
  • Loading branch information
3nids authored and jef-n committed Sep 15, 2013
1 parent 821b1d2 commit 39fab67
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
3 changes: 3 additions & 0 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -294,6 +294,9 @@ class QgsVectorLayer : QgsMapLayer
/** Select all the features */
void selectAll();

/** Get all feature Ids */
QgsFeatureIds listAllFeatureIds();

/**
* Invert selection of features found within the search rectangle (in layer's coordinates)
*
Expand Down
40 changes: 22 additions & 18 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -792,27 +792,17 @@ void QgsVectorLayer::modifySelection( QgsFeatureIds selectIds, QgsFeatureIds des

void QgsVectorLayer::invertSelection()
{
// copy the ids of selected features to tmp
QgsFeatureIds tmp = mSelectedFeatureIds;

QgsFeatureIterator fit = getFeatures( QgsFeatureRequest()
.setFlags( QgsFeatureRequest::NoGeometry )
.setSubsetOfAttributes( QgsAttributeList() ) );

QgsFeatureIds ids;

QgsFeature fet;
while ( fit.nextFeature( fet ) )
{
ids << fet.id();
}

QgsFeatureIds ids = listAllFeatureIds();
ids.subtract( mSelectedFeatureIds );

setSelectedFeatures( ids );
}

void QgsVectorLayer::selectAll()
{
setSelectedFeatures( listAllFeatureIds() );
}

QgsFeatureIds QgsVectorLayer::listAllFeatureIds()
{
QgsFeatureIterator fit = getFeatures( QgsFeatureRequest()
.setFlags( QgsFeatureRequest::NoGeometry )
Expand All @@ -826,7 +816,7 @@ void QgsVectorLayer::selectAll()
ids << fet.id();
}

setSelectedFeatures( ids );
return ids;
}

void QgsVectorLayer::invertSelectionInRectangle( QgsRectangle & rect )
Expand Down Expand Up @@ -2682,9 +2672,23 @@ bool QgsVectorLayer::rollBack( bool deleteBuffer )
void QgsVectorLayer::setSelectedFeatures( const QgsFeatureIds& ids )
{
QgsFeatureIds deselectedFeatures = mSelectedFeatureIds - ids;
// TODO: check whether features with these ID exist

mSelectedFeatureIds = ids;

QgsFeatureIds allIds = listAllFeatureIds();
QgsFeatureIds::iterator id = mSelectedFeatureIds.begin();
while ( id != mSelectedFeatureIds.end() )
{
if ( !allIds.contains( *id ) )
{
id = mSelectedFeatureIds.erase( id );
}
else
{
++id;
}
}

// invalidate cache
setCacheImage( 0 );

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgsvectorlayer.h
Expand Up @@ -631,6 +631,9 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Select all the features */
void selectAll();

/** Get all feature Ids */
QgsFeatureIds listAllFeatureIds();

/**
* Invert selection of features found within the search rectangle (in layer's coordinates)
*
Expand Down

0 comments on commit 39fab67

Please sign in to comment.