Skip to content

Commit d55233f

Browse files
committed
Takes care of sort caches
1 parent 1d9cfdc commit d55233f

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/gui/attributetable/qgsattributetablemodel.cpp

+37-3
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,12 @@ void QgsAttributeTableModel::fieldFormatterRemoved( QgsFieldFormatter *fieldForm
301301

302302
void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
303303
{
304-
// Skip all updates if an edit command is running
304+
// Defer all updates if an edit command is running
305305
if ( mBulkEditCommandRunning )
306+
{
307+
mAttributeValueChanges.insert( QPair<QgsFeatureId, int>( fid, idx ), value );
306308
return;
309+
}
307310
QgsDebugMsgLevel( QStringLiteral( "(%4) fid: %1, idx: %2, value: %3" ).arg( fid ).arg( idx ).arg( value.toString() ).arg( mFeatureRequest.filterType() ), 3 );
308311

309312
for ( SortCache &cache : mSortCaches )
@@ -810,13 +813,44 @@ bool QgsAttributeTableModel::fieldIsEditable( const QgsVectorLayer &layer, int f
810813
void QgsAttributeTableModel::bulkEditCommandStarted()
811814
{
812815
mBulkEditCommandRunning = true;
816+
mAttributeValueChanges.clear();
813817
}
814818

815819
void QgsAttributeTableModel::bulkEditCommandEnded()
816820
{
817-
// Invalidate the whole model
818821
mBulkEditCommandRunning = false;
819-
emit dataChanged( createIndex( 0, 0 ), createIndex( rowCount() - 1, columnCount() - 1 ) );
822+
// 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
843+
{
844+
minRow = std::min<int>( row, minRow );
845+
minCol = std::min<int>( col, minCol );
846+
maxRow = std::max<int>( row, maxRow );
847+
maxCol = std::max<int>( col, maxCol );
848+
}
849+
}
850+
// Invalidates the whole model: triggers an update in the view
851+
if ( fullModelUpdate )
852+
emit dataChanged( createIndex( minRow, minCol ), createIndex( maxRow, maxCol ) );
853+
mAttributeValueChanges.clear();
820854
}
821855

822856
void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelIndex &index2 )

src/gui/attributetable/qgsattributetablemodel.h

+3
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
400400
//! Clears the flag for massive changes operations and tells the view to update
401401
void bulkEditCommandEnded();
402402

403+
//! Changed attribute values within a bulk edit command
404+
QMap<QPair<QgsFeatureId, int>, QVariant> mAttributeValueChanges;
405+
403406
friend class TestQgsAttributeTable;
404407

405408
};

0 commit comments

Comments
 (0)