QGIS API Documentation  3.0.2-Girona (307d082)
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  , mSingleLayer( false )
23  , mSpecificLayerType( false )
24  , mLayerType( QgsMapLayer::VectorLayer )
25  , mTargets( targets )
26  , mFlags( flags )
27 {
28 }
29 
30 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer *layer, Targets targets, const QIcon &icon, QgsMapLayerAction::Flags flags )
31  : QAction( icon, name, parent )
32  , mSingleLayer( true )
33  , mActionLayer( layer )
34  , mSpecificLayerType( false )
35  , mLayerType( QgsMapLayer::VectorLayer )
36  , mTargets( targets )
37  , mFlags( flags )
38 {
39 }
40 
41 QgsMapLayerAction::QgsMapLayerAction( const QString &name, QObject *parent, QgsMapLayer::LayerType layerType, Targets targets, const QIcon &icon, QgsMapLayerAction::Flags flags )
42  : QAction( icon, name, parent )
43  , mSingleLayer( false )
44  , mSpecificLayerType( true )
45  , mLayerType( layerType )
46  , mTargets( targets )
47  , mFlags( flags )
48 {
49 }
50 
52 {
53  //remove action from registry
55 }
56 
57 QgsMapLayerAction::Flags QgsMapLayerAction::flags() const
58 {
59  return mFlags;
60 }
61 
63 {
64  if ( mFlags & EnabledOnlyWhenEditable )
65  {
66  // action is only enabled for editable layers
67  if ( !layer )
68  return false;
69  if ( layer->type() != QgsMapLayer::VectorLayer )
70  return false;
71  if ( !qobject_cast<QgsVectorLayer *>( layer )->isEditable() )
72  return false;
73  }
74 
75  //check layer details
76  if ( !mSingleLayer && !mSpecificLayerType )
77  {
78  //action is not a single layer of specific layer type action,
79  //so return true
80  return true;
81  }
82  if ( mSingleLayer && layer == mActionLayer )
83  {
84  //action is a single layer type and layer matches
85  return true;
86  }
87  else if ( mSpecificLayerType && layer && layer->type() == mLayerType )
88  {
89  //action is for a layer type and layer type matches
90  return true;
91  }
92 
93  return false;
94 }
95 
96 void QgsMapLayerAction::triggerForFeatures( QgsMapLayer *layer, const QList<QgsFeature> &featureList )
97 {
98  emit triggeredForFeatures( layer, featureList );
99 }
100 
102 {
103  emit triggeredForFeature( layer, *feature );
104 }
105 
107 {
108  emit triggeredForLayer( layer );
109 }
110 
112 {
113  return mFlags & EnabledOnlyWhenEditable;
114 }
115 
116 //
117 // Main class begins now...
118 //
119 
120 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
121 {
122  // constructor does nothing
123 }
124 
126 {
127  mMapLayerActionList.append( action );
128  emit changed();
129 }
130 
131 QList< QgsMapLayerAction * > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer *layer, QgsMapLayerAction::Targets targets )
132 {
133  QList< QgsMapLayerAction * > validActions;
134 
135  Q_FOREACH ( QgsMapLayerAction *action, mMapLayerActionList )
136  {
137  if ( action->canRunUsingLayer( layer ) && ( targets & action->targets() ) )
138  {
139  validActions.append( action );
140  }
141  }
142  return validActions;
143 }
144 
145 
147 {
148  if ( mMapLayerActionList.indexOf( action ) != -1 )
149  {
150  mMapLayerActionList.removeAll( action );
151 
152  //also remove this action from the default layer action map
153  QMap<QgsMapLayer *, QgsMapLayerAction *>::iterator defaultIt;
154  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
155  {
156  if ( defaultIt.value() == action )
157  {
158  defaultIt.value() = nullptr;
159  }
160  }
161  emit changed();
162  return true;
163  }
164  //not found
165  return false;
166 }
167 
169 {
170  mDefaultLayerActionMap[ layer ] = action;
171 }
172 
174 {
175  if ( !mDefaultLayerActionMap.contains( layer ) )
176  {
177  return nullptr;
178  }
179 
180  return mDefaultLayerActionMap[ layer ];
181 }
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.
bool isEnabledOnlyWhenEditable() const
Returns true if the action is only enabled for layers in editable mode.
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.
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.
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
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.
QgsMapLayerAction::Flags flags() const
Layer behavior flags.
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:66
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.