Skip to content

Commit

Permalink
new map tool to select single feature
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Aug 19, 2014
1 parent 1b205be commit 4588d9b
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 9 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -74,6 +74,7 @@
%Include qgsmaptool.sip
%Include qgsmaptoolemitpoint.sip
%Include qgsmaptoolidentify.sip
%Include qgsmaptoolidentifyfeature.sip
%Include qgsmaptoolpan.sip
%Include qgsmaptooltouch.sip
%Include qgsmaptoolzoom.sip
Expand Down
25 changes: 21 additions & 4 deletions python/gui/qgsmaptoolidentify.sip
Expand Up @@ -78,10 +78,6 @@ class QgsMapToolIdentify : QgsMapTool
@return a list of IdentifyResult*/
QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );

protected:
//! rubber bands for layer select mode
void deleteRubberBands();

public slots:
void formatChanged( QgsRasterLayer *layer );
void layerDestroyed();
Expand All @@ -90,4 +86,25 @@ class QgsMapToolIdentify : QgsMapTool
void identifyProgress( int, int );
void identifyMessage( QString );
void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult>& );

protected:
//! rubber bands for layer select mode
void deleteRubberBands();

/** Performs the identification.
To avoid beeing forced to specify IdentifyMode with a list of layers
this has been made private and two publics methods are offered
@param x x coordinates of mouseEvent
@param y y coordinates of mouseEvent
@param mode Identification mode. Can use Qgis default settings or a defined mode.
@param layerList Performs the identification within the given list of layers.
@param layerType Only performs identification in a certain type of layers (raster, vector).
@return true if identification succeeded and a feature has been found, false otherwise.*/
QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, QList<QgsMapLayer*> layerList, LayerType layerType = AllLayers );

/** call the right method depending on layer type */
bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType = AllLayers );

bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel );
bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point );
};
14 changes: 14 additions & 0 deletions python/gui/qgsmaptoolidentifyfeature.sip
@@ -0,0 +1,14 @@
class QgsMapToolIdentifyFeature : QgsMapToolIdentify
{
%TypeHeaderCode
#include <qgsmaptoolidentifyfeature.h>
%End

public:
QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas );

void canvasReleaseEvent( QMouseEvent* e );

signals:
void featureIdentified( QgsFeature );
};
3 changes: 3 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -181,6 +181,7 @@ qgsmaptip.cpp
qgsmaptool.cpp
qgsmaptoolemitpoint.cpp
qgsmaptoolidentify.cpp
qgsmaptoolidentifyfeature.cpp
qgsmaptoolpan.cpp
qgsmaptoolzoom.cpp
qgsmessagebar.cpp
Expand Down Expand Up @@ -372,6 +373,7 @@ qgsmapoverviewcanvas.h
qgsmaptool.h
qgsmaptoolemitpoint.h
qgsmaptoolidentify.h
qgsmaptoolidentifyfeature.h
qgsmessagebaritem.h
qgsmessagebar.h
qgsmessagelogviewer.h
Expand Down Expand Up @@ -454,6 +456,7 @@ qgsmaptip.h
qgsmaptool.h
qgsmaptoolemitpoint.h
qgsmaptoolidentify.h
qgsmaptoolidentifyfeature.h
qgsmaptoolpan.h
qgsmaptoolzoom.h
qgsmessagebaritem.h
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgsmaptool.cpp
Expand Up @@ -85,6 +85,8 @@ void QgsMapTool::activate()
// set cursor (map tools usually set it in constructor)
mCanvas->setCursor( mCursor );
QgsDebugMsg( "Cursor has been set" );

emit activated();
}


Expand All @@ -94,6 +96,8 @@ void QgsMapTool::deactivate()
mAction->setChecked( false );
if ( mButton )
mButton->setChecked( false );

emit deactivated();
}

void QgsMapTool::setAction( QAction* action )
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsmaptool.h
Expand Up @@ -153,6 +153,12 @@ class GUI_EXPORT QgsMapTool : public QObject
//! emit signal to clear previous message
void messageDiscarded();

