Skip to content

Commit

Permalink
switch freehand selection map tool to click-click
Browse files Browse the repository at this point in the history
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 20, 2018
1 parent fe13c0e commit 510a228
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
79 changes: 40 additions & 39 deletions src/app/qgsmaptoolselectfreehand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() ) );
Expand All @@ -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 );
}
8 changes: 4 additions & 4 deletions src/app/qgsmaptoolselectfreehand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 510a228

Please sign in to comment.