QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 ) : QAction( name, parent ),
20  mSingleLayer( false ),
21  mActionLayer( 0 ),
22  mSpecificLayerType( false )
23 {
24 
25 }
26 
28 QgsMapLayerAction::QgsMapLayerAction( QString name, QObject* parent, QgsMapLayer* layer ) : QAction( name, parent ),
29  mSingleLayer( true ),
30  mActionLayer( layer ),
31  mSpecificLayerType( false )
32 {
33 
34 }
35 
37 QgsMapLayerAction::QgsMapLayerAction( QString name, QObject* parent, QgsMapLayer::LayerType layerType ) : QAction( name, parent ),
38  mSingleLayer( false ),
39  mActionLayer( 0 ),
40  mSpecificLayerType( true ),
41  mLayerType( layerType )
42 {
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 
76 {
77  emit triggeredForFeature( layer, feature );
78  //also trigger this action for the specified layer
79  triggerForLayer( layer );
80 }
81 
83 {
84  emit triggeredForLayer( layer );
85  //also emit triggered signal
86  emit triggered();
87 }
88 
89 //
90 // Static calls to enforce singleton behaviour
91 //
94 {
95  if ( mInstance == 0 )
96  {
98  }
99  return mInstance;
100 }
101 
102 //
103 // Main class begins now...
104 //
105 
106 QgsMapLayerActionRegistry::QgsMapLayerActionRegistry( QObject *parent ) : QObject( parent )
107 {
108  // constructor does nothing
109 }
110 
112 {
113 
114 }
115 
117 {
118  mMapLayerActionList.append( action );
119  emit changed();
120 }
121 
122 QList< QgsMapLayerAction* > QgsMapLayerActionRegistry::mapLayerActions( QgsMapLayer* layer )
123 {
124  QList< QgsMapLayerAction* > validActions;
125  QList<QgsMapLayerAction*>::iterator actionIt;
126  for ( actionIt = mMapLayerActionList.begin(); actionIt != mMapLayerActionList.end(); ++actionIt )
127  {
128  if (( *actionIt )->canRunUsingLayer( layer ) )
129  {
130  validActions.append(( *actionIt ) );
131  }
132  }
133  return validActions;
134 }
135 
136 
138 {
139  if ( mMapLayerActionList.indexOf( action ) != -1 )
140  {
141  mMapLayerActionList.removeAll( action );
142 
143  //also remove this action from the default layer action map
144  QMap<QgsMapLayer*, QgsMapLayerAction*>::iterator defaultIt;
145  for ( defaultIt = mDefaultLayerActionMap.begin(); defaultIt != mDefaultLayerActionMap.end(); ++defaultIt )
146  {
147  if ( defaultIt.value() == action )
148  {
149  defaultIt.value() = 0;
150  }
151  }
152  emit changed();
153  return true;
154  }
155  //not found
156  return false;
157 }
158 
160 {
161  mDefaultLayerActionMap[ layer ] = action;
162 }
163 
165 {
166  if ( !mDefaultLayerActionMap.contains( layer ) )
167  {
168  return 0;
169  }
170 
171  return mDefaultLayerActionMap[ layer ];
172 }
QgsMapLayerAction * defaultActionForLayer(QgsMapLayer *layer)
Returns the default action for a layer.
Base class for all map layer types.
Definition: qgsmaplayer.h:47
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:86
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
void triggerForFeature(QgsMapLayer *layer, QgsFeature *feature)
Triggers the action with the specified layer and feature.
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.
LayerType
Layers enum defining the types of layers that can be added to a map.
Definition: qgsmaplayer.h:53
QList< QgsMapLayerAction * > mapLayerActions(QgsMapLayer *layer)
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 triggeredForFeature(QgsMapLayer *layer, QgsFeature *feature)
Triggered when action has been run for a specific feature.
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
QgsMapLayerAction(QString name, QObject *parent)
Creates a map layer action which can run on any layer.
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.
static QgsMapLayerActionRegistry * mInstance
void triggerForLayer(QgsMapLayer *layer)
Triggers the action with the specified layer.
An action which can run on map layers.