Skip to content

Commit

Permalink
Add api for creating context menus for history nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 23, 2023
1 parent 1806c6e commit 363bf0c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
6 changes: 6 additions & 0 deletions python/gui/auto_generated/history/qgshistoryentrynode.sip.in
Expand Up @@ -70,6 +70,12 @@ create the node's content.
Called when the node is double-clicked. The default implementation does nothing.
%End

virtual void populateContextMenu( QMenu *menu, const QgsHistoryWidgetContext &context );
%Docstring
Allows the node to populate a context ``menu`` before display to the user.

Actions should be parented to the specified ``menu``.
%End

virtual bool matchesString( const QString &searchString ) const;
%Docstring
Expand Down
6 changes: 2 additions & 4 deletions src/gui/history/qgshistoryentrynode.cpp
Expand Up @@ -41,12 +41,10 @@ void QgsHistoryEntryNode::doubleClicked( const QgsHistoryWidgetContext & )

}

#if 0
QList<QAction *> QgsHistoryEntryNode::actions( QWidget * )
void QgsHistoryEntryNode::populateContextMenu( QMenu *, const QgsHistoryWidgetContext & )
{
return {};

}
#endif

bool QgsHistoryEntryNode::matchesString( const QString &string ) const
{
Expand Down
13 changes: 4 additions & 9 deletions src/gui/history/qgshistoryentrynode.h
Expand Up @@ -26,6 +26,7 @@

class QWidget;
class QAction;
class QMenu;
class QgsHistoryEntryGroup;
class QgsHistoryWidgetContext;

Expand Down Expand Up @@ -92,18 +93,12 @@ class GUI_EXPORT QgsHistoryEntryNode
*/
virtual void doubleClicked( const QgsHistoryWidgetContext &context );

#if 0 // currently unused

/**
* Returns a list of actions which users can trigger to interact with the history
* entry. Buttons corresponding to each action will be automatically created and
* shown to users.
* Allows the node to populate a context \a menu before display to the user.
*
* Actions should be parented to the specified \a parent widget.
* Actions should be parented to the specified \a menu.
*/
virtual QList< QAction * > actions( const QgsHistoryWidgetContext &context, QWidget *parent );

#endif
virtual void populateContextMenu( QMenu *menu, const QgsHistoryWidgetContext &context );

/**
* Returns true if the node matches the specified \a searchString, and
Expand Down
20 changes: 20 additions & 0 deletions src/gui/history/qgshistorywidget.cpp
Expand Up @@ -19,6 +19,8 @@
#include "qgshistoryentrynode.h"

#include <QTextBrowser>
#include <QtGlobal>
#include <QMenu>

QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProviderBackends backends, QgsHistoryProviderRegistry *registry, const QgsHistoryWidgetContext &context, QWidget *parent )
: QgsPanelWidget( parent )
Expand All @@ -38,6 +40,9 @@ QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProv
connect( mTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsHistoryWidget::currentItemChanged );
connect( mTreeView, &QTreeView::doubleClicked, this, &QgsHistoryWidget::nodeDoubleClicked );

mTreeView->setContextMenuPolicy( Qt::CustomContextMenu );
connect( mTreeView, &QWidget::customContextMenuRequested, this, &QgsHistoryWidget::showNodeContextMenu );

// expand first group (usually most recent date group)
const QModelIndex firstGroup = mProxyModel->index( 0, 0, QModelIndex() );
mTreeView->expand( firstGroup );
Expand Down Expand Up @@ -86,6 +91,21 @@ void QgsHistoryWidget::nodeDoubleClicked( const QModelIndex &index )
}
}

void QgsHistoryWidget::showNodeContextMenu( const QPoint &pos )
{
if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( mTreeView->currentIndex() ) ) )
{
QMenu *menu = new QMenu();

node->populateContextMenu( menu, mContext );
if ( !menu->isEmpty() )
{
menu->exec( mTreeView->mapToGlobal( pos ) );
}
delete menu;
}
}

//
// QgsHistoryEntryProxyModel
//
Expand Down
1 change: 1 addition & 0 deletions src/gui/history/qgshistorywidget.h
Expand Up @@ -80,6 +80,7 @@ class GUI_EXPORT QgsHistoryWidget : public QgsPanelWidget, private Ui::QgsHistor

void currentItemChanged( const QModelIndex &selected, const QModelIndex &previous );
void nodeDoubleClicked( const QModelIndex &index );
void showNodeContextMenu( const QPoint &pos );

private:

Expand Down

0 comments on commit 363bf0c

Please sign in to comment.