QGIS API Documentation 4.1.0-Master (4aad578bf8d)
Loading...
Searching...
No Matches
qgsmaptoolselect.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmaptoolselect.cpp - map tool for selecting features
3 ----------------------
4 begin : January 2006
5 copyright : (C) 2006 by Martin Dobias
6 email : wonder.sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16
17#include "qgsmaptoolselect.h"
18
19#include "qgsapplication.h"
20#include "qgsgeometry.h"
21#include "qgsmapcanvas.h"
22#include "qgsmapmouseevent.h"
24#include "qgspointxy.h"
25#include "qgsvectorlayer.h"
26
27#include <QColor>
28#include <QMenu>
29#include <QMouseEvent>
30#include <QRect>
31
32#include "moc_qgsmaptoolselect.cpp"
33
41{
42 mToolName = tr( "Select features" );
43
44 mSelectionHandler = std::make_unique<QgsMapToolSelectionHandler>( canvas );
45 connect( mSelectionHandler.get(), &QgsMapToolSelectionHandler::geometryChanged, this, &QgsMapToolSelect::selectFeatures );
47}
48
50{
51 mSelectionHandler->setSelectionMode( selectionMode );
52 if ( selectionMode == QgsMapToolSelectionHandler::SelectSimple )
54 else
55 mCursor = Qt::ArrowCursor;
56}
57
59{
60 mSelectionHandler->canvasPressEvent( e );
61}
62
64{
65 mSelectionHandler->canvasMoveEvent( e );
66}
67
69{
70 mSelectionHandler->canvasReleaseEvent( e );
71}
72
74{
75 if ( !e->isAutoRepeat() )
76 {
77 switch ( e->key() )
78 {
79 case Qt::Key_Shift:
80 case Qt::Key_Control:
81 case Qt::Key_Alt:
82 case Qt::Key_Meta:
83 //note -- if ctrl and shift are already depressed, pressing alt reports the "meta" key eventZ
84 modifiersChanged(
85 e->modifiers() & Qt::ControlModifier || e->key() == Qt::Key_Control,
86 e->modifiers() & Qt::ShiftModifier || e->key() == Qt::Key_Shift,
87 e->modifiers() & Qt::AltModifier || e->key() == Qt::Key_Alt || ( e->modifiers() & Qt::ControlModifier && e->modifiers() & Qt::ShiftModifier && e->key() == Qt::Key_Meta )
88 );
89 break;
90
91 default:
92 break;
93 }
94 }
95
97}
98
100{
101 if ( mSelectionHandler->keyReleaseEvent( e ) )
102 return;
103
104 if ( !e->isAutoRepeat() )
105 {
106 switch ( e->key() )
107 {
108 case Qt::Key_Shift:
109 case Qt::Key_Control:
110 case Qt::Key_Alt:
111 case Qt::Key_Meta:
112 modifiersChanged(
113 e->modifiers() & Qt::ControlModifier && e->key() != Qt::Key_Control,
114 e->modifiers() & Qt::ShiftModifier && e->key() != Qt::Key_Shift,
115 e->modifiers() & Qt::AltModifier && e->key() != Qt::Key_Alt && !( e->modifiers() & Qt::ControlModifier && e->modifiers() & Qt::ShiftModifier && e->key() == Qt::Key_Meta )
116 );
117 break;
118
119 default:
120 break;
121 }
122 }
123
125}
126
128{
129 mSelectionHandler->deactivate();
131}
132
149
151{
152 Q_ASSERT( menu );
154
155 if ( !layer || layer->type() != Qgis::LayerType::Vector )
156 return false;
157
158 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
159 if ( !vlayer->isSpatial() )
160 return false;
161
162 menu->addSeparator();
163
164 Qt::KeyboardModifiers modifiers = Qt::NoModifier;
165 QgsPointXY mapPoint;
166 if ( event )
167 {
168 modifiers = event->modifiers();
169 mapPoint = event->mapPoint();
170 }
172 if ( modifiers & Qt::ShiftModifier && modifiers & Qt::ControlModifier )
174 else if ( modifiers & Qt::ShiftModifier )
176 else if ( modifiers & Qt::ControlModifier )
178
180
182
183 menuActions->populateMenu( menu );
184
185 // cppcheck wrongly believes menuActions will leak
186 // cppcheck-suppress memleak
187 return true;
188}
189
190void QgsMapToolSelect::selectFeatures( Qt::KeyboardModifiers modifiers )
191{
192 if ( mSelectionHandler->selectionMode() == QgsMapToolSelectionHandler::SelectSimple && mSelectionHandler->selectedGeometry().type() == Qgis::GeometryType::Point )
193 {
195 const QgsRectangle r = QgsMapToolSelectUtils::expandSelectRectangle( mSelectionHandler->selectedGeometry().asPoint(), mCanvas, layer );
197 }
198 else
199 QgsMapToolSelectUtils::selectMultipleFeatures( mCanvas, mSelectionHandler->selectedGeometry(), modifiers );
200}
201
202void QgsMapToolSelect::modifiersChanged( bool ctrlModifier, bool shiftModifier, bool altModifier )
203{
204 if ( !ctrlModifier && !shiftModifier && !altModifier )
206 else if ( !ctrlModifier && !shiftModifier && altModifier )
208 else if ( !ctrlModifier && shiftModifier && !altModifier )
210 else if ( !ctrlModifier && shiftModifier && altModifier )
212 else if ( ctrlModifier && !shiftModifier && !altModifier )
214 else if ( ctrlModifier && !shiftModifier && altModifier )
216 else if ( ctrlModifier && shiftModifier && !altModifier )
218 else if ( ctrlModifier && shiftModifier && altModifier )
220}
@ Point
Points.
Definition qgis.h:380
@ Vector
Vector layer.
Definition qgis.h:207
SelectBehavior
Specifies how a selection should be applied.
Definition qgis.h:1891
@ SetSelection
Set selection, removing any existing selection.
Definition qgis.h:1892
@ AddToSelection
Add selection to current selection.
Definition qgis.h:1893
@ IntersectSelection
Modify current selection to include only select features which match.
Definition qgis.h:1894
@ RemoveFromSelection
Remove from current selection.
Definition qgis.h:1895
static QCursor getThemeCursor(Cursor cursor)
Helper to get a theme cursor.
@ Select
Select a rectangle.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
Base class for all map layer types.
Definition qgsmaplayer.h:83
A mouse event which is the result of a user interaction with a QgsMapCanvas.
Handles actions which can be displayed in a context menu related to feature selection.
void populateMenu(QMenu *menu)
Populates the menu with "All Feature" action and a empty menu that could contain later the "One Featu...
bool populateContextMenuWithEvent(QMenu *menu, QgsMapMouseEvent *event) override
Allows the tool to populate and customize the given menu, prior to showing it in response to a right-...
void keyPressEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
void canvasMoveEvent(QgsMapMouseEvent *e) override
Mouse move event for overriding. Default implementation does nothing.
void setSelectionMode(QgsMapToolSelectionHandler::SelectionMode selectionMode)
Sets the current selection mode.
Flags flags() const override
Returns the flags for the map tool.
void modeChanged(QgsMapToolSelect::Mode mode)
Emitted when the selection mode changes, usually when qt modifiers are changed.
void deactivate() override
called when map tool is being deactivated
@ GeometryIntersectsSubtractFromSelection
@ GeometryIntersectsIntersectWithSelection
void canvasPressEvent(QgsMapMouseEvent *e) override
Mouse press event for overriding. Default implementation does nothing.
void canvasReleaseEvent(QgsMapMouseEvent *e) override
Mouse release event for overriding. Default implementation does nothing.
void keyReleaseEvent(QKeyEvent *e) override
Key event for overriding. Default implementation does nothing.
SelectionMode
Select features to identify by:
@ SelectSimple
SelectSimple - single click or drawing a rectangle, default option.
@ SelectRadius
SelectRadius - a circle selection.
@ SelectOnMouseOver
SelectOnMouseMove - selection on mouse over.
@ SelectPolygon
SelectPolygon - drawing a polygon or right-click on existing polygon feature.
@ SelectFreehand
SelectFreehand - free hand selection.
void geometryChanged(Qt::KeyboardModifiers modifiers=Qt::NoModifier)
emitted when a new geometry has been picked (selectedGeometry())
QgsMapLayer * layer(const QString &id)
Returns the map layer with the matching ID, or nullptr if no layers could be found.
QgsMapCanvas * canvas() const
returns pointer to the tool's map canvas
QgsMapTool(QgsMapCanvas *canvas)
Constructor takes a map canvas as a parameter.
QFlags< Flag > Flags
Definition qgsmaptool.h:117
QPointer< QgsMapCanvas > mCanvas
The pointer to the map canvas.
Definition qgsmaptool.h:403
QString mToolName
The translated name of the map tool.
Definition qgsmaptool.h:421
friend class QgsMapCanvas
Definition qgsmaptool.h:423
virtual void keyPressEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
virtual void keyReleaseEvent(QKeyEvent *e)
Key event for overriding. Default implementation does nothing.
virtual Flags flags() const
Returns the flags for the map tool.
Definition qgsmaptool.h:122
@ ShowContextMenu
Show a context menu when right-clicking with the tool (since QGIS 3.14). See populateContextMenu().
Definition qgsmaptool.h:115
QCursor mCursor
The cursor used in the map tool.
Definition qgsmaptool.h:406
virtual void deactivate()
called when map tool is being deactivated
Represents a 2D point.
Definition qgspointxy.h:62
A rectangle specified with double values.
Represents a vector layer which manages a vector based dataset.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsMapToolSelect(QgsMapCanvas *canvas)
A map tool for selecting features on a map canvas.
void selectSingleFeature(QgsMapCanvas *canvas, const QgsGeometry &selectGeometry, Qt::KeyboardModifiers modifiers)
Selects a single feature from within currently selected layer.
void selectMultipleFeatures(QgsMapCanvas *canvas, const QgsGeometry &selectGeometry, Qt::KeyboardModifiers modifiers)
Selects multiple matching features from within currently selected layer.
QgsMapLayer * getCurrentTargetLayer(QgsMapCanvas *canvas)
Get the current selected canvas map layer.
QgsRectangle GUI_EXPORT expandSelectRectangle(QgsPointXY mapPoint, QgsMapCanvas *canvas, QgsMapLayer *layer)
Expands a point to a rectangle with minimum size for selection based on the layer.