QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
static void setProjectVariable(QgsProject *project, const QString &name, const QVariant &value)
Sets a project context variable.
@ FlagNotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
@ FlagHideFromToolbox
Algorithm should be hidden from the toolbox.
@ FlagSkipGenericModelLogging
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.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Custom exception class for processing related exceptions.
Definition: qgsexception.h:83
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
A string parameter for processing algorithms.