Skip to content

Commit f1dfd3d

Browse files
committedJul 25, 2017
Add an interface for creation of QgsLayoutView context menus
Allows display of custom right click menus when right click events are not handled by the current layout view tool.
1 parent cbc5782 commit f1dfd3d

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
 

‎python/gui/layout/qgslayoutview.sip

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ class QgsLayoutView: QGraphicsView
9090
.. seealso:: setHorizontalRuler()
9191
%End
9292

93+
void setMenuProvider( QgsLayoutViewMenuProvider *provider /Transfer/ );
94+
%Docstring
95+
Sets a ``provider`` for context menus. Ownership of the provider is transferred to the view.
96+
.. seealso:: menuProvider()
97+
%End
98+
99+
QgsLayoutViewMenuProvider *menuProvider() const;
100+
%Docstring
101+
Returns the provider for context menus. Returned value may be None if no provider is set.
102+
.. seealso:: setMenuProvider()
103+
:rtype: QgsLayoutViewMenuProvider
104+
%End
105+
93106
public slots:
94107

95108
void zoomFull();
@@ -191,6 +204,33 @@ class QgsLayoutView: QGraphicsView
191204

192205
};
193206

207+
208+
class QgsLayoutViewMenuProvider
209+
{
210+
%Docstring
211+
212+
Interface for a QgsLayoutView context menu.
213+
214+
Implementations of this interface can be made to allow QgsLayoutView
215+
instances to provide custom context menus (opened upon right-click).
216+
217+
.. seealso:: QgsLayoutView
218+
.. versionadded:: 3.0
219+
%End
220+
221+
%TypeHeaderCode
222+
#include "qgslayoutview.h"
223+
%End
224+
public:
225+
virtual ~QgsLayoutViewMenuProvider();
226+
227+
virtual QMenu *createContextMenu( QWidget *parent /Transfer/, QgsLayout *layout, QPointF layoutPoint ) const = 0 /Factory/;
228+
%Docstring
229+
Return a newly created menu instance (or null pointer on error)
230+
:rtype: QMenu
231+
%End
232+
};
233+
194234
/************************************************************************
195235
* This file has been generated automatically from *
196236
* *

‎src/gui/layout/qgslayoutview.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "qgsapplication.h"
2929
#include <memory>
3030
#include <QDesktopWidget>
31+
#include <QMenu>
3132

3233
#define MIN_VIEW_SCALE 0.05
3334
#define MAX_VIEW_SCALE 1000.0
@@ -139,6 +140,16 @@ void QgsLayoutView::setVerticalRuler( QgsLayoutRuler *ruler )
139140
updateRulers();
140141
}
141142

143+
void QgsLayoutView::setMenuProvider( QgsLayoutViewMenuProvider *provider )
144+
{
145+
mMenuProvider.reset( provider );
146+
}
147+
148+
QgsLayoutViewMenuProvider *QgsLayoutView::menuProvider() const
149+
{
150+
return mMenuProvider.get();
151+
}
152+
142153
void QgsLayoutView::zoomFull()
143154
{
144155
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
@@ -205,6 +216,15 @@ void QgsLayoutView::mousePressEvent( QMouseEvent *event )
205216
setTool( mMidMouseButtonPanTool );
206217
event->accept();
207218
}
219+
else if ( event->button() == Qt::RightButton && mMenuProvider )
220+
{
221+
QMenu *menu = mMenuProvider->createContextMenu( this, currentLayout(), mapToScene( event->pos() ) );
222+
if ( menu )
223+
{
224+
menu->exec( event->globalPos() );
225+
delete menu;
226+
}
227+
}
208228
else
209229
{
210230
QGraphicsView::mousePressEvent( event );

‎src/gui/layout/qgslayoutview.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
#include "qgis_gui.h"
2323
#include <QPointer>
2424
#include <QGraphicsView>
25+
#include <memory>
2526

27+
class QMenu;
2628
class QgsLayout;
2729
class QgsLayoutViewTool;
2830
class QgsLayoutViewToolTemporaryKeyPan;
2931
class QgsLayoutViewToolTemporaryKeyZoom;
3032
class QgsLayoutViewToolTemporaryMousePan;
3133
class QgsLayoutRuler;
34+
class QgsLayoutViewMenuProvider;
3235

3336
/**
3437
* \ingroup gui
@@ -112,6 +115,18 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
112115
*/
113116
void setVerticalRuler( QgsLayoutRuler *ruler );
114117

118+
/**
119+
* Sets a \a provider for context menus. Ownership of the provider is transferred to the view.
120+
* \see menuProvider()
121+
*/
122+
void setMenuProvider( QgsLayoutViewMenuProvider *provider SIP_TRANSFER );
123+
124+
/**
125+
* Returns the provider for context menus. Returned value may be nullptr if no provider is set.
126+
* \see setMenuProvider()
127+
*/
128+
QgsLayoutViewMenuProvider *menuProvider() const;
129+
115130
public slots:
116131

117132
/**
@@ -228,9 +243,31 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
228243

229244
QgsLayoutRuler *mHorizontalRuler = nullptr;
230245
QgsLayoutRuler *mVerticalRuler = nullptr;
246+
std::unique_ptr< QgsLayoutViewMenuProvider > mMenuProvider;
231247

232248
friend class TestQgsLayoutView;
233249

234250
};
235251

252+
253+
/**
254+
* \ingroup gui
255+
*
256+
* Interface for a QgsLayoutView context menu.
257+
*
258+
* Implementations of this interface can be made to allow QgsLayoutView
259+
* instances to provide custom context menus (opened upon right-click).
260+
*
261+
* \see QgsLayoutView
262+
* \since QGIS 3.0
263+
*/
264+
class GUI_EXPORT QgsLayoutViewMenuProvider
265+
{
266+
public:
267+
virtual ~QgsLayoutViewMenuProvider() = default;
268+
269+
//! Return a newly created menu instance (or null pointer on error)
270+
virtual QMenu *createContextMenu( QWidget *parent SIP_TRANSFER, QgsLayout *layout, QPointF layoutPoint ) const = 0 SIP_FACTORY;
271+
};
272+
236273
#endif // QGSLAYOUTVIEW_H

0 commit comments

Comments
 (0)
Please sign in to comment.