QGIS API Documentation  2.99.0-Master (19b062c)
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 
19 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, Targets targets, const QIcon &icon )
20  : QAction( icon, name, parent )
21  , mSingleLayer( false )
22  , mSpecificLayerType( false )
23  , mLayerType( QgsMapLayer::VectorLayer )
24  , mTargets( targets )
25 {
26 }
27 
28 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer *layer, Targets targets, const QIcon &icon )
29  : QAction( icon, name, parent )
30  , mSingleLayer( true )
31  , mActionLayer( layer )
32  , mSpecificLayerType( false )
33  , mLayerType( QgsMapLayer::VectorLayer )
34  , mTargets( targets )
35 {
36 }
37 
38 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer::LayerType layerType, Targets targets, const QIcon &icon )
39  : QAction( icon, name, parent )
40  , mSingleLayer( false )
41  , mSpecificLayerType( true )
42  , mLayerType( layerType )
43  , mTargets( targets )
44 {
45 }
46 
48 {
49  //remove action from registry
51 }
52 
54 {
55  //check layer details
56  if ( !mSingleLayer && !mSpecificLayerType )
57  {
58  //action is not a single layer of specific layer type action,
59  //so return true
60  return true;
61  }
62  if ( mSingleLayer && layer == mActionLayer )
63  {
64  //action is a single layer type and layer matches
65  return true;
66  }
67  else if ( mSpecificLayerType && layer->type() == mLayerType )
68  {
69  //action is for a layer type and layer type matches
70  return true;
71  }
72 
73  return false;
74 }
75 
76 void QgsMapLayerAction::triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList )
77 {
78  emit triggeredForFeatures( layer, featureList );
79 }
80 
82 {
83  emit triggeredForFeature( layer, *feature );
84 }
85 
87 {
88  emit triggeredForLayer( layer );
89 }
90 
91 //
92 // Main class begins now...
93 //
94 
95 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
96 {
97  // constructor does nothing
98 }
99 
101 {
102  mMapLayerActionList.append( action );
103  emit changed();
104 }
105 
106 QList< QgsMapLayerAction * > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer *layer, QgsMapLayerAction::Targets targets )
107 {
108  QList< QgsMapLayerAction * > validActions;
109 
110  Q_FOREACH ( QgsMapLayerAction *action, mMapLayerActionList )
111  {
112  if ( action->canRunUsingLayer( layer ) && ( targets & action->targets() ) )
113  {
114  validActions.append( action );
115  }
116  }
117  return validActions;
118 }
119 
120 
122 {
123  if ( mMapLayerActionList.indexOf( action ) != -1 )
124  {
125  mMapLayerActionList.removeAll( action );
126 
127  //also remove this action from the default layer action map
128  QMap<QgsMapLayer *, QgsMapLayerAction *>::iterator defaultIt;
129  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
130  {
131  if ( defaultIt.value() == action )
132  {
133  defaultIt.value() = nullptr;
134  }
135  }
136  emit changed();
137  return true;
138  }
139  //not found
140  return false;
141 }
142 
144 {
145  mDefaultLayerActionMap[ layer ] = action;
146 }
147 
149 {
150  if ( !mDefaultLayerActionMap.contains( layer ) )
151  {
152  return nullptr;
153  }
154 
155  return mDefaultLayerActionMap[ layer ];
156 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:56
QList< QgsMapLayerAction *> mMapLayerActionList
QgsMapLayerActionRegistry(QObject *parent=nullptr)
Constructor for QgsMapLayerActionRegistry.
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:62
QgsMapLayer::LayerType type() const
Returns the type of the layer.
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:94
void setDefaultActionForLayer(QgsMapLayer *layer, QgsMapLayerAction *action)
Sets the default action for a layer.
void changed()
Triggered when an action is added or removed from the registry.
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.
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.
static QgsMapLayerActionRegistry * mapLayerActionRegistry()
Returns the global map layer action registry, used for registering map layer actions.
Definition: qgsgui.cpp:64
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.