QGIS API Documentation  2.11.0-Master
qgsattributeaction.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsattributeaction.h
3 
4  These classes store and control the managment and execution of actions
5  associated with particulay Qgis layers. Actions are defined to be
6  external programs that are run with user-specified inputs that can
7  depend on the contents of layer attributes.
8 
9  -------------------
10  begin : Oct 24 2004
11  copyright : (C) 2004 by Gavin Macaulay
12  email : gavin at macaulay dot co dot nz
13  ***************************************************************************/
14 
15 /***************************************************************************
16  * *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or *
20  * (at your option) any later version. *
21  * *
22  ***************************************************************************/
23 
24 #ifndef QGSATTRIBUTEACTION_H
25 #define QGSATTRIBUTEACTION_H
26 
27 #include <QString>
28 #include <QIcon>
29 
30 #include "qgsfeature.h"
31 #include "qgsexpressioncontext.h"
32 
33 class QDomNode;
34 class QDomDocument;
35 class QgsPythonUtils;
36 class QgsVectorLayer;
37 
41 class CORE_EXPORT QgsAction
42 {
43  public:
45  {
48  Mac,
52  };
53 
54  QgsAction( ActionType type, QString name, QString action, bool capture ) :
55  mType( type ), mName( name ), mAction( action ), mCaptureOutput( capture ) {}
56 
57  QgsAction( ActionType type, QString name, QString action, const QString& icon, bool capture ) :
58  mType( type ), mName( name ), mIcon( icon ), mAction( action ), mCaptureOutput( capture ) {}
59 
61  QString name() const { return mName; }
62 
64  const QString iconPath() const { return mIcon; }
65 
67  const QIcon icon() const { return QIcon( mIcon ); }
68 
70  QString action() const { return mAction; }
71 
73  ActionType type() const { return mType; }
74 
76  bool capture() const { return mCaptureOutput; }
77 
79  bool runable() const
80  {
81  return mType == Generic ||
82  mType == GenericPython ||
83  mType == OpenUrl ||
84 #if defined(Q_OS_WIN)
85  mType == Windows
86 #elif defined(Q_OS_MAC)
87  mType == Mac
88 #else
89  mType == Unix
90 #endif
91  ;
92  }
93 
94  private:
95  ActionType mType;
96  QString mName;
97  QString mIcon;
98  QString mAction;
99  bool mCaptureOutput;
100 };
101 
107 class CORE_EXPORT QgsAttributeAction
108 {
109  public:
111  QgsAttributeAction( QgsVectorLayer *layer ) : mLayer( layer ), mDefaultAction( -1 ) {}
112 
114  virtual ~QgsAttributeAction() {}
115 
122  void addAction( QgsAction::ActionType type, QString name, QString action, bool capture = false );
123 
130  void addAction( QgsAction::ActionType type, QString name, QString action, const QString& icon, bool capture = false );
131 
133  void removeAction( int index );
134 
139  void doAction( int index,
140  const QgsFeature &feat,
141  int defaultValueIndex = 0 );
142 
149  void doAction( int index,
150  const QgsFeature &feat,
151  const QMap<QString, QVariant> *substitutionMap );
152 
154  void clearActions() { mActions.clear(); }
155 
157  const QList<QgsAction>& listActions() { return mActions; }
158 
160  QgsVectorLayer *layer() { return mLayer; }
161 
165  QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
166 
175  QString expandAction( QString action,
176  QgsFeature &feat,
177  const QMap<QString, QVariant> *substitutionMap = 0 );
178 
179 
181  bool writeXML( QDomNode& layer_node, QDomDocument& doc ) const;
182 
184  bool readXML( const QDomNode& layer_node );
185 
186  int size() const { return mActions.size(); }
187  QgsAction &at( int idx ) { return mActions[idx]; }
188  QgsAction &operator[]( int idx ) { return mActions[idx]; }
189 
191  static void setPythonExecute( void ( * )( const QString & ) );
192 
194  int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? -1 : mDefaultAction; }
195  void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; }
196 
197  private:
198  QList<QgsAction> mActions;
199  QgsVectorLayer *mLayer;
200  static void ( *smPythonExecute )( const QString & );
201 
202  void runAction( const QgsAction &action,
203  void ( *executePython )( const QString & ) = 0 );
204 
205  int mDefaultAction;
206 
207  QgsExpressionContext createExpressionContext() const;
208 };
209 
210 #endif
static unsigned index
const QString iconPath() const
The path to the icon.
QgsAction(ActionType type, QString name, QString action, const QString &icon, bool capture)
QgsAction & operator[](int idx)
Storage and management of actions associated with Qgis layer attributes.
QgsAction(ActionType type, QString name, QString action, bool capture)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:162
bool capture() const
Whether to capture output for display when this action is run.
QString name() const
The name of the action.
const QIcon icon() const
The icon.
Utility class that encapsulates an action based on vector attributes.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsAction & at(int idx)
QString action() const
The action.
ActionType type() const
The action type.
virtual ~QgsAttributeAction()
Destructor.
const QList< QgsAction > & listActions()
List all actions.
QgsVectorLayer * layer()
Return the layer.
bool runable() const
Whether the action is runable on the current platform.
QgsAttributeAction(QgsVectorLayer *layer)
Constructor.
Represents a vector layer which manages a vector based data sets.
void setDefaultAction(int actionNumber)
void clearActions()
Removes all actions.
int defaultAction() const
Whether the action is the default action.