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