QGIS API Documentation 3.43.0-Master (e01d6d7c4c0)
qgsalgorithmcalculateexpression.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcalculateexpression.cpp
3 ---------------------
4 begin : August 2023
5 copyright : (C) 2023 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
21
22QString QgsCalculateExpressionAlgorithm::name() const
23{
24 return QStringLiteral( "calculateexpression" );
25}
26
27Qgis::ProcessingAlgorithmFlags QgsCalculateExpressionAlgorithm::flags() const
28{
30}
31
32QString QgsCalculateExpressionAlgorithm::displayName() const
33{
34 return QObject::tr( "Calculate expression" );
35}
36
37QStringList QgsCalculateExpressionAlgorithm::tags() const
38{
39 return QObject::tr( "evaluate,variable,store" ).split( ',' );
40}
41
42QString QgsCalculateExpressionAlgorithm::group() const
43{
44 return QObject::tr( "Modeler tools" );
45}
46
47QString QgsCalculateExpressionAlgorithm::groupId() const
48{
49 return QStringLiteral( "modelertools" );
50}
51
52QString QgsCalculateExpressionAlgorithm::shortHelpString() const
53{
54 return QObject::tr( "This algorithm calculates the result of a QGIS expression and makes it available for use in other parts of the model." );
55}
56
57QString QgsCalculateExpressionAlgorithm::shortDescription() const
58{
59 return QObject::tr( "Calculates the result of a QGIS expression and makes it available for use in other parts of the model." );
60}
61
62QgsCalculateExpressionAlgorithm *QgsCalculateExpressionAlgorithm::createInstance() const
63{
64 return new QgsCalculateExpressionAlgorithm();
65}
66
67void QgsCalculateExpressionAlgorithm::initAlgorithm( const QVariantMap & )
68{
69 // possibly this should be a new dedicated parameter type for "QgsProcessingParameterVariant", as the values specified for the parameter will
70 // be whatever the model calculates as the result of the expression. But this works for now...
71 auto inputParameter = std::make_unique<QgsProcessingParameterString>( QStringLiteral( "INPUT" ), QObject::tr( "Input" ), QVariant(), false, false );
72 // we limit the available sources for this parameter to just precalculated expressions -- otherwise it's confusing if we allow users
73 // to enter a literal value for this parameter, as they could enter an expression in there and expect it to be evaluated.
74 inputParameter->setMetadata(
75 { QVariantMap( { { QStringLiteral( "model_widget" ), QVariantMap( { { QStringLiteral( "accepted_sources" ), QVariantList { static_cast<int>( Qgis::ProcessingModelChildParameterSource::Expression ) } } } ) } } )
76 }
77 );
78 addParameter( inputParameter.release() );
79
80 addOutput( new QgsProcessingOutputVariant( QStringLiteral( "OUTPUT" ), QObject::tr( "Value" ) ) );
81}
82
83QVariantMap QgsCalculateExpressionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &, QgsProcessingFeedback * )
84{
85 const QVariant res = parameters.value( QStringLiteral( "INPUT" ) );
86
87 QVariantMap outputs;
88 outputs.insert( QStringLiteral( "OUTPUT" ), res );
89 return outputs;
90}
91
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3476
@ Expression
Parameter value is taken from an expression, evaluated just before the algorithm runs.
@ HideFromToolbox
Algorithm should be hidden from the toolbox.
@ SkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Contains information about the context in which a processing algorithm is executed.
Base class for providing feedback from a processing algorithm.
A variant output for processing algorithms, capable of storing any QVariant value.