QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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( QString name, QObject* parent, Targets targets, QIcon icon )
20  : QAction( icon, name, parent )
21  , mSingleLayer( false )
22  , mActionLayer( 0 )
23  , mSpecificLayerType( false )
24  , mTargets( targets )
25 {
26 }
27 
29 QgsMapLayerAction::QgsMapLayerAction( QString name, QObject* parent, QgsMapLayer* layer , Targets targets, QIcon icon )
30  : QAction( icon, name, parent )
31  , mSingleLayer( true )
32  , mActionLayer( layer )
33  , mSpecificLayerType( false )
34  , mTargets( targets )
35 {
36 }
37 
39 QgsMapLayerAction::QgsMapLayerAction( QString name, QObject* parent, QgsMapLayer::LayerType layerType, Targets targets, QIcon icon )
40  : QAction( icon, name, parent )
41  , mSingleLayer( false )
42  , mActionLayer( 0 )
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 // Static calls to enforce singleton behaviour
95 //
96 QgsMapLayerActionRegistry *QgsMapLayerActionRegistry::mInstance = 0;
98 {
99  if ( mInstance == 0 )
100  {
101  mInstance = new QgsMapLayerActionRegistry();
102  }
103  return mInstance;
104 }
105 
106 //
107 // Main class begins now...
108 //
109 
110 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
111 {
112  // constructor does nothing
113 }
114 
116 {
117 
118 }
119 
121 {
122  mMapLayerActionList.append( action );
123  emit changed();
124 }
125 
126 QList< QgsMapLayerAction* > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer* layer, QgsMapLayerAction::Targets targets )
127 {
128  QList< QgsMapLayerAction* > validActions;
129  QList<QgsMapLayerAction*>::iterator actionIt;
130  for ( actionIt = mMapLayerActionList.begin(); actionIt != mMapLayerActionList.end(); ++actionIt )
131  {
132  if (( *actionIt )->canRunUsingLayer( layer ) && ( targets & ( *actionIt )->targets() ) )
133  {
134  validActions.append(( *actionIt ) );
135  }
136  }
137  return validActions;
138 }
139 
140 
142 {
143  if ( mMapLayerActionList.indexOf( action ) != -1 )
144  {
145  mMapLayerActionList.removeAll( action );
146 
147  //also remove this action from the default layer action map
148  QMap<QgsMapLayer*, QgsMapLayerAction*>::iterator defaultIt;
149  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
150  {
151  if ( defaultIt.value() == action )
152  {
153  defaultIt.value() = 0;
154  }
155  }
156  emit changed();
157  return true;
158  }
159  //not found
160  return false;
161 }
162 
164 {
165  mDefaultLayerActionMap[ layer ] = action;
166 }
167 
169 {
170  if ( !mDefaultLayerActionMap.contains( layer ) )
171  {
172  return 0;
173  }
174 
175  return mDefaultLayerActionMap[ layer ];
176 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:48
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:89
QgsMapLayerAction(QString name, QObject *parent, Targets targets=AllActions, QIcon icon=QIcon())
Creates a map layer action which can run on any layer.
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:54
void triggeredForFeatures(QgsMapLayer *layer, const QList< QgsFeature > featureList)
Triggered when action has been run for a specific list of features.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
static QIcon icon(QString icon)
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.
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.
QgsMapLayerActionRegistry(QObject *parent=0)
protected constructor
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.
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.
void triggerForFeatures(QgsMapLayer *layer, const QList< QgsFeature > featureList)
Triggers the action with the specified layer and list of feature.
An action which can run on map layers.