Skip to content

Commit 7e9c874

Browse files
committedOct 17, 2018
Take into account cache size when deciding to go for full update
... also prune some dead code
1 parent d55233f commit 7e9c874

File tree

2 files changed

+34
-61
lines changed

2 files changed

+34
-61
lines changed
 

‎src/gui/attributetable/qgsattributetablemodel.cpp

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ void QgsAttributeTableModel::editCommandEnded()
261261
{
262262
// do not do reload(...) due would trigger (dataChanged) row sort
263263
// giving issue: https://issues.qgis.org/issues/15976
264-
mChangedCellBounds = QRect();
265264
bulkEditCommandEnded( );
266265
}
267266

@@ -301,13 +300,13 @@ void QgsAttributeTableModel::fieldFormatterRemoved( QgsFieldFormatter *fieldForm
301300

302301
void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
303302
{
304-
// Defer all updates if an edit command is running
303+
// Defer all updates if a bulk edit/rollback command is running
305304
if ( mBulkEditCommandRunning )
306305
{
307306
mAttributeValueChanges.insert( QPair<QgsFeatureId, int>( fid, idx ), value );
308307
return;
309308
}
310-
QgsDebugMsgLevel( QStringLiteral( "(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.filterType() ), 3 );
309+
QgsDebugMsgLevel( QStringLiteral( "(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.filterType() ), 2 );
311310

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

747-
if ( mChangedCellBounds.isNull() )
748-
{
749-
mChangedCellBounds = QRect( index.column(), index.row(), 1, 1 );
750-
}
751-
else
752-
{
753-
if ( index.column() < mChangedCellBounds.left() )
754-
{
755-
mChangedCellBounds.setLeft( index.column() );
756-
}
757-
if ( index.row() < mChangedCellBounds.top() )
758-
{
759-
mChangedCellBounds.setTop( index.row() );
760-
}
761-
if ( index.column() > mChangedCellBounds.right() )
762-
{
763-
mChangedCellBounds.setRight( index.column() );
764-
}
765-
if ( index.row() > mChangedCellBounds.bottom() )
766-
{
767-
mChangedCellBounds.setBottom( index.row() );
768-
}
769-
}
770-
771746
mRowStylesMap.remove( index.row() );
772747

773748
return true;
@@ -820,36 +795,43 @@ void QgsAttributeTableModel::bulkEditCommandEnded()
820795
{
821796
mBulkEditCommandRunning = false;
822797
// Full model update if the changed rows are more than half the total rows
823-
// or if their count is > 1000
824-
int minRow = rowCount();
825-
int minCol = columnCount();
826-
int maxRow = 0;
827-
int maxCol = 0;
828-
bool fullModelUpdate = mAttributeValueChanges.count() >= 1000 ||
829-
mAttributeValueChanges.count() >= rowCount() * 0.5;
830-
const auto keys = mAttributeValueChanges.keys();
831-
for ( const auto &key : keys )
832-
{
833-
834-
attributeValueChanged( key.first, key.second, mAttributeValueChanges.value( key ) );
835-
int row( idToRow( key.first ) );
836-
int col( fieldCol( key.second ) );
837-
if ( ! fullModelUpdate )
838-
{
839-
QModelIndex index( createIndex( row, col ) );
840-
emit dataChanged( index, index );
841-
}
842-
else
798+
// or if their count is > layer cache size
799+
int changeCount( mAttributeValueChanges.count() );
800+
bool fullModelUpdate = changeCount > mLayerCache->cacheSize() ||
801+
changeCount > rowCount() * 0.5;
802+
803+
QgsDebugMsgLevel( QStringLiteral( "Bulk edit command ended with %1 modified rows over (%4), cache size is %2, starting %3 update." )
804+
.arg( changeCount )
805+
.arg( mLayerCache->cacheSize() )
806+
.arg( fullModelUpdate ? QStringLiteral( "full" ) : QStringLiteral( "incremental" ) )
807+
.arg( rowCount() ),
808+
3 );
809+
// Invalidates the whole model
810+
if ( fullModelUpdate )
811+
{
812+
// Invalidates the cache (there is no API for doing this directly)
813+
mLayerCache->layer()->dataChanged();
814+
emit dataChanged( createIndex( 0, 0 ), createIndex( rowCount() - 1, columnCount() - 1 ) );
815+
}
816+
else
817+
{
818+
int minRow = rowCount();
819+
int minCol = columnCount();
820+
int maxRow = 0;
821+
int maxCol = 0;
822+
const auto keys = mAttributeValueChanges.keys();
823+
for ( const auto &key : keys )
843824
{
825+
attributeValueChanged( key.first, key.second, mAttributeValueChanges.value( key ) );
826+
int row( idToRow( key.first ) );
827+
int col( fieldCol( key.second ) );
844828
minRow = std::min<int>( row, minRow );
845829
minCol = std::min<int>( col, minCol );
846830
maxRow = std::max<int>( row, maxRow );
847831
maxCol = std::max<int>( col, maxCol );
848832
}
849-
}
850-
// Invalidates the whole model: triggers an update in the view
851-
if ( fullModelUpdate )
852833
emit dataChanged( createIndex( minRow, minCol ), createIndex( maxRow, maxCol ) );
834+
}
853835
mAttributeValueChanges.clear();
854836
}
855837

‎src/gui/attributetable/qgsattributetablemodel.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
309309
virtual void attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value );
310310

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

379379
std::vector<SortCache> mSortCaches;
380380

381-
/**
382-
* Holds the bounds of changed cells while an update operation is running
383-
* top = min row
384-
* left = min column
385-
* bottom = max row
386-
* right = max column
387-
*/
388-
QRect mChangedCellBounds;
389-
390381
QgsAttributeEditorContext mEditorContext;
391382

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

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

403394
//! Changed attribute values within a bulk edit command

0 commit comments

Comments
 (0)
Please sign in to comment.