QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
45 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
46 {
47  Q_OBJECT
48 
49  public:
50 
52  {
53  DefaultQgsSetting = -1,
57  LayerSelection
58  };
59  Q_ENUM( IdentifyMode )
60 
61  enum Type
62  {
63  VectorLayer = 1,
64  RasterLayer = 2,
65  AllLayers = VectorLayer | RasterLayer
66  };
67  Q_DECLARE_FLAGS( LayerType, Type )
68  Q_FLAG( LayerType )
69 
71  {
73  IdentifyResult() = default;
74 
75  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
76  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
77 
78  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
79  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
80 
81  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
82  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
83 
84  QgsMapLayer *mLayer = nullptr;
85  QString mLabel;
88  QMap< QString, QString > mAttributes;
89  QMap< QString, QString > mDerivedAttributes;
90  QMap< QString, QVariant > mParams;
91  };
92 
95 
96  ~QgsMapToolIdentify() override;
97 
98  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
99  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
100  void canvasPressEvent( QgsMapMouseEvent *e ) override;
101  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
102  void activate() override;
103  void deactivate() override;
104 
112  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting );
113 
123  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
124 
126  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType );
128  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType );
129 
130 
135  QgsIdentifyMenu *identifyMenu() {return mIdentifyMenu;}
136 
137  public slots:
138  void formatChanged( QgsRasterLayer *layer );
139 
140  signals:
141  void identifyProgress( int, int );
142  void identifyMessage( const QString & );
143  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
144 
145  protected:
146 
157  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers );
158 
159  QgsIdentifyMenu *mIdentifyMenu = nullptr;
160 
162  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
163 
164  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
165  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
166 
168  QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
169 
182  void setCanvasPropertiesOverrides( double searchRadiusMapUnits );
183 
189  void restoreCanvasPropertiesOverrides();
190 
191  private:
192 
193  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
194  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
195  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry );
196 
202  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
203 
209  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
210 
216  QString formatDistance( double distance ) const;
217 
223  QString formatArea( double area ) const;
224 
229  QString formatDistance( double distance, QgsUnitTypes::DistanceUnit unit ) const;
230 
235  QString formatArea( double area, QgsUnitTypes::AreaUnit unit ) const;
236 
237  QMap< QString, QString > featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
238 
242  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
243 
247  void closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );
248 
249  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
250  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
251  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
252 
253  // Last geometry (point or polygon) in map CRS
254  QgsGeometry mLastGeometry;
255 
256  double mLastMapUnitsPerPixel;
257 
258  QgsRectangle mLastExtent;
259 
260  int mCoordinatePrecision;
261 
262  double mOverrideCanvasSearchRadius = -1;
263 };
264 
265 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
266 
267 #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:40
Base class for all map layer types.
Definition: qgsmaplayer.h:63
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:157
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:94
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
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
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:167
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
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:56
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:99
Abstract base class for all geometries.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:53
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
QMap< QString, QVariant > mParams
Flags flags() const override
Returns the flags for the map tool.
QgsIdentifyMenu * identifyMenu()
Returns a pointer to the identify menu which will be used in layer selection mode this menu can also ...
Represents a vector layer which manages a vector based data sets.
AreaUnit
Units of area.
Definition: qgsunittypes.h:79
QMap< QString, QString > mDerivedAttributes
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:172