Skip to content

Commit ae89b4f

Browse files
authoredMay 18, 2018
Merge pull request #7015 from m-kuhn/ensureEditSelectionOnFeatureSelectionChange
Update currently edited feature in attribute table when selection changes
2 parents bc25781 + 66da1b0 commit ae89b4f

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed
 

‎src/gui/attributetable/qgsfeaturelistview.cpp

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ void QgsFeatureListView::setModel( QgsFeatureListModel *featureListModel )
5858

5959
mFeatureSelectionModel = new QgsFeatureSelectionModel( featureListModel, featureListModel, mFeatureSelectionManager, this );
6060
setSelectionModel( mFeatureSelectionModel );
61+
connect( featureListModel->layerCache()->layer(), &QgsVectorLayer::selectionChanged, this, [ this ]()
62+
{
63+
ensureEditSelection( true );
64+
} );
6165

6266
if ( mItemDelegate && mItemDelegate->parent() == this )
6367
{
@@ -75,9 +79,9 @@ void QgsFeatureListView::setModel( QgsFeatureListModel *featureListModel )
7579
this, static_cast<void ( QgsFeatureListView::* )()>( &QgsFeatureListView::repaintRequested ) );
7680
connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged, this, &QgsFeatureListView::editSelectionChanged );
7781
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(); } );
8185
}
8286

8387
bool QgsFeatureListView::setDisplayExpression( const QString &expression )
@@ -339,17 +343,87 @@ void QgsFeatureListView::selectRow( const QModelIndex &index, bool anchor )
339343
mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
340344
}
341345

342-
void QgsFeatureListView::ensureEditSelection()
346+
void QgsFeatureListView::ensureEditSelection( bool inSelection )
343347
{
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 )
349400
{
350-
QTimer::singleShot( 0, this, [ this ]()
401+
QTimer::singleShot( 0, this, [ this, inSelection, validEditSelectionAvailable ]()
351402
{
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 );
353427
} );
354428
}
355429
}

‎src/gui/attributetable/qgsfeaturelistview.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,11 @@ class GUI_EXPORT QgsFeatureListView : public QListView
184184

185185
/**
186186
* Make sure, there is an edit selection. If there is none, choose the first item.
187+
* If \a inSelection is set to true, the edit selection is done in selected entries if
188+
* there is a selected entry visible.
189+
*
187190
*/
188-
void ensureEditSelection();
191+
void ensureEditSelection( bool inSelection = false );
189192

190193
private:
191194
void selectRow( const QModelIndex &index, bool anchor );

0 commit comments

Comments
 (0)
Please sign in to comment.