QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 "qgsexpressioncontext.h"
24 #include "qgis_gui.h"
25 #include "qgis_sip.h"
26 
27 #ifndef SIP_RUN
29 class CustomActionRegistry : public QgsMapLayerActionRegistry
30 {
31  Q_OBJECT
32 
33  public:
34 
35  CustomActionRegistry() = default;
36  // remove all actions
37  void clear() { mMapLayerActionList.clear(); }
38 };
40 #endif
41 
49 class GUI_EXPORT QgsIdentifyMenu : public QMenu
50 {
51 
52  Q_OBJECT
53 
54  public:
55  enum MenuLevel
56  {
58  FeatureLevel
59  };
60 
61  struct ActionData
62  {
64  ActionData() = default;
65 
66  ActionData( QgsMapLayer *layer, QgsMapLayerAction *mapLayerAction = nullptr )
67  : mIsValid( true )
68  , mAllResults( !layer )
69  , mIsExternalAction( nullptr != mapLayerAction )
70  , mLayer( layer )
71  , mMapLayerAction( mapLayerAction )
72  {}
73 
74  ActionData( QgsMapLayer *layer, QgsFeatureId fid, QgsMapLayerAction *mapLayerAction = nullptr )
75  : mIsValid( true )
76  , mIsExternalAction( nullptr != mapLayerAction )
77  , mLayer( layer )
78  , mFeatureId( fid )
79  , mLevel( FeatureLevel )
80  , mMapLayerAction( mapLayerAction )
81  {}
82 
83  bool mIsValid = false;
84  bool mAllResults = false;
85  bool mIsExternalAction = false;
86  QgsMapLayer *mLayer = nullptr;
87  QgsFeatureId mFeatureId = 0;
88  QgsIdentifyMenu::MenuLevel mLevel = LayerLevel;
89  QgsMapLayerAction *mMapLayerAction = nullptr;
90  };
91 
95  explicit QgsIdentifyMenu( QgsMapCanvas *canvas );
96 
97  ~QgsIdentifyMenu() override;
98 
100  void setAllowMultipleReturn( bool multipleReturn ) { mAllowMultipleReturn = multipleReturn;}
101  bool allowMultipleReturn() { return mAllowMultipleReturn;}
102 
104  void setExecWithSingleResult( bool execWithSingleResult ) { mExecWithSingleResult = execWithSingleResult;}
105  bool execWithSingleResult() { return mExecWithSingleResult;}
106 
112  void setExpressionContextScope( const QgsExpressionContextScope &scope );
113 
119  QgsExpressionContextScope expressionContextScope() const;
120 
127  void setShowFeatureActions( bool showFeatureActions ) { mShowFeatureActions = showFeatureActions; }
128  bool showFeatureActions() { return mShowFeatureActions;}
129 
134  void setResultsIfExternalAction( bool resultsIfExternalAction ) {mResultsIfExternalAction = resultsIfExternalAction;}
135  bool resultsIfExternalAction() {return mResultsIfExternalAction;}
136 
141  void setMaxLayerDisplay( int maxLayerDisplay );
142  int maxLayerDisplay() {return mMaxLayerDisplay;}
143 
148  void setMaxFeatureDisplay( int maxFeatureDisplay );
149  int maxFeatureDisplay() {return mMaxFeatureDisplay;}
150 
152  void addCustomAction( QgsMapLayerAction *action ) {mCustomActionRegistry.addMapLayerAction( action );}
153 
155  void removeCustomActions();
156 
162  QList<QgsMapToolIdentify::IdentifyResult> exec( const QList<QgsMapToolIdentify::IdentifyResult> &idResults, QPoint pos );
163 
169  static void styleHighlight( QgsHighlight *highlight );
170 
171  protected:
172  void closeEvent( QCloseEvent *e ) override;
173 
174  private slots:
175  void handleMenuHover();
176  void deleteRubberBands();
177  void layerDestroyed();
178  void triggerMapLayerAction();
179 
180  private:
181 
183  void addRasterLayer( QgsMapLayer *layer );
184 
189  void addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapToolIdentify::IdentifyResult> &results, bool singleLayer = false );
190 
192  QList<QgsMapToolIdentify::IdentifyResult> results( QAction *action, bool &externalAction );
193 
194  QgsMapCanvas *mCanvas = nullptr;
195  QList<QgsHighlight *> mRubberBands;
196  bool mAllowMultipleReturn;
197  bool mExecWithSingleResult;
198  bool mShowFeatureActions;
199  bool mResultsIfExternalAction;
200  int mMaxLayerDisplay;
201  int mMaxFeatureDisplay;
202 
203  QgsExpressionContextScope mExpressionContextScope;
204 
205  // name of the action to be displayed for feature default action, if other actions are shown
206  QString mDefaultActionName;
207 
208  // custom menu actions regirstry
209  CustomActionRegistry mCustomActionRegistry;
210 
211  // map layers with their results, this is the odering of the menu
212  QMap <QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > mLayerIdResults;
213 };
214 
216 
217 #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:62
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 The class can be used in two manners:
Base class for all map layer types.
Definition: qgsmaplayer.h:70
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)