Skip to content
Permalink
Browse files

Feature iterators: add a mechanism to check if it must be interrupted.

Add a setInterruptionChecker() method on iterators that provides an interface
that can be called by iterators to check if they must return as soon as
possible. This is set by QgsVectorLayerRenderer::render() to connect to
QgsRenderContext::renderingStopped(). This is useful for some iterators,
like the to be-committed QgsWFSFeatureIterator that can wait for a long
time before returning. By regularly checking if it must be interrupted,
this improves the GUI responsiveness a lot.
This is an enhancement and existing iterators do not need to be modified.
  • Loading branch information
rouault committed Apr 5, 2016
1 parent 9fe6a9f commit 114c1087eb5f161ef2bb11e4ddceceb59b68f763
@@ -224,6 +224,10 @@ bool QgsAbstractFeatureIterator::prepareOrderBy( const QList<QgsFeatureRequest::
return false;
}

void QgsAbstractFeatureIterator::setInterruptionChecker( QgsInterruptionChecker* )
{
}

///////

QgsFeatureIterator& QgsFeatureIterator::operator=( const QgsFeatureIterator & other )
@@ -21,6 +21,19 @@

class QgsAbstractGeometrySimplifier;

/** \ingroup core
* Interface that can be optionaly attached to an iterator so its
* nextFeature() implementaton can check if it must stop as soon as possible.
* @note Added in QGIS 2.16
* @note not available in Python bindings
*/
class CORE_EXPORT QgsInterruptionChecker
{
public:
//! return true if the iterator must stop as soon as possible
virtual bool mustStop() const = 0;
};

/** \ingroup core
* Internal feature iterator to be implemented within data providers
*/
@@ -41,6 +54,16 @@ class CORE_EXPORT QgsAbstractFeatureIterator
//! end of iterating: free the resources / lock
virtual bool close() = 0;

/** Attach an object that can be queried regularly by the iterator to check
* if it must stopped. This is mostly useful for iterators where a single
* nextFeature()/fetchFeature() iteration might be very long. A typical use case is the
* WFS provider. When nextFeature()/fetchFeature() is reasonably fast, it is not necessary
* to implement this method. The default implementation does nothing.
* @note added in QGIS 2.16
* @note not available in Python bindings
*/
virtual void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );

protected:
/**
* If you write a feature iterator for your provider, this is the method you
@@ -168,8 +191,6 @@ class QgsAbstractFeatureIteratorFromSource : public QgsAbstractFeatureIterator
bool mOwnSource;
};



/**
* \ingroup core
* Wrapper for iterator of features from vector data provider or vector layer
@@ -195,6 +216,15 @@ class CORE_EXPORT QgsFeatureIterator
//! find out whether the iterator is still valid or closed already
bool isClosed() const;

/** Attach an object that can be queried regularly by the iterator to check
* if it must stopped. This is mostly useful for iterators where a single
* nextFeature()/fetchFeature() iteration might be very long. A typical use case is the
* WFS provider.
* @note added in QGIS 2.16
* @note not available in Python bindings
*/
void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker );

friend bool operator== ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );
friend bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2 );

@@ -265,4 +295,10 @@ inline bool operator!= ( const QgsFeatureIterator &fi1, const QgsFeatureIterator
return !( fi1 == fi2 );
}

inline void QgsFeatureIterator::setInterruptionChecker( QgsInterruptionChecker* interruptionChecker )
{
if ( mIter )
mIter->setInterruptionChecker( interruptionChecker );
}

