Skip to content

Commit c969295

Browse files
committedNov 20, 2017
Fixes #16852 by adding click_x and click_y variables to resolve actions
1 parent 33e939c commit c969295

10 files changed

+109
-9
lines changed
 

‎src/app/qgsidentifyresultsdialog.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ void QgsIdentifyResultsDialog::doAction( QTreeWidgetItem *item, int action )
12371237
}
12381238

12391239
int featIdx = featItem->data( 0, Qt::UserRole + 1 ).toInt();
1240-
layer->actions()->doAction( action, mFeatures[ featIdx ], idx );
1240+
layer->actions()->doAction( action, mFeatures[ featIdx ], idx, mExpressionContextScope );
12411241
}
12421242

12431243
void QgsIdentifyResultsDialog::doMapLayerAction( QTreeWidgetItem *item, QgsMapLayerAction* action )
@@ -1951,6 +1951,16 @@ void QgsIdentifyResultsDialog::formatChanged( int index )
19511951
}
19521952
}
19531953

1954+
void QgsIdentifyResultsDialog::setExpressionContextScope( const QgsExpressionContextScope &scope )
1955+
{
1956+
mExpressionContextScope = scope;
1957+
}
1958+
1959+
QgsExpressionContextScope QgsIdentifyResultsDialog::expressionContextScope() const
1960+
{
1961+
return mExpressionContextScope;
1962+
}
1963+
19541964
/*
19551965
* QgsIdentifyResultsDialogMapLayerAction
19561966
*/

‎src/app/qgsidentifyresultsdialog.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,22 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
152152
/** Map tool was activated */
153153
void activate();
154154

155+
/**
156+
* Sets an expression context scope to consider for resolving underlying
157+
* actions.
158+
*
159+
* @note Added in QGIS 2.18
160+
*/
161+
void setExpressionContextScope( const QgsExpressionContextScope &scope );
162+
163+
/**
164+
* Returns an expression context scope used for resolving underlying
165+
* actions.
166+
*
167+
* @note Added in QGIS 2.18
168+
*/
169+
QgsExpressionContextScope expressionContextScope() const;
170+
155171
signals:
156172
void selectedFeatureChanged( QgsVectorLayer *, QgsFeatureId featureId );
157173

@@ -263,6 +279,8 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
263279
QgsDockWidget *mDock;
264280

265281
QVector<QgsIdentifyPlotCurve *> mPlotCurves;
282+
283+
QgsExpressionContextScope mExpressionContextScope;
266284
};
267285

268286
class QgsIdentifyResultsDialogMapLayerAction : public QAction

‎src/app/qgsmaptoolidentifyaction.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QgsMapMouseEvent* e )
118118
connect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
119119
connect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );
120120

121+
setClickContextScope( toMapCoordinates( e->pos() ) );
122+
121123
identifyMenu()->setResultsIfExternalAction( false );
122124

123125
// enable the right click for extended menu so it behaves as a contextual menu
@@ -200,4 +202,16 @@ void QgsMapToolIdentifyAction::handleCopyToClipboard( QgsFeatureStore & featureS
200202
emit copyToClipboard( featureStore );
201203
}
202204

205+
void QgsMapToolIdentifyAction::setClickContextScope( const QgsPoint &point )
206+
{
207+
QgsExpressionContextScope clickScope;
208+
clickScope.addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_x" ), point.x(), true ) );
209+
clickScope.addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_y" ), point.y(), true ) );
210+
211+
resultsDialog()->setExpressionContextScope( clickScope );
203212

213+
if ( mIdentifyMenu )
214+
{
215+
mIdentifyMenu->setExpressionContextScope( clickScope );
216+
}
217+
}

‎src/app/qgsmaptoolidentifyaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class APP_EXPORT QgsMapToolIdentifyAction : public QgsMapToolIdentify
8282

8383
virtual QGis::UnitType displayDistanceUnits() const override;
8484
virtual QgsUnitTypes::AreaUnit displayAreaUnits() const override;
85-
85+
void setClickContextScope( const QgsPoint &point );
8686
};
8787

8888
#endif

‎src/core/qgsactionmanager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ void QgsActionManager::removeAction( int index )
6363
}
6464
}
6565

66-
void QgsActionManager::doAction( int index, const QgsFeature& feat, int defaultValueIndex )
66+
void QgsActionManager::doAction( int index, const QgsFeature& feat, int defaultValueIndex, const QgsExpressionContextScope &scope )
6767
{
6868
QgsExpressionContext context = createExpressionContext();
69-
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope();
69+
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope( scope );
7070
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QString( "current_field" ), feat.attribute( defaultValueIndex ), true ) );
7171
context << actionScope;
7272
doAction( index, feat, context );

‎src/core/qgsactionmanager.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,20 @@ class CORE_EXPORT QgsActionManager
7878
//! Remove an action at given index
7979
void removeAction( int index );
8080

