QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
32 class QDomNode;
33 class QDomDocument;
34 class QgsPythonUtils;
35 class QgsVectorLayer;
36 
40 class CORE_EXPORT QgsAction
41 {
42  public:
44  {
47  Mac,
51  };
52 
53  QgsAction( ActionType type, QString name, QString action, bool capture ) :
54  mType( type ), mName( name ), mAction( action ), mCaptureOutput( capture ) {}
55 
56  QgsAction( ActionType type, QString name, QString action, const QString& icon, bool capture ) :
57  mType( type ), mName( name ), mIcon( icon ), mAction( action ), mCaptureOutput( capture ) {}
58 
60  QString name() const { return mName; }
61 
63  const QString iconPath() const { return mIcon; }
64 
66  const QIcon icon() const { return QIcon( mIcon ); }
67 
69  QString action() const { return mAction; }
70 
72  ActionType type() const { return mType; }
73 
75  bool capture() const { return mCaptureOutput; }
76 
78  bool runable() const
79  {
80  return mType == Generic ||
81  mType == GenericPython ||
82  mType == OpenUrl ||
83 #if defined(Q_OS_WIN)
84  mType == Windows
85 #elif defined(Q_OS_MAC)
86  mType == Mac
87 #else
88  mType == Unix
89 #endif
90  ;
91  }
92 
93  private:
94  ActionType mType;
95  QString mName;
96  QString mIcon;
97  QString mAction;
98  bool mCaptureOutput;
99 };
100 
106 class CORE_EXPORT QgsAttributeAction
107 {
108  public:
110  QgsAttributeAction( QgsVectorLayer *layer ) : mLayer( layer ), mDefaultAction( -1 ) {}
111 
113  virtual ~QgsAttributeAction() {}
114 
121  void addAction( QgsAction::ActionType type, QString name, QString action, bool capture = false );
122 
129  void addAction( QgsAction::ActionType type, QString name, QString action, const QString& icon, bool capture = false );
130 
132  void removeAction( int index );
133 
138  void doAction( int index,
139  const QgsFeature &feat,
140  int defaultValueIndex = 0 );
141 
148  void doAction( int index,
149  const QgsFeature& feat,
150  const QMap<QString, QVariant> *substitutionMap );
151 
153  void clearActions() { mActions.clear(); }
154 
156  const QList<QgsAction>& listActions() { return mActions; }
157 
159  QgsVectorLayer *layer() { return mLayer; }
160 
164  QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
165 
174  QString expandAction( QString action,
175  QgsFeature &feat,
176  const QMap<QString, QVariant> *substitutionMap = 0 );
177 
178 
180  bool writeXML( QDomNode& layer_node, QDomDocument& doc ) const;
181 
183  bool readXML( const QDomNode& layer_node );
184 
185  int size() const { return mActions.size(); }
186  QgsAction &at( int idx ) { return mActions[idx]; }
187  QgsAction &operator[]( int idx ) { return mActions[idx]; }
188 
190  static void setPythonExecute( void ( * )( const QString & ) );
191 
193  int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? -1 : mDefaultAction; }
194  void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; }
195 
196  private:
197  QList<QgsAction> mActions;
198  QgsVectorLayer *mLayer;
199  static void ( *smPythonExecute )( const QString & );
200 
201  void runAction( const QgsAction &action,
202  void ( *executePython )( const QString & ) = 0 );
203 
204  int mDefaultAction;
205 };
206 
207 #endif