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