QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsmaplayeractionregistry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaplayeractionregistry.h
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 
16 #ifndef QGSMAPLAYERACTIONREGISTRY_H
17 #define QGSMAPLAYERACTIONREGISTRY_H
18 
19 #include <QObject>
20 #include <QList>
21 #include <QMap>
22 #include <QAction>
23 
24 #include "qgsmaplayer.h"
25 
26 class QgsFeature;
27 
31 class GUI_EXPORT QgsMapLayerAction : public QAction
32 {
33  Q_OBJECT
34  Q_FLAGS( Availability )
35 
36  public:
37  enum Target
38  {
39  Layer = 1,
40  SingleFeature = 2,
41  MultipleFeatures = 4,
42  AllActions = Layer | SingleFeature | MultipleFeatures
43  };
44  Q_DECLARE_FLAGS( Targets, Target )
45 
46 
47 
48  QgsMapLayerAction( QString name, QObject *parent, Targets targets = AllActions, QIcon icon = QIcon() );
49 
51  QgsMapLayerAction( QString name, QObject *parent, QgsMapLayer* layer, Targets targets = AllActions, QIcon icon = QIcon() );
52 
54  QgsMapLayerAction( QString name, QObject *parent, QgsMapLayer::LayerType layerType, Targets targets = AllActions, QIcon icon = QIcon() );
55 
57 
59  bool canRunUsingLayer( QgsMapLayer* layer ) const;
60 
62  void triggerForFeatures( QgsMapLayer* layer, const QList<QgsFeature> featureList );
63 
65  void triggerForFeature( QgsMapLayer* layer, const QgsFeature* feature );
66 
68  void triggerForLayer( QgsMapLayer* layer );
69 
71  void setTargets( Targets targets ) {mTargets = targets;}
73  const Targets& targets() const {return mTargets;}
74 
75  signals:
77  void triggeredForFeatures( QgsMapLayer* layer, const QList<QgsFeature> featureList );
78 
80  void triggeredForFeature( QgsMapLayer* layer, const QgsFeature& feature );
81 
83  void triggeredForLayer( QgsMapLayer* layer );
84 
85  private:
86 
87  // true if action is only valid for a single layer
88  bool mSingleLayer;
89  // layer if action is only valid for a single layer
90  QgsMapLayer* mActionLayer;
91 
92  // true if action is only valid for a specific layer type
93  bool mSpecificLayerType;
94  // layer type if action is only valid for a specific layer type
95  QgsMapLayer::LayerType mLayerType;
96 
97  // determine if the action can be run on layer and/or single feature and/or multiple features
98  Targets mTargets;
99 };
100 
101 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerAction::Targets )
102 
103 
106 class GUI_EXPORT QgsMapLayerActionRegistry : public QObject
107 {
108  Q_OBJECT
109 
110  public:
112  static QgsMapLayerActionRegistry * instance();
113 
115 
117  void addMapLayerAction( QgsMapLayerAction * action );
118 
120  QList<QgsMapLayerAction *> mapLayerActions( QgsMapLayer* layer, QgsMapLayerAction::Targets targets = QgsMapLayerAction::AllActions );
121 
123  bool removeMapLayerAction( QgsMapLayerAction *action );
124 
126  void setDefaultActionForLayer( QgsMapLayer* layer, QgsMapLayerAction* action );
128  QgsMapLayerAction * defaultActionForLayer( QgsMapLayer* layer );
129 
130  protected:
132  QgsMapLayerActionRegistry( QObject * parent = 0 );
133 
134  QList< QgsMapLayerAction* > mMapLayerActionList;
135 
136  signals:
138  void changed();
139 
140  private:
141 
142  static QgsMapLayerActionRegistry *mInstance;
143 
144  QMap< QgsMapLayer*, QgsMapLayerAction* > mDefaultLayerActionMap;
145 
146 };
147 
148 #endif // QGSMAPLAYERACTIONREGISTRY_H