diff --git a/src/gui/attributetable/qgsfeaturelistview.cpp b/src/gui/attributetable/qgsfeaturelistview.cpp index 2ccbb19bcc7..b6b8592fb4b 100644 --- a/src/gui/attributetable/qgsfeaturelistview.cpp +++ b/src/gui/attributetable/qgsfeaturelistview.cpp @@ -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 ) { diff --git a/src/gui/attributetable/qgsfeaturelistview.h b/src/gui/attributetable/qgsfeaturelistview.h index 22cae58c959..b53e18d8676 100644 --- a/src/gui/attributetable/qgsfeaturelistview.h +++ b/src/gui/attributetable/qgsfeaturelistview.h @@ -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; }; diff --git a/tests/src/app/testqgsattributetable.cpp b/tests/src/app/testqgsattributetable.cpp index 73e05a79ec0..7845b243b3b 100644 --- a/tests/src/app/testqgsattributetable.cpp +++ b/tests/src/app/testqgsattributetable.cpp @@ -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 )