Skip to content

Commit f18d1c3

Browse files
committedNov 16, 2016
Rename listActions to actions and fix test
1 parent a6eb7b6 commit f18d1c3

13 files changed

+89
-81
lines changed
 

‎python/core/qgsactionmanager.sip

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class QgsActionManager
5959
*/
6060
void addAction( const QgsAction& action );
6161

62+
/**
63+
* Remove an action by its id.
64+
*
65+
* @note Added in QGIS 3.0
66+
*/
67+
void removeAction( const QUuid& actionId );
68+
6269
/** Does the given values. defaultValueIndex is the index of the
6370
* field to be used if the action has a $currfield placeholder.
6471
* @note available in python bindings as doActionFeature
@@ -80,7 +87,7 @@ class QgsActionManager
8087
* Return a list of actions that are available in the given action scope.
8188
* If no action scope is provided, all actions will be returned.
8289
*/
83-
QList<QgsAction> listActions( const QString& actionScope = QString() ) const;
90+
QList<QgsAction> actions( const QString& actionScope = QString() ) const;
8491

8592
//! Return the layer
8693
QgsVectorLayer* layer() const;

‎src/app/qgisapp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,7 +5812,7 @@ void QgisApp::refreshFeatureActions()
58125812
if ( !vlayer )
58135813
return;
58145814

5815-
QList<QgsAction> actions = vlayer->actions()->listActions( QStringLiteral( "Canvas" ) );
5815+
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Canvas" ) );
58165816
Q_FOREACH ( const QgsAction& action, actions )
58175817
{
58185818
QAction* qAction = new QAction( action.icon(), action.shortTitle(), mFeatureActionMenu );
@@ -10623,7 +10623,7 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
1062310623

1062410624
bool isEditable = vlayer->isEditable();
1062510625
bool layerHasSelection = vlayer->selectedFeatureCount() > 0;
10626-
bool layerHasActions = !vlayer->actions()->listActions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty();
10626+
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty();
1062710627

1062810628
mActionLocalHistogramStretch->setEnabled( false );
1062910629
mActionFullHistogramStretch->setEnabled( false );
@@ -10909,7 +10909,7 @@ void QgisApp::refreshActionFeatureAction()
1090910909
if ( !vlayer )
1091010910
return;
1091110911

10912-
bool layerHasActions = !vlayer->actions()->listActions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty();
10912+
bool layerHasActions = !vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() || !QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty();
1091310913
mActionFeatureAction->setEnabled( layerHasActions );
1091410914
}
1091510915

‎src/app/qgsattributeactiondialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void QgsAttributeActionDialog::init( const QgsActionManager& actions, const QgsA
6767

6868
int i = 0;
6969
// Populate with our actions.
70-
Q_FOREACH ( const QgsAction& action, actions.listActions() )
70+
Q_FOREACH ( const QgsAction& action, actions.actions() )
7171
{
7272
insertRow( i++, action );
7373
}

‎src/app/qgsattributetabledialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
309309
mActionSearchForm->setToolTip( tr( "Search is not supported when using custom UI forms" ) );
310310
}
311311

312-
QList<QgsAction> actions = mLayer->actions()->listActions( QStringLiteral( "Layer" ) );
312+
QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Layer" ) );
313313

