Scroll to newly added feature in the attribute table view

Refs #37847
This commit is contained in:
Ivan Ivanov 2020-10-16 03:44:35 +03:00 committed by GitHub
parent b00d6fa61a
commit b7ff23aa33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 0 deletions

View File

@ -70,6 +70,16 @@ Returns the selected features in the attribute table in table sorted order.
%End
void scrollToFeature( const QgsFeatureId &fid, int column = -1 );
%Docstring
Scroll to a feature with a given ``fid``.
Optionally a ``column`` can be specified, which will also bring that column into view.
.. versionadded:: 3.16
%End
protected:
virtual void mousePressEvent( QMouseEvent *event );

View File

@ -627,6 +627,7 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeTable_triggered()
{
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() );
mMainView->tableView()->scrollToFeature( action.feature().id(), 0 );
}
}
@ -648,6 +649,7 @@ void QgsAttributeTableDialog::mActionAddFeatureViaAttributeForm_triggered()
{
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
mMainView->setCurrentEditSelection( QgsFeatureIds() << action.feature().id() );
mMainView->tableView()->scrollToFeature( action.feature().id(), 0 );
}
}

View File

@ -497,3 +497,20 @@ void QgsAttributeTableView::recreateActionWidgets()
}
mActionWidgets.clear();
}
void QgsAttributeTableView::scrollToFeature( const QgsFeatureId &fid, int col )
{
QModelIndex index = mFilterModel->fidToIndex( fid );
if ( !index.isValid() )
return;
scrollTo( index );
QModelIndex selectionIndex = index.sibling( index.row(), col );
if ( !selectionIndex.isValid() )
return;
selectionModel()->setCurrentIndex( index, QItemSelectionModel::SelectCurrent );
}

View File

@ -89,6 +89,16 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
QList<QgsFeatureId> selectedFeaturesIds() const;
/**
* Scroll to a feature with a given \a fid.
Optionally a \a column can be specified, which will also bring that column into view.
* \since QGIS 3.16
*/
void scrollToFeature( const QgsFeatureId &fid, int column = -1 );
protected:
/**