Skip to content

Commit bac3080

Browse files
committed
[FEATURE] Identify: more selection modes (simple / polygon / freehand / radius)
This extends identify tool with the ability to switch to different selection modes (the ones offered also by the selection map tool)
1 parent d87f75a commit bac3080

28 files changed

+1153
-949
lines changed

python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
%Include qgsmaptoolidentify.sip
145145
%Include qgsmaptoolidentifyfeature.sip
146146
%Include qgsmaptoolpan.sip
147+
%Include qgsmaptoolselectionhandler.sip
147148
%Include qgsmaptoolzoom.sip
148149
%Include qgsmaplayerconfigwidget.sip
149150
%Include qgsmenuheader.sip

python/gui/qgsmaptoolidentify.sip.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ this has been made private and two publics methods are offered
108108
:return: a list of IdentifyResult*
109109
%End
110110

111+
QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType );
112+
QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType );
113+
114+
111115
QgsIdentifyMenu *identifyMenu();
112116
%Docstring
113117
return a pointer to the identify menu which will be used in layer selection mode
@@ -144,9 +148,12 @@ this has been made private and two publics methods are offered
144148
%Docstring
145149
Call the right method depending on layer type
146150
%End
151+
bool identifyLayer( QList<IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, LayerType layerType );
147152

148153
bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
154+
bool identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
149155
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
156+
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry );
150157

151158
};
152159

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/qgsmaptoolselectionhandler.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
14+
15+
16+
class QgsMapToolSelectionHandler : QObject
17+
{
18+
%Docstring
19+
Utility class for handling various methods to create geometry for selection in layers.
20+
21+
.. versionadded:: 3.2
22+
%End
23+
24+
%TypeHeaderCode
25+
#include "qgsmaptoolselectionhandler.h"
26+
%End
27+
public:
28+
29+
enum SelectionMode
30+
{
31+
//! SelectSimple
32+
SelectSimple,
33+
//! SelectPolygon
34+
SelectPolygon,
35+
//! SelectFreehand
36+
SelectFreehand,
37+
//! SelectRadius
38+
SelectRadius
39+
};
40+
41+
QgsMapToolSelectionHandler( QgsMapCanvas *canvas,
42+
QgsMapToolSelectionHandler::SelectionMode selectionMode = QgsMapToolSelectionHandler::SelectionMode::SelectSimple );
43+
%Docstring
44+
constructor
45+
%End
46+
47+
~QgsMapToolSelectionHandler();
48+
49+
void setInterface( QgisInterface *iface );
50+
%Docstring
51+
Configures QGIS interface - used to register selection radius widget
52+
%End
53+
54+
QgsGeometry selectedGeometry();
55+
%Docstring
56+
Returns most recently selected geometry (may be a point or a polygon)
57+
%End
58+
59+
SelectionMode selectionMode();
60+
%Docstring
61+
Sets the current selection mode
62+
%End
63+
void setSelectionMode( SelectionMode mode );
64+
%Docstring
65+
Returns the current selection mode
66+
%End
67+
68+
void deactivate();
69+
%Docstring
70+
Deactivates handler (when map tool gets deactivated)
71+
%End
72+
73+
void canvasMoveEvent( QgsMapMouseEvent *e );
74+
%Docstring
75+
Handles mouse move event from map tool
76+
%End
77+
void canvasPressEvent( QgsMapMouseEvent *e );
78+
%Docstring
79+
Handles mouse press event from map tool
80+
%End
81+
void canvasReleaseEvent( QgsMapMouseEvent *e );
82+
%Docstring
83+
Handles mouse releasae event from map tool
84+
%End
85+
bool keyReleaseEvent( QKeyEvent *e );
86+
%Docstring
87+
Handles escape press event - returns true if the even has been processed
88+
%End
89+
90+
signals:
91+
void geometryChanged( Qt::KeyboardModifiers modifiers = Qt::NoModifier );
92+
%Docstring
93+
emitted when a new geometry has been picked (selectedGeometry())
94+
%End
95+
96+
};
97+
98+
/************************************************************************
99+
* This file has been generated automatically from *
100+
* *
101+
* src/gui/qgsmaptoolselectionhandler.h *
102+
* *
103+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
104+
************************************************************************/

src/app/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,6 @@ SET(QGIS_APP_SRCS
9797
qgsmaptoolrotatelabel.cpp
9898
qgsmaptoolrotatepointsymbols.cpp
9999
qgsmaptoolselect.cpp
100-
qgsmaptoolselectrectangle.cpp
101-
qgsmaptoolselectfreehand.cpp
102-
qgsmaptoolselectpolygon.cpp
103-
qgsmaptoolselectradius.cpp
104100
qgsmaptoolselectutils.cpp
105101
qgsmaptoolsimplify.cpp
106102
qgsmaptoolsplitfeatures.cpp
@@ -310,11 +306,7 @@ SET (QGIS_APP_MOC_HDRS
310306
qgsmaptoolrotatefeature.h
311307
qgsmaptoolrotatelabel.h
312308
qgsmaptoolrotatepointsymbols.h
313-
qgsmaptoolselectfreehand.h
314309
qgsmaptoolselect.h
315-
qgsmaptoolselectpolygon.h
316-
qgsmaptoolselectradius.h
317-
qgsmaptoolselectrectangle.h
318310
qgsmaptoolsimplify.h
319311
qgsmaptoolsplitfeatures.h
320312
qgsmaptoolsplitparts.h

src/app/qgisapp.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,6 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
384384
#include "qgsmaptooloffsetpointsymbol.h"
385385
#include "qgsmaptoolpan.h"
386386
#include "qgsmaptoolselect.h"
387-
#include "qgsmaptoolselectrectangle.h"
388-
#include "qgsmaptoolselectfreehand.h"
389-
#include "qgsmaptoolselectpolygon.h"
390-
#include "qgsmaptoolselectradius.h"
391387
#include "qgsmaptoolsvgannotation.h"
392388
#include "qgsmaptoolreshape.h"
393389
#include "qgsmaptoolrotatepointsymbols.h"
@@ -1315,6 +1311,12 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
13151311
// should come after fileNewBlank to ensure project is properly set up to receive any data source files
13161312
QgsApplication::setFileOpenEventReceiver( this );
13171313

1314+
// finish initialization of map tools that need access to QGIS interface
1315+
mMapTools.mSelectFeatures->setInterface( mQgisInterface );
1316+
mMapTools.mSelectPolygon->setInterface( mQgisInterface );
1317+
mMapTools.mSelectFreehand->setInterface( mQgisInterface );
1318+
mMapTools.mSelectRadius->setInterface( mQgisInterface );
1319+
mMapTools.mIdentify->setInterface( mQgisInterface );
13181320

13191321
#ifdef ANDROID
13201322
toggleFullScreen();
@@ -3406,14 +3408,18 @@ void QgisApp::createCanvasTools()
34063408
mMapTools.mSplitFeatures->setAction( mActionSplitFeatures );
34073409
mMapTools.mSplitParts = new QgsMapToolSplitParts( mMapCanvas );
34083410
mMapTools.mSplitParts->setAction( mActionSplitParts );
3409-
mMapTools.mSelectFeatures = new QgsMapToolSelectFeatures( mMapCanvas );
3411+
mMapTools.mSelectFeatures = new QgsMapToolSelect( mMapCanvas );
34103412
mMapTools.mSelectFeatures->setAction( mActionSelectFeatures );
3411-
mMapTools.mSelectPolygon = new QgsMapToolSelectPolygon( mMapCanvas );
3413+
mMapTools.mSelectFeatures->setSelectionMode( QgsMapToolSelectionHandler::SelectSimple );
3414+
mMapTools.mSelectPolygon = new QgsMapToolSelect( mMapCanvas );
34123415
mMapTools.mSelectPolygon->setAction( mActionSelectPolygon );
3413-
mMapTools.mSelectFreehand = new QgsMapToolSelectFreehand( mMapCanvas );
3416+
mMapTools.mSelectPolygon->setSelectionMode( QgsMapToolSelectionHandler::SelectPolygon );
3417+
mMapTools.mSelectFreehand = new QgsMapToolSelect( mMapCanvas );
34143418
mMapTools.mSelectFreehand->setAction( mActionSelectFreehand );
3415-
mMapTools.mSelectRadius = new QgsMapToolSelectRadius( mMapCanvas );
3419+
mMapTools.mSelectFreehand->setSelectionMode( QgsMapToolSelectionHandler::SelectFreehand );
3420+
mMapTools.mSelectRadius = new QgsMapToolSelect( mMapCanvas );
34163421
mMapTools.mSelectRadius->setAction( mActionSelectRadius );
3422+
mMapTools.mSelectRadius->setSelectionMode( QgsMapToolSelectionHandler::SelectRadius );
34173423
mMapTools.mAddRing = new QgsMapToolAddRing( mMapCanvas );
34183424
mMapTools.mAddRing->setAction( mActionAddRing );
34193425
mMapTools.mFillRing = new QgsMapToolFillRing( mMapCanvas );

src/app/qgisapp.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class QgsMapToolAddFeature;
7676
class QgsMapToolDigitizeFeature;
7777
class QgsMapToolAdvancedDigitizing;
7878
class QgsMapToolIdentifyAction;
79+
class QgsMapToolSelect;
7980
class QgsPluginLayer;
8081
class QgsPluginLayer;
8182
class QgsPluginManager;
@@ -1962,11 +1963,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
19621963
QgsMapTool *mReshapeFeatures = nullptr;
19631964
QgsMapTool *mSplitFeatures = nullptr;
19641965
QgsMapTool *mSplitParts = nullptr;
1965-
QgsMapTool *mSelect = nullptr;
1966-
QgsMapTool *mSelectFeatures = nullptr;
1967-
QgsMapTool *mSelectPolygon = nullptr;
1968-
QgsMapTool *mSelectFreehand = nullptr;
1969-
QgsMapTool *mSelectRadius = nullptr;
1966+
QgsMapToolSelect *mSelectFeatures = nullptr;
1967+
QgsMapToolSelect *mSelectPolygon = nullptr;
1968+
QgsMapToolSelect *mSelectFreehand = nullptr;
1969+
QgsMapToolSelect *mSelectRadius = nullptr;
19701970
QgsMapTool *mVertexAdd = nullptr;
19711971
QgsMapTool *mVertexMove = nullptr;
19721972
QgsMapTool *mVertexDelete = nullptr;

src/app/qgsidentifyresultsdialog.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgsfeaturestore.h"
2828
#include "qgsgeometry.h"
2929
#include "qgshighlight.h"
30+
#include "qgsmaptoolidentifyaction.h"
3031
#include "qgsidentifyresultsdialog.h"
3132
#include "qgslogger.h"
3233
#include "qgsmapcanvas.h"
@@ -408,6 +409,8 @@ QgsIdentifyResultsDialog::QgsIdentifyResultsDialog( QgsMapCanvas *canvas, QWidge
408409
connect( mOpenFormAction, &QAction::triggered, this, &QgsIdentifyResultsDialog::featureForm );
409410
connect( mClearResultsAction, &QAction::triggered, this, &QgsIdentifyResultsDialog::clear );
410411
connect( mHelpToolButton, &QAbstractButton::clicked, this, &QgsIdentifyResultsDialog::showHelp );
412+
413+
initSelectionModes();
411414
}
412415

413416
QgsIdentifyResultsDialog::~QgsIdentifyResultsDialog()
@@ -424,6 +427,31 @@ QgsIdentifyResultsDialog::~QgsIdentifyResultsDialog()
424427
mPlotCurves.clear();
425428
}
426429

430+
void QgsIdentifyResultsDialog::initSelectionModes()
431+
{
432+
mSelectModeButton = new QToolButton( mIdentifyToolbar );
433+
mSelectModeButton->setPopupMode( QToolButton::MenuButtonPopup );
434+
QList<QAction *> selectActions;
435+
selectActions << mActionSelectFeatures << mActionSelectPolygon
436+
<< mActionSelectFreehand << mActionSelectRadius;
437+
438+
QActionGroup *group = new QActionGroup( this );
439+
group->addAction( mActionSelectFeatures );
440+
group->addAction( mActionSelectPolygon );
441+
group->addAction( mActionSelectFreehand );
442+
group->addAction( mActionSelectRadius );
443+
444+
mSelectModeButton->addActions( selectActions );
445+
mSelectModeButton->setDefaultAction( mActionSelectFeatures );
446+
447+
mIdentifyToolbar->addWidget( mSelectModeButton );
448+
449+
connect( mActionSelectFeatures, &QAction::triggered, this, &QgsIdentifyResultsDialog::setSelectionMode );
450+
connect( mActionSelectPolygon, &QAction::triggered, this, &QgsIdentifyResultsDialog::setSelectionMode );
451+
connect( mActionSelectFreehand, &QAction::triggered, this, &QgsIdentifyResultsDialog::setSelectionMode );
452+
connect( mActionSelectRadius, &QAction::triggered, this, &QgsIdentifyResultsDialog::setSelectionMode );
453+
}
454+
427455
QTreeWidgetItem *QgsIdentifyResultsDialog::layerItem( QObject *object )
428456
{
429457
for ( int i = 0; i < lstResults->topLevelItemCount(); i++ )
@@ -1991,6 +2019,40 @@ void QgsIdentifyResultsDialog::showHelp()
19912019
QgsHelp::openHelp( QStringLiteral( "introduction/general_tools.html#identify" ) );
19922020
}
19932021

2022+
void QgsIdentifyResultsDialog::setSelectionMode()
2023+
{
2024+
QObject *obj = sender();
2025+
QgsMapToolSelectionHandler::SelectionMode oldMode = mSelectionMode;
2026+
if ( obj == mActionSelectFeatures )
2027+
{
2028+
mSelectModeButton->setDefaultAction( mActionSelectFeatures );
2029+
mSelectionMode = QgsMapToolSelectionHandler::SelectSimple;
2030+
}
2031+
else if ( obj == mActionSelectPolygon )
2032+
{
2033+
mSelectModeButton->setDefaultAction( mActionSelectPolygon );
2034+
mSelectionMode = QgsMapToolSelectionHandler::SelectPolygon;
2035+
}
2036+
else if ( obj == mActionSelectFreehand )
2037+
{
2038+
mSelectModeButton->setDefaultAction( mActionSelectFreehand );
2039+
mSelectionMode = QgsMapToolSelectionHandler::SelectFreehand;
2040+
}
2041+
else if ( obj == mActionSelectRadius )
2042+
{
2043+
mSelectModeButton->setDefaultAction( mActionSelectRadius );
2044+
mSelectionMode = QgsMapToolSelectionHandler::SelectRadius;
2045+
}
2046+
2047+
if ( oldMode != mSelectionMode )
2048+
emit selectionModeChanged();
2049+
}
2050+
2051+
QgsMapToolSelectionHandler::SelectionMode QgsIdentifyResultsDialog::selectionMode()
2052+
{
2053+
return mSelectionMode;
2054+
}
2055+
19942056
void QgsIdentifyResultsDialog::setExpressionContextScope( const QgsExpressionContextScope &scope )
19952057
{
19962058
mExpressionContextScope = scope;

src/app/qgsidentifyresultsdialog.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgsmaptoolidentify.h"
2727
#include "qgswebview.h"
2828
#include "qgsexpressioncontext.h"
29+
#include "qgsmaptoolselectionhandler.h"
2930

3031
#include <QWidget>
3132
#include <QList>
@@ -167,6 +168,8 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
167168
*/
168169
QgsExpressionContextScope expressionContextScope() const;
169170

171+
QgsMapToolSelectionHandler::SelectionMode selectionMode();
172+
170173
signals:
171174
void selectedFeatureChanged( QgsVectorLayer *, QgsFeatureId featureId );
172175

@@ -177,6 +180,8 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
177180

178181
void activateLayer( QgsMapLayer * );
179182

183+
void selectionModeChanged();
184+
180185
public slots:
181186
//! Remove results
182187
void clear();
@@ -251,6 +256,7 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
251256
QList<QgsFeature> mFeatures;
252257
QMap< QString, QMap< QString, QVariant > > mWidgetCaches;
253258
QgsExpressionContextScope mExpressionContextScope;
259+
QToolButton *mSelectModeButton;
254260

255261
QgsMapLayer *layer( QTreeWidgetItem *item );
256262
QgsVectorLayer *vectorLayer( QTreeWidgetItem *item );
@@ -259,6 +265,7 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
259265
QTreeWidgetItem *layerItem( QTreeWidgetItem *item );
260266
QTreeWidgetItem *layerItem( QObject *layer );
261267

268+
262269
void highlightLayer( QTreeWidgetItem *object );
263270
void layerProperties( QTreeWidgetItem *object );
264271
void disconnectLayer( QObject *object );
@@ -279,6 +286,12 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
279286
QVector<QgsIdentifyPlotCurve *> mPlotCurves;
280287

281288
void showHelp();
289+
290+
QgsMapToolSelectionHandler::SelectionMode mSelectionMode = QgsMapToolSelectionHandler::SelectSimple;
291+
292+
void setSelectionMode();
293+
294+
void initSelectionModes();
282295
};
283296

284297
class QgsIdentifyResultsDialogMapLayerAction : public QAction

0 commit comments

Comments
 (0)