Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add click_x and click_y variables for right click triggered actions, Fix #16852 #5071

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions python/core/qgsactionmanager.sip
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ Constructor
.. versionadded:: 3.0
%End

void doActionWithContext( const QUuid &actionId,
const QgsFeature &feat,
QgsExpressionContextScope *actionScope,
int defaultValueIndex = 0 );
%Docstring
Does the action like doAction() providing
action context scope.
%End

void doAction( const QUuid &actionId, const QgsFeature &feature, int defaultValueIndex = 0 ) /PyName=doActionFeature/;
%Docstring
Does the given action. defaultValueIndex is the index of the
Expand Down
5 changes: 5 additions & 0 deletions python/gui/qgsactionmenu.sip
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class QgsActionMenu : QMenu
as long as the menu is displayed and the action is running.
%End

void setExpressionContextScope( const QgsExpressionContextScope &expressionContextScope );
%Docstring
Set expression context scope.
%End

signals:
void reinit();

Expand Down
5 changes: 5 additions & 0 deletions python/gui/qgsidentifymenu.sip
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ remove all custom actions from the menu to be built
:rtype: list of QgsMapToolIdentify.IdentifyResult
%End

void setExpressionContextScope( const QgsExpressionContextScope &expressionContextScope );
%Docstring
Set expression context scope.
%End

protected:
virtual void closeEvent( QCloseEvent *e );

Expand Down
10 changes: 9 additions & 1 deletion src/app/qgsidentifyresultsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,11 @@ void QgsIdentifyResultsDialog::activate()
}
}

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

void QgsIdentifyResultsDialog::deactivate()
{
Q_FOREACH ( QgsHighlight *h, mHighlights )
Expand Down Expand Up @@ -1244,8 +1249,11 @@ void QgsIdentifyResultsDialog::doAction( QTreeWidgetItem *item, const QString &a
}
}

// Add action context scope.
QgsExpressionContextScope *actionScope = new QgsExpressionContextScope( mExpressionContextScope );

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

void QgsIdentifyResultsDialog::doMapLayerAction( QTreeWidgetItem *item, QgsMapLayerAction *action )
Expand Down
9 changes: 9 additions & 0 deletions src/app/qgsidentifyresultsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsmaptoolidentify.h"
#include "qgswebview.h"
#include "qgsexpressioncontext.h"

#include <QWidget>
#include <QList>
Expand Down Expand Up @@ -150,6 +151,11 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti
//! Map tool was activated
void activate();

/**
* \brief Set expression context scope.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add \since QGIS 3.0?

void setExpressionContextScope( const QgsExpressionContextScope &expressionContextScope );

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

Expand Down Expand Up @@ -260,6 +266,9 @@ class APP_EXPORT QgsIdentifyResultsDialog: public QDialog, private Ui::QgsIdenti

QVector<QgsIdentifyPlotCurve *> mPlotCurves;

// expression context scope.
QgsExpressionContextScope mExpressionContextScope;

void showHelp();
};

Expand Down
14 changes: 14 additions & 0 deletions src/app/qgsmaptoolidentifyaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ void QgsMapToolIdentifyAction::canvasPressEvent( QgsMapMouseEvent *e )

void QgsMapToolIdentifyAction::canvasReleaseEvent( QgsMapMouseEvent *e )
{
// Store clicked x & y into identify menu.
if ( mIdentifyMenu )
{
QgsPointXY point = toMapCoordinates( e->pos() );

QgsExpressionContextScope contextScope;

contextScope.addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_x" ), point.x(), true ) );
contextScope.addVariable( QgsExpressionContextScope::StaticVariable( QString( "click_y" ), point.y(), true ) );

mIdentifyMenu->setExpressionContextScope( contextScope );
resultsDialog()->setExpressionContextScope( contextScope );
}

resultsDialog()->clear();
connect( this, &QgsMapToolIdentifyAction::identifyProgress, QgisApp::instance(), &QgisApp::showProgress );
connect( this, &QgsMapToolIdentifyAction::identifyMessage, QgisApp::instance(), &QgisApp::showStatusMessage );
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ SET(QGIS_CORE_SRCS
qgscolorscheme.cpp
qgscolorschemeregistry.cpp
qgsconditionalstyle.cpp
qgscontextaction.cpp
qgscoordinatereferencesystem.cpp
qgscoordinatetransform.cpp
qgscoordinatetransform_p.cpp
Expand Down Expand Up @@ -576,6 +577,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsanimatedicon.h
qgsauxiliarystorage.h
qgsbrowsermodel.h
qgscontextaction.h
qgscoordinatereferencesystem.h
qgscredentials.h
qgsdataitem.h
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgsactionmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ void QgsActionManager::removeAction( const QUuid &actionId )
}
}

void QgsActionManager::doActionWithContext( const QUuid &actionId, const QgsFeature &feature, QgsExpressionContextScope *ctxScope, int defaultValueIndex )
{
QgsExpressionContext context = createExpressionContext();
QgsExpressionContextScope *actionScope = new QgsExpressionContextScope();
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_index" ), defaultValueIndex, true ) );
if ( defaultValueIndex >= 0 && defaultValueIndex < feature.fields().size() )
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_name" ), feature.fields().at( defaultValueIndex ).name(), true ) );
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "field_value" ), feature.attribute( defaultValueIndex ), true ) );
context << actionScope;
context << ctxScope;
doAction( actionId, feature, context );
}

void QgsActionManager::doAction( const QUuid &actionId, const QgsFeature &feature, int defaultValueIndex )
{
QgsExpressionContext context = createExpressionContext();
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsactionmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ class CORE_EXPORT QgsActionManager: public QObject
*/
void removeAction( const QUuid &actionId );

