Skip to content

Commit

Permalink
Merge pull request #40248 from vcloarec/selectContextMenu
Browse files Browse the repository at this point in the history
select feature context menu
  • Loading branch information
m-kuhn committed Dec 3, 2020
2 parents d080626 + ac19bd3 commit e1d4cf9
Show file tree
Hide file tree
Showing 9 changed files with 601 additions and 33 deletions.
23 changes: 23 additions & 0 deletions python/gui/auto_generated/qgsmaptool.sip.in
Expand Up @@ -11,6 +11,7 @@




%ModuleHeaderCode
// fix to allow compilation with sip 4.7 that for some reason
// doesn't add these includes to the file where the code from
Expand Down Expand Up @@ -210,6 +211,28 @@ The default implementation does nothing.
is present in :py:func:`~QgsMapTool.flags`.

.. versionadded:: 3.14
%End

virtual bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event );
%Docstring
Allows the tool to populate and customize the given ``menu``,
prior to showing it in response to a right-mouse button click.

``menu`` will be initially populated with a set of default, generic actions.
Any new actions added to the menu should be correctly parented to ``menu``.

A pointer to the map mouse ``event`` can be provided to allow particular behavior depending on the map tool.

This method can return true to inform the caller that the menu was effectively populated.

The default implementation does nothing and returns false.

.. note::

The context menu is only shown when the ShowContextMenu flag
is present in :py:func:`~QgsMapTool.flags`.

.. versionadded:: 3.18
%End

signals:
Expand Down
40 changes: 40 additions & 0 deletions src/app/qgsmaptoolselect.cpp
Expand Up @@ -18,14 +18,17 @@
#include "qgsmaptoolselectutils.h"
#include "qgsrubberband.h"
#include "qgsmapcanvas.h"
#include "qgsmapmouseevent.h"
#include "qgsvectorlayer.h"
#include "qgsgeometry.h"
#include "qgspointxy.h"
#include "qgis.h"
#include "qgsapplication.h"
#include "qgslogger.h"
#include "qgshighlight.h"

#include <QMouseEvent>
#include <QMenu>
#include <QRect>
#include <QColor>

Expand Down Expand Up @@ -138,6 +141,43 @@ QgsMapTool::Flags QgsMapToolSelect::flags() const
return QgsMapTool::flags();
}

bool QgsMapToolSelect::populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event )
{
Q_ASSERT( menu );
QgsVectorLayer *vlayer = QgsMapToolSelectUtils::getCurrentVectorLayer( mCanvas );

if ( !vlayer )
return false;

menu->addSeparator();

Qt::KeyboardModifiers modifiers = Qt::NoModifier;
QgsPointXY mapPoint;
if ( event )
{
modifiers = event->modifiers();
mapPoint = event->mapPoint();
}
QgsVectorLayer::SelectBehavior behavior = QgsVectorLayer::SetSelection;
if ( modifiers & Qt::ShiftModifier && modifiers & Qt::ControlModifier )
behavior = QgsVectorLayer::IntersectSelection;
else if ( modifiers & Qt::ShiftModifier )
behavior = QgsVectorLayer::AddToSelection;
else if ( modifiers & Qt::ControlModifier )
behavior = QgsVectorLayer::RemoveFromSelection;

QgsRectangle r = QgsMapToolSelectUtils::expandSelectRectangle( mapPoint, mCanvas, vlayer );

QgsMapToolSelectUtils::QgsMapToolSelectMenuActions *menuActions
= new QgsMapToolSelectUtils::QgsMapToolSelectMenuActions( mCanvas, vlayer, behavior, QgsGeometry::fromRect( r ), menu );

menuActions->populateMenu( menu );

// cppcheck wrongly believes menuActions will leak
// cppcheck-suppress memleak
return true;
}

void QgsMapToolSelect::selectFeatures( Qt::KeyboardModifiers modifiers )
{
if ( mSelectionHandler->selectionMode() == QgsMapToolSelectionHandler::SelectSimple &&
Expand Down
4 changes: 4 additions & 0 deletions src/app/qgsmaptoolselect.h
Expand Up @@ -21,6 +21,8 @@
#include "qgsmaptoolselectionhandler.h"

class QgsMapCanvas;
class QgsHighlight;

class QMouseEvent;

class APP_EXPORT QgsMapToolSelect : public QgsMapTool
Expand Down Expand Up @@ -52,6 +54,8 @@ class APP_EXPORT QgsMapToolSelect : public QgsMapTool
void deactivate() override;
Flags flags() const override;

bool populateContextMenuWithEvent( QMenu *menu, QgsMapMouseEvent *event ) override;

signals:

void modeChanged( Mode mode );
Expand Down

0 comments on commit e1d4cf9

Please sign in to comment.