Skip to content

Commit

Permalink
Merge pull request #52046 from signedav/preserve_index
Browse files Browse the repository at this point in the history
Fix index lost on reload in Attribute Table
  • Loading branch information
signedav committed Mar 7, 2023
2 parents c5536d9 + e8460b9 commit 771c85a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
48 changes: 35 additions & 13 deletions src/gui/attributetable/qgsfeaturelistview.cpp
Expand Up @@ -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() )
{
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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 )
{
Expand Down
4 changes: 3 additions & 1 deletion src/gui/attributetable/qgsfeaturelistview.h
Expand Up @@ -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.
Expand Down Expand Up @@ -269,6 +269,8 @@ class GUI_EXPORT QgsFeatureListView : public QListView
QTimer mUpdateEditSelectionTimerWithSelection;
QTimer mUpdateEditSelectionTimerWithoutSelection;

QgsFeatureId mLastEditSelectionFid;

friend class QgsDualView;
};

Expand Down
5 changes: 2 additions & 3 deletions tests/src/app/testqgsattributetable.cpp
Expand Up @@ -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 )
Expand Down

0 comments on commit 771c85a

Please sign in to comment.