Skip to content

Commit

Permalink
Merge pull request #6982 from elpaso/currentformfeature-expressions-4
Browse files Browse the repository at this point in the history
[feature][need-docs] Current feature/current value form context expressions
  • Loading branch information
elpaso committed May 16, 2018
2 parents c27ce39 + b2fb029 commit a44eeae
Show file tree
Hide file tree
Showing 33 changed files with 1,064 additions and 90 deletions.
8 changes: 8 additions & 0 deletions python/core/auto_generated/expression/qgsexpression.sip.in
Expand Up @@ -178,6 +178,14 @@ which is determined at runtime.
.. versionadded:: 3.0
%End

QSet<QString> referencedFunctions() const;
%Docstring
Return a list of the names of all functions which are used in this expression.

.. versionadded:: 3.2
%End


QSet<int> referencedAttributeIndexes( const QgsFields &fields ) const;
%Docstring
Return a list of field name indexes obtained from the provided fields.
Expand Down
Expand Up @@ -196,6 +196,11 @@ to evaluate child nodes.
virtual QSet<QString> referencedVariables() const = 0;
%Docstring
Return a set of all variables which are used in this expression.
%End

virtual QSet<QString> referencedFunctions() const = 0;
%Docstring
Return a set of all functions which are used in this expression.
%End

virtual bool needsGeometry() const = 0;
Expand Down
17 changes: 13 additions & 4 deletions python/core/auto_generated/expression/qgsexpressionnodeimpl.sip.in
Expand Up @@ -55,7 +55,7 @@ Returns the node the operator will operate upon.

virtual QSet<QString> referencedVariables() const;

virtual bool needsGeometry() const;
virtual QSet<QString> referencedFunctions() const;

virtual QgsExpressionNode *clone() const /Factory/;

Expand Down Expand Up @@ -154,6 +154,8 @@ Returns the node to the right of the operator.

virtual QSet<QString> referencedVariables() const;

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;
Expand Down Expand Up @@ -224,7 +226,7 @@ Returns the list of nodes to search for matching values within.

virtual QSet<QString> referencedVariables() const;

virtual bool needsGeometry() const;
virtual QSet<QString> referencedFunctions() const;

virtual QgsExpressionNode *clone() const /Factory/;

Expand Down Expand Up @@ -275,7 +277,8 @@ Returns a list of arguments specified for the function.

virtual QSet<QString> referencedVariables() const;

virtual bool needsGeometry() const;
virtual QSet<QString> referencedFunctions() const;


virtual QgsExpressionNode *clone() const /Factory/;

Expand Down Expand Up @@ -323,7 +326,8 @@ The value of the literal.

virtual QSet<QString> referencedVariables() const;

virtual bool needsGeometry() const;
virtual QSet<QString> referencedFunctions() const;


virtual QgsExpressionNode *clone() const /Factory/;

Expand Down Expand Up @@ -367,6 +371,8 @@ The name of the column.

virtual QSet<QString> referencedVariables() const;

virtual QSet<QString> referencedFunctions() const;

virtual bool needsGeometry() const;


Expand Down Expand Up @@ -466,6 +472,9 @@ The ELSE expression used for the condition.

virtual QSet<QString> referencedVariables() const;

virtual QSet<QString> referencedFunctions() const;


virtual bool needsGeometry() const;

virtual QgsExpressionNode *clone() const /Factory/;
Expand Down
Expand Up @@ -8,6 +8,7 @@




