Skip to content
Permalink
Browse files

Fixes #16852 by adding click_x and click_y variables to resolve actions

  • Loading branch information
pblottiere committed Nov 16, 2017
1 parent 33e939c commit c9692959459a2e90e9170b8b08f6cb12e91f2c89
@@ -1237,7 +1237,7 @@ void QgsIdentifyResultsDialog::doAction( QTreeWidgetItem *item, int action )
}

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

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

void QgsIdentifyResultsDialog::setExpressionContextScope( const QgsExpressionContextScope &scope )
{
mExpressionContextScope = scope;
}

QgsExpressionContextScope QgsIdentifyResultsDialog::expressionContextScope() const
{
return mExpressionContextScope;
}

/*
* QgsIdentifyResultsDialogMapLayerAction
*/
@@ -152,6 +152,22 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
/** Map tool was activated */
void activate();

/**
* Sets an expression context scope to consider for resolving underlying
* actions.
*
* @note Added in QGIS 2.18
*/
void setExpressionContextScope( const QgsExpressionContextScope &scope );

/**
* Returns an expression context scope used for resolving underlying
* actions.
*
* @note Added in QGIS 2.18
*/
QgsExpressionContextScope expressionContextScope() const;

signals:
void selectedFeatureChanged( QgsVectorLayer *, QgsFeatureId featureId );

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

QVector<QgsIdentifyPlotCurve *> mPlotCurves;

QgsExpressionContextScope mExpressionContextScope;
};

class QgsIdentifyResultsDialogMapLayerAction : public QAction
@@ -118,6 +118,8 @@ void QgsMapToolIdentifyAction::canvasReleaseEvent( QgsMapMouseEvent* e )
connect( this, SIGNAL( identifyProgress( int, int ) ), QgisApp::instance(), SLOT( showProgress( int, int ) ) );
connect( this, SIGNAL( identifyMessage( QString ) ), QgisApp::instance(), SLOT( showStatusMessage( QString ) ) );

setClickContextScope( toMapCoordinates( e->pos() ) );

identifyMenu()->setResultsIfExternalAction( false );

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

void QgsMapToolIdentifyAction::setClickContextScope( const QgsPoint &point )
{
QgsExpressionContextScope clickScope;
clickScope.addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_x" ), point.x(), true ) );
clickScope.addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_y" ), point.y(), true ) );

resultsDialog()->setExpressionContextScope( clickScope );

if ( mIdentifyMenu )
{
mIdentifyMenu->setExpressionContextScope( clickScope );
}
}
@@ -82,7 +82,7 @@ class APP_EXPORT QgsMapToolIdentifyAction : public QgsMapToolIdentify

virtual QGis::UnitType displayDistanceUnits() const override;
virtual QgsUnitTypes::AreaUnit displayAreaUnits() const override;

void setClickContextScope( const QgsPoint &point );
};

#endif
@@ -63,10 +63,10 @@ void QgsActionManager::removeAction( int index )
}
}

void QgsActionManager::doAction( int index, const QgsFeature& feat, int defaultValueIndex )
void QgsActionManager::doAction( int index, const QgsFeature& feat, int defaultValueIndex, const QgsExpressionContextScope &scope )
{
QgsExpressionContext context = createExpressionContext();
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope();
QgsExpressionContextScope* actionScope = new QgsExpressionContextScope( scope );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QString( "current_field" ), feat.attribute( defaultValueIndex ), true ) );
context << actionScope;
doAction( index, feat, context );
@@ -78,13 +78,20 @@ class CORE_EXPORT QgsActionManager
//! Remove an action at given index
void removeAction( int index );

/** Does the given values. defaultValueIndex is the index of the
* field to be used if the action has a $currfield placeholder.
* @note available in python bindings as doActionFeature
/**
* Does the given action.
*
* @param actionId action id
* @param feature feature to run action for
* @param defaultValueIndex index of the field to be used if the action has a $currfield placeholder.
* @param scope expression context scope to add during expression evaluation
*
* @note available in python bindings as doActionFeature
*/
void doAction( int index,
const QgsFeature &feat,
int defaultValueIndex = 0 );
int defaultValueIndex = 0,
const QgsExpressionContextScope &scope = QgsExpressionContextScope() );

/** Does the action using the expression engine to replace any embedded expressions
* in the action definition.
@@ -106,7 +106,7 @@ void QgsActionMenu::triggerAction()
}
else if ( data.actionType == AttributeAction )
{
mActions->doAction( data.actionId.id, *feature() );
mActions->doAction( data.actionId.id, *feature(), 0, mExpressionContextScope );
}
}

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

void QgsActionMenu::setExpressionContextScope( const QgsExpressionContextScope &scope )
{
mExpressionContextScope = scope;
}

QgsExpressionContextScope QgsActionMenu::expressionContextScope() const
{
return mExpressionContextScope;
}
@@ -108,6 +108,20 @@ class GUI_EXPORT QgsActionMenu : public QMenu
*/
void setFeature( QgsFeature* feature );

/**
* Sets an expression context scope used to resolve underlying actions.
*
* @note Added in QGIS 2.18
*/
void setExpressionContextScope( const QgsExpressionContextScope &scope );

/**
* Returns an expression context scope used to resolve underlying actions.
*
* @note Added in QGIS 2.18
*/
QgsExpressionContextScope expressionContextScope() const;

private slots:
void triggerAction();
void reloadActions();
@@ -124,6 +138,7 @@ class GUI_EXPORT QgsActionMenu : public QMenu
const QgsFeature* mFeature;
QgsFeatureId mFeatureId;
bool mOwnsFeature;
QgsExpressionContextScope mExpressionContextScope;
};

Q_DECLARE_METATYPE( QgsActionMenu::ActionData )
@@ -349,6 +349,7 @@ void QgsIdentifyMenu::addVectorLayer( QgsVectorLayer* layer, const QList<QgsMapT
if ( mShowFeatureActions )
{
featureActionMenu = new QgsActionMenu( layer, result.mFeature.id(), layerMenu );
featureActionMenu->setExpressionContextScope( mExpressionContextScope );
}

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

}

void QgsIdentifyMenu::setExpressionContextScope( const QgsExpressionContextScope &scope )
{
mExpressionContextScope = scope;
}

QgsExpressionContextScope QgsIdentifyMenu::expressionContextScope() const
{
return mExpressionContextScope;
}
@@ -148,6 +148,20 @@ class GUI_EXPORT QgsIdentifyMenu : public QMenu
*/
QList<QgsMapToolIdentify::IdentifyResult> exec( const QList<QgsMapToolIdentify::IdentifyResult>& idResults, QPoint pos );

/**
* Sets an expression context scope used to resolve underlying actions.
*
* @note Added in QGIS 2.18
*/
void setExpressionContextScope( const QgsExpressionContextScope &scope );

/**
* Returns an expression context scope used to resolve underlying actions.
*
* @note Added in QGIS 2.18
*/
QgsExpressionContextScope expressionContextScope() const;

protected:
virtual void closeEvent( QCloseEvent *e ) override;

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

QgsExpressionContextScope mExpressionContextScope;

// name of the action to be displayed for feature default action, if other actions are shown
QString mDefaultActionName;

0 comments on commit c969295

Please sign in to comment.
You can’t perform that action at this time.