QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 QgsVectorTileLayer;
32 class QgsMapLayer;
33 class QgsMapCanvas;
34 class QgsMeshLayer;
35 class QgsHighlight;
36 class QgsIdentifyMenu;
37 
47 class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
48 {
49  Q_OBJECT
50 
51  public:
52 
54  {
55  DefaultQgsSetting = -1,
59  LayerSelection
60  };
61  Q_ENUM( IdentifyMode )
62 
63  enum Type
64  {
67  MeshLayer = 4,
70  };
71  Q_DECLARE_FLAGS( LayerType, Type )
72  Q_FLAG( LayerType )
73 
75  {
77  IdentifyResult() = default;
78 
79  IdentifyResult( QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
80  : mLayer( layer ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
81 
82  IdentifyResult( QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes )
83  : mLayer( layer ), mLabel( label ), mAttributes( attributes ), mDerivedAttributes( derivedAttributes ) {}
84 
85  IdentifyResult( QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes )
86  : mLayer( layer ), mLabel( label ), mFields( fields ), mFeature( feature ), mDerivedAttributes( derivedAttributes ) {}
87 
88  QgsMapLayer *mLayer = nullptr;
89  QString mLabel;
92  QMap< QString, QString > mAttributes;
93  QMap< QString, QString > mDerivedAttributes;
94  QMap< QString, QVariant > mParams;
95  };
96 
99 
100  ~QgsMapToolIdentify() override;
101 
102  Flags flags() const override { return QgsMapTool::AllowZoomRect; }
103  void canvasMoveEvent( QgsMapMouseEvent *e ) override;
104  void canvasPressEvent( QgsMapMouseEvent *e ) override;
105  void canvasReleaseEvent( QgsMapMouseEvent *e ) override;
106  void activate() override;
107  void deactivate() override;
108 
116  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, const QList<QgsMapLayer *> &layerList = QList<QgsMapLayer *>(), IdentifyMode mode = DefaultQgsSetting );
117 
128  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, LayerType layerType = AllLayers );
129 
131  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, LayerType layerType );
133  QList<QgsMapToolIdentify::IdentifyResult> identify( const QgsGeometry &geometry, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType );
134 
135 
140  QgsIdentifyMenu *identifyMenu() {return mIdentifyMenu;}
141 
142  public slots:
143  void formatChanged( QgsRasterLayer *layer );
144 
145  signals:
146  void identifyProgress( int, int );
147  void identifyMessage( const QString & );
148  void changedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> & );
149 
150  protected:
151 
163  QList<QgsMapToolIdentify::IdentifyResult> identify( int x, int y, IdentifyMode mode, const QList<QgsMapLayer *> &layerList, LayerType layerType = AllLayers );
164 
165  QgsIdentifyMenu *mIdentifyMenu = nullptr;
166 
168  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsPointXY &point, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
169 
170  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, QgsPointXY point, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
171  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsPointXY &point );
172 
179  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsPointXY &point );
180 
182  QMap< QString, QString > derivedAttributesForPoint( const QgsPoint &point );
183 
196  void setCanvasPropertiesOverrides( double searchRadiusMapUnits );
197 
203  void restoreCanvasPropertiesOverrides();
204 
205  private:
206 
207  bool identifyLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMapLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel, QgsMapToolIdentify::LayerType layerType = AllLayers );
208  bool identifyRasterLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsRasterLayer *layer, const QgsGeometry &geometry, const QgsRectangle &viewExtent, double mapUnitsPerPixel );
209  bool identifyVectorLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorLayer *layer, const QgsGeometry &geometry );
210  bool identifyMeshLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsMeshLayer *layer, const QgsGeometry &geometry );
211  bool identifyVectorTileLayer( QList<QgsMapToolIdentify::IdentifyResult> *results, QgsVectorTileLayer *layer, const QgsGeometry &geometry );
212 
218  virtual QgsUnitTypes::DistanceUnit displayDistanceUnits() const;
219 
225  virtual QgsUnitTypes::AreaUnit displayAreaUnits() const;
226 
232  QString formatDistance( double distance ) const;
233 
239  QString formatArea( double area ) const;
240 
245  QString formatDistance( double distance, QgsUnitTypes::DistanceUnit unit ) const;
246 
251  QString formatArea( double area, QgsUnitTypes::AreaUnit unit ) const;
252 
253  QMap< QString, QString > featureDerivedAttributes( const QgsFeature &feature, QgsMapLayer *layer, const QgsPointXY &layerPoint = QgsPointXY() );
254 
258  void closestVertexAttributes( const QgsAbstractGeometry &geometry, QgsVertexId vId, QgsMapLayer *layer, QMap< QString, QString > &derivedAttributes );
259 
263  void closestPointAttributes( const QgsAbstractGeometry &geometry, const QgsPointXY &layerPoint, QMap< QString, QString > &derivedAttributes );
264 
265  QString formatCoordinate( const QgsPointXY &canvasPoint ) const;
266  QString formatXCoordinate( const QgsPointXY &canvasPoint ) const;
267  QString formatYCoordinate( const QgsPointXY &canvasPoint ) const;
268 
269  // Last geometry (point or polygon) in map CRS
270  QgsGeometry mLastGeometry;
271 
272  double mLastMapUnitsPerPixel;
273 
274  QgsRectangle mLastExtent;
275 
276  int mCoordinatePrecision;
277 
278  double mOverrideCanvasSearchRadius = -1;
279 };
280 
281 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapToolIdentify::LayerType )
282 
283 #endif
qgsfields.h
QgsMapToolIdentify::Type
Type
Definition: qgsmaptoolidentify.h:63
QgsVectorTileLayer
Definition: qgsvectortilelayer.h:83
QgsMapLayerType::MeshLayer
@ MeshLayer
Added in 3.2.
QgsMapLayerType::VectorLayer
@ VectorLayer
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
QgsMapToolIdentify::ActiveLayer
@ ActiveLayer
Definition: qgsmaptoolidentify.h:56
QgsMapToolIdentify::identifyMenu
QgsIdentifyMenu * identifyMenu()
Returns a pointer to the identify menu which will be used in layer selection mode this menu can also ...
Definition: qgsmaptoolidentify.h:140
QgsMapToolIdentify
Map tool for identifying features in layers.
Definition: qgsmaptoolidentify.h:47
QgsFields
Definition: qgsfields.h:44
QgsMapCanvas
Definition: qgsmapcanvas.h:83
QgsMapTool::deactivate
virtual void deactivate()
called when map tool is being deactivated
Definition: qgsmaptool.cpp:99
qgsfeature.h
qgsunittypes.h
QgsMapToolIdentify::IdentifyResult::mFields
QgsFields mFields
Definition: qgsmaptoolidentify.h:90
QgsMapToolIdentify::IdentifyResult::mLabel
QString mLabel
Definition: qgsmaptoolidentify.h:89
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsRectangle
Definition: qgsrectangle.h:41
QgsMapToolIdentify::TopDownStopAtFirst
@ TopDownStopAtFirst
Definition: qgsmaptoolidentify.h:57
QgsHighlight
Definition: qgshighlight.h:57
QgsMapToolIdentify::TopDownAll
@ TopDownAll
Definition: qgsmaptoolidentify.h:58
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QString &label, const QMap< QString, QString > &attributes, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:82
QgsMapTool
Definition: qgsmaptool.h:63
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QString &label, const QgsFields &fields, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:85
QgsMapToolIdentify::IdentifyMode
IdentifyMode
Definition: qgsmaptoolidentify.h:53
QgsMapToolIdentify::IdentifyResult::mFeature
QgsFeature mFeature
Definition: qgsmaptoolidentify.h:91
QgsMapToolIdentify::IdentifyResult::mAttributes
QMap< QString, QString > mAttributes
Definition: qgsmaptoolidentify.h:92
QgsMeshLayer
Definition: qgsmeshlayer.h:94
QgsMapLayerType::RasterLayer
@ RasterLayer
qgsmaptool.h
QgsMapToolIdentify::flags
Flags flags() const override
Returns the flags for the map tool.
Definition: qgsmaptoolidentify.h:102
QgsUnitTypes::AreaUnit
AreaUnit
Units of area.
Definition: qgsunittypes.h:93
QgsRasterLayer
Definition: qgsrasterlayer.h:72
QgsMapTool::activate
virtual void activate()
called when set as currently active map tool
Definition: qgsmaptool.cpp:83
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
QgsPointXY
Definition: qgspointxy.h:43
QgsMapTool::canvasPressEvent
virtual void canvasPressEvent(QgsMapMouseEvent *e)
Mouse press event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:167
QgsMapMouseEvent
Definition: qgsmapmouseevent.h:35
QgsMapTool::canvasMoveEvent
virtual void canvasMoveEvent(QgsMapMouseEvent *e)
Mouse move event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:157
QgsGeometry
Definition: qgsgeometry.h:122
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapTool::canvasReleaseEvent
virtual void canvasReleaseEvent(QgsMapMouseEvent *e)
Mouse release event for overriding. Default implementation does nothing.
Definition: qgsmaptool.cpp:172
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsVertexId
Utility class for identifying a unique vertex within a geometry.
Definition: qgsabstractgeometry.h:1033
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Added in 3.14.
QgsMapToolIdentify::IdentifyResult::IdentifyResult
IdentifyResult(QgsMapLayer *layer, const QgsFeature &feature, const QMap< QString, QString > &derivedAttributes)
Definition: qgsmaptoolidentify.h:79
QgsFeature
Definition: qgsfeature.h:55
QgsMapTool::AllowZoomRect
@ AllowZoomRect
Allow zooming by rectangle (by holding shift and dragging) while the tool is active.
Definition: qgsmaptool.h:95
QgsMapToolIdentify::IdentifyResult::mParams
QMap< QString, QVariant > mParams
Definition: qgsmaptoolidentify.h:94
QgsIdentifyMenu
The QgsIdentifyMenu class builds a menu to be used with identify results (.
Definition: qgsidentifymenu.h:48
qgspointxy.h
QgsMapToolIdentify::IdentifyResult
Definition: qgsmaptoolidentify.h:74
QgsMapToolIdentify::IdentifyResult::mDerivedAttributes
QMap< QString, QString > mDerivedAttributes
Definition: qgsmaptoolidentify.h:93