@@ -58,6 +58,10 @@ void QgsFeatureListView::setModel( QgsFeatureListModel *featureListModel )
58
58
59
59
mFeatureSelectionModel = new QgsFeatureSelectionModel ( featureListModel, featureListModel, mFeatureSelectionManager , this );
60
60
setSelectionModel ( mFeatureSelectionModel );
61
+ connect ( featureListModel->layerCache ()->layer (), &QgsVectorLayer::selectionChanged, this , [ this ]()
62
+ {
63
+ ensureEditSelection ( true );
64
+ } );
61
65
62
66
if ( mItemDelegate && mItemDelegate ->parent () == this )
63
67
{
@@ -75,9 +79,9 @@ void QgsFeatureListView::setModel( QgsFeatureListModel *featureListModel )
75
79
this , static_cast <void ( QgsFeatureListView::* )()>( &QgsFeatureListView::repaintRequested ) );
76
80
connect ( mCurrentEditSelectionModel , &QItemSelectionModel::selectionChanged, this , &QgsFeatureListView::editSelectionChanged );
77
81
connect ( mModel ->layerCache ()->layer (), &QgsVectorLayer::attributeValueChanged, this , [ = ] { repaintRequested (); } );
78
- connect ( featureListModel, &QgsFeatureListModel::rowsRemoved, this , &QgsFeatureListView:: ensureEditSelection );
79
- connect ( featureListModel, &QgsFeatureListModel::rowsInserted, this , &QgsFeatureListView:: ensureEditSelection );
80
- connect ( featureListModel, &QgsFeatureListModel::modelReset, this , &QgsFeatureListView:: ensureEditSelection );
82
+ connect ( featureListModel, &QgsFeatureListModel::rowsRemoved, this , [ this ]() { ensureEditSelection (); } );
83
+ connect ( featureListModel, &QgsFeatureListModel::rowsInserted, this , [ this ]() { ensureEditSelection (); } );
84
+ connect ( featureListModel, &QgsFeatureListModel::modelReset, this , [ this ]() { ensureEditSelection (); } );
81
85
}
82
86
83
87
bool QgsFeatureListView::setDisplayExpression ( const QString &expression )
@@ -339,17 +343,87 @@ void QgsFeatureListView::selectRow( const QModelIndex &index, bool anchor )
339
343
mFeatureSelectionModel ->selectFeatures ( QItemSelection ( tl, br ), command );
340
344
}
341
345
342
- void QgsFeatureListView::ensureEditSelection ()
346
+ void QgsFeatureListView::ensureEditSelection ( bool inSelection )
343
347
{
344
- QModelIndexList selectedIndexes = mCurrentEditSelectionModel ->selectedIndexes ();
345
- // If there is no selection or an invalid selection (and there would be something we could select) : select it
346
- if ( ( selectedIndexes.isEmpty ()
347
- || mModel ->mapFromMaster ( selectedIndexes.first () ).row () == -1 )
348
- && mModel ->rowCount () )
348
+ if ( !mModel ->rowCount () )
349
+ return ;
350
+
351
+ const QModelIndexList selectedIndexes = mCurrentEditSelectionModel ->selectedIndexes ();
352
+
353
+ // We potentially want a new edit selection
354
+ // If we it should be in the feature selection
355
+ // but we don't find a matching one we might
356
+ // still stick to the old edit selection
357
+ bool editSelectionUpdateRequested = false ;
358
+ // There is a valid selection available which we
359
+ // could fall back to
360
+ bool validEditSelectionAvailable = false ;
361
+
362
+ if ( selectedIndexes.isEmpty () || mModel ->mapFromMaster ( selectedIndexes.first () ).row () == -1 )
363
+ {
364
+ validEditSelectionAvailable = false ;
365
+ }
366
+ else
367
+ {
368
+ validEditSelectionAvailable = true ;
369
+ }
370
+
371
+ // If we want to force the edit selection to be within the feature selection
372
+ // let's do some additional checks
373
+ if ( inSelection )
374
+ {
375
+ // no valid edit selection, update anyway
376
+ if ( !validEditSelectionAvailable )
377
+ {
378
+ editSelectionUpdateRequested = true ;
379
+ }
380
+ else
381
+ {
382
+ // valid selection: update only if it's not in the feature selection
383
+ const QgsFeatureIds selectedFids = layerCache ()->layer ()->selectedFeatureIds ();
384
+
385
+ if ( !selectedFids.contains ( mModel ->idxToFid ( mModel ->mapFromMaster ( selectedIndexes.first () ) ) ) )
386
+ {
387
+ editSelectionUpdateRequested = true ;
388
+ }
389
+ }
390
+ }
391
+ else
392
+ {
393
+ // we don't care if the edit selection is in the feature selection?
394
+ // well then, only update if there is no valid edit selection available
395
+ if ( !validEditSelectionAvailable )
396
+ editSelectionUpdateRequested = true ;
397
+ }
398
+
399
+ if ( editSelectionUpdateRequested )
349
400
{
350
- QTimer::singleShot ( 0 , this , [ this ]()
401
+ QTimer::singleShot ( 0 , this , [ this , inSelection, validEditSelectionAvailable ]()
351
402
{
352
- setEditSelection ( mModel ->mapToMaster ( mModel ->index ( 0 , 0 ) ), QItemSelectionModel::ClearAndSelect );
403
+ int rowToSelect = -1 ;
404
+
405
+ if ( inSelection )
406
+ {
407
+ const QgsFeatureIds selectedFids = layerCache ()->layer ()->selectedFeatureIds ();
408
+ const int rowCount = mModel ->rowCount ();
409
+
410
+ for ( int i = 0 ; i < rowCount; i++ )
411
+ {
412
+ if ( selectedFids.contains ( mModel ->idxToFid ( mModel ->index ( i, 0 ) ) ) )
413
+ {
414
+ rowToSelect = i;
415
+ break ;
416
+ }
417
+
418
+ if ( rowToSelect == -1 && !validEditSelectionAvailable )
419
+ rowToSelect = 0 ;
420
+ }
421
+ }
422
+ else
423
+ rowToSelect = 0 ;
424
+
425
+ if ( rowToSelect != -1 )
426
+ setEditSelection ( mModel ->mapToMaster ( mModel ->index ( rowToSelect, 0 ) ), QItemSelectionModel::ClearAndSelect );
353
427
} );
354
428
}
355
429
}
0 commit comments