/**
* Does the action like doAction() providing
* action context scope.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some documentation for parameters? Moreover, \since QGIS 3.0 is required in this case.

void doActionWithContext( const QUuid &actionId,
const QgsFeature &feat,
QgsExpressionContextScope *actionScope,
int defaultValueIndex = 0 );

/**
* Does the given action. defaultValueIndex is the index of the
* field to be used if the action has a $currfield placeholder.
Expand Down
41 changes: 41 additions & 0 deletions src/core/qgscontextaction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/***************************************************************************
qgsactionscope.cpp - QgsActionScope
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


---------------------
begin : 25.09.2017
copyright : (C) 2017 by C. MARCEL
email : clement.marcel@nwanda.fr
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscontextaction.h"

QgsContextAction::QgsContextAction( QObject *parent ):
QAction( parent )
{
}

QgsContextAction::QgsContextAction( const QString &text, QObject *parent ):
QAction( text, parent )
{
}

QgsContextAction::QgsContextAction( const QIcon &icon, const QString &text, QObject *parent ):
QAction( icon, text, parent )
{
}

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

void QgsContextAction::setExpressionContextScope( const QgsExpressionContextScope &expressionContextScope )
{
mExpressionContextScope = expressionContextScope;
}
73 changes: 73 additions & 0 deletions src/core/qgscontextaction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/***************************************************************************
qgsactionscope.h - QgsActionScope
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo


---------------------
begin : 25.09.2017
copyright : (C) 2017 by C. MARCEL
email : clement.marcel@nwanda.fr
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSCONTEXTACTION_H
#define QGSCONTEXTACTION_H

#include "qgis_core.h"
#include <QString>
#include <QAction>
#include "qgsexpressioncontext.h"

#define SIP_NO_FILE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this excluded from sip bindings?


/**
* \ingroup core
* QgsContextAction is derived from QAction and contains a context expression scope.
*
* \since QGIS 3.0
*/
class CORE_EXPORT QgsContextAction: public QAction
{
public:

/**
* Creates a new context action.
*
* \since QGIS 3.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that the whole class is tagged with \since QGIS 3.0, I think that it's not necessary to add another for each underlying methods.

*/
explicit QgsContextAction( QObject *parent );

/**
* Creates a new context action providing action text.
*
* \since QGIS 3.0
*/
QgsContextAction( const QString &text, QObject *parent );

/**
* Creates a new context action providing action text & icon.
*
* \since QGIS 3.0
*/
QgsContextAction( const QIcon &icon, const QString &text, QObject *parent );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some documentation for parameters?


/**
* Action context scope.
*
* \since QGIS 3.0
*/
QgsExpressionContextScope expressionContextScope() const;

/**
* \copydoc expressionContextScope()
*/
void setExpressionContextScope( const QgsExpressionContextScope &expressionContextScope );

private:
QgsExpressionContextScope mExpressionContextScope;
};

#endif // QGSCONTEXTACTION_H
18 changes: 17 additions & 1 deletion src/gui/qgsactionmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgsactionmanager.h"
#include "qgsfeatureiterator.h"
#include "qgsgui.h"
#include "qgscontextaction.h"

QgsActionMenu::QgsActionMenu( QgsVectorLayer *layer, const QgsFeature &feature, const QString &actionScope, QWidget *parent )
: QMenu( parent )
Expand Down Expand Up @@ -63,6 +64,11 @@ void QgsActionMenu::setFeature( const QgsFeature &feature )
mFeature = feature;
}

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

void QgsActionMenu::triggerAction()
{
if ( !feature().isValid() )
Expand Down Expand Up @@ -93,6 +99,15 @@ void QgsActionMenu::triggerAction()
QgsExpressionContextScope *actionScope = new QgsExpressionContextScope();
actionScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "action_scope" ), mActionScope, true ) );
context << actionScope;
context << new QgsExpressionContextScope( mExpressionContextScope );

QgsContextAction *contextAction = dynamic_cast<QgsContextAction *>( action );
if ( contextAction )
{
QgsExpressionContextScope *expressionContextScope = new QgsExpressionContextScope( contextAction->expressionContextScope() );
context << expressionContextScope;
}

QgsAction act = data.actionData.value<QgsAction>();
act.run( context );
}
Expand All @@ -106,9 +121,10 @@ void QgsActionMenu::reloadActions()

Q_FOREACH ( const QgsAction &action, mActions )
{
QAction *qAction = new QAction( action.icon(), action.name(), this );
QgsContextAction *qAction = new QgsContextAction( action.icon(), action.name(), this );
qAction->setData( QVariant::fromValue<ActionData>( ActionData( action, mFeatureId, mLayer ) ) );
qAction->setIcon( action.icon() );
qAction->setExpressionContextScope( mExpressionContextScope );

// Only enable items on supported platforms
if ( !action.runable() )
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsactionmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ class GUI_EXPORT QgsActionMenu : public QMenu
*/
void setFeature( const QgsFeature &feature );

/**
* \brief Set expression context scope.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\since QGIS 3.0

void setExpressionContextScope( const QgsExpressionContextScope &expressionContextScope );

signals:
void reinit();

Expand All @@ -107,6 +112,9 @@ class GUI_EXPORT QgsActionMenu : public QMenu
QgsFeature mFeature;
QgsFeatureId mFeatureId;
QString mActionScope;

// expression context scope.
QgsExpressionContextScope mExpressionContextScope;
};


Expand Down
Loading