class QgsValueRelationFieldFormatter : QgsFieldFormatter
{
%Docstring
Expand Down Expand Up @@ -56,25 +57,70 @@ Constructor for QgsValueRelationFieldFormatter.
virtual QVariant createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const;


static QgsValueRelationFieldFormatter::ValueRelationCache createCache( const QVariantMap &config );
static QStringList valueToStringList( const QVariant &value );
%Docstring
Utility to convert an array or a string representation of an array ``value`` to a string list

.. versionadded:: 3.2
%End

static QgsValueRelationFieldFormatter::ValueRelationCache createCache( const QVariantMap &config, const QgsFeature &formFeature = QgsFeature() );
%Docstring
Create a cache for a value relation field.
This can be used to keep the value map in the local memory
if doing multiple lookups in a loop.

:param config: The widget configuration
:param formFeature: The feature currently being edited with current attribute values

:return: A kvp list of values for the widget

.. versionadded:: 3.0
%End

static QStringList valueToStringList( const QVariant &value );
static bool expressionRequiresFormScope( const QString &expression );
%Docstring
Utility to convert an array or a string representation of and array ``value`` to a string list
Check if the ``expression`` requires a form scope (i.e. if it uses fields
or geometry of the currently edited feature).

:param value: The value to be converted
:param expression: The widget's filter expression

:return: A string list
:return: true if the expression requires a form scope

.. versionadded:: 3.2
%End

static QSet<QString> expressionFormAttributes( const QString &expression );
%Docstring
Return a list of attributes required by the form context ``expression``

:param expression: Form filter expression

:return: list of attributes required by the expression

.. versionadded:: 3.2
%End

static QSet<QString> expressionFormVariables( const QString &expression );
%Docstring
Return a list of variables required by the form context ``expression``

:param expression: Form filter expression

:return: list of variables required by the expression

.. versionadded:: 3.2
%End

static bool expressionIsUsable( const QString &expression, const QgsFeature &feature );
%Docstring
Check whether the ``feature`` has all values required by the ``expression``

:return: True if the expression can be used

.. versionadded:: 3.2
%End

};


Expand Down
8 changes: 8 additions & 0 deletions python/core/auto_generated/qgsexpressioncontext.sip.in
Expand Up @@ -763,6 +763,14 @@ Creates a new scope which contains variables and functions relating to the globa
For instance, QGIS version numbers and variables specified through QGIS options.

.. seealso:: :py:func:`setGlobalVariable`
%End

static QgsExpressionContextScope *formScope( const QgsFeature &formFeature = QgsFeature( ) ) /Factory/;
%Docstring
Creates a new scope which contains functions and variables from the current attribute form/table feature.
The variables and values in this scope will reflect the current state of the form/row being edited.

.. versionadded:: 3.2
%End

static void setGlobalVariable( const QString &name, const QVariant &value );
Expand Down
Expand Up @@ -213,6 +213,7 @@ This will be disabled when the form is not editable.
.. versionadded:: 3.0
%End


signals:

void valueChanged( const QVariant &value );
Expand Down Expand Up @@ -279,6 +280,34 @@ change the visual cue.
.. versionadded:: 2.16
%End


QgsFeature formFeature() const;
%Docstring
The feature currently being edited, in its current state

:return: the feature currently being edited, in its current state

.. versionadded:: 3.2
%End

void setFormFeature( const QgsFeature &feature );
%Docstring
Set the feature currently being edited to ``feature``

.. versionadded:: 3.2
%End

bool setFormFeatureAttribute( const QString &attributeName, const QVariant &attributeValue );
%Docstring
Update the feature currently being edited by changing its
attribute ``attributeName`` to ``attributeValue``

:return: bool true on success

.. versionadded:: 3.2
%End


};


Expand Down
19 changes: 19 additions & 0 deletions python/gui/auto_generated/qgsattributeeditorcontext.sip.in
Expand Up @@ -180,6 +180,25 @@ QGIS forms

const QgsAttributeEditorContext *parentContext() const;

QgsFeature formFeature() const;
%Docstring
Return current feature from the currently edited form or table row

.. seealso:: :py:func:`setFormFeature`

.. versionadded:: 3.2
%End

void setFormFeature( const QgsFeature &feature );
%Docstring
Set current ``feature`` for the currently edited form or table row

.. seealso:: :py:func:`formFeature`

.. versionadded:: 3.2
%End


};

