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;
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.
Container of fields for a vector layer.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QString iconPath(QString iconFile)
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.
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.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
void doAction(int index, const QgsFeature &feat, int defaultValueIndex=0)
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.
void removeAction(int index)
Remove an action at given index.
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0, const QgsDistanceArea *distanceArea=0)
This function currently replaces each expression between [% and %] in the string with the result of i...