Skip to content
Permalink
Browse files

switch freehand selection map tool to click-click

to be coherent with other map tools
add-ons: possible map move/zoom while selecting, and operation canceling (right-click or ESC)
  • Loading branch information
3nids committed Jan 18, 2018
1 parent fe13c0e commit 510a228e80e26c6a59f930900205a848a812e6c6
Showing with 44 additions and 43 deletions.
  1. +40 −39 src/app/qgsmaptoolselectfreehand.cpp
  2. +4 −4 src/app/qgsmaptoolselectfreehand.h
@@ -25,7 +25,7 @@ email : jpalmer at linz dot govt dot nz

QgsMapToolSelectFreehand::QgsMapToolSelectFreehand( QgsMapCanvas *canvas )
: QgsMapTool( canvas )
, mDragging( false )
, mActive( false )
{
mRubberBand = nullptr;
mCursor = Qt::ArrowCursor;
@@ -38,25 +38,10 @@ QgsMapToolSelectFreehand::~QgsMapToolSelectFreehand()
delete mRubberBand;
}

void QgsMapToolSelectFreehand::canvasPressEvent( QgsMapMouseEvent *e )
{
if ( e->button() != Qt::LeftButton )
return;

if ( !mRubberBand )
{
mRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::PolygonGeometry );
mRubberBand->setFillColor( mFillColor );
mRubberBand->setStrokeColor( mStrokeColor );
}
mRubberBand->addPoint( toMapCoordinates( e->pos() ) );
mDragging = true;
}


void QgsMapToolSelectFreehand::canvasMoveEvent( QgsMapMouseEvent *e )
{
if ( !mDragging || !mRubberBand )
if ( !mActive || !mRubberBand )
return;

mRubberBand->addPoint( toMapCoordinates( e->pos() ) );
@@ -65,34 +50,50 @@ void QgsMapToolSelectFreehand::canvasMoveEvent( QgsMapMouseEvent *e )

void QgsMapToolSelectFreehand::canvasReleaseEvent( QgsMapMouseEvent *e )
{
if ( !mRubberBand )
return;
if ( !mActive )
{
if ( e->button() != Qt::LeftButton )
return;

bool singleSelect = false;
if ( mRubberBand->numberOfVertices() > 0 && mRubberBand->numberOfVertices() <= 2 )
if ( !mRubberBand )
{
mRubberBand = new QgsRubberBand( mCanvas, QgsWkbTypes::PolygonGeometry );
mRubberBand->setFillColor( mFillColor );
mRubberBand->setStrokeColor( mStrokeColor );
}
else
{
mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
}
mRubberBand->addPoint( toMapCoordinates( e->pos() ) );
mActive = true;
}
else
{
// single click, not drag - create a rectangle around clicked point
QgsVectorLayer *vlayer = QgsMapToolSelectUtils::getCurrentVectorLayer( mCanvas );
if ( vlayer )
if ( e->button() == Qt::LeftButton )
{
QRect selectRect;
QgsMapToolSelectUtils::expandSelectRectangle( selectRect, vlayer, e->pos() );
QgsMapToolSelectUtils::setRubberBand( mCanvas, selectRect, mRubberBand );
singleSelect = true;
if ( mRubberBand && mRubberBand->numberOfVertices() > 2 )
{
QgsGeometry shapeGeom = mRubberBand->asGeometry();
QgsMapToolSelectUtils::selectMultipleFeatures( mCanvas, shapeGeom, e->modifiers() );
}
}

mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
delete mRubberBand;
mRubberBand = nullptr;
mActive = false;
}
}

if ( mRubberBand->numberOfVertices() > 2 )
void QgsMapToolSelectFreehand::keyReleaseEvent( QKeyEvent *e )
{
if ( mActive && e->key() == Qt::Key_Escape )
{
QgsGeometry shapeGeom = mRubberBand->asGeometry();
if ( singleSelect )
QgsMapToolSelectUtils::selectSingleFeature( mCanvas, shapeGeom, e->modifiers() );
else
QgsMapToolSelectUtils::selectMultipleFeatures( mCanvas, shapeGeom, e->modifiers() );
delete mRubberBand;
mRubberBand = nullptr;
mActive = false;
return;
}

mRubberBand->reset( QgsWkbTypes::PolygonGeometry );
delete mRubberBand;
mRubberBand = nullptr;
mDragging = false;
QgsMapTool::keyReleaseEvent( e );
}
@@ -34,18 +34,18 @@ class APP_EXPORT QgsMapToolSelectFreehand : public QgsMapTool
//! Overridden mouse move event
void canvasMoveEvent( QgsMapMouseEvent *e ) override;

//! Overridden mouse press event
void canvasPressEvent( QgsMapMouseEvent *e ) override;

//! Overridden mouse release event
void canvasReleaseEvent( QgsMapMouseEvent *e ) override;

//! catch escape when active to cancel selection
void keyReleaseEvent( QKeyEvent *e ) override;

private:

//! used for storing all of the maps point for the freehand sketch
QgsRubberBand *mRubberBand = nullptr;

bool mDragging;
bool mActive;

QColor mFillColor;
QColor mStrokeColor;

0 comments on commit 510a228

Please sign in to comment.
You can’t perform that action at this time.