QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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, QgsMapLayer::LayerType 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() != QgsMapLayer::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  Q_FOREACH ( QgsMapLayerAction *action, mMapLayerActionList )
130  {
131  if ( action->canRunUsingLayer( layer ) && ( targets & action->targets() ) )
132  {
133  validActions.append( action );
134  }
135  }
136  return validActions;
137 }
138 
139 
141 {
142  if ( mMapLayerActionList.indexOf( action ) != -1 )
143  {
144  mMapLayerActionList.removeAll( action );
145 
146  //also remove this action from the default layer action map
147  QMap<QgsMapLayer *, QgsMapLayerAction *>::iterator defaultIt;
148  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
149  {
150  if ( defaultIt.value() == action )
151  {
152  defaultIt.value() = nullptr;
153  }
154  }
155  emit changed();
156  return true;
157  }
158  //not found
159  return false;
160 }
161 
163 {
164  mDefaultLayerActionMap[ layer ] = action;
165 }
166 
168 {
169  if ( !mDefaultLayerActionMap.contains( layer ) )
170  {
171  return nullptr;
172  }
173 
174  return mDefaultLayerActionMap[ layer ];
175 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
QgsMapLayerAction::Flags flags() const
Layer behavior flags.
Base class for all map layer types.
Definition: qgsmaplayer.h:63
QList< QgsMapLayerAction * > mMapLayerActionList
QgsMapLayer::LayerType type() const
Returns the type of the layer.
QgsMapLayerActionRegistry(QObject *parent=nullptr)
Constructor for QgsMapLayerActionRegistry.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
void triggeredForFeature(QgsMapLayer *layer, const QgsFeature &feature)
Triggered when action has been run for a specific feature.
bool canRunUsingLayer(QgsMapLayer *layer) const
True if action can run using the specified layer.
void triggeredForLayer(QgsMapLayer *layer)
Triggered when action has been run for a specific layer.
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer, QgsMapLayerAction::Targets targets=QgsMapLayerAction::AllActions)
Returns the map layer actions which can run on the specified layer.
LayerType
Types of layers that can be added to a map.
Definition: qgsmaplayer.h:105
void setDefaultActionForLayer(QgsMapLayer *layer, QgsMapLayerAction *action)
Sets the default action for a layer.
Action should be shown only for editable layers.
QgsMapLayerAction(const QString &name, QObject *parent, Targets targets=AllActions, const QIcon &icon=QIcon(), QgsMapLayerAction::Flags flags=nullptr)
Action behavior flags.
void changed()
Triggered when an action is added or removed from the registry.
const Targets & targets() const
Returns 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 isEnabledOnlyWhenEditable() const
Returns true if the action is only enabled for layers in editable mode.
bool removeMapLayerAction(QgsMapLayerAction *action)
Removes a map layer action from the registry.
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:78
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.