Quantum GIS API Documentation  1.8
src/core/qgsattributeaction.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           qgsattributeaction.h
00003 
00004  These classes store and control the managment and execution of actions
00005  associated with particulay Qgis layers. Actions are defined to be
00006  external programs that are run with user-specified inputs that can
00007  depend on the contents of layer attributes.
00008 
00009                              -------------------
00010     begin                : Oct 24 2004
00011     copyright            : (C) 2004 by Gavin Macaulay
00012     email                : gavin at macaulay dot co dot nz
00013  ***************************************************************************/
00014 
00015 /***************************************************************************
00016  *                                                                         *
00017  *   This program is free software; you can redistribute it and/or modify  *
00018  *   it under the terms of the GNU General Public License as published by  *
00019  *   the Free Software Foundation; either version 2 of the License, or     *
00020  *   (at your option) any later version.                                   *
00021  *                                                                         *
00022  ***************************************************************************/
00023 
00024 #ifndef QGSATTRIBUTEACTION_H
00025 #define QGSATTRIBUTEACTION_H
00026 
00027 #include <QString>
00028 #include <QObject>
00029 
00030 #include <qgsfeature.h>
00031 
00032 class QDomNode;
00033 class QDomDocument;
00034 class QgsPythonUtils;
00035 class QgsVectorLayer;
00036 
00040 class CORE_EXPORT QgsAction
00041 {
00042   public:
00043     enum ActionType
00044     {
00045       Generic,
00046       GenericPython,
00047       Mac,
00048       Windows,
00049       Unix,
00050       OpenUrl,
00051     };
00052 
00053     QgsAction( ActionType type, QString name, QString action, bool capture ) :
00054         mType( type ), mName( name ), mAction( action ), mCaptureOutput( capture ) {}
00055 
00057     QString name() const { return mName; }
00058 
00060     QString action() const { return mAction; }
00061 
00063     ActionType type() const { return mType; }
00064 
00066     bool capture() const { return mCaptureOutput; }
00067 
00069     bool runable() const
00070     {
00071       return mType == Generic ||
00072              mType == GenericPython ||
00073              mType == OpenUrl ||
00074 #if defined(Q_OS_WIN)
00075              mType == Windows
00076 #elif defined(Q_OS_MAC)
00077              mType == Mac
00078 #else
00079              mType == Unix
00080 #endif
00081              ;
00082     }
00083 
00084   private:
00085     ActionType mType;
00086     QString mName;
00087     QString mAction;
00088     bool mCaptureOutput;
00089 };
00090 
00096 class  CORE_EXPORT QgsAttributeAction
00097 {
00098   public:
00100     QgsAttributeAction( QgsVectorLayer *layer ) : mLayer( layer ) {}
00101 
00103     virtual ~QgsAttributeAction() {}
00104 
00106     // Will happily have duplicate names and actions. If
00107     // capture is true, when running the action using doAction(),
00108     // any stdout from the process will be captured and displayed in a
00109     // dialog box.
00110     void addAction( QgsAction::ActionType type, QString name, QString action, bool capture = false );
00111 
00118     Q_DECL_DEPRECATED void doAction( int index,
00119                                      const QgsAttributeMap &attributes,
00120                                      int defaultValueIndex = 0,
00121                                      void ( *executePython )( const QString & ) = 0 );
00122 
00127     void doAction( int index,
00128                    QgsFeature &feat,
00129                    int defaultValueIndex = 0 );
00130 
00137     void doAction( int index,
00138                    QgsFeature &feat,
00139                    const QMap<QString, QVariant> *substitutionMap = 0 );
00140 
00142     void clearActions() { mActions.clear(); }
00143 
00145     QgsVectorLayer *layer() { return mLayer; }
00146 
00150     QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
00151 
00162     QString expandAction( QString action,
00163                           QgsFeature &feat,
00164                           const QMap<QString, QVariant> *substitutionMap = 0 );
00165 
00166 
00168     bool writeXML( QDomNode& layer_node, QDomDocument& doc ) const;
00169 
00171     bool readXML( const QDomNode& layer_node );
00172 
00173     int size() const { return mActions.size(); }
00174     QgsAction &at( int idx ) { return mActions[idx]; }
00175     QgsAction &operator[]( int idx ) { return mActions[idx]; }
00176 
00178     static void setPythonExecute( void ( * )( const QString & ) );
00179 
00181     int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? 0 : mDefaultAction; }
00182     void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; }
00183 
00184   private:
00185     QList<QgsAction> mActions;
00186     QgsVectorLayer *mLayer;
00187     static void ( *smPythonExecute )( const QString & );
00188 
00189     void runAction( const QgsAction &action,
00190                     void ( *executePython )( const QString & ) = 0 );
00191 
00192     int mDefaultAction;
00193 };
00194 
00195 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines