Skip to content

Commit

Permalink
Rename UseSelection -> UseSelectionIfPresent, clarify docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 26, 2017
1 parent 1c6f165 commit b067bd7
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion python/core/processing/qgsprocessingcontext.sip
Expand Up @@ -28,7 +28,7 @@ class QgsProcessingContext

enum Flag
{
UseSelection,
UseSelectionIfPresent,
};
typedef QFlags<QgsProcessingContext::Flag> Flags;

Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/tests/ToolsTest.py
Expand Up @@ -86,7 +86,7 @@ def testValues(self):
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

# test with selected features
context.setFlags(QgsProcessingContext.UseSelection)
context.setFlags(QgsProcessingContext.UseSelectionIfPresent)
test_layer.selectByIds([2, 4, 6])
res = vector.values(test_layer, context, 1)
self.assertEqual(set(res[1]), set([5, 7, 3]))
Expand All @@ -111,7 +111,7 @@ def testUniqueValues(self):
self.assertEqual(set(v), set([2, 1, 0]))

# test with selected features
context.setFlags(QgsProcessingContext.UseSelection)
context.setFlags(QgsProcessingContext.UseSelectionIfPresent)
test_layer.selectByIds([2, 4, 6])
v = vector.uniqueValues(test_layer, context, 'id')
self.assertEqual(len(v), len(set(v)))
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/tools/general.py
Expand Up @@ -108,7 +108,7 @@ def createContext():

use_selection = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
if use_selection:
context.setFlags(QgsProcessingContext.UseSelection)
context.setFlags(QgsProcessingContext.UseSelectionIfPresent)

invalid_features_method = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
context.setInvalidGeometryCheck(invalid_features_method)
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/tools/vector.py
Expand Up @@ -94,7 +94,7 @@ def uniqueValues(layer, context, attribute):
"""

fieldIndex = resolveFieldIndex(layer, attribute)
if context.flags() & QgsProcessingContext.UseSelection \
if context.flags() & QgsProcessingContext.UseSelectionIfPresent \
and layer.selectedFeatureCount() > 0:

# iterate through selected features
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingcontext.h
Expand Up @@ -41,7 +41,7 @@ class CORE_EXPORT QgsProcessingContext
//! Flags that affect how processing algorithms are run
enum Flag
{
UseSelection = 1 << 0, //!< Filter to selected features when running algorithms
UseSelectionIfPresent = 1 << 0, //!< Filter to selected features when running algorithms (if a selection exists)
};
Q_DECLARE_FLAGS( Flags, Flag )

Expand Down
4 changes: 2 additions & 2 deletions src/core/processing/qgsprocessingutils.cpp
Expand Up @@ -175,7 +175,7 @@ QString QgsProcessingUtils::normalizeLayerSource( const QString &source )

QgsFeatureIterator QgsProcessingUtils::getFeatures( QgsVectorLayer *layer, const QgsProcessingContext &context, const QgsFeatureRequest &request )
{
bool useSelection = context.flags() & QgsProcessingContext::UseSelection && layer->selectedFeatureCount() > 0;
bool useSelection = context.flags() & QgsProcessingContext::UseSelectionIfPresent && layer->selectedFeatureCount() > 0;

QgsFeatureRequest req( request );
req.setInvalidGeometryCheck( context.invalidGeometryCheck() );
Expand All @@ -192,7 +192,7 @@ QgsFeatureIterator QgsProcessingUtils::getFeatures( QgsVectorLayer *layer, const

long QgsProcessingUtils::featureCount( QgsVectorLayer *layer, const QgsProcessingContext &context )
{
bool useSelection = context.flags() & QgsProcessingContext::UseSelection && layer->selectedFeatureCount() > 0;
bool useSelection = context.flags() & QgsProcessingContext::UseSelectionIfPresent && layer->selectedFeatureCount() > 0;
if ( useSelection )
return layer->selectedFeatureCount();
else
Expand Down
6 changes: 3 additions & 3 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -465,7 +465,7 @@ void TestQgsProcessing::features()
QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 5L );

// test with selected features
context.setFlags( QgsProcessingContext::UseSelection );
context.setFlags( QgsProcessingContext::UseSelectionIfPresent );
layer->selectByIds( QgsFeatureIds() << 2 << 4 );
ids = getIds( QgsProcessingUtils::getFeatures( layer, context ) );
QCOMPARE( ids, QgsFeatureIds() << 2 << 4 );
Expand All @@ -479,7 +479,7 @@ void TestQgsProcessing::features()
QCOMPARE( QgsProcessingUtils::featureCount( layer, context ), 5L );

// using selected features, but no selection
context.setFlags( QgsProcessingContext::UseSelection );
context.setFlags( QgsProcessingContext::UseSelectionIfPresent );
layer->removeSelection();
ids = getIds( QgsProcessingUtils::getFeatures( layer, context ) );
QCOMPARE( ids, QgsFeatureIds() << 1 << 2 << 3 << 4 << 5 );
Expand All @@ -496,7 +496,7 @@ void TestQgsProcessing::features()


//test that feature request is honored when using selections
context.setFlags( QgsProcessingContext::UseSelection );
context.setFlags( QgsProcessingContext::UseSelectionIfPresent );
layer->selectByIds( QgsFeatureIds() << 2 << 4 );
ids = getIds( QgsProcessingUtils::getFeatures( layer, context, QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ) ) );
QCOMPARE( ids, QgsFeatureIds() << 2 << 4 );
Expand Down

0 comments on commit b067bd7

Please sign in to comment.