34 #include <QStringList>
35 #include <QDomElement>
37 #include <QDesktopServices>
50 if ( index >= 0 && index <
mActions.size() )
58 QMap<QString, QVariant> substitutionMap;
59 if ( defaultValueIndex >= 0 )
61 QVariant defaultValue = feat.
attribute( defaultValueIndex );
62 if ( defaultValue.isValid() )
63 substitutionMap.insert(
"$currfield", defaultValue );
66 doAction( index, feat, &substitutionMap );
70 const QMap<QString, QVariant> *substitutionMap )
72 if ( index < 0 || index >=
size() )
81 if ( expandedAction.isEmpty() )
92 QFileInfo finfo( action.
action() );
93 if ( finfo.exists() && finfo.isFile() )
94 QDesktopServices::openUrl( QUrl::fromLocalFile( action.
action() ) );
96 QDesktopServices::openUrl( QUrl( action.
action(), QUrl::TolerantMode ) );
103 executePython( action.
action() );
125 uint clickedOnValue )
142 QString expanded_action;
143 if ( attributes.contains( clickedOnValue ) )
144 expanded_action = action.replace(
"%%", attributes[clickedOnValue].toString() );
146 expanded_action = action;
150 for (
int i = 0; i < 4; i++ )
152 for ( QgsAttributeMap::const_iterator it = attributes.begin(); it != attributes.end(); ++it )
154 int attrIdx = it.key();
155 if ( attrIdx < 0 || attrIdx >= fields.
count() )
161 case 0: to_replace =
"[%" + fields[attrIdx].name() +
"]";
break;
163 case 2: to_replace =
"%" + fields[attrIdx].name();
break;
167 expanded_action = expanded_action.replace( to_replace, it.value().toString() );
171 return expanded_action;
186 while ( index < action.size() )
188 QRegExp rx = QRegExp(
"\\[%([^\\]]+)%\\]" );
190 int pos = rx.indexIn( action, index );
195 index = pos + rx.matchedLength();
197 QString to_replace = rx.cap( 1 ).trimmed();
200 if ( substitutionMap && substitutionMap->contains( to_replace ) )
202 expr_action += action.mid( start, pos - start ) + substitutionMap->value( to_replace ).toString();
210 expr_action += action.mid( start, index - start );
218 expr_action += action.mid( start, index - start );
222 QgsDebugMsg(
"Expression result is: " + result.toString() );
223 expr_action += action.mid( start, pos - start ) + result.toString();
226 expr_action += action.mid( index );
233 QDomElement aActions = doc.createElement(
"attributeactions" );
235 for (
int i = 0; i <
mActions.size(); i++ )
237 QDomElement actionSetting = doc.createElement(
"actionsetting" );
238 actionSetting.setAttribute(
"type",
mActions[i].type() );
239 actionSetting.setAttribute(
"name",
mActions[i].name() );
240 actionSetting.setAttribute(
"action",
mActions[i].action() );
241 actionSetting.setAttribute(
"capture",
mActions[i].capture() );
242 aActions.appendChild( actionSetting );
244 layer_node.appendChild( aActions );
253 QDomNode aaNode = layer_node.namedItem(
"attributeactions" );
255 if ( !aaNode.isNull() )
257 QDomNodeList actionsettings = aaNode.childNodes();
258 for (
unsigned int i = 0; i < actionsettings.length(); ++i )
260 QDomElement setting = actionsettings.item( i ).toElement();
262 setting.attributeNode(
"name" ).value(),
263 setting.attributeNode(
"action" ).value(),
264 setting.attributeNode(
"capture" ).value().toInt() != 0 );
Class for parsing and evaluation of expressions (formerly called "search strings").
bool hasEvalError() const
Returns true if an error occurred when evaluating last input.
void addAction(QgsAction::ActionType type, QString name, QString action, bool capture=false)
Add an action with the given name and action details.
bool hasParserError() const
Returns true if an error occurred when parsing the input expression.
QMap< int, QVariant > QgsAttributeMap
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
static void(* smPythonExecute)(const QString &)
Container of fields for a vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
bool capture() const
Whether to capture output for display when this action is run.
bool writeXML(QDomNode &layer_node, QDomDocument &doc) const
Writes the actions out in XML format.
QString name() const
The name of the action.
QString attributeDisplayName(int attributeIndex) const
Convenience function that returns the attribute alias if defined or the field name else...
static bool run(QString command, QString messageOnError=QString())
execute a python statement
Utility class that encapsulates an action based on vector attributes.
QList< QgsAction > mActions
void doAction(int index, QgsFeature &feat, int defaultValueIndex=0)
int count() const
Return number of items.
QString expandAction(QString action, const QgsAttributeMap &attributes, uint defaultValueIndex)
QString action() const
The action.
static void setPythonExecute(void(*)(const QString &))
ActionType type() const
The action type.
void runAction(const QgsAction &action, void(*executePython)(const QString &)=0)
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
bool runable() const
Whether the action is runable on the current platform.
const QgsFields & pendingFields() const
returns field list in the to-be-committed state
static QgsRunProcess * create(const QString &action, bool capture)
QString parserErrorString() const
Returns parser error.
QString evalErrorString() const
Returns evaluation error.
bool readXML(const QDomNode &layer_node)
Reads the actions in in XML format.
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0)
This function currently replaces each expression between [% and %] in the string with the result of i...
void removeAction(int index)
Remove an action at given index.