Skip to content

Commit

Permalink
Greatly speed up attribute table loading
Browse files Browse the repository at this point in the history
Don't advise for rows added when a model reset is in progress.
Otherwise the rows are tested for sort order, etc triggering
a bunch of useless calculations, given that the model is in
the process of being reset anyway.

Tested using a 150k point shapefile, decreased attribute table
load times from 50+ seconds to 4 seconds.

Refs #16577, #16239

(forward port from b97a980)
  • Loading branch information
nyalldawson committed May 22, 2017
1 parent 48ecee5 commit ebd3e0d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -67,7 +67,7 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
connect( layer(), &QgsVectorLayer::attributeDeleted, this, &QgsAttributeTableModel::attributeDeleted );
connect( layer(), &QgsVectorLayer::updatedFields, this, &QgsAttributeTableModel::updatedFields );
connect( layer(), &QgsVectorLayer::editCommandEnded, this, &QgsAttributeTableModel::editCommandEnded );
connect( mLayerCache, &QgsVectorLayerCache::featureAdded, this, &QgsAttributeTableModel::featureAdded );
connect( mLayerCache, &QgsVectorLayerCache::featureAdded, this, [ = ]( QgsFeatureId id ) { featureAdded( id ); } );
connect( mLayerCache, &QgsVectorLayerCache::cachedLayerDeleted, this, &QgsAttributeTableModel::layerDeleted );
}

Expand Down Expand Up @@ -202,7 +202,7 @@ bool QgsAttributeTableModel::removeRows( int row, int count, const QModelIndex &
return true;
}

void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid, bool resettingModel )
{
QgsDebugMsgLevel( QString( "(%2) fid: %1" ).arg( fid ).arg( mFeatureRequest.filterType() ), 4 );
bool featOk = true;
Expand Down Expand Up @@ -230,10 +230,12 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
if ( ! mIdRowMap.contains( fid ) )
{
int n = mRowIdMap.size();
beginInsertRows( QModelIndex(), n, n );
if ( !resettingModel )
beginInsertRows( QModelIndex(), n, n );
mIdRowMap.insert( fid, n );
mRowIdMap.insert( n, fid );
endInsertRows();
if ( !resettingModel )
endInsertRows();
reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
}
}
Expand Down Expand Up @@ -430,7 +432,7 @@ void QgsAttributeTableModel::loadLayer()

t.restart();
}
featureAdded( mFeat.id() );
featureAdded( mFeat.id(), true );
}

emit finished();
Expand Down
4 changes: 3 additions & 1 deletion src/gui/attributetable/qgsattributetablemodel.h
Expand Up @@ -313,8 +313,10 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
/**
* Launched when a feature has been added
* \param fid feature id
* \param resettingModel set to true if model is in the process of being reset
* and the normal begin/EndInsertRows calls should not be made
*/
virtual void featureAdded( QgsFeatureId fid );
virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );

/**
* Launched when layer has been deleted
Expand Down

0 comments on commit ebd3e0d

Please sign in to comment.