@@ -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 )
@@ -334,17 +338,87 @@ void QgsFeatureListView::selectRow( const QModelIndex &index, bool anchor )
334
338
mFeatureSelectionModel ->selectFeatures ( QItemSelection ( tl, br ), command );
335
339
}
336
340
337
- void QgsFeatureListView::ensureEditSelection ()
341
+ void QgsFeatureListView::ensureEditSelection ( bool inSelection )
338
342
{
339
- QModelIndexList selectedIndexes = mCurrentEditSelectionModel ->selectedIndexes ();
340
- // If there is no selection or an invalid selection (and there would be something we could select) : select it
341
- if ( ( selectedIndexes.isEmpty ()
342
- || mModel ->mapFromMaster ( selectedIndexes.first () ).row () == -1 )
343
- && mModel ->rowCount () )
343
+ if ( !mModel ->rowCount () )
344
+ return ;
345
+
346
+ const QModelIndexList selectedIndexes = mCurrentEditSelectionModel ->selectedIndexes ();
347
+
348
+ // We potentially want a new edit selection
349
+ // If we it should be in the feature selection
350
+ // but we don't find a matching one we might
351
+ // still stick to the old edit selection
352
+ bool editSelectionUpdateRequested = false ;
353
+ // There is a valid selection available which we
354
+ // could fall back to
355
+ bool validEditSelectionAvailable = false ;
356
+
357
+ if ( selectedIndexes.isEmpty () || mModel ->mapFromMaster ( selectedIndexes.first () ).row () == -1 )
358
+ {
359
+ validEditSelectionAvailable = false ;
360
+ }
361
+ else
362
+ {
363
+ validEditSelectionAvailable = true ;
364
+ }
365
+
366
+ // If we want to force the edit selection to be within the feature selection
367
+ // let's do some additional checks
368
+ if ( inSelection )
369
+ {
370
+ // no valid edit selection, update anyway
371
+ if ( !validEditSelectionAvailable )
372
+ {
373
+ editSelectionUpdateRequested = true ;
374
+ }
375
+ else
376
+ {
377
+ // valid selection: update only if it's not in the feature selection
378
+ const QgsFeatureIds selectedFids = layerCache ()->layer ()->selectedFeatureIds ();
379
+
380
+ if ( !selectedFids.contains ( mModel ->idxToFid ( mModel ->mapFromMaster ( selectedIndexes.first () ) ) ) )
381
+ {
382
+ editSelectionUpdateRequested = true ;
383
+ }
384
+ }
385
+ }
386
+ else
387
+ {
388
+ // we don't care if the edit selection is in the feature selection?
389
+ // well then, only update if there is no valid edit selection availble
390
+ if ( !validEditSelectionAvailable )
391
+ editSelectionUpdateRequested = true ;
392
+ }
393
+
394
+ if ( editSelectionUpdateRequested )
344
395
{
345
- QTimer::singleShot ( 0 , this , [ this ]()
396
+ QTimer::singleShot ( 0 , this , [ this , inSelection, validEditSelectionAvailable ]()
346
397
{
347
- setEditSelection ( mModel ->mapToMaster ( mModel ->index ( 0 , 0 ) ), QItemSelectionModel::ClearAndSelect );
398
+ int rowToSelect = -1 ;
399
+
400
+ if ( inSelection )
401
+ {
402
+ const QgsFeatureIds selectedFids = layerCache ()->layer ()->selectedFeatureIds ();
403
+ const int rowCount = mModel ->rowCount ();
404
+
405
+ for ( int i = 0 ; i < rowCount; i++ )
406
+ {
407
+ if ( selectedFids.contains ( mModel ->idxToFid ( mModel ->index ( i, 0 ) ) ) )
408
+ {
409
+ rowToSelect = i;
410
+ break ;
411
+ }
412
+
413
+ if ( rowToSelect == -1 && !validEditSelectionAvailable )
414
+ rowToSelect = 0 ;
415
+ }
416
+ }
417
+ else
418
+ rowToSelect = 0 ;
419
+
420
+ if ( rowToSelect != -1 )
421
+ setEditSelection ( mModel ->mapToMaster ( mModel ->index ( rowToSelect, 0 ) ), QItemSelectionModel::ClearAndSelect );
348
422
} );
349
423
}
350
424
}
0 commit comments