34 #include <QStringList>
35 #include <QDomElement>
37 #include <QDesktopServices>
45 mActions <<
QgsAction( type, name, action, capture );
50 mActions <<
QgsAction( type, name, action, icon, capture );
55 if ( index >= 0 && index < mActions.size() )
57 mActions.removeAt( index );
63 QMap<QString, QVariant> substitutionMap;
64 if ( defaultValueIndex >= 0 )
66 QVariant defaultValue = feat.
attribute( defaultValueIndex );
67 if ( defaultValue.isValid() )
68 substitutionMap.insert(
"$currfield", defaultValue );
71 doAction( index, feat, &substitutionMap );
76 if ( index < 0 || index >=
size() )
85 if ( expandedAction.isEmpty() )
89 runAction( newAction );
92 void QgsAttributeAction::runAction(
const QgsAction &action,
void ( *executePython )(
const QString & ) )
96 QFileInfo finfo( action.
action() );
97 if ( finfo.exists() && finfo.isFile() )
98 QDesktopServices::openUrl( QUrl::fromLocalFile( action.
action() ) );
100 QDesktopServices::openUrl( QUrl( action.
action(), QUrl::TolerantMode ) );
107 executePython( action.
action() );
109 else if ( smPythonExecute )
112 smPythonExecute( action.
action() );
129 uint clickedOnValue )
146 QString expanded_action;
147 if ( attributes.contains( clickedOnValue ) )
148 expanded_action = action.replace(
"%%", attributes[clickedOnValue].toString() );
150 expanded_action = action;
154 for (
int i = 0; i < 4; i++ )
156 for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
158 int attrIdx = it.key();
159 if ( attrIdx < 0 || attrIdx >= fields.
count() )
165 case 0: to_replace =
"[%" + fields[attrIdx].name() +
"]";
break;
167 case 2: to_replace =
"%" + fields[attrIdx].name();
break;
171 expanded_action = expanded_action.replace( to_replace, it.value().toString() );
175 return expanded_action;
190 while ( index < action.size() )
192 QRegExp rx = QRegExp(
"\\[%([^\\]]+)%\\]" );
194 int pos = rx.indexIn( action, index );
199 index = pos + rx.matchedLength();
201 QString to_replace = rx.cap( 1 ).trimmed();
204 if ( substitutionMap && substitutionMap->contains( to_replace ) )
206 expr_action += action.mid( start, pos - start ) + substitutionMap->value( to_replace ).toString();
214 expr_action += action.mid( start, index - start );
222 expr_action += action.mid( start, index - start );
226 QgsDebugMsg(
"Expression result is: " + result.toString() );
227 expr_action += action.mid( start, pos - start ) + result.toString();
230 expr_action += action.mid( index );
237 QDomElement aActions = doc.createElement(
"attributeactions" );
239 for (
int i = 0; i < mActions.size(); i++ )
241 QDomElement actionSetting = doc.createElement(
"actionsetting" );
242 actionSetting.setAttribute(
"type", mActions[i].type() );
243 actionSetting.setAttribute(
"name", mActions[i].name() );
244 actionSetting.setAttribute(
"icon", mActions[i].
iconPath() );
245 actionSetting.setAttribute(
"action", mActions[i].action() );
246 actionSetting.setAttribute(
"capture", mActions[i].capture() );
247 aActions.appendChild( actionSetting );
249 layer_node.appendChild( aActions );
258 QDomNode aaNode = layer_node.namedItem(
"attributeactions" );
260 if ( !aaNode.isNull() )
262 QDomNodeList actionsettings = aaNode.childNodes();
263 for (
unsigned int i = 0; i < actionsettings.length(); ++i )
265 QDomElement setting = actionsettings.item( i ).toElement();
267 setting.attributeNode(
"name" ).value(),
268 setting.attributeNode(
"action" ).value(),
269 setting.attributeNode(
"icon" ).value(),
270 setting.attributeNode(
"capture" ).value().toInt() != 0 );
276 void ( *QgsAttributeAction::smPythonExecute )(
const QString & ) = 0;
280 smPythonExecute = runPython;