QGIS API Documentation  3.0.2-Girona (307d082)
qgsmaptoolidentify.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptoolidentify.h - map tool for identifying 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 #ifndef QGSMAPTOOLIDENTIFY_H
17 #define QGSMAPTOOLIDENTIFY_H
18 
19 #include "qgsfeature.h"
20 #include "qgsfields.h"
21 #include "qgsmaptool.h"
22 #include "qgspointxy.h"
23 #include "qgsunittypes.h"
24 
25 #include <QObject>
26 #include <QPointer>
27 #include "qgis_gui.h"
28 
29 class QgsRasterLayer;
30 class QgsVectorLayer;
31 class QgsMapLayer;
32 class QgsMapCanvas;
33 class QgsHighlight;
34 class QgsIdentifyMenu;
35 class QgsDistanceArea;
36 
46 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
47 {
48  Q_OBJECT
49 
50  public:
51 
53  {
54  DefaultQgsSetting = -1,
58  LayerSelection
59  };
60  Q_ENUM( IdentifyMode )
61 
62  enum Type
63  {
64  VectorLayer = 1,
65  RasterLayer = 2,
66  AllLayers = VectorLayer | RasterLayer
67  };
68  Q_DECLARE_FLAGS( LayerType, Type )
69  Q_FLAG( LayerType )
70 
72  {
74  IdentifyResult() = default;
75 
76  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
77  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
78 
79  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
80  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
81 
82  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
83  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
84 
85  QgsMapLayer *mLayer = nullptr;
86  QString mLabel;
89  QMap< QString, QString > mAttributes;
90  QMap< QString, QString > mDerivedAttributes;
91  QMap< QString, QVariant > mParams;
92  };
93 
96 
97  ~QgsMapToolIdentify() override;
98 
99  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
100  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
101  void canvasPressEvent( QgsMapMouseEvent *e ) override;
102  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
103  void activate() override;
104  void deactivate() override;
105 
113  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting );
114 
124  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
125 
130  QgsIdentifyMenu *identifyMenu() {return mIdentifyMenu;}
131 
132  public slots:
133  void formatChanged( QgsRasterLayer *layer );
134 
135  signals:
136  void identifyProgress( int, int );
137  void identifyMessage( const QString & );
138  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
139 
140  protected:
141 
152  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers );
153 
154  QgsIdentifyMenu *mIdentifyMenu = nullptr;
155 
157  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
158 
159  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
160  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
161 
162  private:
163 
169  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
170 
176  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
177 
183  QString formatDistance( double distance ) const;
184 
190  QString formatArea( double area ) const;
191 
192  QMap< QString, QString > featureDerivedAttributes( QgsFeature *feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
193 
197  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
198 
199  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
200  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
201  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
202 
203  // Last point in canvas CRS
204  QgsPointXY mLastPoint;
205 
206  double mLastMapUnitsPerPixel;
207 
208  QgsRectangle mLastExtent;
209 
210  int mCoordinatePrecision;
211 };
212 
213 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
214 
215 #endif
IdentifyResult(QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
IdentifyResult(QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:148
QMap< QString, QString > mAttributes
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
A class to represent a 2D point.
Definition: qgspointxy.h:43
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:95
A QgsMapMouseEvent is the result of a user interaction with the mouse on a QgsMapCanvas.
Container of fields for a vector layer.
Definition: qgsfields.h:42
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
IdentifyResult(QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes)
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:158
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:81
Map tool for identifying features in layers.
Utility class for identifying a unique vertex within a geometry.
A class for highlight features on the map.
Definition: qgshighlight.h:49
The QgsIdentifyMenu class builds a menu to be used with identify results (.
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:97
Abstract base class for all geometries.
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
Abstract base class for all map tools.
Definition: qgsmaptool.h:63
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
QMap< QString, QVariant > mParams
Flags flags() const override
Returns the flags for the map tool.
QgsIdentifyMenu * identifyMenu()
return a pointer to the identify menu which will be used in layer selection mode this menu can also b...
Represents a vector layer which manages a vector based data sets.
AreaUnit
Units of area.
Definition: qgsunittypes.h:69
QMap< QString, QString > mDerivedAttributes
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:163