Alternative approach to fixing #32819

Partially reverts 5d27d7c, fixes #33133
This commit is contained in:
Nyall Dawson 2019-11-29 10:29:34 +10:00
parent d8be3f5a43
commit 85095a70ff
2 changed files with 19 additions and 8 deletions

View File

@ -166,7 +166,8 @@ bool QgsAttributeTableModel::removeRows( int row, int count, const QModelIndex &
if ( row < 0 || count < 1 )
return false;
beginRemoveRows( parent, row, row + count - 1 );
if ( !mResettingModel )
beginRemoveRows( parent, row, row + count - 1 );
#ifdef QGISDEBUG
if ( 3 <= QgsLogger::debugLevel() )
@ -208,12 +209,13 @@ bool QgsAttributeTableModel::removeRows( int row, int count, const QModelIndex &
Q_ASSERT( mRowIdMap.size() == mIdRowMap.size() );
endRemoveRows();
if ( !mResettingModel )
endRemoveRows();
return true;
}
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid, bool resettingModel )
void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
{
QgsDebugMsgLevel( QStringLiteral( "(%2) fid: %1" ).arg( fid ).arg( mFeatureRequest.filterType() ), 4 );
bool featOk = true;
@ -244,11 +246,11 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid, bool resettingModel
if ( ! mIdRowMap.contains( fid ) )
{
int n = mRowIdMap.size();
if ( !resettingModel )
if ( !mResettingModel )
beginInsertRows( QModelIndex(), n, n );
mIdRowMap.insert( fid, n );
mRowIdMap.insert( n, fid );
if ( !resettingModel )
if ( !mResettingModel )
endInsertRows();
reload( index( rowCount() - 1, 0 ), index( rowCount() - 1, columnCount() ) );
}
@ -436,8 +438,12 @@ void QgsAttributeTableModel::loadLayer()
// (emit of progress() signal may enter event loop and thus attribute
// table view may be updated with inconsistent model which may assume
// wrong number of attributes)
loadAttributes();
mResettingModel = true;
beginResetModel();
if ( rowCount() != 0 )
{
removeRows( 0, rowCount() );
@ -472,6 +478,10 @@ void QgsAttributeTableModel::loadLayer()
emit finished();
connect( mLayerCache, &QgsVectorLayerCache::invalidated, this, &QgsAttributeTableModel::loadLayer, Qt::UniqueConnection );
}
endResetModel();
mResettingModel = false;
}

View File

@ -318,10 +318,8 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
/**
* Launched when a feature has been added
* \param fid feature id
* \param resettingModel set to TRUE if model is in the process of being reset
* and the normal begin/EndInsertRows calls should not be made
*/
virtual void featureAdded( QgsFeatureId fid, bool resettingModel = false );
virtual void featureAdded( QgsFeatureId fid );
/**
* Launched when layer has been deleted
@ -384,6 +382,9 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
//! Flag for massive changes operations, set by edit command or rollback
bool mBulkEditCommandRunning = false;
//! TRUE if model is in the midst of a reset operation
bool mResettingModel = false;
//! Sets the flag for massive changes operations
void bulkEditCommandStarted();