QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsprocessingparameteraggregate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingparameteraggregate.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  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgsvectorlayer.h"
19 
20 
21 QgsProcessingParameterAggregate::QgsProcessingParameterAggregate( const QString &name, const QString &description, const QString &parentLayerParameterName, bool optional )
22  : QgsProcessingParameterDefinition( name, description, QVariant(), optional )
23  , mParentLayerParameterName( parentLayerParameterName )
24 {
25 }
26 
28 {
29  return new QgsProcessingParameterAggregate( *this );
30 }
31 
33 {
34  return typeName();
35 }
36 
38 {
39  if ( !input.isValid() )
40  return mFlags & FlagOptional;
41 
42  if ( input.type() != QVariant::List )
43  return false;
44 
45  const QVariantList inputList = input.toList();
46  for ( const QVariant &inputItem : inputList )
47  {
48  if ( inputItem.type() != QVariant::Map )
49  return false;
50 
51  const QVariantMap inputItemMap = inputItem.toMap();
52 
53  if ( !inputItemMap.contains( "name" ) )
54  return false;
55  if ( !inputItemMap.contains( "type" ) )
56  return false;
57  if ( !inputItemMap.contains( "input" ) )
58  return false;
59  if ( !inputItemMap.contains( "aggregate" ) )
60  return false;
61  }
62 
63  return true;
64 }
65 
67 {
69 }
70 
72 {
73  switch ( outputType )
74  {
76  {
77  QString code = QStringLiteral( "QgsProcessingParameterAggregate('%1', '%2'" ).arg( name(), description() );
78  if ( !mParentLayerParameterName.isEmpty() )
79  code += QStringLiteral( ", parentLayerParameterName=%1" ).arg( QgsProcessingUtils::stringToPythonLiteral( mParentLayerParameterName ) );
80 
81  if ( mFlags & FlagOptional )
82  code += QLatin1String( ", optional=True" );
83  code += ')';
84  return code;
85  }
86  }
87  return QString();
88 }
89 
91 {
93  map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
94  return map;
95 }
96 
98 {
100  mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
101  return true;
102 }
103 
105 {
106  QStringList depends;
107  if ( !mParentLayerParameterName.isEmpty() )
108  depends << mParentLayerParameterName;
109  return depends;
110 }
111 
113 {
114  return mParentLayerParameterName;
115 }
116 
118 {
119  mParentLayerParameterName = name;
120 }
121 
QgsProcessingParameterDefinition::toVariantMap
virtual QVariantMap toVariantMap() const
Saves this parameter to a QVariantMap.
Definition: qgsprocessingparameters.cpp:2357
QgsProcessingParameterAggregate::parentLayerParameterName
QString parentLayerParameterName() const
Returns the name of the parent layer parameter, or an empty string if this is not set.
Definition: qgsprocessingparameteraggregate.cpp:112
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:474
QgsProcessingParameterAggregate::QgsProcessingParameterAggregate
QgsProcessingParameterAggregate(const QString &name, const QString &description=QString(), const QString &parentLayerParameterName=QString(), bool optional=false)
Constructor for QgsProcessingParameterAggregate.
Definition: qgsprocessingparameteraggregate.cpp:21
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:331
QgsProcessingParameterAggregate::asPythonString
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonQgsProcessingAlgorithmSubclass) const override
Returns the parameter definition as a Python command which can be used within a Python Processing scr...
Definition: qgsprocessingparameteraggregate.cpp:71
QgsProcessingUtils::stringToPythonLiteral
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
Definition: qgsprocessingutils.cpp:611
QgsProcessingParameterAggregate::dependsOnOtherParameters
QStringList dependsOnOtherParameters() const override
Returns a list of other parameter names on which this parameter is dependent (e.g.
Definition: qgsprocessingparameteraggregate.cpp:104
QgsProcessingParameterAggregate::checkValueIsAcceptable
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
Definition: qgsprocessingparameteraggregate.cpp:37
QgsProcessingParameterAggregate::type
QString type() const override
Unique parameter type name.
Definition: qgsprocessingparameteraggregate.cpp:32
QgsProcessingParameterAggregate::clone
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Definition: qgsprocessingparameteraggregate.cpp:27
qgsprocessingparameteraggregate.h
QgsProcessing::PythonQgsProcessingAlgorithmSubclass
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:60
QgsProcessingParameterDefinition::fromVariantMap
virtual bool fromVariantMap(const QVariantMap &map)
Restores this parameter to a QVariantMap.
Definition: qgsprocessingparameters.cpp:2370
QgsProcessingParameterDefinition::mFlags
Flags mFlags
Parameter flags.
Definition: qgsprocessingparameters.h:733
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:44
QgsProcessingParameterDefinition::name
QString name() const
Returns the name of the parameter.
Definition: qgsprocessingparameters.h:460
QgsProcessingParameterAggregate::fromVariantMap
bool fromVariantMap(const QVariantMap &map) override
Restores this parameter to a QVariantMap.
Definition: qgsprocessingparameteraggregate.cpp:97
QgsProcessingParameterAggregate::toVariantMap
QVariantMap toVariantMap() const override
Saves this parameter to a QVariantMap.
Definition: qgsprocessingparameteraggregate.cpp:90
QgsProcessingParameterAggregate::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparameteraggregate.h:47
qgsvectorlayer.h
QgsProcessingParameterAggregate::valueAsPythonString
QString valueAsPythonString(const QVariant &value, QgsProcessingContext &context) const override
Returns a string version of the parameter input value, which is suitable for use as an input paramete...
Definition: qgsprocessingparameteraggregate.cpp:66
QgsProcessingUtils::variantToPythonLiteral
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
Definition: qgsprocessingutils.cpp:521
QgsProcessingParameterDefinition::FlagOptional
@ FlagOptional
Parameter is optional.
Definition: qgsprocessingparameters.h:425
QgsProcessing::PythonOutputType
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:59
QgsProcessingParameterAggregate::setParentLayerParameterName
void setParentLayerParameterName(const QString &name)
Sets the name of the parent layer parameter.
Definition: qgsprocessingparameteraggregate.cpp:117