QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsmaplayeractionregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayeractionregistry.h
3  ---------------------------
4  begin : January 2014
5  copyright : (C) 2014 by Nyall Dawson
6  email : nyall dot dawson 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 QGSMAPLAYERACTIONREGISTRY_H
17 #define QGSMAPLAYERACTIONREGISTRY_H
18 
19 #include <QObject>
20 #include "qgis.h"
21 #include <QList>
22 #include <QMap>
23 #include <QAction>
24 
25 #include "qgsmaplayer.h"
26 #include "qgis_gui.h"
27 
28 class QgsFeature;
29 
34 class GUI_EXPORT QgsMapLayerAction : public QAction
35 {
36  Q_OBJECT
37 
38  public:
39  enum Target
40  {
41  Layer = 1,
42  SingleFeature = 2,
43  MultipleFeatures = 4,
44  AllActions = Layer | SingleFeature | MultipleFeatures
45  };
46  Q_DECLARE_FLAGS( Targets, Target )
47  Q_FLAG( Targets )
48 
53  enum Flag
54  {
55  EnabledOnlyWhenEditable = 1 << 1,
56  };
57 
62  Q_DECLARE_FLAGS( Flags, Flag )
63  Q_FLAG( Flags )
64 
69  QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, Targets targets = AllActions, const QIcon &icon = QIcon(), QgsMapLayerAction::Flags flags = nullptr );
70 
72  QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer *layer, Targets targets = AllActions, const QIcon &icon = QIcon(), QgsMapLayerAction::Flags flags = nullptr );
73 
75  QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer::LayerType layerType, Targets targets = AllActions, const QIcon &icon = QIcon(), QgsMapLayerAction::Flags flags = nullptr );
76 
77  ~QgsMapLayerAction() override;
78 
83  QgsMapLayerAction::Flags flags() const;
84 
86  bool canRunUsingLayer( QgsMapLayer *layer ) const;
87 
89  void triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
90 
92  void triggerForFeature( QgsMapLayer *layer, const QgsFeature *feature );
93 
95  void triggerForLayer( QgsMapLayer *layer );
96 
98  void setTargets( Targets targets ) {mTargets = targets;}
100  const Targets &targets() const {return mTargets;}
101 
106  bool isEnabledOnlyWhenEditable() const;
107 
108  signals:
110  void triggeredForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
111 
113  void triggeredForFeature( QgsMapLayer *layer, const QgsFeature &feature );
114 
116  void triggeredForLayer( QgsMapLayer *layer );
117 
118  private:
119 
120  // true if action is only valid for a single layer
121  bool mSingleLayer = false;
122  // layer if action is only valid for a single layer
123  QgsMapLayer *mActionLayer = nullptr;
124 
125  // true if action is only valid for a specific layer type
126  bool mSpecificLayerType = false;
127  // layer type if action is only valid for a specific layer type
129 
130  // determine if the action can be run on layer and/or single feature and/or multiple features
131  Targets mTargets = nullptr;
132 
133  QgsMapLayerAction::Flags mFlags = nullptr;
134 };
135 
136 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerAction::Targets )
137 
138 
145 class GUI_EXPORT QgsMapLayerActionRegistry : public QObject
146 {
147  Q_OBJECT
148 
149  public:
150 
157  QgsMapLayerActionRegistry( QObject *parent = nullptr );
158 
160  void addMapLayerAction( QgsMapLayerAction *action );
161 
163  QList<QgsMapLayerAction *> mapLayerActions( QgsMapLayer *layer, QgsMapLayerAction::Targets targets = QgsMapLayerAction::AllActions );
164 
166  bool removeMapLayerAction( QgsMapLayerAction *action );
167 
169  void setDefaultActionForLayer( QgsMapLayer *layer, QgsMapLayerAction *action );
171  QgsMapLayerAction *defaultActionForLayer( QgsMapLayer *layer );
172 
173  protected:
174 
175  QList< QgsMapLayerAction * > mMapLayerActionList;
176 
177  signals:
179  void changed();
180 
181  private:
182 
183  QMap< QgsMapLayer *, QgsMapLayerAction * > mDefaultLayerActionMap;
184 
185 };
186 
187 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerAction::Flags )
188 
189 #endif // QGSMAPLAYERACTIONREGISTRY_H
Base class for all map layer types.
Definition: qgsmaplayer.h:63
QList< QgsMapLayerAction * > mMapLayerActionList
Flag
Flags which control action behavior /since QGIS 3.0.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:105
This class tracks map layer actions.
const Targets & targets() const
Returns availibity of action.
An action which can run on map layers.