QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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
27 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
Base class for all map layer types.
Definition: qgsmaplayer.h:78
ActionData(QgsMapLayer *layer, QgsFeatureId fid, QgsMapLayerAction *mapLayerAction=nullptr)
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
void setShowFeatureActions(bool showFeatureActions)
define if attribute actions(1) and map layer actions(2) can be listed and run from the menu ...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
bool resultsIfExternalAction()
void addCustomAction(QgsMapLayerAction *action)
adds a new custom action to the menu
bool execWithSingleResult()
A class for highlight features on the map.
Definition: qgshighlight.h:49
void setResultsIfExternalAction(bool resultsIfExternalAction)
setResultsIfExternalAction if set to false (default) the menu will not return any results if an exter...
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
The QgsIdentifyMenu class builds a menu to be used with identify results (.
Single scope for storing variables and functions for use within a QgsExpressionContext.
This class tracks map layer actions.
void setExecWithSingleResult(bool execWithSingleResult)
define if the menu will be shown with a single identify result
bool allowMultipleReturn()
void setAllowMultipleReturn(bool multipleReturn)
define if the menu executed can return multiple results (e.g. all results or all identified features ...
Represents a vector layer which manages a vector based data sets.
An action which can run on map layers.
ActionData(QgsMapLayer *layer, QgsMapLayerAction *mapLayerAction=nullptr)