QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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::triggerForFeature
virtual void triggerForFeature(QgsMapLayer *layer, const QgsFeature &feature)
Triggers the action with the specified layer and feature.
Definition: qgsmaplayeractionregistry.cpp:95
QgsMapLayerAction::QgsMapLayerAction
QgsMapLayerAction(const QString &name, QObject *parent, Targets targets=AllActions, const QIcon &icon=QIcon(), QgsMapLayerAction::Flags flags=QgsMapLayerAction::Flags())
Action behavior flags.
Definition: qgsmaplayeractionregistry.cpp:20
QgsMapLayerActionRegistry::changed
void changed()
Triggered when an action is added or removed from the registry.
QgsMapLayerAction::triggeredForFeatures
void triggeredForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggered when action has been run for a specific list of features.
QgsMapLayerAction
An action which can run on map layers The class can be used in two manners:
Definition: qgsmaplayeractionregistry.h:38
QgsMapLayerType::VectorLayer
@ VectorLayer
Vector layer.
QgsMapLayerActionRegistry::mMapLayerActionList
QList< QgsMapLayerAction * > mMapLayerActionList
Definition: qgsmaplayeractionregistry.h:179
QgsGui::mapLayerActionRegistry
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:125
qgsgui.h
QgsMapLayerType
QgsMapLayerType
Types of layers that can be added to a map.
Definition: qgis.h:46
QgsMapLayerAction::canRunUsingLayer
virtual bool canRunUsingLayer(QgsMapLayer *layer) const
True if action can run using the specified layer.
Definition: qgsmaplayeractionregistry.cpp:56
QgsMapLayerAction::triggeredForLayer
void triggeredForLayer(QgsMapLayer *layer)
Triggered when action has been run for a specific layer.
QgsMapLayerActionRegistry::defaultActionForLayer
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Definition: qgsmaplayeractionregistry.cpp:168
QgsMapLayerActionRegistry::removeMapLayerAction
bool removeMapLayerAction(QgsMapLayerAction *action)
Removes a map layer action from the registry.
Definition: qgsmaplayeractionregistry.cpp:141
QgsMapLayerAction::triggeredForFeature
void triggeredForFeature(QgsMapLayer *layer, const QgsFeature &feature)
Triggered when action has been run for a specific feature.
QgsMapLayerActionRegistry::mapLayerActions
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
Definition: qgsmaplayeractionregistry.cpp:125
QgsMapLayerAction::triggerForFeatures
virtual void triggerForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggers the action with the specified layer and list of feature.
Definition: qgsmaplayeractionregistry.cpp:90
qgsmaplayeractionregistry.h
qgsvectorlayer.h
QgsMapLayerActionRegistry::addMapLayerAction
void addMapLayerAction(QgsMapLayerAction *action)
Adds a map layer action to the registry.
Definition: qgsmaplayeractionregistry.cpp:119
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsMapLayerAction::EnabledOnlyWhenEditable
@ EnabledOnlyWhenEditable
Action should be shown only for editable layers.
Definition: qgsmaplayeractionregistry.h:59
QgsMapLayerAction::triggerForLayer
virtual void triggerForLayer(QgsMapLayer *layer)
Triggers the action with the specified layer.
Definition: qgsmaplayeractionregistry.cpp:100
QgsMapLayerAction::flags
QgsMapLayerAction::Flags flags() const
Layer behavior flags.
Definition: qgsmaplayeractionregistry.cpp:51
QgsMapLayerActionRegistry::setDefaultActionForLayer
void setDefaultActionForLayer(QgsMapLayer *layer, QgsMapLayerAction *action)
Sets the default action for a layer.
Definition: qgsmaplayeractionregistry.cpp:163
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsMapLayerAction::~QgsMapLayerAction
~QgsMapLayerAction() override
Definition: qgsmaplayeractionregistry.cpp:45
QgsMapLayerActionRegistry::QgsMapLayerActionRegistry
QgsMapLayerActionRegistry(QObject *parent=nullptr)
Constructor for QgsMapLayerActionRegistry.
Definition: qgsmaplayeractionregistry.cpp:114
QgsMapLayerAction::isEnabledOnlyWhenEditable
bool isEnabledOnlyWhenEditable() const
Returns true if the action is only enabled for layers in editable mode.
Definition: qgsmaplayeractionregistry.cpp:105
QgsMapLayer::type
QgsMapLayerType type
Definition: qgsmaplayer.h:80