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
[FEATURE] Add is_selected and num_selected expressions #3681
Conversation
@@ -2172,7 +2172,7 @@ class TestQgsExpression: public QObject | |||
QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, QgsFields() ); | |||
|
|||
QVariantList builderExpected; | |||
QCOMPARE( QgsExpression( "array()" ).evaluate( &context ), QVariant( ) ); | |||
QCOMPARE( QgsExpression( "array()" ).evaluate( &context ), QVariant( builderExpected ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pvalsecc I assume it's expected that array()
returns an emty QVariantList()
and not an invalid QVariant()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that would be better.
wonderful feature... was in my plan since a lot of time, but I haven't to work on. I didn't follow in detail the evolution of QgsExpressionContext, since when the QgsExpressionContext.variable( "layer" ) is available is available? |
layer_id and layer_name for quite some time. |
Thanks for this new feature @m-kuhn. Looking forward master availability in osgeo4w Would that sound reasonable to have among the expression functions a list of the layers in the project? There are more and more functions that access layers, but user still needs to type its name or id (instead of simple double-click, as done with fields, values and functions). |
* is_selected() returns if the current feature is selected * num_selected() returns the number of selected features on the current layer * is_selected(layer, feature) returns if the "feature" is selected. "feature" must be on "layer". * num_selected(layer) returns the number of selected features on "layer"
Here's an idea: copying the list box that shows up when the user selects a field to insert, a list of layers could pop up when the user selects a function that relies on layer names. |
@@ -312,14 +312,19 @@ static QgsExpression::Node* getNode( const QVariant& value, QgsExpression* paren | |||
|
|||
QgsVectorLayer* getVectorLayer( const QVariant& value, QgsExpression* ) | |||
{ | |||
QString layerString = value.toString(); | |||
QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerString ) ); //search by id first | |||
QgsVectorLayer* vl = value.value<QgsVectorLayer*>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this safe? What if the layer has been deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You beat me to it
#3683
"description": "Returns if a feature is selected. If called with no parameters checks the current feature.", | ||
"arguments": [ | ||
{"arg":"feature","description":"The feature which should be checked for selection"}, | ||
{"arg":"layer","description":"The layer (or its id or name) on which the selection will be checked"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check out the newly introduced array _to_string help - there's a proper way to mark optional arguments in the help so that there are styled correctly
must be on "layer".