Attribute table pass fid on ctx menu signals

instead of passing an index that could belong to
one of the (three) models that live in the dual
view.

This hopefully makes the code a bit less error
prone and readable.

Fixes #32952
This commit is contained in:
Alessandro Pasotti 2019-11-20 09:52:16 +01:00 committed by Nyall Dawson
parent ea38fcec06
commit bcdbc3a85d
8 changed files with 21 additions and 22 deletions

View File

@ -126,13 +126,12 @@ Saves geometry to the settings on close
signals:
void willShowContextMenu( QMenu *menu, const QModelIndex &atIndex );
void willShowContextMenu( QMenu *menu, const QgsFeatureId featureId );
%Docstring
Emitted in order to provide a hook to add additional* menu entries to the context menu.
:param menu: If additional QMenuItems are added, they will show up in the context menu.
:param atIndex: The QModelIndex, to which the context menu belongs. Relative to the source model.
In most cases, this will be a :py:class:`QgsAttributeTableFilterModel`
:param featureId: The ID of the current feature
%End
void columnResized( int column, int width );

View File

@ -138,12 +138,12 @@ Emitted whenever the display expression is successfully changed
%End
void willShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex );
void willShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId );
%Docstring
Emitted when the context menu is created to add the specific actions to it
:param menu: is the already created context menu
:param atIndex: is the position of the current feature in the model
:param featureId: is the ID of the current feature
%End
public slots:

View File

@ -363,7 +363,7 @@ void QgsAttributeTableView::contextMenuEvent( QContextMenuEvent *event )
mActionPopup->addAction( tr( "Select All" ), this, SLOT( selectAll() ), QKeySequence::SelectAll );
// let some other parts of the application add some actions
emit willShowContextMenu( mActionPopup, idx );
emit willShowContextMenu( mActionPopup, mFilterModel->rowToId( idx ) );
if ( !mActionPopup->actions().isEmpty() )
{

View File

@ -144,10 +144,9 @@ class GUI_EXPORT QgsAttributeTableView : public QTableView
* Emitted in order to provide a hook to add additional* menu entries to the context menu.
*
* \param menu If additional QMenuItems are added, they will show up in the context menu.
* \param atIndex The QModelIndex, to which the context menu belongs. Relative to the source model.
* In most cases, this will be a QgsAttributeTableFilterModel
* \param featureId The ID of the current feature
*/
void willShowContextMenu( QMenu *menu, const QModelIndex &atIndex );
void willShowContextMenu( QMenu *menu, const QgsFeatureId featureId );
/**
* Emitted when a column in the view has been resized.

View File

@ -657,14 +657,19 @@ void QgsDualView::hideEvent( QHideEvent *event )
saveRecentDisplayExpressions();
}
void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atIndex )
void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QgsFeatureId featureId )
{
if ( !menu )
{
return;
}
QModelIndex sourceIndex = mFilterModel->mapToSource( atIndex );
QModelIndex sourceIndex = mFilterModel->fidToIndex( featureId );
if ( ! sourceIndex.isValid() )
{
return;
}
QAction *copyContentAction = new QAction( tr( "Copy Cell Content" ), this );
copyContentAction->setData( QVariant::fromValue<QModelIndex>( sourceIndex ) );
@ -722,9 +727,9 @@ void QgsDualView::viewWillShowContextMenu( QMenu *menu, const QModelIndex &atInd
}
void QgsDualView::widgetWillShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex )
void QgsDualView::widgetWillShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId )
{
emit showContextMenuExternally( menu, mFilterModel->rowToId( atIndex ) );
emit showContextMenuExternally( menu, featureId );
}

View File

@ -312,9 +312,9 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
void previewColumnChanged( QAction *previewAction, const QString &expression );
void viewWillShowContextMenu( QMenu *menu, const QModelIndex &atIndex );
void viewWillShowContextMenu( QMenu *menu, const QgsFeatureId featureId );
void widgetWillShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex );
void widgetWillShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId );
void showViewHeaderMenu( QPoint point );

View File

@ -325,11 +325,7 @@ void QgsFeatureListView::contextMenuEvent( QContextMenuEvent *event )
QgsActionMenu *menu = new QgsActionMenu( mModel->layerCache()->layer(), feature, QStringLiteral( "Feature" ), this );
// Index is from feature list model, but we need an index from the
// filter model to be passed to listeners, using fid instead would
// have been much better in term of bugs (and headaches) but this
// belongs to the API unfortunately.
emit willShowContextMenu( menu, mModel->mapToSource( index ) );
emit willShowContextMenu( menu, feature.id() );
menu->exec( event->globalPos() );
}

View File

@ -156,9 +156,9 @@ class GUI_EXPORT QgsFeatureListView : public QListView
/**
* Emitted when the context menu is created to add the specific actions to it
* \param menu is the already created context menu
* \param atIndex is the position of the current feature in the model
* \param featureId is the ID of the current feature
*/
void willShowContextMenu( QgsActionMenu *menu, const QModelIndex &atIndex );
void willShowContextMenu( QgsActionMenu *menu, const QgsFeatureId featureId );
public slots: