QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsmaplayeractionregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayeractionregistry.cpp
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 
17 #include "qgsgui.h"
18 #include "qgsvectorlayer.h"
19 
20 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, Targets targets, const QIcon &icon, QgsMapLayerAction::Flags flags )
21  : QAction( icon, name, parent )
22  , mTargets( targets )
23  , mFlags( flags )
24 {
25 }
26 
27 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer *layer, Targets targets, const QIcon &icon, QgsMapLayerAction::Flags flags )
28  : QAction( icon, name, parent )
29  , mSingleLayer( true )
30  , mActionLayer( layer )
31  , mTargets( targets )
32  , mFlags( flags )
33 {
34 }
35 
36 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayerType layerType, Targets targets, const QIcon &icon, QgsMapLayerAction::Flags flags )
37  : QAction( icon, name, parent )
38  , mSpecificLayerType( true )
39  , mLayerType( layerType )
40  , mTargets( targets )
41  , mFlags( flags )
42 {
43 }
44 
46 {
47  //remove action from registry
49 }
50 
51 QgsMapLayerAction::Flags QgsMapLayerAction::flags() const
52 {
53  return mFlags;
54 }
55 
57 {
58  if ( mFlags & EnabledOnlyWhenEditable )
59  {
60  // action is only enabled for editable layers
61  if ( !layer )
62  return false;
63  if ( layer->type() != QgsMapLayerType::VectorLayer )
64  return false;
65  if ( !qobject_cast<QgsVectorLayer *>( layer )->isEditable() )
66  return false;
67  }
68 
69  //check layer details
70  if ( !mSingleLayer && !mSpecificLayerType )
71  {
72  //action is not a single layer of specific layer type action,
73  //so return true
74  return true;
75  }
76  if ( mSingleLayer && layer == mActionLayer )
77  {
78  //action is a single layer type and layer matches
79  return true;
80  }
81  else if ( mSpecificLayerType && layer && layer->type() == mLayerType )
82  {
83  //action is for a layer type and layer type matches
84  return true;
85  }
86 
87  return false;
88 }
89 
90 void QgsMapLayerAction::triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList )
91 {
92  emit triggeredForFeatures( layer, featureList );
93 }
94 
96 {
97  emit triggeredForFeature( layer, *feature );
98 }
99 
101 {
102  emit triggeredForLayer( layer );
103 }
104 
106 {
107  return mFlags & EnabledOnlyWhenEditable;
108 }
109 
110 //
111 // Main class begins now...
112 //
113 
114 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
115 {
116  // constructor does nothing
117 }
118 
120 {
121  mMapLayerActionList.append( action );
122  emit changed();
123 }
124 
125 QList< QgsMapLayerAction * > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer *layer, QgsMapLayerAction::Targets targets )
126 {
127  QList< QgsMapLayerAction * > validActions;
128 
129  const auto constMMapLayerActionList = mMapLayerActionList;
130  for ( QgsMapLayerAction *action : constMMapLayerActionList )
131  {
132  if ( action->canRunUsingLayer( layer ) && ( targets & action->targets() ) )
133  {
134  validActions.append( action );
135  }
136  }
137  return validActions;
138 }
139 
140 
142 {
143  if ( mMapLayerActionList.indexOf( action ) != -1 )
144  {
145  mMapLayerActionList.removeAll( action );
146 
147  //also remove this action from the default layer action map
148  QMap<QgsMapLayer *, QgsMapLayerAction *>::iterator defaultIt;
149  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
150  {
151  if ( defaultIt.value() == action )
152  {
153  defaultIt.value() = nullptr;
154  }
155  }
156  emit changed();
157  return true;
158  }
159  //not found
160  return false;
161 }
162 
164 {
165  mDefaultLayerActionMap[ layer ] = action;
166 }
167 
169 {
170  if ( !mDefaultLayerActionMap.contains( layer ) )
171  {
172  return nullptr;
173  }
174 
175  return mDefaultLayerActionMap[ layer ];
176 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:78
QList< QgsMapLayerAction *> mMapLayerActionList
QgsMapLayerActionRegistry(QObject *parent=nullptr)
Constructor for QgsMapLayerActionRegistry.
QgsMapLayerType type() const
Returns the type of the layer.
bool canRunUsingLayer(QgsMapLayer *layer) const
True if action can run using the specified layer.
bool isEnabledOnlyWhenEditable() const
Returns true if the action is only enabled for layers in editable mode.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void triggeredForFeature(QgsMapLayer *layer, const QgsFeature &feature)
Triggered when action has been run for a specific feature.
void triggeredForLayer(QgsMapLayer *layer)
Triggered when action has been run for a specific layer.
void setDefaultActionForLayer(QgsMapLayer *layer, QgsMapLayerAction *action)
Sets the default action for a layer.
Action should be shown only for editable layers.
QgsMapLayerAction(const QString &name, QObject *parent, Targets targets=AllActions, const QIcon &icon=QIcon(), QgsMapLayerAction::Flags flags=nullptr)
Action behavior flags.
void changed()
Triggered when an action is added or removed from the registry.
const Targets & targets() const
Returns availibity of action.
void triggerForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggers the action with the specified layer and list of feature.
void addMapLayerAction(QgsMapLayerAction *action)
Adds a map layer action to the registry.
QgsMapLayerAction::Flags flags() const
Layer behavior flags.
bool removeMapLayerAction(QgsMapLayerAction *action)
Removes a map layer action from the registry.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:65
void triggeredForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggered when action has been run for a specific list of features.
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:79
void triggerForLayer(QgsMapLayer *layer)
Triggers the action with the specified layer.
void triggerForFeature(QgsMapLayer *layer, const QgsFeature *feature)
Triggers the action with the specified layer and feature.
An action which can run on map layers.