QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsalgorithmsetvariable.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsalgorithmsetvariable.cpp
3  ---------------------
4  begin : June 2020
5  copyright : (C) 2020 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 
20 
22 
23 QString QgsSetProjectVariableAlgorithm::name() const
24 {
25  return QStringLiteral( "setprojectvariable" );
26 }
27 
28 QgsProcessingAlgorithm::Flags QgsSetProjectVariableAlgorithm::flags() const
29 {
33 }
34 
35 QString QgsSetProjectVariableAlgorithm::displayName() const
36 {
37  return QObject::tr( "Set project variable" );
38 }
39 
40 QStringList QgsSetProjectVariableAlgorithm::tags() const
41 {
42  return QObject::tr( "expression" ).split( ',' );
43 }
44 
45 QString QgsSetProjectVariableAlgorithm::group() const
46 {
47  return QObject::tr( "Modeler tools" );
48 }
49 
50 QString QgsSetProjectVariableAlgorithm::groupId() const
51 {
52  return QStringLiteral( "modelertools" );
53 }
54 
55 QString QgsSetProjectVariableAlgorithm::shortDescription() const
56 {
57  return QObject::tr( "Sets an expression variable for the current project" );
58 }
59 
60 QString QgsSetProjectVariableAlgorithm::shortHelpString() const
61 {
62  return QObject::tr( "This algorithm sets an expression variable for the current project." );
63 }
64 
65 QgsSetProjectVariableAlgorithm *QgsSetProjectVariableAlgorithm::createInstance() const
66 {
67  return new QgsSetProjectVariableAlgorithm();
68 }
69 
70 bool QgsSetProjectVariableAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
71 {
72  // this is all nice and quick, we can (and should) do it in the main thread without issue
73  const QString name = parameterAsString( parameters, QStringLiteral( "NAME" ), context );
74  const QString value = parameterAsString( parameters, QStringLiteral( "VALUE" ), context );
75 
76  if ( name.isEmpty() )
77  throw QgsProcessingException( QObject::tr( "Variable name cannot be empty" ) );
78 
80  feedback->pushInfo( QObject::tr( "Set variable \'%1\' to \'%2\'" ).arg( name, value ) );
81 
82  return true;
83 }
84 
85 void QgsSetProjectVariableAlgorithm::initAlgorithm( const QVariantMap & )
86 {
87  addParameter( new QgsProcessingParameterString( QStringLiteral( "NAME" ), QObject::tr( "Variable name" ) ) );
88  addParameter( new QgsProcessingParameterString( QStringLiteral( "VALUE" ), QObject::tr( "Variable value" ) ) );
89 }
90 
91 QVariantMap QgsSetProjectVariableAlgorithm::processAlgorithm( const QVariantMap &, QgsProcessingContext &, QgsProcessingFeedback * )
92 {
93  return QVariantMap();
94 }
95 
qgsexpressioncontextutils.h
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:121
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
QgsExpressionContextUtils::setProjectVariable
static void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
Definition: qgsexpressioncontextutils.cpp:302
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgsalgorithmsetvariable.h
QgsProcessingParameterString
A string parameter for processing algorithms.
Definition: qgsprocessingparameters.h:2647
QgsProcessingAlgorithm::FlagHideFromToolbox
@ FlagHideFromToolbox
Algorithm should be hidden from the toolbox.
Definition: qgsprocessingalgorithm.h:71
QgsProcessingAlgorithm::FlagSkipGenericModelLogging
@ FlagSkipGenericModelLogging
When running as part of a model, the generic algorithm setup and results logging should be skipped.
Definition: qgsprocessingalgorithm.h:82
QgsProcessingAlgorithm::FlagNotAvailableInStandaloneTool
@ FlagNotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
Definition: qgsprocessingalgorithm.h:83
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82