Merge pull request #52046 from signedav/preserve_index

Fix index lost on reload in Attribute Table
This commit is contained in:
signedav 2023-03-07 17:15:10 +01:00 committed by GitHub
commit 771c85ac2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 17 deletions

View File

@ -174,7 +174,7 @@ void QgsFeatureListView::mousePressEvent( QMouseEvent *event )
}
}
void QgsFeatureListView::editSelectionChanged( const QItemSelection &deselected, const QItemSelection &selected )
void QgsFeatureListView::editSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
{
if ( isVisible() && updatesEnabled() )
{
@ -183,6 +183,18 @@ void QgsFeatureListView::editSelectionChanged( const QItemSelection &deselected,
viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
}
mLastEditSelectionFid = QgsFeatureId();
if ( !selected.isEmpty() )
{
const QModelIndexList indexList = selected.indexes();
if ( !indexList.isEmpty() )
{
QgsFeature selectedFeature;
mModel->featureByIndex( mModel->mapFromMaster( indexList.first() ), selectedFeature );
mLastEditSelectionFid = selectedFeature.id();
}
}
const QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
if ( currentSelection.size() == 1 )
{
@ -501,25 +513,35 @@ void QgsFeatureListView::updateEditSelection( bool inSelection )
int rowToSelect = -1;
QgsFeatureIds selectedFids;
if ( inSelection )
{
const QgsFeatureIds selectedFids = layerCache()->layer()->selectedFeatureIds();
const int rowCount = mModel->rowCount();
selectedFids = layerCache()->layer()->selectedFeatureIds();
}
for ( int i = 0; i < rowCount; i++ )
//if the selectedFids are empty because of no selection or selection reset, the index should persist
if ( selectedFids.isEmpty() )
{
//if no index can be evaluated from the last position the index should go to 0
selectedFids = QgsFeatureIds() << mLastEditSelectionFid;
}
const int rowCount = mModel->rowCount();
for ( int i = 0; i < rowCount; i++ )
{
if ( selectedFids.contains( mModel->idxToFid( mModel->index( i, 0 ) ) ) )
{
if ( selectedFids.contains( mModel->idxToFid( mModel->index( i, 0 ) ) ) )
{
rowToSelect = i;
break;
}
if ( rowToSelect == -1 && !validEditSelectionAvailable )
rowToSelect = 0;
rowToSelect = i;
break;
}
}
else
if ( rowToSelect == -1 && !validEditSelectionAvailable )
{
// if no index could have been evaluated but no validEditSelectionAvailable, then jump to zero
rowToSelect = 0;
}
if ( rowToSelect != -1 )
{

View File

@ -221,7 +221,7 @@ class GUI_EXPORT QgsFeatureListView : public QListView
private slots:
void editSelectionChanged( const QItemSelection &deselected, const QItemSelection &selected );
void editSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected );
/**
* Make sure, there is an edit selection. If there is none, choose the first item.
@ -269,6 +269,8 @@ class GUI_EXPORT QgsFeatureListView : public QListView
QTimer mUpdateEditSelectionTimerWithSelection;
QTimer mUpdateEditSelectionTimerWithoutSelection;
QgsFeatureId mLastEditSelectionFid;
friend class QgsDualView;
};

View File

@ -891,9 +891,8 @@ void TestQgsAttributeTable::testEnsureEditSelection()
// we reload the layer
layer->reload();
spy.wait( 1 );
// ... and the currentEditSelection jumps to the first one (instead of staying at 2, since it's NOT persistend)
QVERIFY( dlg->mMainView->mFeatureListView->currentEditSelection().contains( 1 ) );
// ... and the currentEditSelection stays on 2 (since lastEditSelectionFid is persisted)
QVERIFY( dlg->mMainView->mFeatureListView->currentEditSelection().contains( 2 ) );
}
QGSTEST_MAIN( TestQgsAttributeTable )