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

[FEATURE] Add is_selected and num_selected expressions #3681

Merged
merged 2 commits into from Oct 27, 2016

Conversation

m-kuhn
Copy link
Member

@m-kuhn m-kuhn commented Oct 27, 2016

  • 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"

@@ -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 ) );
Copy link
Member Author

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()?

Copy link
Contributor

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.

@luipir
Copy link
Contributor

luipir commented Oct 27, 2016

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?

@m-kuhn
Copy link
Member Author

m-kuhn commented Oct 27, 2016

layer_id and layer_name for quite some time.
layer... since after this PR ;)

@DelazJ
Copy link
Contributor

DelazJ commented Oct 27, 2016

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"
@nirvn
Copy link
Contributor

nirvn commented Oct 27, 2016

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.

@m-kuhn m-kuhn merged commit 01f3c9a into qgis:master Oct 27, 2016
@@ -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*>();
Copy link
Collaborator

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?

Copy link
Member Author

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"}
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants