Skip to content

Commit 4588d9b

Browse files
committed
new map tool to select single feature
1 parent 1b205be commit 4588d9b

9 files changed

+151
-9
lines changed

python/gui/gui.sip

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
%Include qgsmaptool.sip
7575
%Include qgsmaptoolemitpoint.sip
7676
%Include qgsmaptoolidentify.sip
77+
%Include qgsmaptoolidentifyfeature.sip
7778
%Include qgsmaptoolpan.sip
7879
%Include qgsmaptooltouch.sip
7980
%Include qgsmaptoolzoom.sip

python/gui/qgsmaptoolidentify.sip

+21-4
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,6 @@ class QgsMapToolIdentify : QgsMapTool
7878
@return a list of IdentifyResult*/
7979
QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
8080

81-
protected:
82-
//! rubber bands for layer select mode
83-
void deleteRubberBands();
84-
8581
public slots:
8682
void formatChanged( QgsRasterLayer *layer );
8783
void layerDestroyed();
@@ -90,4 +86,25 @@ class QgsMapToolIdentify : QgsMapTool
9086
void identifyProgress( int, int );
9187
void identifyMessage( QString );
9288
void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult>& );
89+
90+
protected:
91+
//! rubber bands for layer select mode
92+
void deleteRubberBands();
93+
94+
/** Performs the identification.
95+
To avoid beeing forced to specify IdentifyMode with a list of layers
96+
this has been made private and two publics methods are offered
97+
@param x x coordinates of mouseEvent
98+
@param y y coordinates of mouseEvent
99+
@param mode Identification mode. Can use Qgis default settings or a defined mode.
100+
@param layerList Performs the identification within the given list of layers.
101+
@param layerType Only performs identification in a certain type of layers (raster, vector).
102+
@return true if identification succeeded and a feature has been found, false otherwise.*/
103+
QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );
104+
105+
/** call the right method depending on layer type */
106+
bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType = AllLayers );
107+
108+
bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel );
109+
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point );
93110
};
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class QgsMapToolIdentifyFeature : QgsMapToolIdentify
2+
{
3+
%TypeHeaderCode
4+
#include <qgsmaptoolidentifyfeature.h>
5+
%End
6+
7+
public:
8+
QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas );
9+
10+
void canvasReleaseEvent( QMouseEvent* e );
11+
12+
signals:
13+
void featureIdentified( QgsFeature );
14+
};

src/gui/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ qgsmaptip.cpp
181181
qgsmaptool.cpp
182182
qgsmaptoolemitpoint.cpp
183183
qgsmaptoolidentify.cpp
184+
qgsmaptoolidentifyfeature.cpp
184185
qgsmaptoolpan.cpp
185186
qgsmaptoolzoom.cpp
186187
qgsmessagebar.cpp
@@ -372,6 +373,7 @@ qgsmapoverviewcanvas.h
372373
qgsmaptool.h
373374
qgsmaptoolemitpoint.h
374375
qgsmaptoolidentify.h
376+
qgsmaptoolidentifyfeature.h
375377
qgsmessagebaritem.h
376378
qgsmessagebar.h
377379
qgsmessagelogviewer.h
@@ -454,6 +456,7 @@ qgsmaptip.h
454456
qgsmaptool.h
455457
qgsmaptoolemitpoint.h
456458
qgsmaptoolidentify.h
459+
qgsmaptoolidentifyfeature.h
457460
qgsmaptoolpan.h
458461
qgsmaptoolzoom.h
459462
qgsmessagebaritem.h

src/gui/qgsmaptool.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ void QgsMapTool::activate()
8585
// set cursor (map tools usually set it in constructor)
8686
mCanvas->setCursor( mCursor );
8787
QgsDebugMsg( "Cursor has been set" );
88+
89+
emit activated();
8890
}
8991

9092

@@ -94,6 +96,8 @@ void QgsMapTool::deactivate()
9496
mAction->setChecked( false );
9597
if ( mButton )
9698
mButton->setChecked( false );
99+
100+
emit deactivated();
97101
}
98102

99103
void QgsMapTool::setAction( QAction* action )

src/gui/qgsmaptool.h

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ class GUI_EXPORT QgsMapTool : public QObject
153153
//! emit signal to clear previous message
154154
void messageDiscarded();
155155

156+
//! signal emitted once the map tool is activated
157+
void activated();
158+
159+
//! signal emitted once the map tool is deactivated
160+
void deactivated();
161+
156162
private slots:
157163
//! clear pointer when action is destroyed
158164
void actionDestroyed();

src/gui/qgsmaptoolidentify.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
123123
@return a list of IdentifyResult*/
124124
QList<IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
125125

126-
protected:
127-
//! rubber bands for layer select mode
128-
void deleteRubberBands();
129-
130126
public slots:
131127
void formatChanged( QgsRasterLayer *layer );
132128
void layerDestroyed();
@@ -136,7 +132,10 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
136132
void identifyMessage( QString );
137133
void changedRasterResults( QList<IdentifyResult>& );
138134

139-
private:
135+
protected:
136+
//! rubber bands for layer select mode
137+
void deleteRubberBands();
138+
140139
/** Performs the identification.
141140
To avoid beeing forced to specify IdentifyMode with a list of layers
142141
this has been made private and two publics methods are offered
@@ -154,6 +153,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
154153
bool identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel );
155154
bool identifyVectorLayer( QList<IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point );
156155

156+
157+
private:
157158
//! Private helper
158159
virtual void convertMeasurement( QgsDistanceArea &calc, double &measure, QGis::UnitType &u, bool isArea );
159160

src/gui/qgsmaptoolidentifyfeature.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/***************************************************************************
2+
qgsmaptoolidentifyfeature.cpp
3+
--------------------------------------
4+
Date : 22.5.2014
5+
Copyright : (C) 2014 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.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+
#include <QMouseEvent>
17+
18+
#include "qgsmaptoolidentifyfeature.h"
19+
#include "qgsmapcanvas.h"
20+
21+
QgsMapToolIdentifyFeature::QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas )
22+
: QgsMapToolIdentify( canvas )
23+
, mLayer( vl )
24+
, mCanvas( canvas )
25+
{
26+
}
27+
28+
void QgsMapToolIdentifyFeature::canvasReleaseEvent( QMouseEvent* e )
29+
{
30+
31+
QgsPoint point = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );
32+
33+
QList<IdentifyResult> results;
34+
if ( !identifyVectorLayer( &results, mLayer, point ) )
35+
return;
36+
37+
// TODO: display a menu when several features identified
38+
39+
emit featureIdentified( results[0].mFeature );
40+
emit featureIdentified( results[0].mFeature.id() );
41+
}
42+
43+
void QgsMapToolIdentifyFeature::keyPressEvent( QKeyEvent* e )
44+
{
45+
if ( e->key() == Qt::Key_Escape )
46+
{
47+
deactivate();
48+
}
49+
}

src/gui/qgsmaptoolidentifyfeature.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgsmaptoolidentifyfeature.h
3+
--------------------------------------
4+
Date : 22.5.2014
5+
Copyright : (C) 2014 Denis Rouzaud
6+
Email : denis.rouzaud@gmail.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+
#ifndef QGSMAPTOOLIDENTIFYFEATURE_H
17+
#define QGSMAPTOOLIDENTIFYFEATURE_H
18+
19+
#include "qgsmaptoolidentify.h"
20+
21+
/**
22+
* @brief The QgsMapToolIdentifyFeature class is a map tool to be used to identify a feature on a chosen layer.
23+
* Once the map tool is enable, user can click on the map canvas to identify a feature.
24+
* A signal will then be emitted.
25+
*/
26+
class GUI_EXPORT QgsMapToolIdentifyFeature : public QgsMapToolIdentify
27+
{
28+
Q_OBJECT
29+
30+
public:
31+
QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas );
32+
33+
virtual void canvasReleaseEvent( QMouseEvent* e );
34+
35+
signals:
36+
void featureIdentified( const QgsFeature& );
37+
void featureIdentified( QgsFeatureId );
38+
39+
protected:
40+
virtual void keyPressEvent( QKeyEvent* e );
41+
42+
private:
43+
QgsVectorLayer* mLayer;
44+
QgsMapCanvas* mCanvas;
45+
};
46+
47+
#endif // QGSMAPTOOLIDENTIFYFEATURE_H

0 commit comments

Comments
 (0)