QGIS API Documentation  2.13.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, const QString& name, const QString& action, bool capture ) :
55  mType( type ), mName( name ), mAction( action ), mCaptureOutput( capture ) {}
56 
57  QgsAction( ActionType type, const QString& name, const 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, const QString& name, const QString& action, bool capture = false );
123 
130  void addAction( QgsAction::ActionType type, const QString& name, const 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( const QString& action,
176  QgsFeature &feat,
177  const QMap<QString, QVariant> *substitutionMap = nullptr );
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 
193  Q_DECL_DEPRECATED static void setPythonExecute( void ( * )( const QString & ) );
194 
196  int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? -1 : mDefaultAction; }
197  void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; }
198 
199  private:
200  QList<QgsAction> mActions;
201  QgsVectorLayer *mLayer;
202  static void ( *smPythonExecute )( const QString & );
203 
204  void runAction( const QgsAction &action,
205  void ( *executePython )( const QString & ) = nullptr );
206 
207  int mDefaultAction;
208 
209  QgsExpressionContext createExpressionContext() const;
210 };
211 
212 #endif
static unsigned index
const QString iconPath() const
The path to the icon.
QgsAction & operator[](int idx)
Storage and management of actions associated with Qgis layer attributes.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
QgsAction(ActionType type, const QString &name, const QString &action, bool capture)
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.
QgsAction(ActionType type, const QString &name, const QString &action, const QString &icon, bool capture)
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.