00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <QList>
00027
00028 #include <QStringList>
00029 #include <QDomElement>
00030
00031 #include "qgsattributeaction.h"
00032 #include "qgsrunprocess.h"
00033 #include "qgsvectorlayer.h"
00034
00035 static const char * const ident_ = "$Id$";
00036
00037 void QgsAttributeAction::addAction( QgsAction::ActionType type, QString name, QString action, bool capture )
00038 {
00039 mActions << QgsAction( type, name, action, capture );
00040 }
00041
00042 void QgsAttributeAction::doAction( int index, const QgsAttributeMap &attributes,
00043 int defaultValueIndex, void ( *executePython )( const QString & ) )
00044 {
00045 if ( index < 0 || index >= size() )
00046 return;
00047
00048 const QgsAction &action = at( index );
00049
00050 if ( !action.runable() )
00051 return;
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 QString expandedAction = expandAction( action.action(), attributes, defaultValueIndex );
00066 if ( action.type() == QgsAction::GenericPython )
00067 {
00068 if ( executePython )
00069 {
00070 executePython( expandedAction );
00071 }
00072 else if ( smPythonExecute )
00073 {
00074 smPythonExecute( expandedAction );
00075 }
00076 }
00077 else
00078 {
00079 QgsRunProcess::create( expandedAction, action.capture() );
00080 }
00081 }
00082
00083 QString QgsAttributeAction::expandAction( QString action, const QgsAttributeMap &attributes,
00084 uint clickedOnValue )
00085 {
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 QString expanded_action;
00102 if ( clickedOnValue >= 0 && attributes.contains( clickedOnValue ) )
00103 expanded_action = action.replace( "%%", attributes[clickedOnValue].toString() );
00104 else
00105 expanded_action = action;
00106
00107 const QgsFieldMap &fields = mLayer->pendingFields();
00108
00109 for ( int i = 0; i < 4; i++ )
00110 {
00111 for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); it++ )
00112 {
00113 QgsFieldMap::const_iterator fit = fields.find( it.key() );
00114 if ( fit == fields.constEnd() )
00115 continue;
00116
00117 QString to_replace;
00118 switch ( i )
00119 {
00120 case 0: to_replace = "[%" + fit->name() + "]"; break;
00121 case 1: to_replace = "[%" + mLayer->attributeDisplayName( it.key() ) + "]"; break;
00122 case 2: to_replace = "%" + fit->name(); break;
00123 case 3: to_replace = "%" + mLayer->attributeDisplayName( it.key() ); break;
00124 }
00125
00126 expanded_action = expanded_action.replace( to_replace, it.value().toString() );
00127 }
00128 }
00129
00130
00131 return expanded_action;
00132 }
00133
00134 bool QgsAttributeAction::writeXML( QDomNode& layer_node, QDomDocument& doc ) const
00135 {
00136 QDomElement aActions = doc.createElement( "attributeactions" );
00137
00138 for ( int i = 0; i < mActions.size(); i++ )
00139 {
00140 QDomElement actionSetting = doc.createElement( "actionsetting" );
00141 actionSetting.setAttribute( "type", mActions[i].type() );
00142 actionSetting.setAttribute( "name", mActions[i].name() );
00143 actionSetting.setAttribute( "action", mActions[i].action() );
00144 actionSetting.setAttribute( "capture", mActions[i].capture() );
00145 aActions.appendChild( actionSetting );
00146 }
00147 layer_node.appendChild( aActions );
00148
00149 return true;
00150 }
00151
00152 bool QgsAttributeAction::readXML( const QDomNode& layer_node )
00153 {
00154 mActions.clear();
00155
00156 QDomNode aaNode = layer_node.namedItem( "attributeactions" );
00157
00158 if ( !aaNode.isNull() )
00159 {
00160 QDomNodeList actionsettings = aaNode.childNodes();
00161 for ( unsigned int i = 0; i < actionsettings.length(); ++i )
00162 {
00163 QDomElement setting = actionsettings.item( i ).toElement();
00164 addAction(( QgsAction::ActionType ) setting.attributeNode( "type" ).value().toInt(),
00165 setting.attributeNode( "name" ).value(),
00166 setting.attributeNode( "action" ).value(),
00167 setting.attributeNode( "capture" ).value().toInt() != 0 );
00168 }
00169 }
00170 return true;
00171 }
00172
00173 void ( *QgsAttributeAction::smPythonExecute )( const QString & ) = 0;
00174
00175 void QgsAttributeAction::setPythonExecute( void ( *runPython )( const QString & ) )
00176 {
00177 smPythonExecute = runPython;
00178 }