81-
/** Does the given values. defaultValueIndex is the index of the
82-
* field to be used if the action has a $currfield placeholder.
83-
* @note available in python bindings as doActionFeature
81+
/**
82+
* Does the given action.
83+
*
84+
* @param actionId action id
85+
* @param feature feature to run action for
86+
* @param defaultValueIndex index of the field to be used if the action has a $currfield placeholder.
87+
* @param scope expression context scope to add during expression evaluation
88+
*
89+
* @note available in python bindings as doActionFeature
8490
*/
8591
void doAction( int index,
8692
const QgsFeature &feat,
87-
int defaultValueIndex = 0 );
93+
int defaultValueIndex = 0,
94+
const QgsExpressionContextScope &scope = QgsExpressionContextScope() );
8895

8996
/** Does the action using the expression engine to replace any embedded expressions
9097
* in the action definition.

‎src/gui/qgsactionmenu.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void QgsActionMenu::triggerAction()
106106
}
107107
else if ( data.actionType == AttributeAction )
108108
{
109-
mActions->doAction( data.actionId.id, *feature() );
109+
mActions->doAction( data.actionId.id, *feature(), 0, mExpressionContextScope );
110110
}
111111
}
112112

@@ -159,3 +159,12 @@ void QgsActionMenu::reloadActions()
159159
emit reinit();
160160
}
161161

162+
void QgsActionMenu::setExpressionContextScope( const QgsExpressionContextScope &scope )
163+
{
164+
mExpressionContextScope = scope;
165+
}
166+
167+
QgsExpressionContextScope QgsActionMenu::expressionContextScope() const
168+
{
169+
return mExpressionContextScope;
170+
}

‎src/gui/qgsactionmenu.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ class GUI_EXPORT QgsActionMenu : public QMenu
108108
*/
109109
void setFeature( QgsFeature* feature );
110110

111+
/**
112+
* Sets an expression context scope used to resolve underlying actions.
113+
*
114+
* @note Added in QGIS 2.18
115+
*/
116+
void setExpressionContextScope( const QgsExpressionContextScope &scope );
117+
118+
/**
119+
* Returns an expression context scope used to resolve underlying actions.
120+
*
121+
* @note Added in QGIS 2.18
122+
*/
123+
QgsExpressionContextScope expressionContextScope() const;
124+
111125
private slots:
112126
void triggerAction();
113127
void reloadActions();
@@ -124,6 +138,7 @@ class GUI_EXPORT QgsActionMenu : public QMenu
124138
const QgsFeature* mFeature;
125139
QgsFeatureId mFeatureId;
126140
bool mOwnsFeature;
141+
QgsExpressionContextScope mExpressionContextScope;
127142
};
128143

129144
Q_DECLARE_METATYPE( QgsActionMenu::ActionData )

‎src/gui/qgsidentifymenu.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer* layer, const QList<QgsMapT
349349
if ( mShowFeatureActions )
350350
{
351351
featureActionMenu = new QgsActionMenu( layer, result.mFeature.id(), layerMenu );
352+
featureActionMenu->setExpressionContextScope( mExpressionContextScope );
352353
}
353354

354355
// feature title
@@ -643,3 +644,13 @@ void QgsIdentifyMenu::removeCustomActions()
643644
mCustomActionRegistry.clear();
644645

645646
}
647+
648+
void QgsIdentifyMenu::setExpressionContextScope( const QgsExpressionContextScope &scope )
649+
{
650+
mExpressionContextScope = scope;
651+
}
652+
653+
QgsExpressionContextScope QgsIdentifyMenu::expressionContextScope() const
654+
{
655+
return mExpressionContextScope;
656+
}

‎src/gui/qgsidentifymenu.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,20 @@ class GUI_EXPORT QgsIdentifyMenu : public QMenu
148148
*/
149149
QList<QgsMapToolIdentify::IdentifyResult> exec( const QList<QgsMapToolIdentify::IdentifyResult>& idResults, QPoint pos );
150150

151+
/**
152+
* Sets an expression context scope used to resolve underlying actions.
153+
*
154+
* @note Added in QGIS 2.18
155+
*/
156+
void setExpressionContextScope( const QgsExpressionContextScope &scope );
157+
158+
/**
159+
* Returns an expression context scope used to resolve underlying actions.
160+
*
161+
* @note Added in QGIS 2.18
162+
*/
163+
QgsExpressionContextScope expressionContextScope() const;
164+
151165
protected:
152166
virtual void closeEvent( QCloseEvent *e ) override;
153167

@@ -178,6 +192,8 @@ class GUI_EXPORT QgsIdentifyMenu : public QMenu
178192
int mMaxLayerDisplay;
179193
int mMaxFeatureDisplay;
180194

195+
QgsExpressionContextScope mExpressionContextScope;
196+
181197
// name of the action to be displayed for feature default action, if other actions are shown
182198
QString mDefaultActionName;
183199

0 commit comments

Comments
 (0)
Please sign in to comment.