QGIS API Documentation  3.6.0-Noosa (5873452)
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 
163  protected:
164  void closeEvent( QCloseEvent *e ) override;
165 
166  private slots:
167  void handleMenuHover();
168  void deleteRubberBands();
169  void layerDestroyed();
170  void triggerMapLayerAction();
171 
172  private:
173 
175  void addRasterLayer( QgsMapLayer *layer );
176 
181  void addVectorLayer( QgsVectorLayer *layer, const QList<QgsMapToolIdentify::IdentifyResult> &results, bool singleLayer = false );
182 
184  QList<QgsMapToolIdentify::IdentifyResult> results( QAction *action, bool &externalAction );
185 
186  QgsMapCanvas *mCanvas = nullptr;
187  QList<QgsHighlight *> mRubberBands;
188  bool mAllowMultipleReturn;
189  bool mExecWithSingleResult;
190  bool mShowFeatureActions;
191  bool mResultsIfExternalAction;
192  int mMaxLayerDisplay;
193  int mMaxFeatureDisplay;
194 
195  QgsExpressionContextScope mExpressionContextScope;
196 
197  // name of the action to be displayed for feature default action, if other actions are shown
198  QString mDefaultActionName;
199 
200  // custom menu actions regirstry
201  CustomActionRegistry mCustomActionRegistry;
202 
203  // map layers with their results, this is the odering of the menu
204  QMap <QgsMapLayer *, QList<QgsMapToolIdentify::IdentifyResult> > mLayerIdResults;
205 };
206 
208 
209 #endif // QGSIDENTIFYMENU_H
Base class for all map layer types.
Definition: qgsmaplayer.h:64
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 ...
Q_DECLARE_METATYPE(QModelIndex)
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()
void setResultsIfExternalAction(bool resultsIfExternalAction)
setResultsIfExternalAction if set to false (default) the menu will not return any results if an exter...
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)