Skip to content
Permalink
Browse files

[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
  • Loading branch information
m-kuhn committed Oct 26, 2017
1 parent 0f6a5c8 commit 4312a8466fbf6cbcca775a972219e83223d70923
@@ -48,7 +48,6 @@ QgsDualView::QgsDualView( QWidget *parent )

mConditionalFormatWidget->hide();


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

@@ -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();
@@ -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() );
@@ -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;
@@ -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 );
}
}

@@ -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 )
@@ -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>();
}
@@ -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;
@@ -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;

0 comments on commit 4312a84

Please sign in to comment.
You can’t perform that action at this time.