QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 ) : QAction( name, parent ),
20  mSingleLayer( false ),
21  mActionLayer( 0 ),
22  mSpecificLayerType( false ),
23  mTargets( targets )
24 {
25 }
26 
28 QgsMapLayerAction::QgsMapLayerAction( QString name, QObject* parent, QgsMapLayer* layer , Targets targets ) : QAction( name, parent ),
29  mSingleLayer( true ),
30  mActionLayer( layer ),
31  mSpecificLayerType( false ),
32  mTargets( targets )
33 {
34 }
35 
37 QgsMapLayerAction::QgsMapLayerAction( QString name, QObject* parent, QgsMapLayer::LayerType layerType, Targets targets ) : QAction( name, parent ),
38  mSingleLayer( false ),
39  mActionLayer( 0 ),
40  mSpecificLayerType( true ),
41  mLayerType( layerType ),
42  mTargets( targets )
43 {
44 }
45 
47 {
48  //remove action from registry
50 }
51 
53 {
54  //check layer details
56  {
57  //action is not a single layer of specific layer type action,
58  //so return true
59  return true;
60  }
61  if ( mSingleLayer && layer == mActionLayer )
62  {
63  //action is a single layer type and layer matches
64  return true;
65  }
66  else if ( mSpecificLayerType && layer->type() == mLayerType )
67  {
68  //action is for a layer type and layer type matches
69  return true;
70  }
71 
72  return false;
73 }
74 
75 void QgsMapLayerAction::triggerForFeatures( QgsMapLayer* layer, QList<const QgsFeature*> featureList )
76 {
77  emit triggeredForFeatures( layer, featureList );
78 }
79 
81 {
82  emit triggeredForFeature( layer, feature );
83 }
84 
86 {
87  emit triggeredForLayer( layer );
88 }
89 
90 //
91 // Static calls to enforce singleton behaviour
92 //
95 {
96  if ( mInstance == 0 )
97  {
99  }
100  return mInstance;
101 }
102 
103 //
104 // Main class begins now...
105 //
106 
107 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
108 {
109  // constructor does nothing
110 }
111 
113 {
114 
115 }
116 
118 {
119  mMapLayerActionList.append( action );
120  emit changed();
121 }
122 
123 QList< QgsMapLayerAction* > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer* layer, QgsMapLayerAction::Targets targets )
124 {
125  QList< QgsMapLayerAction* > validActions;
126  QList<QgsMapLayerAction*>::iterator actionIt;
127  for ( actionIt = mMapLayerActionList.begin(); actionIt != mMapLayerActionList.end(); ++actionIt )
128  {
129  if (( *actionIt )->canRunUsingLayer( layer ) && ( targets & ( *actionIt )->targets() ) )
130  {
131  validActions.append(( *actionIt ) );
132  }
133  }
134  return validActions;
135 }
136 
137 
139 {
140  if ( mMapLayerActionList.indexOf( action ) != -1 )
141  {
142  mMapLayerActionList.removeAll( action );
143 
144  //also remove this action from the default layer action map
145  QMap<QgsMapLayer*, QgsMapLayerAction*>::iterator defaultIt;
146  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
147  {
148  if ( defaultIt.value() == action )
149  {
150  defaultIt.value() = 0;
151  }
152  }
153  emit changed();
154  return true;
155  }
156  //not found
157  return false;
158 }
159 
161 {
162  mDefaultLayerActionMap[ layer ] = action;
163 }
164 
166 {
167  if ( !mDefaultLayerActionMap.contains( layer ) )
168  {
169  return 0;
170  }
171 
172  return mDefaultLayerActionMap[ layer ];
173 }
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
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:54
void triggeredForFeatures(QgsMapLayer *layer, QList< const QgsFeature * > featureList)
Triggered when action has been run for a specific list of features.
void triggerForFeatures(QgsMapLayer *layer, QList< const QgsFeature * > featureList)
Triggers the action with the specified layer and list of feature.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
QMap< QgsMapLayer *, QgsMapLayerAction * > mDefaultLayerActionMap
QgsMapLayer::LayerType mLayerType
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.
QgsMapLayerAction(QString name, QObject *parent, Targets targets=AllActions)
Creates a map layer action which can run on any 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.
void triggeredForFeature(QgsMapLayer *layer, const QgsFeature *feature)
Triggered when action has been run for a specific feature.
bool removeMapLayerAction(QgsMapLayerAction *action)
Removes a map layer action from the registry.
static QgsMapLayerActionRegistry * mInstance
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.