314314
if ( actions.isEmpty() )
315315
{

‎src/app/qgsfeatureaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ QgsAttributeDialog *QgsFeatureAction::newDialog( bool cloneFeature )
6666
QgsAttributeDialog *dialog = new QgsAttributeDialog( mLayer, f, cloneFeature, parentWidget(), true, context );
6767
dialog->setWindowFlags( dialog->windowFlags() | Qt::Tool );
6868

69-
QList<QgsAction> actions = mLayer->actions()->listActions( QStringLiteral( "Feature" ) );
69+
QList<QgsAction> actions = mLayer->actions()->actions( QStringLiteral( "Feature" ) );
7070
if ( !actions.isEmpty() )
7171
{
7272
dialog->setContextMenuPolicy( Qt::ActionsContextMenu );

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
480480

481481
//get valid QgsMapLayerActions for this layer
482482
QList< QgsMapLayerAction* > registeredActions = QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer );
483-
QList<QgsAction> actions = vlayer->actions()->listActions( QStringLiteral( "Feature" ) );
483+
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Feature" ) );
484484

485485
if ( !vlayer->fields().isEmpty() || !actions.isEmpty() || !registeredActions.isEmpty() )
486486
{
@@ -1092,7 +1092,7 @@ void QgsIdentifyResultsDialog::contextMenuEvent( QContextMenuEvent* event )
10921092

10931093
if ( featItem && vlayer )
10941094
{
1095-
QList<QgsAction> actions = vlayer->actions()->listActions( QStringLiteral( "Field" ) );
1095+
QList<QgsAction> actions = vlayer->actions()->actions( QStringLiteral( "Field" ) );
10961096
if ( !actions.isEmpty() )
10971097
{
10981098
mActionPopup->addSeparator();

‎src/app/qgsmaptoolfeatureaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void QgsMapToolFeatureAction::canvasReleaseEvent( QgsMapMouseEvent* e )
7373
}
7474

7575
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
76-
if ( vlayer->actions()->listActions( QStringLiteral( "Canvas" ) ).isEmpty() && QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty() )
76+
if ( vlayer->actions()->actions( QStringLiteral( "Canvas" ) ).isEmpty() && QgsMapLayerActionRegistry::instance()->mapLayerActions( vlayer ).isEmpty() )
7777
{
7878
emit messageEmitted( tr( "The active vector layer has no defined actions" ), QgsMessageBar::INFO );
7979
return;

‎src/core/qgsactionmanager.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,27 @@ void QgsActionManager::addAction( const QgsAction& action )
5959
mActions.append( action );
6060
}
6161

62+
void QgsActionManager::removeAction( const QUuid& actionId )
63+
{
64+
int i = 0;
65+
Q_FOREACH ( const QgsAction& action, mActions )
66+
{
67+
if ( action.id() == actionId )
68+
{
69+
mActions.removeAt( i );
70+
return;
71+
}
72+
++i;
73+
}
74+
}
75+
6276
void QgsActionManager::doAction( const QUuid& actionId, const QgsFeature& feature, int defaultValueIndex )
6377
{
6478
QgsExpressionContext context = createExpressionContext();
6579
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope();
6680
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_index" ), defaultValueIndex, true ) );
6781
if ( defaultValueIndex >= 0 && defaultValueIndex < feature.fields().size() )
68-
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_name" ), feature.fields().at( defaultValueIndex ), true ) );
82+
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_name" ), feature.fields().at( defaultValueIndex ).name(), true ) );
6983
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_value" ), feature.attribute( defaultValueIndex ), true ) );
7084
context << actionScope;
7185
doAction( actionId, feature, context );
@@ -97,7 +111,7 @@ void QgsActionManager::clearActions()
97111
mActions.clear();
98112
}
99113

100-
QList<QgsAction> QgsActionManager::listActions( const QString& actionScope ) const
114+
QList<QgsAction> QgsActionManager::actions( const QString& actionScope ) const
101115
{
102116
if ( actionScope.isNull() )
103117
return mActions;

‎src/core/qgsactionmanager.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class CORE_EXPORT QgsActionManager
7474
*/
7575
void addAction( const QgsAction& action );
7676

77+
/**
78+
* Remove an action by its id.
79+
*
80+
* @note Added in QGIS 3.0
81+
*/
82+
void removeAction( const QUuid& actionId );
83+
7784
/** Does the given values. defaultValueIndex is the index of the
7885
* field to be used if the action has a $currfield placeholder.
7986
* @note available in python bindings as doActionFeature
@@ -94,8 +101,10 @@ class CORE_EXPORT QgsActionManager
94101
/**
95102
* Return a list of actions that are available in the given action scope.
96103
* If no action scope is provided, all actions will be returned.
104+
*
105+
* @note Added in QGIS 3.0
97106
*/
98-
QList<QgsAction> listActions( const QString& actionScope = QString() ) const;
107+
QList<QgsAction> actions( const QString& actionScope = QString() ) const;
99108

100109
//! Return the layer
101110
QgsVectorLayer* layer() const { return mLayer; }

‎src/gui/attributetable/qgsattributetableview.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ QWidget* QgsAttributeTableView::createActionWidget( QgsFeatureId fid )
176176
QAction* defaultAction = nullptr;
177177

178178
// first add user created layer actions
179-
QList<QgsAction> actions = mFilterModel->layer()->actions()->listActions( QStringLiteral( "Feature" ) );
179+
QList<QgsAction> actions = mFilterModel->layer()->actions()->actions( QStringLiteral( "Feature" ) );
180180
Q_FOREACH ( const QgsAction& action, actions )
181181
{
182182
QString actionTitle = !action.shortTitle().isEmpty() ? action.shortTitle() : action.icon().isNull() ? action.name() : QLatin1String( "" );

‎src/gui/attributetable/qgsdualview.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ void QgsDualView::viewWillShowContextMenu( QMenu* menu, const QModelIndex& atInd
390390
}
391391

392392
//add user-defined actions to context menu
393-
QList<QgsAction> actions = mLayerCache->layer()->actions()->listActions( QStringLiteral( "Field" ) );
393+
QList<QgsAction> actions = mLayerCache->layer()->actions()->actions( QStringLiteral( "Field" ) );
394394
if ( !actions.isEmpty() )
395395
{
396396
QAction* a = menu->addAction( tr( "Run layer action" ) );

‎src/gui/qgsactionmenu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void QgsActionMenu::reloadActions()
105105
{
106106
clear();
107107

108-
mActions = mLayer->actions()->listActions( mActionScope );
108+
mActions = mLayer->actions()->actions( mActionScope );
109109

110110
Q_FOREACH ( const QgsAction& action, mActions )
111111
{

‎tests/src/python/test_qgsactionmanager.py

Lines changed: 43 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
QgsField,
2323
QgsFields
2424
)
25-
from qgis.PyQt.QtCore import QDir, QTemporaryFile
25+
from qgis.PyQt.QtCore import QDir, QTemporaryFile, QUuid
2626

2727
from qgis.testing import (start_app,
2828
unittest
@@ -70,26 +70,26 @@ def testAddAction(self):
7070
""" Test adding actions """
7171

7272
# should be empty to start with
73-
self.assertEqual(self.manager.listActions(), [])
73+
self.assertEqual(self.manager.actions(), [])
7474

7575
# add an action
7676
action1 = QgsAction(QgsAction.GenericPython, 'Test Action', 'i=1')
7777
self.manager.addAction(action1)
78-
self.assertEqual(len(self.manager.listActions()), 1)
79-
self.assertEqual(self.manager.listActions()[0].type(), QgsAction.GenericPython)
80-
self.assertEqual(self.manager.listActions()[0].name(), 'Test Action')
81-
self.assertEqual(self.manager.listActions()[0].command(), 'i=1')
78+
self.assertEqual(len(self.manager.actions()), 1)
79+
self.assertEqual(self.manager.actions()[0].type(), QgsAction.GenericPython)
80+
self.assertEqual(self.manager.actions()[0].name(), 'Test Action')
81+
self.assertEqual(self.manager.actions()[0].command(), 'i=1')
8282

8383
# add another action
8484
action2 = QgsAction(QgsAction.Windows, 'Test Action2', 'i=2')
8585
self.manager.addAction(action2)
86-
self.assertEqual(len(self.manager.listActions()), 2)
86+
self.assertEqual(len(self.manager.actions()), 2)
8787
self.assertEqual(self.manager.action(action2.id()).type(), QgsAction.Windows)
8888
self.assertEqual(self.manager.action(action2.id()).name(), 'Test Action2')
8989
self.assertEqual(self.manager.action(action2.id()).command(), 'i=2')
9090

9191
id3 = self.manager.addAction(QgsAction.Generic, 'Test Action3', 'i=3')
92-
self.assertEqual(len(self.manager.listActions()), 3)
92+
self.assertEqual(len(self.manager.actions()), 3)
9393
self.assertEqual(self.manager.action(id3).type(), QgsAction.Generic)
9494
self.assertEqual(self.manager.action(id3).name(), 'Test Action3')
9595
self.assertEqual(self.manager.action(id3).command(), 'i=3')
@@ -102,75 +102,52 @@ def testRemoveActions(self):
102102

103103
# clear the manager and check that it's empty
104104
self.manager.clearActions()
105-
self.assertEqual(self.manager.size(), 0)
106-
self.assertEqual(self.manager.listActions(), [])
105+
self.assertEqual(self.manager.actions(), [])
107106

108107
# add some actions
109108
id1 = self.manager.addAction(QgsAction.GenericPython, 'test_action', 'i=1')
110109
id2 = self.manager.addAction(QgsAction.GenericPython, 'test_action2', 'i=2')
111110
id3 = self.manager.addAction(QgsAction.GenericPython, 'test_action3', 'i=3')
112111

113112
# remove non-existant action
114-
self.manager.removeAction(5)
113+
self.manager.removeAction(QUuid.createUuid())
115114

116115
# remove them one by one
117-
self.manager.removeAction(1)
118-
self.assertEqual(self.manager.size(), 2)
119-
self.assertEqual(self.manager.listActions()[0].name(), 'test_action')
120-
self.assertEqual(self.manager.listActions()[1].name(), 'test_action3')
121-
self.manager.removeAction(0)
122-
self.assertEqual(self.manager.size(), 1)
123-
self.assertEqual(self.manager.listActions()[0].name(), 'test_action3')
124-
self.manager.removeAction(0)
125-
self.assertEqual(self.manager.size(), 0)
126-
127-
def testRetrieveAction(self):
128-
""" test retrieving actions """
129-
self.manager.clearActions()
130-
131-
# test that exceptions are thrown when retrieving bad indices
132-
133-
with self.assertRaises(KeyError):
134-
self.manager[0]
135-
136-
with self.assertRaises(KeyError):
137-
self.manager.at(0)
138-
139-
self.manager.addAction(QgsAction.GenericPython, 'test_action', 'i=1')
140-
141-
with self.assertRaises(KeyError):
142-
self.manager[-1]
143-
144-
with self.assertRaises(KeyError):
145-
self.manager.at(-1)
146-
147-
with self.assertRaises(KeyError):
148-
self.manager[5]
149-
150-
with self.assertRaises(KeyError):
151-
self.manager.at(5)
116+
self.manager.removeAction(id2)
117+
self.assertEqual(len(self.manager.actions()), 2)
118+
self.assertEqual(self.manager.action(id1).name(), 'test_action')
119+
self.assertEqual(self.manager.action(id3).name(), 'test_action3')
120+
self.manager.removeAction(id1)
121+
self.assertEqual(len(self.manager.actions()), 1)
122+
self.assertEqual(self.manager.action(id3).name(), 'test_action3')
123+
self.manager.removeAction(id3)
124+
self.assertEqual(len(self.manager.actions()), 0)
152125

153126
def testDefaultAction(self):
154127
""" test default action for layer"""
155128

156129
self.manager.clearActions()
157-
self.manager.addAction(QgsAction.GenericPython, 'test_action', 'i=1')
158-
self.manager.addAction(QgsAction.GenericPython, 'test_action2', 'i=2')
130+
action1 = QgsAction(QgsAction.GenericPython, 'test_action', '', 'i=1', False, actionScopes={'Feature'})
131+
self.manager.addAction(action1)
132+
action2 = QgsAction(QgsAction.GenericPython, 'test_action2', 'i=2')
133+
self.manager.addAction(action2)
159134

160135
# initially should be not set
161-
self.assertEqual(self.manager.defaultAction(), -1)
136+
self.assertFalse(self.manager.defaultAction('Feature').isValid())
162137

163138
# set bad default action
164-
self.manager.setDefaultAction(10)
165-
self.assertEqual(self.manager.defaultAction(), -1)
139+
self.manager.setDefaultAction('Feature', QUuid.createUuid())
140+
self.assertFalse(self.manager.defaultAction('Feature').isValid())
166141

167142
# set good default action
168-
self.manager.setDefaultAction(1)
169-
self.assertEqual(self.manager.defaultAction(), 1)
143+
self.manager.setDefaultAction('Feature', action1.id())
144+
self.assertTrue(self.manager.defaultAction('Feature').isValid())
145+
self.assertEquals(self.manager.defaultAction('Feature').id(), action1.id())
146+
self.assertNotEquals(self.manager.defaultAction('Feature').id(), action2.id())
170147

171148
# if default action is removed, should be reset to -1
172149
self.manager.clearActions()
173-
self.assertEqual(self.manager.defaultAction(), -1)
150+
self.assertFalse(self.manager.defaultAction('Feature').isValid())
174151

175152
def check_action_result(self, temp_file):
176153
with open(temp_file, 'r') as result:
@@ -185,7 +162,7 @@ def testDoAction(self):
185162

186163
# simple action
187164
temp_file = self.get_temp_filename()
188-
self.manager.addAction(QgsAction.Unix, 'test_action', self.create_action(temp_file, 'test output'))
165+
id1 = self.manager.addAction(QgsAction.Unix, 'test_action', self.create_action(temp_file, 'test output'))
189166

190167
fields = QgsFields()
191168
fields.append(QgsField('my_field'))
@@ -195,28 +172,29 @@ def testDoAction(self):
195172
f.setAttributes([5, 'val'])
196173

197174
c = QgsExpressionContext()
198-
self.manager.doAction(0, f, c)
175+
self.manager.doAction(id1, f, c)
199176
time.sleep(0.5)
200177

201-
self.assertEqual(self.check_action_result(temp_file), 'test output')
178+
self.assertEquals(self.check_action_result(temp_file), 'test output')
202179

203180
# action with substitutions
204181
temp_file = self.get_temp_filename()
205-
self.manager.addAction(QgsAction.Unix, 'test_action', self.create_action(temp_file, 'test [% $id %] output [% @layer_name %]'))
206-
self.manager.doAction(1, f, c)
182+
id2 = self.manager.addAction(QgsAction.Unix, 'test_action', self.create_action(temp_file, 'test [% $id %] output [% @layer_name %]'))
183+
self.manager.doAction(id2, f, c)
207184
time.sleep(0.5)
208185

209-
self.assertEqual(self.check_action_result(temp_file), 'test 1 output test_layer')
186+
self.assertEquals(self.check_action_result(temp_file), 'test 1 output test_layer')
210187

211188
# test doAction using field variant
212189
temp_file = self.get_temp_filename()
213-
self.manager.addAction(QgsAction.Unix, 'test_action', self.create_action(temp_file, 'test [% @current_field %]'))
214-
self.manager.doActionFeature(2, f, 0)
190+
id3 = self.manager.addAction(QgsAction.Unix, 'test_action',
191+
self.create_action(temp_file, 'test : [% @field_index %] : [% @field_name %] : [% @field_value%]'))
192+
self.manager.doActionFeature(id3, f, 0)
215193
time.sleep(0.5)
216-
self.assertEqual(self.check_action_result(temp_file), 'test 5')
217-
self.manager.doActionFeature(2, f, 1)
194+
self.assertEquals(self.check_action_result(temp_file), 'test : 0 : my_field : 5')
195+
self.manager.doActionFeature(id3, f, 1)
218196
time.sleep(0.5)
219-
self.assertEqual(self.check_action_result(temp_file), 'test val')
197+
self.assertEquals(self.check_action_result(temp_file), 'test : 1 : my_other_field : val')
220198

221199
if __name__ == '__main__':
222200
unittest.main()

0 commit comments

Comments
 (0)