Skip to content

Commit

Permalink
Merge pull request #4230 from nyalldawson/cache_table_218
Browse files Browse the repository at this point in the history
Backport attribute table optimisations from 3.0
  • Loading branch information
nyalldawson authored Mar 7, 2017
2 parents 2a6bceb + 8e875ed commit e543ff4
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 70 deletions.
11 changes: 1 addition & 10 deletions python/core/qgsvectorlayercache.sip
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,10 @@ class QgsVectorLayerCache : QObject
*/
int cacheSize();

/**
* Enable or disable the caching of geometries
*
* @param cacheGeometry Enable or disable the caching of geometries
*/
void setCacheGeometry( bool cacheGeometry );

bool cacheGeometry() const;

/**
* Set the subset of attributes to be cached
*
* @param attributes The attributes to be cached
*/
void setCacheSubsetOfAttributes( const QgsAttributeList& attributes );

/**
Expand Down
12 changes: 2 additions & 10 deletions python/gui/attributetable/qgsdualview.sip
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,8 @@ class QgsDualView : QStackedWidget
explicit QgsDualView( QWidget* parent /TransferThis/ = 0 );
virtual ~QgsDualView();

/**
* Has to be called to initialize the dual view.
*
* @param layer The layer which should be used to fetch features
* @param mapCanvas The mapCanvas (used for the FilterMode
* {@link QgsAttributeTableFilterModel::ShowVisible}
* @param request Use a modified request to limit the shown features
* @param context The context in which this view is shown
*/
void init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsFeatureRequest& request = QgsFeatureRequest(), const QgsAttributeEditorContext& context = QgsAttributeEditorContext() );
void init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const QgsFeatureRequest& request = QgsFeatureRequest(), const QgsAttributeEditorContext& context = QgsAttributeEditorContext(),
bool loadFeatures = true );

/**
* Change the current view mode.
Expand Down
16 changes: 13 additions & 3 deletions src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
mEditorContext.setVectorLayerTools( QgisApp::instance()->vectorLayerTools() );

QgsFeatureRequest r;
bool needsGeom = false;
QgsAttributeTableFilterModel::FilterMode initialMode = static_cast< QgsAttributeTableFilterModel::FilterMode>( settings.value( QString( "/qgis/attributeTableBehaviour" ), QgsAttributeTableFilterModel::ShowAll ).toInt() );
if ( mLayer->geometryType() != QGis::NoGeometry &&
settings.value( "/qgis/attributeTableBehaviour", QgsAttributeTableFilterModel::ShowAll ).toInt() == QgsAttributeTableFilterModel::ShowVisible )
initialMode == QgsAttributeTableFilterModel::ShowVisible )
{
QgsMapCanvas *mc = QgisApp::instance()->mapCanvas();
QgsRectangle extent( mc->mapSettings().mapToLayerCoordinates( theLayer, mc->extent() ) );
Expand All @@ -144,10 +146,18 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
delete g;

mActionShowAllFilter->setText( tr( "Show All Features In Initial Canvas Extent" ) );
needsGeom = true;
}
else if ( initialMode == QgsAttributeTableFilterModel::ShowSelected )
{
if ( theLayer->selectedFeatureCount() > 0 )
r.setFilterFids( theLayer->selectedFeaturesIds() );
}
if ( !needsGeom )
r.setFlags( QgsFeatureRequest::NoGeometry );

// Initialize dual view
mMainView->init( mLayer, QgisApp::instance()->mapCanvas(), r, mEditorContext );
mMainView->init( mLayer, QgisApp::instance()->mapCanvas(), r, mEditorContext, false );

QgsAttributeTableConfig config = mLayer->attributeTableConfig();
mMainView->setAttributeTableConfig( config );
Expand Down Expand Up @@ -322,7 +332,7 @@ void QgsAttributeTableDialog::updateTitle()
QWidget *w = mDock ? qobject_cast<QWidget*>( mDock ) : qobject_cast<QWidget*>( this );
w->setWindowTitle( tr( " %1 :: Features total: %2, filtered: %3, selected: %4%5" )
.arg( mLayer->name() )
.arg( mMainView->featureCount() )
.arg( qMax( static_cast< long >( mMainView->featureCount() ), mLayer->featureCount() ) ) // layer count may be estimated, so use larger of the two
.arg( mMainView->filteredFeatureCount() )
.arg( mLayer->selectedFeatureCount() )
.arg( mRubberBand ? tr( ", spatially limited" ) : "" )
Expand Down
11 changes: 9 additions & 2 deletions src/core/qgsvectorlayercache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ int QgsVectorLayerCache::cacheSize()

void QgsVectorLayerCache::setCacheGeometry( bool cacheGeometry )
{
mCacheGeometry = cacheGeometry && mLayer->hasGeometryType();
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 All @@ -67,6 +69,10 @@ void QgsVectorLayerCache::setCacheGeometry( bool cacheGeometry )
{
disconnect( mLayer, SIGNAL( geometryChanged( QgsFeatureId, QgsGeometry& ) ), this, SLOT( geometryChanged( QgsFeatureId, QgsGeometry& ) ) );
}
if ( mustInvalidate )
{
invalidate();
}
}

void QgsVectorLayerCache::setCacheSubsetOfAttributes( const QgsAttributeList& attributes )
Expand Down Expand Up @@ -231,7 +237,7 @@ void QgsVectorLayerCache::attributeAdded( int field )
{
Q_UNUSED( field )
mCachedAttributes.append( field );
mCache.clear();
invalidate();
}

void QgsVectorLayerCache::attributeDeleted( int field )
Expand Down Expand Up @@ -267,6 +273,7 @@ void QgsVectorLayerCache::layerDeleted()
void QgsVectorLayerCache::invalidate()
{
mCache.clear();
mFullCache = false;
emit invalidated();
}

Expand Down
13 changes: 12 additions & 1 deletion src/core/qgsvectorlayercache.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,16 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
* Enable or disable the caching of geometries
*
* @param cacheGeometry Enable or disable the caching of geometries
* @see cacheGeometry()
*/
void setCacheGeometry( bool cacheGeometry );

/**
* Returns true if the cache will fetch and cache feature geometries.
* @note added in QGIS 3.0
* @see setCacheGeometry()
*/
bool cacheGeometry() const { return mCacheGeometry; }

/**
* Set the subset of attributes to be cached
Expand All @@ -131,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 @@ -273,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
Loading

0 comments on commit e543ff4

Please sign in to comment.