diff --git a/python/gui/gui.sip b/python/gui/gui.sip index 8b70a486e06..37c63e4d398 100644 --- a/python/gui/gui.sip +++ b/python/gui/gui.sip @@ -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 diff --git a/python/gui/qgsmaptoolidentify.sip b/python/gui/qgsmaptoolidentify.sip index 6251fccc83c..dfa45c6e9f3 100644 --- a/python/gui/qgsmaptoolidentify.sip +++ b/python/gui/qgsmaptoolidentify.sip @@ -78,10 +78,6 @@ class QgsMapToolIdentify : QgsMapTool @return a list of IdentifyResult*/ QList 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(); @@ -90,4 +86,25 @@ class QgsMapToolIdentify : QgsMapTool void identifyProgress( int, int ); void identifyMessage( QString ); void changedRasterResults( QList& ); + + 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 identify( int x, int y, IdentifyMode mode, QList layerList, LayerType layerType = AllLayers ); + + /** call the right method depending on layer type */ + bool identifyLayer( QList *results, QgsMapLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel, LayerType layerType = AllLayers ); + + bool identifyRasterLayer( QList *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel ); + bool identifyVectorLayer( QList *results, QgsVectorLayer *layer, QgsPoint point ); }; diff --git a/python/gui/qgsmaptoolidentifyfeature.sip b/python/gui/qgsmaptoolidentifyfeature.sip new file mode 100644 index 00000000000..c02b796c438 --- /dev/null +++ b/python/gui/qgsmaptoolidentifyfeature.sip @@ -0,0 +1,14 @@ +class QgsMapToolIdentifyFeature : QgsMapToolIdentify +{ +%TypeHeaderCode +#include +%End + + public: + QgsMapToolIdentifyFeature( QgsVectorLayer* vl, QgsMapCanvas* canvas ); + + void canvasReleaseEvent( QMouseEvent* e ); + + signals: + void featureIdentified( QgsFeature ); +}; diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 2944daa821e..37405e3a139 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -181,6 +181,7 @@ qgsmaptip.cpp qgsmaptool.cpp qgsmaptoolemitpoint.cpp qgsmaptoolidentify.cpp +qgsmaptoolidentifyfeature.cpp qgsmaptoolpan.cpp qgsmaptoolzoom.cpp qgsmessagebar.cpp @@ -372,6 +373,7 @@ qgsmapoverviewcanvas.h qgsmaptool.h qgsmaptoolemitpoint.h qgsmaptoolidentify.h +qgsmaptoolidentifyfeature.h qgsmessagebaritem.h qgsmessagebar.h qgsmessagelogviewer.h @@ -454,6 +456,7 @@ qgsmaptip.h qgsmaptool.h qgsmaptoolemitpoint.h qgsmaptoolidentify.h +qgsmaptoolidentifyfeature.h qgsmaptoolpan.h qgsmaptoolzoom.h qgsmessagebaritem.h diff --git a/src/gui/qgsmaptool.cpp b/src/gui/qgsmaptool.cpp index 58f4d61619f..463a07adfd2 100644 --- a/src/gui/qgsmaptool.cpp +++ b/src/gui/qgsmaptool.cpp @@ -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(); } @@ -94,6 +96,8 @@ void QgsMapTool::deactivate() mAction->setChecked( false ); if ( mButton ) mButton->setChecked( false ); + + emit deactivated(); } void QgsMapTool::setAction( QAction* action ) diff --git a/src/gui/qgsmaptool.h b/src/gui/qgsmaptool.h index 9d28a155cf6..8b7f8f43200 100644 --- a/src/gui/qgsmaptool.h +++ b/src/gui/qgsmaptool.h @@ -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(); diff --git a/src/gui/qgsmaptoolidentify.h b/src/gui/qgsmaptoolidentify.h index c400cf91f31..7749dc62529 100644 --- a/src/gui/qgsmaptoolidentify.h +++ b/src/gui/qgsmaptoolidentify.h @@ -123,10 +123,6 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool @return a list of IdentifyResult*/ QList 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(); @@ -136,7 +132,10 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool void identifyMessage( QString ); void changedRasterResults( QList& ); - 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 @@ -154,6 +153,8 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool bool identifyRasterLayer( QList *results, QgsRasterLayer *layer, QgsPoint point, QgsRectangle viewExtent, double mapUnitsPerPixel ); bool identifyVectorLayer( QList *results, QgsVectorLayer *layer, QgsPoint point ); + + private: //! Private helper virtual void convertMeasurement( QgsDistanceArea &calc, double &measure, QGis::UnitType &u, bool isArea ); diff --git a/src/gui/qgsmaptoolidentifyfeature.cpp b/src/gui/qgsmaptoolidentifyfeature.cpp new file mode 100644 index 00000000000..28d0ca20609 --- /dev/null +++ b/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 + +#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 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(); + } +} diff --git a/src/gui/qgsmaptoolidentifyfeature.h b/src/gui/qgsmaptoolidentifyfeature.h new file mode 100644 index 00000000000..87cbf68bc66 --- /dev/null +++ b/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