Skip to content

Commit

Permalink
Merge pull request #7892 from PeterPetrik/identify_by_feature
Browse files Browse the repository at this point in the history
[feature] Identify/Select polygon from existing feature geometry (#19064)
  • Loading branch information
wonder-sk committed Sep 14, 2018
2 parents 6058e66 + 1d22006 commit 1192b63
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion python/gui/auto_generated/qgsidentifymenu.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ define if the menu executed can return multiple results (e.g. all results or all

void setExecWithSingleResult( bool execWithSingleResult );
%Docstring
define if the menu will be shown with a single idetify result
define if the menu will be shown with a single identify result
%End
bool execWithSingleResult();

Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolselect.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************
qgsmaptoolselect.cpp - map tool for selecting features by single click
qgsmaptoolselect.cpp - map tool for selecting features
----------------------
begin : January 2006
copyright : (C) 2006 by Martin Dobias
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolselect.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************
qgsmaptoolselect.h - map tool for selecting features by single click
qgsmaptoolselect.h - map tool for selecting features
---------------------
begin : May 2010
copyright : (C) 2010 by Jeremy Palmer
Expand Down
43 changes: 42 additions & 1 deletion src/app/qgsmaptoolselectionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "qgsmapmouseevent.h"
#include "qgsrubberband.h"
#include "qgssnapindicator.h"

#include "qgsidentifymenu.h"

/// @cond private

Expand Down Expand Up @@ -101,7 +101,10 @@ QgsMapToolSelectionHandler::QgsMapToolSelectionHandler( QgsMapCanvas *canvas, Qg
: mCanvas( canvas )
, mSelectionMode( selectionMode )
, mSnapIndicator( qgis::make_unique< QgsSnapIndicator >( canvas ) )
, mIdentifyMenu( new QgsIdentifyMenu( mCanvas ) )
{
mIdentifyMenu->setAllowMultipleReturn( false );
mIdentifyMenu->setExecWithSingleResult( true );
}

QgsMapToolSelectionHandler::~QgsMapToolSelectionHandler()
Expand Down Expand Up @@ -243,6 +246,44 @@ void QgsMapToolSelectionHandler::selectPolygonMoveEvent( QgsMapMouseEvent *e )

void QgsMapToolSelectionHandler::selectPolygonPressEvent( QgsMapMouseEvent *e )
{
// Handle immediate right-click on feature to show context menu
if ( !mSelectionRubberBand && ( e->button() == Qt::RightButton ) )
{
QList<QgsMapToolIdentify::IdentifyResult> results;
QMap< QString, QString > derivedAttributes;

const QgsPointXY mapPoint = toMapCoordinates( e->pos() );
double x = mapPoint.x(), y = mapPoint.y();
double sr = QgsMapTool::searchRadiusMU( mCanvas );

const QList<QgsMapLayer *> layers = mCanvas->layers();
for ( auto layer : layers )
{
if ( layer->type() == QgsMapLayer::VectorLayer )
{
auto vectorLayer = static_cast<QgsVectorLayer *>( layer );
if ( vectorLayer->geometryType() == QgsWkbTypes::PolygonGeometry )
{
QgsRectangle r = mCanvas->mapSettings().mapToLayerCoordinates( layer, QgsRectangle( x - sr, y - sr, x + sr, y + sr ) );
QgsFeatureIterator fit = vectorLayer->getFeatures( QgsFeatureRequest().setFilterRect( r ).setFlags( QgsFeatureRequest::ExactIntersect ) );
QgsFeature f;
while ( fit.nextFeature( f ) )
{
results << QgsMapToolIdentify::IdentifyResult( vectorLayer, f, derivedAttributes );
}
}
}
}

QPoint globalPos = mCanvas->mapToGlobal( QPoint( e->pos().x() + 5, e->pos().y() + 5 ) );
const QList<QgsMapToolIdentify::IdentifyResult> selectedFeatures = mIdentifyMenu->exec( results, globalPos );
if ( !selectedFeatures.empty() && selectedFeatures[0].mFeature.hasGeometry() )
setSelectedGeometry( selectedFeatures[0].mFeature.geometry(), e->modifiers() );

return;
}

// Handle definition of polygon by clicking points on cancas
if ( !mSelectionRubberBand )
initRubberBand();

Expand Down
8 changes: 6 additions & 2 deletions src/app/qgsmaptoolselectionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class QgsMapCanvas;
class QgsMapMouseEvent;
class QgsRubberBand;
class QgsSnapIndicator;
class QgsIdentifyMenu;

/// @cond private

Expand Down Expand Up @@ -88,7 +89,7 @@ class QgsMapToolSelectionHandler : public QObject
{
//! SelectSimple - single click or drawing a rectangle, default option
SelectSimple,
//! SelectPolygon - drawing a polygon
//! SelectPolygon - drawing a polygon or right-click on existing polygon feature
SelectPolygon,
//! SelectFreehand - free hand selection
SelectFreehand,
Expand Down Expand Up @@ -119,7 +120,7 @@ class QgsMapToolSelectionHandler : public QObject
void canvasMoveEvent( QgsMapMouseEvent *e );
//! Handles mouse press event from map tool
void canvasPressEvent( QgsMapMouseEvent *e );
//! Handles mouse releasae event from map tool
//! Handles mouse release event from map tool
void canvasReleaseEvent( QgsMapMouseEvent *e );
//! Handles escape press event - returns true if the even has been processed
bool keyReleaseEvent( QKeyEvent *e );
Expand Down Expand Up @@ -196,6 +197,9 @@ class QgsMapToolSelectionHandler : public QObject

QColor mFillColor = QColor( 254, 178, 76, 63 );
QColor mStrokeColor = QColor( 254, 58, 29, 100 );

//! Shows features to select polygon from existing features
QgsIdentifyMenu *mIdentifyMenu = nullptr; // owned by canvas
};

#endif
1 change: 1 addition & 0 deletions src/gui/qgsidentifymenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgssettings.h"
#include "qgsgui.h"

//TODO 4.0 add explicitly qobject parent to constructor
QgsIdentifyMenu::QgsIdentifyMenu( QgsMapCanvas *canvas )
: QMenu( canvas )
, mCanvas( canvas )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsidentifymenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class GUI_EXPORT QgsIdentifyMenu : public QMenu
void setAllowMultipleReturn( bool multipleReturn ) { mAllowMultipleReturn = multipleReturn;}
bool allowMultipleReturn() { return mAllowMultipleReturn;}

//! define if the menu will be shown with a single idetify result
//! define if the menu will be shown with a single identify result
void setExecWithSingleResult( bool execWithSingleResult ) { mExecWithSingleResult = execWithSingleResult;}
bool execWithSingleResult() { return mExecWithSingleResult;}

Expand Down

0 comments on commit 1192b63

Please sign in to comment.