Skip to content

Commit

Permalink
[attributetable] Adjust the edit selection when the filter changes
Browse files Browse the repository at this point in the history
Make sure that we always have a feature on the form that matches the current filter condition
  • Loading branch information
m-kuhn committed Dec 14, 2017
1 parent 0f6a5c8 commit 4312a84
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -48,7 +48,6 @@ QgsDualView::QgsDualView( QWidget *parent )

mConditionalFormatWidget->hide();


mPreviewColumnsMenu = new QMenu( this );
mActionPreviewColumnsMenu->setMenu( mPreviewColumnsMenu );

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

void QgsDualView::mFeatureList_currentEditSelectionChanged( const QgsFeature &feat )
{
if ( !mLayer->isEditable() || mAttributeForm->save() )
if ( !mAttributeForm )
{
mTempAttributeFormFeature = feat;
}
else if ( !mLayer->isEditable() || mAttributeForm->save() )
{
mAttributeForm->setFeature( feat );
setCurrentEditSelection( QgsFeatureIds() << feat.id() );
Expand Down
3 changes: 3 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Expand Up @@ -374,6 +374,9 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
QgsAttributeTableConfig mConfig;
QgsScrollArea *mAttributeEditorScrollArea = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;
// If the current feature is set, while the form is still not initialized
// we will temporarily save it in here and set it on init
QgsFeature mTempAttributeFormFeature;

friend class TestQgsDualView;
friend class TestQgsAttributeTable;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/attributetable/qgsfeaturelistmodel.cpp
Expand Up @@ -45,6 +45,8 @@ void QgsFeatureListModel::setSourceModel( QgsAttributeTableFilterModel *sourceMo
// propagate sort order changes from source model to views connected to this model
connect( mFilterModel, &QAbstractItemModel::layoutAboutToBeChanged, this, &QAbstractItemModel::layoutAboutToBeChanged );
connect( mFilterModel, &QAbstractItemModel::layoutChanged, this, &QAbstractItemModel::layoutChanged );
connect( mFilterModel, &QAbstractItemModel::modelAboutToBeReset, this, &QAbstractItemModel::modelAboutToBeReset );
connect( mFilterModel, &QAbstractItemModel::modelReset, this, &QAbstractItemModel::modelReset );
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/gui/attributetable/qgsfeaturelistview.cpp
Expand Up @@ -76,6 +76,9 @@ void QgsFeatureListView::setModel( QgsFeatureListModel *featureListModel )
this, static_cast<void ( QgsFeatureListView::* )()>( &QgsFeatureListView::repaintRequested ) );
connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged, this, &QgsFeatureListView::editSelectionChanged );
connect( mModel->layerCache()->layer(), &QgsVectorLayer::attributeValueChanged, this, [ = ] { repaintRequested(); } );
connect( featureListModel, &QgsFeatureListModel::rowsRemoved, this, &QgsFeatureListView::ensureEditSelection );
connect( featureListModel, &QgsFeatureListModel::rowsInserted, this, &QgsFeatureListView::ensureEditSelection );
connect( featureListModel, &QgsFeatureListModel::modelReset, this, &QgsFeatureListView::ensureEditSelection );
}

bool QgsFeatureListView::setDisplayExpression( const QString &expression )
Expand Down Expand Up @@ -104,7 +107,8 @@ QString QgsFeatureListView::parserErrorString()
QgsFeatureIds QgsFeatureListView::currentEditSelection()
{
QgsFeatureIds selection;
Q_FOREACH ( const QModelIndex &idx, mCurrentEditSelectionModel->selectedIndexes() )
const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
for ( const QModelIndex &idx : selectedIndexes )
{
selection << idx.data( QgsAttributeTableModel::FeatureIdRole ).value<QgsFeatureId>();
}
Expand Down Expand Up @@ -328,6 +332,18 @@ void QgsFeatureListView::selectRow( const QModelIndex &index, bool anchor )
mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
}

void QgsFeatureListView::ensureEditSelection()
{
QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
// If there is no selection or an invalid selection (and there would be something we could select) : select it
if ( ( selectedIndexes.isEmpty()
|| mModel->mapFromMaster( selectedIndexes.first() ).row() == -1 )
&& mModel->rowCount() )
{
mCurrentEditSelectionModel->select( mModel->mapToMaster( mModel->index( 0, 0 ) ), QItemSelectionModel::Select );
}
}

void QgsFeatureListView::setFeatureSelectionManager( QgsIFeatureSelectionManager *featureSelectionManager )
{
delete mFeatureSelectionManager;
Expand Down
6 changes: 6 additions & 0 deletions src/gui/attributetable/qgsfeaturelistview.h
Expand Up @@ -174,9 +174,15 @@ class GUI_EXPORT QgsFeatureListView : public QListView
private slots:
void editSelectionChanged( const QItemSelection &deselected, const QItemSelection &selected );

/**
* Make sure, there is an edit selection. If there is none, choose the first item.
*/
void ensureEditSelection();

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


QgsFeatureListModel *mModel = nullptr;
QItemSelectionModel *mCurrentEditSelectionModel = nullptr;
QgsFeatureSelectionModel *mFeatureSelectionModel = nullptr;
Expand Down

0 comments on commit 4312a84

Please sign in to comment.