QGIS API Documentation 3.39.0-Master (67e056379ed)
Loading...
Searching...
No Matches
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
57QgsCalculateExpressionAlgorithm *QgsCalculateExpressionAlgorithm::createInstance() const
58{
59 return new QgsCalculateExpressionAlgorithm();
60}
61
62void QgsCalculateExpressionAlgorithm::initAlgorithm( const QVariantMap & )
63{
64 // possibly this should be a new dedicated parameter type for "QgsProcessingParameterVariant", as the values specified for the parameter will
65 // be whatever the model calculates as the result of the expression. But this works for now...
66 std::unique_ptr< QgsProcessingParameterString > inputParameter = std::make_unique< QgsProcessingParameterString >( QStringLiteral( "INPUT" ), QObject::tr( "Input" ), QVariant(), false, false );
67 // we limit the available sources for this parameter to just precalculated expressions -- otherwise it's confusing if we allow users
68 // to enter a literal value for this parameter, as they could enter an expression in there and expect it to be evaluated.
69 inputParameter->setMetadata(
70 {
71 QVariantMap( {{
72 QStringLiteral( "model_widget" ),
73 QVariantMap(
74 { {
75 QStringLiteral( "accepted_sources" ), QVariantList{ static_cast< int >( Qgis::ProcessingModelChildParameterSource::Expression ) }
76 }}
77 )
78 }} )
79 } );
80 addParameter( inputParameter.release() );
81
82 addOutput( new QgsProcessingOutputVariant( QStringLiteral( "OUTPUT" ), QObject::tr( "Value" ) ) );
83}
84
85QVariantMap QgsCalculateExpressionAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &, QgsProcessingFeedback * )
86{
87 const QVariant res = parameters.value( QStringLiteral( "INPUT" ) );
88
89 QVariantMap outputs;
90 outputs.insert( QStringLiteral( "OUTPUT" ), res );
91 return outputs;
92}
93
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3076
@ 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.