Skip to content

Commit

Permalink
Don't prefetch attribute table sort values when no sorting set
Browse files Browse the repository at this point in the history
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 0b95c77
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache,
: QAbstractTableModel( parent )
, mLayerCache( layerCache )
, mFieldCount( 0 )
, mSortCacheExpression( QLatin1String( "" ) )
, mSortFieldIndex( -1 )
, mExtraColumns( 0 )
{
Expand Down Expand Up @@ -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 ) )
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 0b95c77

Please sign in to comment.