QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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_sip.h"
21 #include <QList>
22 #include <QMap>
23 #include <QAction>
24 
25 #include "qgis.h"
26 #include "qgis_gui.h"
27 
28 class QgsFeature;
29 class QgsMapLayer;
30 
38 class GUI_EXPORT QgsMapLayerAction : public QAction
39 {
40  Q_OBJECT
41 
42  public:
43  enum Target
44  {
45  Layer = 1,
46  SingleFeature = 2,
47  MultipleFeatures = 4,
48  AllActions = Layer | SingleFeature | MultipleFeatures
49  };
50  Q_DECLARE_FLAGS( Targets, Target )
51  Q_FLAG( Targets )
52 
53 
57  enum Flag
58  {
59  EnabledOnlyWhenEditable = 1 << 1,
60  };
61 
66  Q_DECLARE_FLAGS( Flags, Flag )
67  Q_FLAG( Flags )
68 
69 
73  QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, Targets targets = AllActions, const QIcon &icon = QIcon(), QgsMapLayerAction::Flags flags = QgsMapLayerAction::Flags() );
74 
76  QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayer *layer, Targets targets = AllActions, const QIcon &icon = QIcon(), QgsMapLayerAction::Flags flags = QgsMapLayerAction::Flags() );
77 
79  QgsMapLayerAction( const QString &name, QObject *parent SIP_TRANSFERTHIS, QgsMapLayerType layerType, Targets targets = AllActions, const QIcon &icon = QIcon(), QgsMapLayerAction::Flags flags = QgsMapLayerAction::Flags() );
80 
81  ~QgsMapLayerAction() override;
82 
87  QgsMapLayerAction::Flags flags() const;
88 
90  virtual bool canRunUsingLayer( QgsMapLayer *layer ) const;
91 
93  virtual void triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
94 
96  virtual void triggerForFeature( QgsMapLayer *layer, const QgsFeature &feature );
97 
99  virtual void triggerForLayer( QgsMapLayer *layer );
100 
102  void setTargets( Targets targets ) {mTargets = targets;}
104  const Targets &targets() const {return mTargets;}
105 
110  bool isEnabledOnlyWhenEditable() const;
111 
112  signals:
114  void triggeredForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList );
115 
117  void triggeredForFeature( QgsMapLayer *layer, const QgsFeature &feature );
118 
121 
122  private:
123 
124  // true if action is only valid for a single layer
125  bool mSingleLayer = false;
126  // layer if action is only valid for a single layer
127  QgsMapLayer *mActionLayer = nullptr;
128 
129  // true if action is only valid for a specific layer type
130  bool mSpecificLayerType = false;
131  // layer type if action is only valid for a specific layer type
133 
134  // determine if the action can be run on layer and/or single feature and/or multiple features
135  Targets mTargets = Targets();
136 
137  QgsMapLayerAction::Flags mFlags = QgsMapLayerAction::Flags();
138 };
139 
140 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerAction::Targets )
141 
142 
149 class GUI_EXPORT QgsMapLayerActionRegistry : public QObject
150 {
151  Q_OBJECT
152 
153  public:
154 
161  QgsMapLayerActionRegistry( QObject *parent = nullptr );
162 
164  void addMapLayerAction( QgsMapLayerAction *action );
165 
167  QList<QgsMapLayerAction *> mapLayerActions( QgsMapLayer *layer, QgsMapLayerAction::Targets targets = QgsMapLayerAction::AllActions );
168 
170  bool removeMapLayerAction( QgsMapLayerAction *action );
171 
173  void setDefaultActionForLayer( QgsMapLayer *layer, QgsMapLayerAction *action );
175  QgsMapLayerAction *defaultActionForLayer( QgsMapLayer *layer );
176 
177  protected:
178 
179  QList< QgsMapLayerAction * > mMapLayerActionList;
180 
181  signals:
183  void changed();
184 
185  private:
186 
187  QMap< QgsMapLayer *, QgsMapLayerAction * > mDefaultLayerActionMap;
188 
189 };
190 
191 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerAction::Flags )
192 
193 #endif // QGSMAPLAYERACTIONREGISTRY_H
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
This class tracks map layer actions.
void changed()
Triggered when an action is added or removed from the registry.
QList< QgsMapLayerAction * > mMapLayerActionList
An action which can run on map layers The class can be used in two manners:
void triggeredForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggered when action has been run for a specific list of features.
const Targets & targets() const
Returns availibity of action.
Flag
Flags which control action behavior /since QGIS 3.0.
void setTargets(Targets targets)
Define the targets of the action.
void triggeredForLayer(QgsMapLayer *layer)
Triggered when action has been run for a specific layer.
void triggeredForFeature(QgsMapLayer *layer, const QgsFeature &feature)
Triggered when action has been run for a specific feature.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgis.h:47
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.