QGIS API Documentation  2.99.0-Master (0a63d1f)
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, 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 
30 QgsMapLayerAction::QgsMapLayerAction( const QString& name, QObject* parent, QgsMapLayer* layer, Targets targets, const QIcon& icon )
31  : QAction( icon, name, parent )
32  , mSingleLayer( true )
33  , mActionLayer( layer )
34  , mSpecificLayerType( false )
35  , mLayerType( QgsMapLayer::VectorLayer )
36  , mTargets( targets )
37 {
38 }
39 
41 QgsMapLayerAction::QgsMapLayerAction( const QString& name, QObject* parent, QgsMapLayer::LayerType layerType, Targets targets, const QIcon& icon )
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 
80 void QgsMapLayerAction::triggerForFeatures( QgsMapLayer* layer, const QList<QgsFeature>& featureList )
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 behavior
97 //
98 QgsMapLayerActionRegistry *QgsMapLayerActionRegistry::sInstance = nullptr;
100 {
101  if ( !sInstance )
102  {
103  sInstance = new QgsMapLayerActionRegistry();
104  }
105  return sInstance;
106 }
107 
108 //
109 // Main class begins now...
110 //
111 
112 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
113 {
114  // constructor does nothing
115 }
116 
118 {
119  mMapLayerActionList.append( action );
120  emit changed();
121 }
122 
123 QList< QgsMapLayerAction* > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer* layer, QgsMapLayerAction::Targets targets )
124 {
125  QList< QgsMapLayerAction* > validActions;
126  QList<QgsMapLayerAction*>::iterator actionIt;
127  for ( actionIt = mMapLayerActionList.begin(); actionIt != mMapLayerActionList.end(); ++actionIt )
128  {
129  if (( *actionIt )->canRunUsingLayer( layer ) && ( targets & ( *actionIt )->targets() ) )
130  {
131  validActions.append(( *actionIt ) );
132  }
133  }
134  return validActions;
135 }
136 
137 
139 {
140  if ( mMapLayerActionList.indexOf( action ) != -1 )
141  {
142  mMapLayerActionList.removeAll( action );
143 
144  //also remove this action from the default layer action map
145  QMap<QgsMapLayer*, QgsMapLayerAction*>::iterator defaultIt;
146  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
147  {
148  if ( defaultIt.value() == action )
149  {
150  defaultIt.value() = nullptr;
151  }
152  }
153  emit changed();
154  return true;
155  }
156  //not found
157  return false;
158 }
159 
161 {
162  mDefaultLayerActionMap[ layer ] = action;
163 }
164 
166 {
167  if ( !mDefaultLayerActionMap.contains( layer ) )
168  {
169  return nullptr;
170  }
171 
172  return mDefaultLayerActionMap[ layer ];
173 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:50
QgsMapLayerActionRegistry(QObject *parent=nullptr)
protected constructor
bool canRunUsingLayer(QgsMapLayer *layer) const
True if action can run using the specified layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
QgsMapLayer::LayerType type() const
Returns the type of the layer.
Definition: qgsmaplayer.cpp:96
QgsMapLayerAction(const QString &name, QObject *parent, Targets targets=AllActions, const QIcon &icon=QIcon())
Creates a map layer action which can run on any layer.
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.
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:59
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.
This class tracks map layer actions.
const Targets & targets() const
Return 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.
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, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
void triggeredForFeatures(QgsMapLayer *layer, const QList< QgsFeature > &featureList)
Triggered when action has been run for a specific list of features.
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.