|
Quantum GIS API Documentation
master-693a1fe
|
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 00113 void removeAction( int index ); 00114 00120 void doAction( int index, 00121 QgsFeature &feat, 00122 int defaultValueIndex = 0 ); 00123 00131 void doAction( int index, 00132 QgsFeature &feat, 00133 const QMap<QString, QVariant> *substitutionMap = 0 ); 00134 00136 void clearActions() { mActions.clear(); } 00137 00139 QgsVectorLayer *layer() { return mLayer; } 00140 00144 QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex ); 00145 00156 QString expandAction( QString action, 00157 QgsFeature &feat, 00158 const QMap<QString, QVariant> *substitutionMap = 0 ); 00159 00160 00162 bool writeXML( QDomNode& layer_node, QDomDocument& doc ) const; 00163 00165 bool readXML( const QDomNode& layer_node ); 00166 00167 int size() const { return mActions.size(); } 00168 QgsAction &at( int idx ) { return mActions[idx]; } 00169 QgsAction &operator[]( int idx ) { return mActions[idx]; } 00170 00172 static void setPythonExecute( void ( * )( const QString & ) ); 00173 00175 int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? 0 : mDefaultAction; } 00176 void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; } 00177 00178 private: 00179 QList<QgsAction> mActions; 00180 QgsVectorLayer *mLayer; 00181 static void ( *smPythonExecute )( const QString & ); 00182 00183 void runAction( const QgsAction &action, 00184 void ( *executePython )( const QString & ) = 0 ); 00185 00186 int mDefaultAction; 00187 }; 00188 00189 #endif