Skip to content

Commit

Permalink
Followup "Ensure that full cache flag is cleared when invalid"
Browse files Browse the repository at this point in the history
This is required - when the cache is invalidated it requires
a full rebuild (eg due to a new attribute being added) in order
to have complete information.

Since this could be a very lengthy process, it's not safe to
immediately rebuild the full cache. Instead, clear the full
cache flag and require users of this class to handle
responsive cache rebuilding by listening to the invalidated()
signal from the cache.

(cherry-picked from e1d80b5)
  • Loading branch information
nyalldawson committed Mar 7, 2017
1 parent 9bbd593 commit dbd6538
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
9 changes: 4 additions & 5 deletions src/core/qgsvectorlayercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ int QgsVectorLayerCache::cacheSize()

void QgsVectorLayerCache::setCacheGeometry( bool cacheGeometry )
{
bool shouldCache = cacheGeometry && mLayer->hasGeometryType();
bool mustInvalidate = shouldCache && !mCacheGeometry; // going from no geometry -> geometry, so have to clear existing cache entries
mCacheGeometry = shouldCache;
bool shouldCacheGeometry = cacheGeometry && mLayer->hasGeometryType();
bool mustInvalidate = shouldCacheGeometry && !mCacheGeometry; // going from no geometry -> geometry, so have to clear existing cache entries
mCacheGeometry = shouldCacheGeometry;
if ( cacheGeometry )
{
connect( mLayer, SIGNAL( geometryChanged( QgsFeatureId, QgsGeometry& ) ), SLOT( geometryChanged( QgsFeatureId, QgsGeometry& ) ) );
Expand Down Expand Up @@ -237,8 +237,7 @@ void QgsVectorLayerCache::attributeAdded( int field )
{
Q_UNUSED( field )
mCachedAttributes.append( field );
mFullCache = false;
mCache.clear();
invalidate();
}

void QgsVectorLayerCache::attributeDeleted( int field )
Expand Down
6 changes: 5 additions & 1 deletion src/core/qgsvectorlayercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* be used for slow data sources, be aware, that the call to this method might take a long time.
*
* @param fullCache True: enable full caching, False: disable full caching
* @note when a cache is invalidated() (e.g. by adding an attribute to a layer) this setting
* is reset. A full cache rebuild must be performed by calling setFullCache( true ) again.
* @see hasFullCache()
*/
void setFullCache( bool fullCache );
Expand Down Expand Up @@ -280,7 +282,9 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
void featureAdded( QgsFeatureId fid );

/**
* The cache has been invalidated and cleared.
* The cache has been invalidated and cleared. Note that when a cache is invalidated
* the fullCache() setting will be cleared, and a full cache rebuild via setFullCache( true )
* will need to be performed.
*/
void invalidated();

Expand Down
24 changes: 15 additions & 9 deletions src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void QgsDualView::setFilterMode( QgsAttributeTableFilterModel::FilterMode filter
switch ( mFilterModel->filterMode() )
{
case QgsAttributeTableFilterModel::ShowVisible:
disconnect( mMapCanvas, SIGNAL(extentsChanged()), this, SLOT(extentChanged()) );
disconnect( mMapCanvas, SIGNAL( extentsChanged() ), this, SLOT( extentChanged() ) );
break;

case QgsAttributeTableFilterModel::ShowAll:
Expand All @@ -254,8 +254,8 @@ void QgsDualView::setFilterMode( QgsAttributeTableFilterModel::FilterMode filter
break;

case QgsAttributeTableFilterModel::ShowSelected:
disconnect( masterModel()->layer(), SIGNAL(selectionChanged()), this,
SLOT(updateSelectedFeatures()) );
disconnect( masterModel()->layer(), SIGNAL( selectionChanged() ), this,
SLOT( updateSelectedFeatures() ) );
break;
}

Expand All @@ -270,7 +270,7 @@ void QgsDualView::setFilterMode( QgsAttributeTableFilterModel::FilterMode filter
switch ( filterMode )
{
case QgsAttributeTableFilterModel::ShowVisible:
connect( mMapCanvas, SIGNAL(extentsChanged()), this, SLOT(extentChanged()) );
connect( mMapCanvas, SIGNAL( extentsChanged() ), this, SLOT( extentChanged() ) );
r.setFilterFids( QgsFeatureIds() );
r.disableFilter();
if ( mMapCanvas )
Expand All @@ -288,7 +288,7 @@ void QgsDualView::setFilterMode( QgsAttributeTableFilterModel::FilterMode filter
break;

case QgsAttributeTableFilterModel::ShowSelected:
connect( masterModel()->layer(), SIGNAL(selectionChanged()), this, SLOT(updateSelectedFeatures()) );
connect( masterModel()->layer(), SIGNAL( selectionChanged() ), this, SLOT( updateSelectedFeatures() ) );
if ( masterModel()->layer()->selectedFeatureCount() > 0 )
r.setFilterFids( masterModel()->layer()->selectedFeaturesIds() );
else
Expand Down Expand Up @@ -318,10 +318,8 @@ void QgsDualView::initLayerCache( bool cacheGeometry )
mLayerCache->setCacheGeometry( cacheGeometry );
if ( 0 == cacheSize || 0 == ( QgsVectorDataProvider::SelectAtId & mLayer->dataProvider()->capabilities() ) )
{
connect( mLayerCache, SIGNAL( progress( int, bool & ) ), this, SLOT( progress( int, bool & ) ) );
connect( mLayerCache, SIGNAL( finished() ), this, SLOT( finished() ) );

mLayerCache->setFullCache( true );
connect( mLayerCache, SIGNAL(invalidated()), this, SLOT(rebuildFullLayerCache()) );
rebuildFullLayerCache();
}
}

Expand Down Expand Up @@ -714,6 +712,14 @@ void QgsDualView::zoomToCurrentFeature()
}
}

void QgsDualView::rebuildFullLayerCache()
{
connect( mLayerCache, SIGNAL(progress(int,bool&)), this, SLOT(progress(int,bool&)), Qt::UniqueConnection );
connect( mLayerCache, SIGNAL(finished()), this, SLOT(finished()), Qt::UniqueConnection );

mLayerCache->setFullCache( true );
}

void QgsDualView::previewExpressionChanged( const QString& expression )
{
mLayer->setDisplayExpression( expression );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
/** Zooms to the active feature*/
void zoomToCurrentFeature();

void rebuildFullLayerCache();

private:
void initLayerCache( bool cacheGeometry );
void initModels( QgsMapCanvas* mapCanvas, const QgsFeatureRequest& request );
Expand Down

0 comments on commit dbd6538

Please sign in to comment.