QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsidentifymenu.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsidentifymenu.h - menu to be used in identify map tool
3  ---------------------
4  begin : August 2014
5  copyright : (C) 2014 by Denis Rouzaud
6  email : [email protected]
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 QGSIDENTIFYMENU_H
17 #define QGSIDENTIFYMENU_H
18 
19 #include <QMenu>
20 
22 #include "qgsmaptoolidentify.h"
23 #include "qgis_gui.h"
24 #include "qgis_sip.h"
25 
26 #ifndef SIP_RUN
28 class CustomActionRegistry : public QgsMapLayerActionRegistry
29 {
30  Q_OBJECT
31 
32  public:
33 
34  CustomActionRegistry() = default;
35  // remove all actions
36  void clear() { mMapLayerActionList.clear(); }
37 };
39 #endif
40 
48 class GUI_EXPORT QgsIdentifyMenu : public QMenu
49 {
50 
51  Q_OBJECT
52 
53  public:
54  enum MenuLevel
55  {
57  FeatureLevel
58  };
59 
60  struct ActionData
61  {
63  ActionData() = default;
64 
65  ActionData( QgsMapLayer *layer, QgsMapLayerAction *mapLayerAction = nullptr )
66  : mIsValid( true )
67  , mAllResults( !layer )
68  , mIsExternalAction( nullptr != mapLayerAction )
69  , mLayer( layer )
70  , mMapLayerAction( mapLayerAction )
71  {}
72 
73  ActionData( QgsMapLayer *layer, QgsFeatureId fid, QgsMapLayerAction *mapLayerAction = nullptr )
74  : mIsValid( true )
75  , mIsExternalAction( nullptr != mapLayerAction )
76  , mLayer( layer )
77  , mFeatureId( fid )
78  , mLevel( FeatureLevel )
79  , mMapLayerAction( mapLayerAction )
80  {}
81 
82  bool mIsValid = false;
83  bool mAllResults = false;
84  bool mIsExternalAction = false;
85  QgsMapLayer *mLayer = nullptr;
86  QgsFeatureId mFeatureId = 0;
87  QgsIdentifyMenu::MenuLevel mLevel = LayerLevel;
88  QgsMapLayerAction *mMapLayerAction = nullptr;
89  };
90 
94  explicit QgsIdentifyMenu( QgsMapCanvas *canvas );
95 
96  ~QgsIdentifyMenu() override;
97 
99  void setAllowMultipleReturn( bool multipleReturn ) { mAllowMultipleReturn = multipleReturn;}
100  bool allowMultipleReturn() { return mAllowMultipleReturn;}
101 
103  void setExecWithSingleResult( bool execWithSingleResult ) { mExecWithSingleResult = execWithSingleResult;}
104  bool execWithSingleResult() { return mExecWithSingleResult;}
105 
111  void setExpressionContextScope( const QgsExpressionContextScope &scope );
112 
118  QgsExpressionContextScope expressionContextScope() const;
119 
126  void setShowFeatureActions( bool showFeatureActions ) { mShowFeatureActions = showFeatureActions; }
127  bool showFeatureActions() { return mShowFeatureActions;}
128 
133  void setResultsIfExternalAction( bool resultsIfExternalAction ) {mResultsIfExternalAction = resultsIfExternalAction;}
134  bool resultsIfExternalAction() {return mResultsIfExternalAction;}
135 
140  void setMaxLayerDisplay( int maxLayerDisplay );
141  int maxLayerDisplay() {return mMaxLayerDisplay;}
142 
147  void setMaxFeatureDisplay( int maxFeatureDisplay );
148  int maxFeatureDisplay() {return mMaxFeatureDisplay;}
149 
151  void addCustomAction( QgsMapLayerAction *action ) {mCustomActionRegistry.addMapLayerAction( action );}
152 
154  void removeCustomActions();
155 
161  QList<QgsMapToolIdentify::IdentifyResult> exec( const QList<QgsMapToolIdentify::IdentifyResult> &idResults, QPoint pos );
162 
168  static void styleHighlight( QgsHighlight *highlight );
169 
170  protected:
171  void closeEvent( QCloseEvent *e ) override;
172 
173  private slots:
174  void handleMenuHover();
175  void deleteRubberBands();
176  void layerDestroyed();
177  void triggerMapLayerAction();
178 
179  private:
180 
182  void addRasterLayer( QgsMapLayer *layer );
183 
188  void addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapToolIdentify::IdentifyResult> &results, bool singleLayer = false );
189 
191  QList<QgsMapToolIdentify::IdentifyResult> results( QAction *action, bool &externalAction );
192 
193  QgsMapCanvas *mCanvas = nullptr;
194  QList<QgsHighlight *> mRubberBands;
195  bool mAllowMultipleReturn;
196  bool mExecWithSingleResult;
197  bool mShowFeatureActions;
198  bool mResultsIfExternalAction;
199  int mMaxLayerDisplay;
200  int mMaxFeatureDisplay;
201 
202  QgsExpressionContextScope mExpressionContextScope;
203 
204  // name of the action to be displayed for feature default action, if other actions are shown
205  QString mDefaultActionName;
206 
207  // custom menu actions regirstry
208  CustomActionRegistry mCustomActionRegistry;
209 
210  // map layers with their results, this is the odering of the menu
211  QMap <QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > mLayerIdResults;
212 };
213 
215 
216 #endif // QGSIDENTIFYMENU_H
Single scope for storing variables and functions for use within a QgsExpressionContext.
A class for highlight features on the map.
Definition: qgshighlight.h:59
The QgsIdentifyMenu class builds a menu to be used with identify results (.
bool execWithSingleResult()
bool allowMultipleReturn()
void setAllowMultipleReturn(bool multipleReturn)
define if the menu executed can return multiple results (e.g. all results or all identified features ...
void setResultsIfExternalAction(bool resultsIfExternalAction)
setResultsIfExternalAction if set to false (default) the menu will not return any results if an exter...
void addCustomAction(QgsMapLayerAction *action)
adds a new custom action to the menu
void setShowFeatureActions(bool showFeatureActions)
define if attribute actions(1) and map layer actions(2) can be listed and run from the menu
void setExecWithSingleResult(bool execWithSingleResult)
define if the menu will be shown with a single identify result
bool resultsIfExternalAction()
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:86
This class tracks map layer actions.
An action which can run on map layers.
Base class for all map layer types.
Definition: qgsmaplayer.h:85
Represents a vector layer which manages a vector based data sets.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
ActionData(QgsMapLayer *layer, QgsMapLayerAction *mapLayerAction=nullptr)
ActionData()=default
Constructor for ActionData.
ActionData(QgsMapLayer *layer, QgsFeatureId fid, QgsMapLayerAction *mapLayerAction=nullptr)