Skip to content

Commit

Permalink
Add selected features only mode to QgsVectorLayer::getValues
Browse files Browse the repository at this point in the history
and getDoubleValues
  • Loading branch information
nyalldawson committed May 17, 2015
1 parent 204cf75 commit 0277421
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
6 changes: 4 additions & 2 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -1136,21 +1136,23 @@ class QgsVectorLayer : QgsMapLayer
/** Fetches all values from a specified field name or expression.
* @param fieldOrExpression field name or an expression string
* @param ok will be set to false if field or expression is invalid, otherwise true
* @param selectedOnly set to true to get values from selected features only
* @returns list of fetched values
* @note added in QGIS 2.9
* @see getDoubleValues
*/
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok );
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );

/** Fetches all double values from a specified field name or expression. Null values or
* invalid expression results are skipped.
* @param fieldOrExpression field name or an expression string evaluating to a double value
* @param ok will be set to false if field or expression is invalid, otherwise true
* @param selectedOnly set to true to get values from selected features only
* @returns list of fetched values
* @note added in QGIS 2.9
* @see getValues
*/
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok );
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );

/** Set the blending mode used for rendering each feature */
void setFeatureBlendMode( const QPainter::CompositionMode &blendMode );
Expand Down
26 changes: 18 additions & 8 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -3154,7 +3154,7 @@ QVariant QgsVectorLayer::maximumValue( int index )
return QVariant();
}

QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, bool& ok )
QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, bool& ok, bool selectedOnly )
{
QList<QVariant> values;

Expand All @@ -3179,11 +3179,21 @@ QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, boo
else
lst = expression->referencedColumns();

QgsFeatureIterator fit = getFeatures( QgsFeatureRequest()
.setFlags(( expression && expression->needsGeometry() ) ?
QgsFeatureRequest::NoFlags :
QgsFeatureRequest::NoGeometry )
.setSubsetOfAttributes( lst, pendingFields() ) );
QgsFeatureRequest request = QgsFeatureRequest()
.setFlags(( expression && expression->needsGeometry() ) ?
QgsFeatureRequest::NoFlags :
QgsFeatureRequest::NoGeometry )
.setSubsetOfAttributes( lst, pendingFields() );

QgsFeatureIterator fit;
if ( !selectedOnly )
{
fit = getFeatures( request );
}
else
{
fit = selectedFeaturesIterator( request );
}

// create list of non-null attribute values
while ( fit.nextFeature( f ) )
Expand All @@ -3195,11 +3205,11 @@ QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, boo
return values;
}

QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool& ok )
QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool& ok, bool selectedOnly )
{
QList<double> values;

QList<QVariant> variantValues = getValues( fieldOrExpression, ok );
QList<QVariant> variantValues = getValues( fieldOrExpression, ok, selectedOnly );
if ( !ok )
return values;

Expand Down
6 changes: 4 additions & 2 deletions src/core/qgsvectorlayer.h
Expand Up @@ -1501,21 +1501,23 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Fetches all values from a specified field name or expression.
* @param fieldOrExpression field name or an expression string
* @param ok will be set to false if field or expression is invalid, otherwise true
* @param selectedOnly set to true to get values from selected features only
* @returns list of fetched values
* @note added in QGIS 2.9
* @see getDoubleValues
*/
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok );
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );

/** Fetches all double values from a specified field name or expression. Null values or
* invalid expression results are skipped.
* @param fieldOrExpression field name or an expression string evaluating to a double value
* @param ok will be set to false if field or expression is invalid, otherwise true
* @param selectedOnly set to true to get values from selected features only
* @returns list of fetched values
* @note added in QGIS 2.9
* @see getValues
*/
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok );
QList< double > getDoubleValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );

/** Set the blending mode used for rendering each feature */
void setFeatureBlendMode( const QPainter::CompositionMode &blendMode );
Expand Down
19 changes: 19 additions & 0 deletions tests/src/core/testqgsvectorlayer.cpp
Expand Up @@ -208,6 +208,11 @@ class TestQgsVectorLayer : public QObject
f4.setAttribute( "col1", QVariant() );
layer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 << f3 << f4 );

//make a selection
QgsFeatureIds ids;
ids << f2.id() << f3.id();
layer->setSelectedFeatures( ids );

bool ok;
QList<QVariant> varList = layer->getValues( "col1", ok );
QVERIFY( ok );
Expand All @@ -217,13 +222,27 @@ class TestQgsVectorLayer : public QObject
QCOMPARE( varList.at( 2 ), QVariant( 3 ) );
QCOMPARE( varList.at( 3 ), QVariant() );

//check with selected features
varList = layer->getValues( "col1", ok, true );
QVERIFY( ok );
QCOMPARE( varList.length(), 2 );
QCOMPARE( varList.at( 0 ), QVariant( 2 ) );
QCOMPARE( varList.at( 1 ), QVariant( 3 ) );

QList<double> doubleList = layer->getDoubleValues( "col1", ok );
QVERIFY( ok );
QCOMPARE( doubleList.length(), 3 );
QCOMPARE( doubleList.at( 0 ), 1.0 );
QCOMPARE( doubleList.at( 1 ), 2.0 );
QCOMPARE( doubleList.at( 2 ), 3.0 );

//check with selected features
doubleList = layer->getDoubleValues( "col1", ok, true );
QVERIFY( ok );
QCOMPARE( doubleList.length(), 2 );
QCOMPARE( doubleList.at( 0 ), 2.0 );
QCOMPARE( doubleList.at( 1 ), 3.0 );

QList<QVariant> expVarList = layer->getValues( "tostring(col1) || ' '", ok );
QVERIFY( ok );
QCOMPARE( expVarList.length(), 4 );
Expand Down

0 comments on commit 0277421

Please sign in to comment.