Skip to content

Commit 0277421

Browse files
committed
Add selected features only mode to QgsVectorLayer::getValues
and getDoubleValues
1 parent 204cf75 commit 0277421

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

python/core/qgsvectorlayer.sip

+4-2
Original file line numberDiff line numberDiff line change
@@ -1136,21 +1136,23 @@ class QgsVectorLayer : QgsMapLayer
11361136
/** Fetches all values from a specified field name or expression.
11371137
* @param fieldOrExpression field name or an expression string
11381138
* @param ok will be set to false if field or expression is invalid, otherwise true
1139+
* @param selectedOnly set to true to get values from selected features only
11391140
* @returns list of fetched values
11401141
* @note added in QGIS 2.9
11411142
* @see getDoubleValues
11421143
*/
1143-
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok );
1144+
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );
11441145

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

11551157
/** Set the blending mode used for rendering each feature */
11561158
void setFeatureBlendMode( const QPainter::CompositionMode &blendMode );

src/core/qgsvectorlayer.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -3154,7 +3154,7 @@ QVariant QgsVectorLayer::maximumValue( int index )
31543154
return QVariant();
31553155
}
31563156

3157-
QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, bool& ok )
3157+
QList<QVariant> QgsVectorLayer::getValues( const QString &fieldOrExpression, bool& ok, bool selectedOnly )
31583158
{
31593159
QList<QVariant> values;
31603160

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

3182-
QgsFeatureIterator fit = getFeatures( QgsFeatureRequest()
3183-
.setFlags(( expression && expression->needsGeometry() ) ?
3184-
QgsFeatureRequest::NoFlags :
3185-
QgsFeatureRequest::NoGeometry )
3186-
.setSubsetOfAttributes( lst, pendingFields() ) );
3182+
QgsFeatureRequest request = QgsFeatureRequest()
3183+
.setFlags(( expression && expression->needsGeometry() ) ?
3184+
QgsFeatureRequest::NoFlags :
3185+
QgsFeatureRequest::NoGeometry )
3186+
.setSubsetOfAttributes( lst, pendingFields() );
3187+
3188+
QgsFeatureIterator fit;
3189+
if ( !selectedOnly )
3190+
{
3191+
fit = getFeatures( request );
3192+
}
3193+
else
3194+
{
3195+
fit = selectedFeaturesIterator( request );
3196+
}
31873197

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

3198-
QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool& ok )
3208+
QList<double> QgsVectorLayer::getDoubleValues( const QString &fieldOrExpression, bool& ok, bool selectedOnly )
31993209
{
32003210
QList<double> values;
32013211

3202-
QList<QVariant> variantValues = getValues( fieldOrExpression, ok );
3212+
QList<QVariant> variantValues = getValues( fieldOrExpression, ok, selectedOnly );
32033213
if ( !ok )
32043214
return values;
32053215

src/core/qgsvectorlayer.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -1501,21 +1501,23 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
15011501
/** Fetches all values from a specified field name or expression.
15021502
* @param fieldOrExpression field name or an expression string
15031503
* @param ok will be set to false if field or expression is invalid, otherwise true
1504+
* @param selectedOnly set to true to get values from selected features only
15041505
* @returns list of fetched values
15051506
* @note added in QGIS 2.9
15061507
* @see getDoubleValues
15071508
*/
1508-
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok );
1509+
QList< QVariant > getValues( const QString &fieldOrExpression, bool &ok, bool selectedOnly = false );
15091510

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

15201522
/** Set the blending mode used for rendering each feature */
15211523
void setFeatureBlendMode( const QPainter::CompositionMode &blendMode );

tests/src/core/testqgsvectorlayer.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ class TestQgsVectorLayer : public QObject
208208
f4.setAttribute( "col1", QVariant() );
209209
layer->dataProvider()->addFeatures( QgsFeatureList() << f1 << f2 << f3 << f4 );
210210

211+
//make a selection
212+
QgsFeatureIds ids;
213+
ids << f2.id() << f3.id();
214+
layer->setSelectedFeatures( ids );
215+
211216
bool ok;
212217
QList<QVariant> varList = layer->getValues( "col1", ok );
213218
QVERIFY( ok );
@@ -217,13 +222,27 @@ class TestQgsVectorLayer : public QObject
217222
QCOMPARE( varList.at( 2 ), QVariant( 3 ) );
218223
QCOMPARE( varList.at( 3 ), QVariant() );
219224

225+
//check with selected features
226+
varList = layer->getValues( "col1", ok, true );
227+
QVERIFY( ok );
228+
QCOMPARE( varList.length(), 2 );
229+
QCOMPARE( varList.at( 0 ), QVariant( 2 ) );
230+
QCOMPARE( varList.at( 1 ), QVariant( 3 ) );
231+
220232
QList<double> doubleList = layer->getDoubleValues( "col1", ok );
221233
QVERIFY( ok );
222234
QCOMPARE( doubleList.length(), 3 );
223235
QCOMPARE( doubleList.at( 0 ), 1.0 );
224236
QCOMPARE( doubleList.at( 1 ), 2.0 );
225237
QCOMPARE( doubleList.at( 2 ), 3.0 );
226238

239+
//check with selected features
240+
doubleList = layer->getDoubleValues( "col1", ok, true );
241+
QVERIFY( ok );
242+
QCOMPARE( doubleList.length(), 2 );
243+
QCOMPARE( doubleList.at( 0 ), 2.0 );
244+
QCOMPARE( doubleList.at( 1 ), 3.0 );
245+
227246
QList<QVariant> expVarList = layer->getValues( "tostring(col1) || ' '", ok );
228247
QVERIFY( ok );
229248
QCOMPARE( expVarList.length(), 4 );

0 commit comments

Comments
 (0)