Skip to content

Commit 4312a84

Browse files
committed
[attributetable] Adjust the edit selection when the filter changes
Make sure that we always have a feature on the form that matches the current filter condition
1 parent 0f6a5c8 commit 4312a84

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

src/gui/attributetable/qgsdualview.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ QgsDualView::QgsDualView( QWidget *parent )
4848

4949
mConditionalFormatWidget->hide();
5050

51-
5251
mPreviewColumnsMenu = new QMenu( this );
5352
mActionPreviewColumnsMenu->setMenu( mPreviewColumnsMenu );
5453

@@ -84,7 +83,8 @@ void QgsDualView::init( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas, const Qg
8483
mTableView->setModel( mFilterModel );
8584
mFeatureList->setModel( mFeatureListModel );
8685
delete mAttributeForm;
87-
mAttributeForm = new QgsAttributeForm( mLayer, QgsFeature(), mEditorContext );
86+
mAttributeForm = new QgsAttributeForm( mLayer, mTempAttributeFormFeature, mEditorContext );
87+
mTempAttributeFormFeature = QgsFeature();
8888
if ( !context.parentContext() )
8989
{
9090
mAttributeEditorScrollArea = new QgsScrollArea();
@@ -410,7 +410,11 @@ void QgsDualView::mFeatureList_aboutToChangeEditSelection( bool &ok )
410410

411411
void QgsDualView::mFeatureList_currentEditSelectionChanged( const QgsFeature &feat )
412412
{
413-
if ( !mLayer->isEditable() || mAttributeForm->save() )
413+
if ( !mAttributeForm )
414+
{
415+
mTempAttributeFormFeature = feat;
416+
}
417+
else if ( !mLayer->isEditable() || mAttributeForm->save() )
414418
{
415419
mAttributeForm->setFeature( feat );
416420
setCurrentEditSelection( QgsFeatureIds() << feat.id() );

src/gui/attributetable/qgsdualview.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
374374
QgsAttributeTableConfig mConfig;
375375
QgsScrollArea *mAttributeEditorScrollArea = nullptr;
376376
QgsMapCanvas *mMapCanvas = nullptr;
377+
// If the current feature is set, while the form is still not initialized
378+
// we will temporarily save it in here and set it on init
379+
QgsFeature mTempAttributeFormFeature;
377380

378381
friend class TestQgsDualView;
379382
friend class TestQgsAttributeTable;

src/gui/attributetable/qgsfeaturelistmodel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void QgsFeatureListModel::setSourceModel( QgsAttributeTableFilterModel *sourceMo
4545
// propagate sort order changes from source model to views connected to this model
4646
connect( mFilterModel, &QAbstractItemModel::layoutAboutToBeChanged, this, &QAbstractItemModel::layoutAboutToBeChanged );
4747
connect( mFilterModel, &QAbstractItemModel::layoutChanged, this, &QAbstractItemModel::layoutChanged );
48+
connect( mFilterModel, &QAbstractItemModel::modelAboutToBeReset, this, &QAbstractItemModel::modelAboutToBeReset );
49+
connect( mFilterModel, &QAbstractItemModel::modelReset, this, &QAbstractItemModel::modelReset );
4850
}
4951
}
5052

src/gui/attributetable/qgsfeaturelistview.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ void QgsFeatureListView::setModel( QgsFeatureListModel *featureListModel )
7676
this, static_cast<void ( QgsFeatureListView::* )()>( &QgsFeatureListView::repaintRequested ) );
7777
connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged, this, &QgsFeatureListView::editSelectionChanged );
7878
connect( mModel->layerCache()->layer(), &QgsVectorLayer::attributeValueChanged, this, [ = ] { repaintRequested(); } );
79+
connect( featureListModel, &QgsFeatureListModel::rowsRemoved, this, &QgsFeatureListView::ensureEditSelection );
80+
connect( featureListModel, &QgsFeatureListModel::rowsInserted, this, &QgsFeatureListView::ensureEditSelection );
81+
connect( featureListModel, &QgsFeatureListModel::modelReset, this, &QgsFeatureListView::ensureEditSelection );
7982
}
8083

8184
bool QgsFeatureListView::setDisplayExpression( const QString &expression )
@@ -104,7 +107,8 @@ QString QgsFeatureListView::parserErrorString()
104107
QgsFeatureIds QgsFeatureListView::currentEditSelection()
105108
{
106109
QgsFeatureIds selection;
107-
Q_FOREACH ( const QModelIndex &idx, mCurrentEditSelectionModel->selectedIndexes() )
110+
const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
111+
for ( const QModelIndex &idx : selectedIndexes )
108112
{
109113
selection << idx.data( QgsAttributeTableModel::FeatureIdRole ).value<QgsFeatureId>();
110114
}
@@ -328,6 +332,18 @@ void QgsFeatureListView::selectRow( const QModelIndex &index, bool anchor )
328332
mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
329333
}
330334

335+
void QgsFeatureListView::ensureEditSelection()
336+
{
337+
QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
338+
// If there is no selection or an invalid selection (and there would be something we could select) : select it
339+
if ( ( selectedIndexes.isEmpty()
340+
|| mModel->mapFromMaster( selectedIndexes.first() ).row() == -1 )
341+
&& mModel->rowCount() )
342+
{
343+
mCurrentEditSelectionModel->select( mModel->mapToMaster( mModel->index( 0, 0 ) ), QItemSelectionModel::Select );
344+
}
345+
}
346+
331347
void QgsFeatureListView::setFeatureSelectionManager( QgsIFeatureSelectionManager *featureSelectionManager )
332348
{
333349
delete mFeatureSelectionManager;

src/gui/attributetable/qgsfeaturelistview.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,15 @@ class GUI_EXPORT QgsFeatureListView : public QListView
174174
private slots:
175175
void editSelectionChanged( const QItemSelection &deselected, const QItemSelection &selected );
176176

177+
/**
178+
* Make sure, there is an edit selection. If there is none, choose the first item.
179+
*/
180+
void ensureEditSelection();
181+
177182
private:
178183
void selectRow( const QModelIndex &index, bool anchor );
179184

185+
180186
QgsFeatureListModel *mModel = nullptr;
181187
QItemSelectionModel *mCurrentEditSelectionModel = nullptr;
182188
QgsFeatureSelectionModel *mFeatureSelectionModel = nullptr;

0 commit comments

Comments
 (0)