Skip to content

Commit

Permalink
Takes care of sort caches
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Oct 16, 2018
1 parent 1d9cfdc commit d55233f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -301,9 +301,12 @@ void QgsAttributeTableModel::fieldFormatterRemoved( QgsFieldFormatter *fieldForm

void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, const QVariant &value )
{
// Skip all updates if an edit command is running
// Defer all updates if an edit 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 );

for ( SortCache &cache : mSortCaches )
Expand Down Expand Up @@ -810,13 +813,44 @@ bool QgsAttributeTableModel::fieldIsEditable( const QgsVectorLayer &layer, int f
void QgsAttributeTableModel::bulkEditCommandStarted()
{
mBulkEditCommandRunning = true;
mAttributeValueChanges.clear();
}

void QgsAttributeTableModel::bulkEditCommandEnded()
{
// Invalidate the whole model
mBulkEditCommandRunning = false;
emit dataChanged( createIndex( 0, 0 ), createIndex( rowCount() - 1, columnCount() - 1 ) );
// 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
{
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();
}

void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelIndex &index2 )
Expand Down
3 changes: 3 additions & 0 deletions src/gui/attributetable/qgsattributetablemodel.h
Expand Up @@ -400,6 +400,9 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
//! Clears the flag for massive changes operations and tells the view to update
void bulkEditCommandEnded();

//! Changed attribute values within a bulk edit command
QMap<QPair<QgsFeatureId, int>, QVariant> mAttributeValueChanges;

friend class TestQgsAttributeTable;

};
Expand Down

0 comments on commit d55233f

Please sign in to comment.