Skip to content

Commit

Permalink
Attribute table pass fid on ctx menu signals
Browse files Browse the repository at this point in the history
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

(cherry picked from commit bcdbc3a)
  • Loading branch information
elpaso authored and nyalldawson committed Nov 21, 2019
1 parent d5543b6 commit 3908247
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 22 deletions.
Expand Up @@ -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 );
Expand Down
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsattributetableview.cpp
Expand Up @@ -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() )
{
Expand Down
5 changes: 2 additions & 3 deletions src/gui/attributetable/qgsattributetableview.h
Expand Up @@ -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.
Expand Down
13 changes: 9 additions & 4 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 );
}


Expand Down
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsdualview.h
Expand Up @@ -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 );

Expand Down
6 changes: 1 addition & 5 deletions src/gui/attributetable/qgsfeaturelistview.cpp
Expand Up @@ -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() );
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/attributetable/qgsfeaturelistview.h
Expand Up @@ -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:

Expand Down

0 comments on commit 3908247

Please sign in to comment.