QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
18 
19 QgsMapLayerAction::QgsMapLayerAction( const QString& name, QObject* parent, const Targets& targets, const QIcon& icon )
20  : QAction( icon, name, parent )
21  , mSingleLayer( false )
22  , mActionLayer( nullptr )
23  , mSpecificLayerType( false )
24  , mLayerType( QgsMapLayer::VectorLayer )
25  , mTargets( targets )
26 {
27 }
28 
31  : QAction( icon, name, parent )
32  , mSingleLayer( true )
33  , mActionLayer( layer )
34  , mSpecificLayerType( false )
35  , mLayerType( QgsMapLayer::VectorLayer )
36  , mTargets( targets )
37 {
38 }
39 
42  : QAction( icon, name, parent )
43  , mSingleLayer( false )
44  , mActionLayer( nullptr )
45  , mSpecificLayerType( true )
46  , mLayerType( layerType )
47  , mTargets( targets )
48 {
49 }
50 
52 {
53  //remove action from registry
55 }
56 
58 {
59  //check layer details
60  if ( !mSingleLayer && !mSpecificLayerType )
61  {
62  //action is not a single layer of specific layer type action,
63  //so return true
64  return true;
65  }
66  if ( mSingleLayer && layer == mActionLayer )
67  {
68  //action is a single layer type and layer matches
69  return true;
70  }
71  else if ( mSpecificLayerType && layer->type() == mLayerType )
72  {
73  //action is for a layer type and layer type matches
74  return true;
75  }
76 
77  return false;
78 }
79 
81 {
82  emit triggeredForFeatures( layer, featureList );
83 }
84 
86 {
87  emit triggeredForFeature( layer, *feature );
88 }
89 
91 {
92  emit triggeredForLayer( layer );
93 }
94 
95 //
96 // Static calls to enforce singleton behaviour
97 //
98 QgsMapLayerActionRegistry *QgsMapLayerActionRegistry::mInstance = nullptr;
100 {
101  if ( !mInstance )
102  {
103  mInstance = new QgsMapLayerActionRegistry();
104  }
105  return mInstance;
106 }
107 
108 //
109 // Main class begins now...
110 //
111 
113 {
114  // constructor does nothing
115 }
116 
118 {
119 
120 }
121 
123 {
124  mMapLayerActionList.append( action );
125  emit changed();
126 }
127 
128 QList< QgsMapLayerAction* > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer* layer, const QgsMapLayerAction::Targets& targets )
129 {
130  QList< QgsMapLayerAction* > validActions;
132  for ( actionIt = mMapLayerActionList.begin(); actionIt != mMapLayerActionList.end(); ++actionIt )
133  {
134  if (( *actionIt )->canRunUsingLayer( layer ) && ( targets & ( *actionIt )->targets() ) )
135  {
136  validActions.append(( *actionIt ) );
137  }
138  }
139  return validActions;
140 }
141 
142 
144 {
145  if ( mMapLayerActionList.indexOf( action ) != -1 )
146  {
147  mMapLayerActionList.removeAll( action );
148 
149  //also remove this action from the default layer action map
151  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
152  {
153  if ( defaultIt.value() == action )
154  {
155  defaultIt.value() = nullptr;
156  }
157  }
158  emit changed();
159  return true;
160  }
161  //not found
162  return false;
163 }
164 
166 {
167  mDefaultLayerActionMap[ layer ] = action;
168 }
169 
171 {
172  if ( !mDefaultLayerActionMap.contains( layer ) )
173  {
174  return nullptr;
175  }
176 
177  return mDefaultLayerActionMap[ layer ];
178 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
bool contains(const Key &key) const
QgsMapLayerActionRegistry(QObject *parent=nullptr)
protected constructor
bool canRunUsingLayer(QgsMapLayer *layer) const
True if action can run using the specified layer.
QIcon icon() const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
int indexOf(const T &value, int from) const
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.
const char * name() const
void append(const T &value)
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:57
int removeAll(const T &value)
void setDefaultActionForLayer(QgsMapLayer *layer, QgsMapLayerAction *action)
Sets the default action for a layer.
QList< QgsMapLayerAction *> mMapLayerActionList
void changed()
Triggered when an action is added or removed from the registry.
iterator end()
This class tracks map layer actions.
const Targets & targets() const
Return availibity of action.
iterator begin()
iterator end()
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.
static QgsMapLayerActionRegistry * instance()
Returns the instance pointer, creating the object on the first call.
bool removeMapLayerAction(QgsMapLayerAction *action)
Removes a map layer action from the registry.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, const QgsMapLayerAction::Targets &targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
QgsMapLayerAction(const QString &name, QObject *parent, const Targets &targets=AllActions, const QIcon &icon=QIcon())
Creates a map layer action which can run on any layer.
void triggeredForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggered when action has been run for a specific list of features.
QObject * parent() const
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.
iterator begin()
An action which can run on map layers.
const T value(const Key &key) const