Skip to content
Permalink
Browse files
Don't prefetch attribute table sort values when no sorting set
Shaves some seconds off opening the attribute table in certain
circumstances (no sorting applied)

Refs #16577, #16239
  • Loading branch information
nyalldawson committed May 22, 2017
1 parent ebd3e0d commit 0b95c7764d0859b46e7305e1031f3d3827b746be
Showing with 15 additions and 9 deletions.
  1. +15 −9 src/gui/attributetable/qgsattributetablemodel.cpp
@@ -44,7 +44,6 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
: QAbstractTableModel( parent )
, mLayerCache( layerCache )
, mFieldCount( 0 )
, mSortCacheExpression( QLatin1String( "" ) )
, mSortFieldIndex( -1 )
, mExtraColumns( 0 )
{
@@ -212,19 +211,19 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid, bool resettingModel

if ( featOk && mFeatureRequest.acceptFeature( mFeat ) )
{
if ( mSortFieldIndex == -1 )
{
mExpressionContext.setFeature( mFeat );
mSortCache[mFeat.id()] = mSortCacheExpression.evaluate( &mExpressionContext );
}
else
if ( mSortFieldIndex >= 0 )
{
QgsFieldFormatter *fieldFormatter = mFieldFormatters.at( mSortFieldIndex );
const QVariant &widgetCache = mAttributeWidgetCaches.at( mSortFieldIndex );
const QVariantMap &widgetConfig = mWidgetConfigs.at( mSortFieldIndex );
QVariant sortValue = fieldFormatter->representValue( layer(), mSortFieldIndex, widgetConfig, widgetCache, mFeat.attribute( mSortFieldIndex ) );
mSortCache.insert( mFeat.id(), sortValue );
}
else if ( mSortCacheExpression.isValid() )
{
mExpressionContext.setFeature( mFeat );
mSortCache[mFeat.id()] = mSortCacheExpression.evaluate( &mExpressionContext );
}

// Skip if the fid is already in the map (do not add twice)!
if ( ! mIdRowMap.contains( fid ) )
@@ -799,7 +798,14 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString )
mSortCache.clear();
mSortCacheAttributes.clear();
mSortFieldIndex = -1;
mSortCacheExpression = QgsExpression( expressionString );
if ( !expressionString.isEmpty() )
mSortCacheExpression = QgsExpression( expressionString );
else
{
// no sorting
mSortCacheExpression = QgsExpression();
return;
}

QgsFieldFormatter *fieldFormatter = nullptr;
QVariant widgetCache;
@@ -852,7 +858,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString )

QString QgsAttributeTableModel::sortCacheExpression() const
{
if ( mSortCacheExpression.rootNode() )
if ( mSortCacheExpression.isValid() )
return mSortCacheExpression.expression();
else
return QString();

0 comments on commit 0b95c77

Please sign in to comment.