#endif // QGSFEATUREITERATOR_H
@@ -94,6 +94,7 @@ QgsVectorLayerFeatureIterator::QgsVectorLayerFeatureIterator( QgsVectorLayerFeat
: QgsAbstractFeatureIteratorFromSource<QgsVectorLayerFeatureSource>( source, ownSource, request )
, mFetchedFid( false )
, mEditGeometrySimplifier( nullptr )
, mInterruptionChecker( nullptr )
{
prepareExpressions();

@@ -236,6 +237,7 @@ bool QgsVectorLayerFeatureIterator::fetchFeature( QgsFeature& f )
{
mChangedFeaturesIterator.close();
mProviderIterator = mSource->mProviderFeatureSource->getFeatures( mProviderRequest );
mProviderIterator.setInterruptionChecker( mInterruptionChecker );
}

while ( mProviderIterator.nextFeature( f ) )
@@ -310,8 +312,11 @@ bool QgsVectorLayerFeatureIterator::close()
return true;
}



void QgsVectorLayerFeatureIterator::setInterruptionChecker( QgsInterruptionChecker* interruptionChecker )
{
mProviderIterator.setInterruptionChecker( interruptionChecker );
mInterruptionChecker = interruptionChecker;
}

bool QgsVectorLayerFeatureIterator::fetchNextAddedFeature( QgsFeature& f )
{
@@ -84,6 +84,8 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera
//! end of iterating: free the resources / lock
virtual bool close() override;

virtual void setInterruptionChecker( QgsInterruptionChecker* interruptionChecker ) override;

protected:
//! fetch next feature, return true on success
virtual bool fetchFeature( QgsFeature& feature ) override;
@@ -179,6 +181,8 @@ class CORE_EXPORT QgsVectorLayerFeatureIterator : public QgsAbstractFeatureItera

QScopedPointer<QgsExpressionContext> mExpressionContext;

QgsInterruptionChecker* mInterruptionChecker;

/**
* Will always return true. We assume that ordering has been done on provider level already.
*
@@ -44,6 +44,7 @@
QgsVectorLayerRenderer::QgsVectorLayerRenderer( QgsVectorLayer* layer, QgsRenderContext& context )
: QgsMapLayerRenderer( layer->id() )
, mContext( context )
, mInterruptionChecker( context )
, mLayer( layer )
, mFields( layer->fields() )
, mRendererV2( nullptr )
@@ -246,6 +247,11 @@ bool QgsVectorLayerRenderer::render()
}

QgsFeatureIterator fit = mSource->getFeatures( featureRequest );
// Attach an interruption checker so that iterators that have potentially
// slow fetchFeature() implementations, such as in the WFS provider, can
// check it, instead of relying on just the mContext.renderingStopped() check
// in drawRendererV2()
fit.setInterruptionChecker( &mInterruptionChecker );

if (( mRendererV2->capabilities() & QgsFeatureRendererV2::SymbolLevels ) && mRendererV2->usingSymbolLevels() )
drawRendererV2Levels( fit );
@@ -392,9 +398,6 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
QgsFeature fet;
while ( fit.nextFeature( fet ) )
{
if ( !fet.constGeometry() )
continue; // skip features without geometry

if ( mContext.renderingStopped() )
{
qDebug( "rendering stop!" );
@@ -403,6 +406,9 @@ void QgsVectorLayerRenderer::drawRendererV2Levels( QgsFeatureIterator& fit )
return;
}

if ( !fet.constGeometry() )
continue; // skip features without geometry

mContext.expressionContext().setFeature( fet );
QgsSymbolV2* sym = mRendererV2->symbolForFeature( fet, mContext );
if ( !sym )
@@ -627,3 +633,18 @@ void QgsVectorLayerRenderer::prepareDiagrams( QgsVectorLayer* layer, QStringList
mContext.labelingEngine()->prepareDiagramLayer( layer, attributeNames, mContext ); // will make internal copy of diagSettings + initialize it

}

/* ----------------------------------------- */
/* QgsVectorLayerRendererInterruptionChecker */
/* ----------------------------------------- */

QgsVectorLayerRendererInterruptionChecker::QgsVectorLayerRendererInterruptionChecker
( const QgsRenderContext& context )
: mContext( context )
{
}

bool QgsVectorLayerRendererInterruptionChecker::mustStop() const
{
return mContext.renderingStopped();
}
@@ -44,6 +44,19 @@ typedef QList<int> QgsAttributeList;
class QgsVectorLayerLabelProvider;
class QgsVectorLayerDiagramProvider;

/** Interruption checker used by QgsVectorLayerRenderer::render()
* @note not available in Python bindings
*/
class QgsVectorLayerRendererInterruptionChecker: public QgsInterruptionChecker
{
public:
/** Constructor */
QgsVectorLayerRendererInterruptionChecker( const QgsRenderContext& context );
bool mustStop() const override;
private:
const QgsRenderContext& mContext;
};

/**
* Implementation of threaded rendering for vector layers.
*
@@ -87,6 +100,8 @@ class QgsVectorLayerRenderer : public QgsMapLayerRenderer

QgsRenderContext& mContext;

QgsVectorLayerRendererInterruptionChecker mInterruptionChecker;

/** The rendered layer */
QgsVectorLayer* mLayer;

0 comments on commit 114c108

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