Skip to content
Permalink
Browse files

Add selected features only mode to QgsVectorLayer::getValues

and getDoubleValues
  • Loading branch information
nyalldawson committed May 17, 2015
1 parent 204cf75 commit 02774210ebcbb6a348d6fd87a18bfd4d215b6173
Showing with 45 additions and 12 deletions.
  1. +4 −2 python/core/qgsvectorlayer.sip
  2. +18 −8 src/core/qgsvectorlayer.cpp
  3. +4 −2 src/core/qgsvectorlayer.h
  4. +19 −0 tests/src/core/testqgsvectorlayer.cpp
@@ -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 );
@@ -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;

@@ -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 ) )
@@ -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;

@@ -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 );
@@ -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 );
@@ -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 );

0 comments on commit 0277421

Please sign in to comment.
You can’t perform that action at this time.