Skip to content

Commit bc779a0

Browse files
committed
QgsActionManager: improve docs + add tests
1 parent 3d494ac commit bc779a0

File tree

5 files changed

+302
-18
lines changed

5 files changed

+302
-18
lines changed

python/core/qgsactionmanager.sip

+53-5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class QgsActionManager
5454
*/
5555
void addAction( QgsAction::ActionType type, const QString& name, const QString& action, const QString& icon, bool capture = false );
5656

57+
/**
58+
* Add a new action to this list.
59+
*/
60+
void addAction( const QgsAction& action );
61+
5762
//! Remove an action at given index
5863
void removeAction( int index );
5964

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

73+
/** Does the action using the expression engine to replace any embedded expressions
74+
* in the action definition.
75+
* @param index action index
76+
* @param feature feature to run action for
77+
* @param context expression context to evalute expressions under
78+
* @param substitutionMap deprecated - kept for compatibilty with projects, will be removed for 3.0
79+
*/
80+
// TODO QGIS 3.0 remove substition map - force use of expression variables
81+
void doAction( int index,
82+
const QgsFeature& feature,
83+
const QgsExpressionContext& context,
84+
const QMap<QString, QVariant> *substitutionMap = nullptr );
85+
86+
6887
/** Does the action using the expression builder to expand it
6988
* and getting values from the passed feature attribute map.
7089
* substitutionMap is used to pass custom substitutions, to replace
@@ -86,8 +105,9 @@ class QgsActionManager
86105

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

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

104125

105126
//! Writes the actions out in XML format
@@ -112,23 +133,50 @@ class QgsActionManager
112133
/**
113134
* Get the action at the specified index.
114135
*/
115-
QgsAction at( int idx ) const;
136+
QgsAction at( int idx ) const /Factory/;
137+
%MethodCode
138+
if ( a0 < 0 || a0 >= sipCpp->size() )
139+
{
140+
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
141+
sipIsErr = 1;
142+
}
143+
else
144+
{
145+
sipRes = new QgsAction( sipCpp->at( a0 ) );
146+
}
147+
%End
148+
116149
/**
117150
* Get the action at the specified index.
118151
*/
119152
QgsAction operator[]( int idx ) const;
153+
%MethodCode
154+
if ( a0 < 0 || a0 >= sipCpp->size() )
155+
{
156+
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
157+
sipIsErr = 1;
158+
}
159+
else
160+
{
161+
sipRes = new QgsAction( sipCpp->at( a0 ) );
162+
}
163+
%End
120164

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

126170
/**
127-
* Get the index of the default action
171+
* Returns the index of the default action, or -1 if no default action is available.
172+
* @see setDefaultAction()
128173
*/
129174
int defaultAction() const;
175+
130176
/**
131-
* Set the index of the default action
177+
* Set the index of the default action.
178+
* @param actionNumber index of action which should be made the default for the layer
179+
* @see defaultAction()
132180
*/
133181
void setDefaultAction( int actionNumber );
134182
};

src/core/qgsactionmanager.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ class CORE_EXPORT QgsActionManager
122122

123123
/** Expands the given action, replacing all %'s with the value as
124124
* given.
125+
* @deprecated use QgsExpression::replaceExpressionText() instead
125126
*/
126-
QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
127+
Q_DECL_DEPRECATED QString expandAction( QString action, const QgsAttributeMap &attributes, uint defaultValueIndex );
127128

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

140142

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

167169
/**
168-
* Get the index of the default action
170+
* Returns the index of the default action, or -1 if no default action is available.
171+
* @see setDefaultAction()
169172
*/
170173
int defaultAction() const { return mDefaultAction < 0 || mDefaultAction >= size() ? -1 : mDefaultAction; }
174+
171175
/**
172-
* Set the index of the default action
176+
* Set the index of the default action.
177+
* @param actionNumber index of action which should be made the default for the layer
178+
* @see defaultAction()
173179
*/
174180
void setDefaultAction( int actionNumber ) { mDefaultAction = actionNumber ; }
175181

src/core/qgsattributeaction.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/***************************************************************************
2-
qgsactionmanager.h
2+
qgsactionmanager.h
33
4-
This is a legacy header to keep backwards compatibility until QGIS 3.
4+
This is a legacy header to keep backwards compatibility until QGIS 3.
55
6-
-------------------
7-
begin : Oct 24 2004
8-
copyright : (C) 2004 by Gavin Macaulay
9-
email : gavin at macaulay dot co dot nz
10-
***************************************************************************/
6+
-------------------
7+
begin : Oct 24 2004
8+
copyright : (C) 2004 by Gavin Macaulay
9+
email : gavin at macaulay dot co dot nz
10+
***************************************************************************/
1111

1212
/***************************************************************************
1313
* *

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ IF (WITH_SERVER)
77
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
88
ENDIF (WITH_SERVER)
99

10+
ADD_PYTHON_TEST(PyQgsActionManager test_qgsactionmanager.py)
1011
ADD_PYTHON_TEST(PyQgsAggregateCalculator test_qgsaggregatecalculator.py)
1112
ADD_PYTHON_TEST(PyQgsAnalysis test_qgsanalysis.py)
1213
ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)

0 commit comments

Comments
 (0)