Skip to content

Commit

Permalink
Add a "contextMenuAboutToShow" signal to QgsLayerTreeView
Browse files Browse the repository at this point in the history
Allows plugins to hook into this menu and add custom entries
and customise the standard entries

(cherry picked from commit a79adfc)
  • Loading branch information
nyalldawson committed Mar 16, 2023
1 parent 73ecf0c commit 2f48b1e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
9 changes: 9 additions & 0 deletions python/gui/auto_generated/layertree/qgslayertreeview.sip.in
Expand Up @@ -351,6 +351,15 @@ Returns the show private layers status
void currentLayerChanged( QgsMapLayer *layer );
%Docstring
Emitted when a current layer is changed
%End

void contextMenuAboutToShow( QMenu *menu );
%Docstring
Emitted when the context menu is about to show.

Allows customisation of the menu.

.. versionadded:: 3.32
%End

protected:
Expand Down
11 changes: 8 additions & 3 deletions src/gui/layertree/qgslayertreeview.cpp
Expand Up @@ -178,9 +178,14 @@ void QgsLayerTreeView::contextMenuEvent( QContextMenuEvent *event )
setCurrentIndex( QModelIndex() );

QMenu *menu = mMenuProvider->createContextMenu();
if ( menu && menu->actions().count() != 0 )
menu->exec( mapToGlobal( event->pos() ) );
delete menu;
if ( menu )
{
emit contextMenuAboutToShow( menu );

if ( menu->actions().count() != 0 )
menu->exec( mapToGlobal( event->pos() ) );
delete menu;
}
}


Expand Down
9 changes: 9 additions & 0 deletions src/gui/layertree/qgslayertreeview.h
Expand Up @@ -350,6 +350,15 @@ class GUI_EXPORT QgsLayerTreeView : public QTreeView
//! Emitted when a current layer is changed
void currentLayerChanged( QgsMapLayer *layer );

/**
* Emitted when the context menu is about to show.
*
* Allows customisation of the menu.
*
* \since QGIS 3.32
*/
void contextMenuAboutToShow( QMenu *menu );

protected:
void contextMenuEvent( QContextMenuEvent *event ) override;

Expand Down

0 comments on commit 2f48b1e

Please sign in to comment.