Skip to content

Commit

Permalink
Take into account cache size when deciding to go for full update
Browse files Browse the repository at this point in the history
... also prune some dead code
  • Loading branch information
elpaso committed Oct 17, 2018
1 parent d55233f commit 7e9c874
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 61 deletions.
82 changes: 32 additions & 50 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ void QgsAttributeTableModel::editCommandEnded()
{
// do not do reload(...) due would trigger (dataChanged) row sort
// giving issue: https://issues.qgis.org/issues/15976
mChangedCellBounds = QRect();
bulkEditCommandEnded( );
}

Expand Down Expand Up @@ -301,13 +300,13 @@ void QgsAttributeTableModel::fieldFormatterRemoved( QgsFieldFormatter *fieldForm

void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
{
// Defer all updates if an edit command is running
// Defer all updates if a bulk edit/rollback command is running
if ( mBulkEditCommandRunning )
{
mAttributeValueChanges.insert( QPair<QgsFeatureId, int>( fid, idx ), value );
return;
}
QgsDebugMsgLevel( QStringLiteral( "(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.filterType() ), 3 );
QgsDebugMsgLevel( QStringLiteral( "(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.filterType() ), 2 );

for ( SortCache &cache : mSortCaches )
{
Expand Down Expand Up @@ -744,30 +743,6 @@ bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &
if ( !layer()->isModified() )
return false;

if ( mChangedCellBounds.isNull() )
{
mChangedCellBounds = QRect( index.column(), index.row(), 1, 1 );
}
else
{
if ( index.column() < mChangedCellBounds.left() )
{
mChangedCellBounds.setLeft( index.column() );
}
if ( index.row() < mChangedCellBounds.top() )
{
mChangedCellBounds.setTop( index.row() );
}
if ( index.column() > mChangedCellBounds.right() )
{
mChangedCellBounds.setRight( index.column() );
}
if ( index.row() > mChangedCellBounds.bottom() )
{
mChangedCellBounds.setBottom( index.row() );
}
}

mRowStylesMap.remove( index.row() );

return true;
Expand Down Expand Up @@ -820,36 +795,43 @@ void QgsAttributeTableModel::bulkEditCommandEnded()
{
mBulkEditCommandRunning = false;
// Full model update if the changed rows are more than half the total rows
// or if their count is > 1000
int minRow = rowCount();
int minCol = columnCount();
int maxRow = 0;
int maxCol = 0;
bool fullModelUpdate = mAttributeValueChanges.count() >= 1000 ||
mAttributeValueChanges.count() >= rowCount() * 0.5;
const auto keys = mAttributeValueChanges.keys();
for ( const auto &key : keys )
{

attributeValueChanged( key.first, key.second, mAttributeValueChanges.value( key ) );
int row( idToRow( key.first ) );
int col( fieldCol( key.second ) );
if ( ! fullModelUpdate )
{
QModelIndex index( createIndex( row, col ) );
emit dataChanged( index, index );
}
else
// or if their count is > layer cache size
int changeCount( mAttributeValueChanges.count() );
bool fullModelUpdate = changeCount > mLayerCache->cacheSize() ||
changeCount > rowCount() * 0.5;

QgsDebugMsgLevel( QStringLiteral( "Bulk edit command ended with %1 modified rows over (%4), cache size is %2, starting %3 update." )
.arg( changeCount )
.arg( mLayerCache->cacheSize() )
.arg( fullModelUpdate ? QStringLiteral( "full" ) : QStringLiteral( "incremental" ) )
.arg( rowCount() ),
3 );
// Invalidates the whole model
if ( fullModelUpdate )
{
// Invalidates the cache (there is no API for doing this directly)
mLayerCache->layer()->dataChanged();
emit dataChanged( createIndex( 0, 0 ), createIndex( rowCount() - 1, columnCount() - 1 ) );
}
else
{
int minRow = rowCount();
int minCol = columnCount();
int maxRow = 0;
int maxCol = 0;
const auto keys = mAttributeValueChanges.keys();
for ( const auto &key : keys )
{
attributeValueChanged( key.first, key.second, mAttributeValueChanges.value( key ) );
int row( idToRow( key.first ) );
int col( fieldCol( key.second ) );
minRow = std::min<int>( row, minRow );
minCol = std::min<int>( col, minCol );
maxRow = std::max<int>( row, maxRow );
maxCol = std::max<int>( col, maxCol );
}
}
// Invalidates the whole model: triggers an update in the view
if ( fullModelUpdate )
emit dataChanged( createIndex( minRow, minCol ), createIndex( maxRow, maxCol ) );
}
mAttributeValueChanges.clear();
}

Expand Down
13 changes: 2 additions & 11 deletions src/gui/attributetable/qgsattributetablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );

/**
* Launched when eatures have been deleted
* Launched when features have been deleted
* \param fids feature ids
*/
virtual void featuresDeleted( const QgsFeatureIds &fids );
Expand Down Expand Up @@ -378,15 +378,6 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel

std::vector<SortCache> mSortCaches;

/**
* Holds the bounds of changed cells while an update operation is running
* top = min row
* left = min column
* bottom = max row
* right = max column
*/
QRect mChangedCellBounds;

QgsAttributeEditorContext mEditorContext;

int mExtraColumns = 0;
Expand All @@ -397,7 +388,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
//! Sets the flag for massive changes operations
void bulkEditCommandStarted();

//! Clears the flag for massive changes operations and tells the view to update
//! Clears the flag for massive changes operations, updates/rebuilds the layer cache and tells the view to update
void bulkEditCommandEnded();

//! Changed attribute values within a bulk edit command
Expand Down

0 comments on commit 7e9c874

Please sign in to comment.