//! signal emitted once the map tool is activated
void activated();

//! signal emitted once the map tool is deactivated
void deactivated();

private slots:
//! clear pointer when action is destroyed
void actionDestroyed();
Expand Down
11 changes: 6 additions & 5 deletions src/gui/qgsmaptoolidentify.h
Expand Up @@ -123,10 +123,6 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
@return a list of IdentifyResult*/
QList<IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );

protected:
//! rubber bands for layer select mode
void deleteRubberBands();

public slots:
void formatChanged( QgsRasterLayer *layer );
void layerDestroyed();
Expand All @@ -136,7 +132,10 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
void identifyMessage( QString );
void changedRasterResults( QList<IdentifyResult>& );

private:
protected:
//! rubber bands for layer select mode
void deleteRubberBands();

/** Performs the identification.
To avoid beeing forced to specify IdentifyMode with a list of layers
this has been made private and two publics methods are offered
Expand All @@ -154,6 +153,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
bool identifyRasterLayer( QList<IdentifyResult> *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel );
bool identifyVectorLayer( QList<IdentifyResult> *results, QgsVectorLayer *layer, QgsPoint point );


private:
//! Private helper
virtual void convertMeasurement( QgsDistanceArea &calc, double &measure, QGis::UnitType &u, bool isArea );

Expand Down
49 changes: 49 additions & 0 deletions src/gui/qgsmaptoolidentifyfeature.cpp
@@ -0,0 +1,49 @@
/***************************************************************************
qgsmaptoolidentifyfeature.cpp
--------------------------------------
Date : 22.5.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* 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 <QMouseEvent>

#include "qgsmaptoolidentifyfeature.h"
#include "qgsmapcanvas.h"

QgsMapToolIdentifyFeature::QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas )
: QgsMapToolIdentify( canvas )
, mLayer( vl )
, mCanvas( canvas )
{
}

void QgsMapToolIdentifyFeature::canvasReleaseEvent( QMouseEvent* e )
{

QgsPoint point = mCanvas->getCoordinateTransform()->toMapCoordinates( e->x(), e->y() );

QList<IdentifyResult> results;
if ( !identifyVectorLayer( &results, mLayer, point ) )
return;

// TODO: display a menu when several features identified

emit featureIdentified( results[0].mFeature );
emit featureIdentified( results[0].mFeature.id() );
}

void QgsMapToolIdentifyFeature::keyPressEvent( QKeyEvent* e )
{
if ( e->key() == Qt::Key_Escape )
{
deactivate();
}
}
47 changes: 47 additions & 0 deletions src/gui/qgsmaptoolidentifyfeature.h
@@ -0,0 +1,47 @@
/***************************************************************************
qgsmaptoolidentifyfeature.h
--------------------------------------
Date : 22.5.2014
Copyright : (C) 2014 Denis Rouzaud
Email : denis.rouzaud@gmail.com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSMAPTOOLIDENTIFYFEATURE_H
#define QGSMAPTOOLIDENTIFYFEATURE_H

#include "qgsmaptoolidentify.h"

/**
* @brief The QgsMapToolIdentifyFeature class is a map tool to be used to identify a feature on a chosen layer.
* Once the map tool is enable, user can click on the map canvas to identify a feature.
* A signal will then be emitted.
*/
class GUI_EXPORT QgsMapToolIdentifyFeature : public QgsMapToolIdentify
{
Q_OBJECT

public:
QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas );

virtual void canvasReleaseEvent( QMouseEvent* e );

signals:
void featureIdentified( const QgsFeature& );
void featureIdentified( QgsFeatureId );

protected:
virtual void keyPressEvent( QKeyEvent* e );

private:
QgsVectorLayer* mLayer;
QgsMapCanvas* mCanvas;
};

#endif // QGSMAPTOOLIDENTIFYFEATURE_H

0 comments on commit 4588d9b

Please sign in to comment.