Skip to content

Commit

Permalink
Add an interface for creation of QgsLayoutView context menus
Browse files Browse the repository at this point in the history
Allows display of custom right click menus when right click
events are not handled by the current layout view tool.
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent cbc5782 commit f1dfd3d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
40 changes: 40 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -90,6 +90,19 @@ class QgsLayoutView: QGraphicsView
.. seealso:: setHorizontalRuler()
%End

void setMenuProvider( QgsLayoutViewMenuProvider *provider /Transfer/ );
%Docstring
Sets a ``provider`` for context menus. Ownership of the provider is transferred to the view.
.. seealso:: menuProvider()
%End

QgsLayoutViewMenuProvider *menuProvider() const;
%Docstring
Returns the provider for context menus. Returned value may be None if no provider is set.
.. seealso:: setMenuProvider()
:rtype: QgsLayoutViewMenuProvider
%End

public slots:

void zoomFull();
Expand Down Expand Up @@ -191,6 +204,33 @@ class QgsLayoutView: QGraphicsView

};


class QgsLayoutViewMenuProvider
{
%Docstring

Interface for a QgsLayoutView context menu.

Implementations of this interface can be made to allow QgsLayoutView
instances to provide custom context menus (opened upon right-click).

.. seealso:: QgsLayoutView
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutview.h"
%End
public:
virtual ~QgsLayoutViewMenuProvider();

virtual QMenu *createContextMenu( QWidget *parent /Transfer/, QgsLayout *layout, QPointF layoutPoint ) const = 0 /Factory/;
%Docstring
Return a newly created menu instance (or null pointer on error)
:rtype: QMenu
%End
};

/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
20 changes: 20 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsapplication.h"
#include <memory>
#include <QDesktopWidget>
#include <QMenu>

#define MIN_VIEW_SCALE 0.05
#define MAX_VIEW_SCALE 1000.0
Expand Down Expand Up @@ -139,6 +140,16 @@ void QgsLayoutView::setVerticalRuler( QgsLayoutRuler *ruler )
updateRulers();
}

void QgsLayoutView::setMenuProvider( QgsLayoutViewMenuProvider *provider )
{
mMenuProvider.reset( provider );
}

QgsLayoutViewMenuProvider *QgsLayoutView::menuProvider() const
{
return mMenuProvider.get();
}

void QgsLayoutView::zoomFull()
{
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
Expand Down Expand Up @@ -205,6 +216,15 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
setTool( mMidMouseButtonPanTool );
event->accept();
}
else if ( event->button() == Qt::RightButton && mMenuProvider )
{
QMenu *menu = mMenuProvider->createContextMenu( this, currentLayout(), mapToScene( event->pos() ) );
if ( menu )
{
menu->exec( event->globalPos() );
delete menu;
}
}
else
{
QGraphicsView::mousePressEvent( event );
Expand Down
37 changes: 37 additions & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -22,13 +22,16 @@
#include "qgis_gui.h"
#include <QPointer>
#include <QGraphicsView>
#include <memory>

class QMenu;
class QgsLayout;
class QgsLayoutViewTool;
class QgsLayoutViewToolTemporaryKeyPan;
class QgsLayoutViewToolTemporaryKeyZoom;
class QgsLayoutViewToolTemporaryMousePan;
class QgsLayoutRuler;
class QgsLayoutViewMenuProvider;

/**
* \ingroup gui
Expand Down Expand Up @@ -112,6 +115,18 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
*/
void setVerticalRuler( QgsLayoutRuler *ruler );

/**
* Sets a \a provider for context menus. Ownership of the provider is transferred to the view.
* \see menuProvider()
*/
void setMenuProvider( QgsLayoutViewMenuProvider *provider SIP_TRANSFER );

/**
* Returns the provider for context menus. Returned value may be nullptr if no provider is set.
* \see setMenuProvider()
*/
QgsLayoutViewMenuProvider *menuProvider() const;

public slots:

/**
Expand Down Expand Up @@ -228,9 +243,31 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView

QgsLayoutRuler *mHorizontalRuler = nullptr;
QgsLayoutRuler *mVerticalRuler = nullptr;
std::unique_ptr< QgsLayoutViewMenuProvider > mMenuProvider;

friend class TestQgsLayoutView;

};


/**
* \ingroup gui
*
* Interface for a QgsLayoutView context menu.
*
* Implementations of this interface can be made to allow QgsLayoutView
* instances to provide custom context menus (opened upon right-click).
*
* \see QgsLayoutView
* \since QGIS 3.0
*/
class GUI_EXPORT QgsLayoutViewMenuProvider
{
public:
virtual ~QgsLayoutViewMenuProvider() = default;

//! Return a newly created menu instance (or null pointer on error)
virtual QMenu *createContextMenu( QWidget *parent SIP_TRANSFER, QgsLayout *layout, QPointF layoutPoint ) const = 0 SIP_FACTORY;
};

#endif // QGSLAYOUTVIEW_H

0 comments on commit f1dfd3d

Please sign in to comment.