Skip to content

Commit

Permalink
QgsActionManager: improve docs + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 28, 2016
1 parent 3d494ac commit bc779a0
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 18 deletions.
58 changes: 53 additions & 5 deletions python/core/qgsactionmanager.sip
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class QgsActionManager
*/
void addAction( QgsAction::ActionType type, const QString& name, const QString& action, const QString& icon, bool capture = false );

/**
* Add a new action to this list.
*/
void addAction( const QgsAction& action );

//! Remove an action at given index
void removeAction( int index );

Expand All @@ -65,6 +70,20 @@ class QgsActionManager
const QgsFeature &feat,
int defaultValueIndex = 0 ) /PyName=doActionFeature/;

/** Does the action using the expression engine to replace any embedded expressions
* in the action definition.
* @param index action index
* @param feature feature to run action for
* @param context expression context to evalute expressions under
* @param substitutionMap deprecated - kept for compatibilty with projects, will be removed for 3.0
*/
// TODO QGIS 3.0 remove substition map - force use of expression variables
void doAction( int index,
const QgsFeature& feature,
const QgsExpressionContext& context,
const QMap<QString, QVariant> *substitutionMap = nullptr );


/** Does the action using the expression builder to expand it
* and getting values from the passed feature attribute map.
* substitutionMap is used to pass custom substitutions, to replace
Expand All @@ -86,8 +105,9 @@ class QgsActionManager

/** Expands the given action, replacing all %'s with the value as
* given.
* @deprecated use QgsExpression::replaceExpressionText() instead
*/
QString expandAction( QString action, const QMap<int, QVariant> &attributes, uint defaultValueIndex );
QString expandAction( QString action, const QMap<int, QVariant> &attributes, uint defaultValueIndex ) /Deprecated/;

/** Expands the given action using the expression builder
* This function currently replaces each expression between [% and %]
Expand All @@ -96,10 +116,11 @@ class QgsActionManager
*
* Additional substitutions can be passed through the substitutionMap
* parameter
* @deprecated use QgsExpression::replaceExpressionText() instead
*/
QString expandAction( const QString& action,
QgsFeature &feat,
const QMap<QString, QVariant> *substitutionMap = 0 );
const QMap<QString, QVariant> *substitutionMap = 0 ) /Deprecated/;


//! Writes the actions out in XML format
Expand All @@ -112,23 +133,50 @@ class QgsActionManager
/**
* Get the action at the specified index.
*/
QgsAction at( int idx ) const;
QgsAction at( int idx ) const /Factory/;
%MethodCode
if ( a0 < 0 || a0 >= sipCpp->size() )
{
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
sipIsErr = 1;
}
else
{
sipRes = new QgsAction( sipCpp->at( a0 ) );
}
%End

/**
* Get the action at the specified index.
*/
QgsAction operator[]( int idx ) const;
%MethodCode
if ( a0 < 0 || a0 >= sipCpp->size() )
{
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
sipIsErr = 1;
}
else
{
sipRes = new QgsAction( sipCpp->at( a0 ) );
}
%End

/** @deprecated Initialize QgsPythonRunner instead
* @note not available in Python bindings
*/
// Q_DECL_DEPRECATED static void setPythonExecute( void ( * )( const QString & ) );

/**
* Get the index of the default action
* Returns the index of the default action, or -1 if no default action is available.
* @see setDefaultAction()
*/
int defaultAction() const;

/**
* Set the index of the default action
* Set the index of the default action.
* @param actionNumber index of action which should be made the default for the layer
* @see defaultAction()
*/
void setDefaultAction( int actionNumber );
};
18 changes: 12 additions & 6 deletions src/core/qgsactionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ class CORE_EXPORT QgsActionManager

/** Expands the given action, replacing all %'s with the value as
* given.
* @deprecated use QgsExpression::replaceExpressionText() instead
*/
QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
Q_DECL_DEPRECATED QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );

/** Expands the given action using the expression builder
* This function currently replaces each expression between [% and %]
Expand All @@ -132,10 +133,11 @@ class CORE_EXPORT QgsActionManager
*
* Additional substitutions can be passed through the substitutionMap
* parameter
* @deprecated use QgsExpression::replaceExpressionText() instead
*/
QString expandAction( const QString& action,
QgsFeature &feat,
const QMap<QString, QVariant> *substitutionMap = nullptr );
Q_DECL_DEPRECATED QString expandAction( const QString& action,
QgsFeature &feat,
const QMap<QString, QVariant> *substitutionMap = nullptr );


//! Writes the actions out in XML format
Expand Down Expand Up @@ -165,11 +167,15 @@ class CORE_EXPORT QgsActionManager
Q_DECL_DEPRECATED static void setPythonExecute( void ( * )( const QString & ) );

/**
* Get the index of the default action
* Returns the index of the default action, or -1 if no default action is available.
* @see setDefaultAction()
*/
int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? -1 : mDefaultAction; }

/**
* Set the index of the default action
* Set the index of the default action.
* @param actionNumber index of action which should be made the default for the layer
* @see defaultAction()
*/
void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; }

Expand Down
14 changes: 7 additions & 7 deletions src/core/qgsattributeaction.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/***************************************************************************
qgsactionmanager.h
qgsactionmanager.h
This is a legacy header to keep backwards compatibility until QGIS 3.
This is a legacy header to keep backwards compatibility until QGIS 3.
-------------------
begin : Oct 24 2004
copyright : (C) 2004 by Gavin Macaulay
email : gavin at macaulay dot co dot nz
***************************************************************************/
-------------------
begin : Oct 24 2004
copyright : (C) 2004 by Gavin Macaulay
email : gavin at macaulay dot co dot nz
***************************************************************************/

/***************************************************************************
* *
Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
ENDIF (WITH_SERVER)

ADD_PYTHON_TEST(PyQgsActionManager test_qgsactionmanager.py)
ADD_PYTHON_TEST(PyQgsAggregateCalculator test_qgsaggregatecalculator.py)
ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
Expand Down
Loading

0 comments on commit bc779a0

Please sign in to comment.