/
qgsmaptoolselectfreehand.cpp
98 lines (85 loc) · 2.76 KB
/
qgsmaptoolselectfreehand.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/***************************************************************************
qgsmaptoolselectfreehand.cpp - map tool for selecting features by freehand
---------------------
begin : May 2010
copyright : (C) 2010 by Jeremy Palmer
email : jpalmer at linz dot govt dot nz
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsmaptoolselectfreehand.h"
#include "qgsmaptoolselectutils.h"
#include "qgsgeometry.h"
#include "qgsrubberband.h"
#include "qgsmapcanvas.h"
#include "qgis.h"
#include <QMouseEvent>
QgsMapToolSelectFreehand::QgsMapToolSelectFreehand( QgsMapCanvas *canvas )
: QgsMapTool( canvas )
{
mRubberBand = nullptr;
mCursor = Qt::ArrowCursor;
mFillColor = QColor( 254, 178, 76, 63 );
mStrokeColor = QColor( 254, 58, 29, 100 );
}
QgsMapToolSelectFreehand::~QgsMapToolSelectFreehand()
{
delete mRubberBand;
}
void QgsMapToolSelectFreehand::canvasMoveEvent( QgsMapMouseEvent *e )
{
if ( !mActive || !mRubberBand )
return;
mRubberBand->addPoint( toMapCoordinates( e->pos() ) );
}
void QgsMapToolSelectFreehand::canvasReleaseEvent( QgsMapMouseEvent *e )
{
if ( !mActive )
{
if ( e->button() != Qt::LeftButton )
return;
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
{
if ( e->button() == Qt::LeftButton )
{
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;
}
}
void QgsMapToolSelectFreehand::keyReleaseEvent( QKeyEvent *e )
{
if ( mActive && e->key() == Qt::Key_Escape )
{
delete mRubberBand;
mRubberBand = nullptr;
mActive = false;
return;
}
QgsMapTool::keyReleaseEvent( e );
}