/************************************************************************
Expand Down
3 changes: 2 additions & 1 deletion python/gui/auto_generated/qgsattributeform.sip.in
Expand Up @@ -141,7 +141,8 @@ on all attribute widgets.

void attributeChanged( const QString &attribute, const QVariant &value ) /Deprecated/;
%Docstring
Notifies about changes of attributes
Notifies about changes of attributes, this signal is not emitted when the value is set
back to the original one.

:param attribute: The name of the attribute that changed.
:param value: The new value of the attribute.
Expand Down
7 changes: 7 additions & 0 deletions resources/function_help/json/current_value
@@ -0,0 +1,7 @@
{
"name": "current_value",
"type": "function",
"description": "Returns the current, unsaved value of a field in the form or table row currently being edited. This will differ from the feature's actual attribute values for features which are currently being edited or have not yet been added to a layer.",
"arguments": [ {"arg":"field_name","description":"a field name in the current form or table row"}],
"examples": [ { "expression":"current_value( 'FIELD_NAME' )","returns":"The current value of field 'FIELD_NAME'."} ]
}
23 changes: 23 additions & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -276,6 +276,14 @@ QSet<QString> QgsExpression::referencedVariables() const
return d->mRootNode->referencedVariables();
}

QSet<QString> QgsExpression::referencedFunctions() const
{
if ( !d->mRootNode )
return QSet<QString>();

return d->mRootNode->referencedFunctions();
}

QSet<int> QgsExpression::referencedAttributeIndexes( const QgsFields &fields ) const
{
if ( !d->mRootNode )
Expand Down Expand Up @@ -771,6 +779,10 @@ void QgsExpression::initVariableHelp()

//provider notification
sVariableHelpTexts.insert( QStringLiteral( "notification_message" ), QCoreApplication::translate( "notification_message", "Content of the notification message sent by the provider (available only for actions triggered by provider notifications)." ) );

//form context variable
sVariableHelpTexts.insert( QStringLiteral( "current_geometry" ), QCoreApplication::translate( "current_geometry", "Represents the geometry of the feature currently being edited in the form or the table row. Can be used for in a form/row context to filter the related features." ) );
sVariableHelpTexts.insert( QStringLiteral( "current_feature" ), QCoreApplication::translate( "current_feature", "Represents the feature currently being edited in the form or the table row. Can be used for in a form/row context to filter the related features." ) );
}

QString QgsExpression::variableHelpText( const QString &variableName )
Expand Down Expand Up @@ -945,3 +957,14 @@ bool QgsExpression::isField() const
{
return d->mRootNode && d->mRootNode->nodeType() == QgsExpressionNode::ntColumnRef;
}

QList<const QgsExpressionNode *> QgsExpression::nodes() const
{
if ( !d->mRootNode )
return QList<const QgsExpressionNode *>();

return d->mRootNode->nodes();
}



40 changes: 39 additions & 1 deletion src/core/expression/qgsexpression.h
Expand Up @@ -29,6 +29,7 @@
#include "qgis.h"
#include "qgsunittypes.h"
#include "qgsinterval.h"
#include "qgsexpressionnode.h"

class QgsFeature;
class QgsGeometry;
Expand All @@ -41,7 +42,6 @@ class QgsDistanceArea;
class QDomElement;
class QgsExpressionContext;
class QgsExpressionPrivate;
class QgsExpressionNode;
class QgsExpressionFunction;

/**
Expand Down Expand Up @@ -258,6 +258,44 @@ class CORE_EXPORT QgsExpression
*/
QSet<QString> referencedVariables() const;

/**
* Return a list of the names of all functions which are used in this expression.
*
* \since QGIS 3.2
*/
QSet<QString> referencedFunctions() const;

#ifndef SIP_RUN

/**
* Return a list of all nodes which are used in this expression
*
* \note not available in Python bindings
* \since QGIS 3.2
*/
QList<const QgsExpressionNode *> nodes( ) const;

/**
* Return a list of all nodes of the given class which are used in this expression
*
* \note not available in Python bindings
* \since QGIS 3.2
*/
template <class T>
QList<const T *> findNodes( ) const
{
QList<const T *> lst;
const QList<const QgsExpressionNode *> allNodes( nodes() );
for ( const auto &node : allNodes )
{
const T *n = dynamic_cast<const T *>( node );
if ( n )
lst << n;
}
return lst;
}
#endif

/**
* Return a list of field name indexes obtained from the provided fields.
*
Expand Down
1 change: 1 addition & 0 deletions src/core/expression/qgsexpressionnode.cpp
Expand Up @@ -57,3 +57,4 @@ void QgsExpressionNode::cloneTo( QgsExpressionNode *target ) const
target->parserFirstColumn = parserFirstColumn;
target->parserFirstLine = parserFirstLine;
}

0 comments on commit a44eeae

Please sign in to comment.