QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsprocessingparametertininputlayers.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprocessingparametertininputlayers.cpp
3  ---------------------
4  Date : August 2020
5  Copyright : (C) 2020 by Vincent Cloarec
6  Email : vcloarec 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 #include "qgsvectorlayer.h"
18 
19 QgsProcessingParameterTinInputLayers::QgsProcessingParameterTinInputLayers( const QString &name, const QString &description ):
20  QgsProcessingParameterDefinition( name, description )
21 {}
22 
24 {
26 }
27 
29 {
30  return typeName();
31 }
32 
34 {
35  if ( input.type() != QVariant::List )
36  return false;
37 
38  const QVariantList variantLayers = input.toList();
39 
40  if ( variantLayers.isEmpty() )
41  return false;
42 
43  for ( const QVariant &variantLayer : variantLayers )
44  {
45  if ( variantLayer.type() != QVariant::Map )
46  return false;
47  const QVariantMap layerMap = variantLayer.toMap();
48 
49  if ( !layerMap.contains( QStringLiteral( "source" ) ) ||
50  !layerMap.contains( QStringLiteral( "type" ) ) ||
51  !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
52  return false;
53 
54  if ( !context )
55  continue; // when called without context, we will skip checking whether the layer can be resolved
56 
57  QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "source" ) ).toString(), *context );
58  if ( !mapLayer || mapLayer->type() != QgsMapLayerType::VectorLayer )
59  return false;
60 
61  QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
62 
63  if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
64  return false;
65  }
66 
67  return true;
68 }
69 
71 {
72  Q_UNUSED( context );
73  QStringList parts;
74  const QVariantList variantLayers = value.toList();
75  for ( const QVariant &variantLayer : variantLayers )
76  {
77  const QVariantMap layerMap = variantLayer.toMap();
78  QStringList layerDefParts;
79  layerDefParts << QStringLiteral( "'source': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "source" ) ) );
80  layerDefParts << QStringLiteral( "'type': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "type" ) ) );
81  layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "attributeIndex" ) ) );
82  QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
83  parts.append( layerDef );
84  }
85  return parts.join( ',' ).prepend( '[' ).append( ']' );
86 }
87 
89 {
90  switch ( outputType )
91  {
93  {
94  QString code = QStringLiteral( "QgsProcessingParameterTinInputLayers('%1', '%2')" ).arg( name(), description() );
95  return code;
96  }
97  }
98  return QString();
99 }
QgsProcessingParameterTinInputLayers::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: qgsprocessingparametertininputlayers.cpp:70
qgsprocessingparametertininputlayers.h
QgsMapLayerType::VectorLayer
@ VectorLayer
QgsProcessingParameterDefinition::description
QString description() const
Returns the description for the parameter.
Definition: qgsprocessingparameters.h:474
QgsProcessingParameterTinInputLayers::QgsProcessingParameterTinInputLayers
QgsProcessingParameterTinInputLayers(const QString &name, const QString &description=QString())
Constructor.
Definition: qgsprocessingparametertininputlayers.cpp:19
QgsFields::count
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
QgsProcessingParameterDefinition
Base class for the definition of processing parameters.
Definition: qgsprocessingparameters.h:331
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3283
QgsProcessing::PythonQgsProcessingAlgorithmSubclass
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:60
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
QgsProcessingParameterTinInputLayers::type
QString type() const override
Unique parameter type name.
Definition: qgsprocessingparametertininputlayers.cpp:28
qgsvectorlayer.h
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsProcessingUtils::variantToPythonLiteral
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
Definition: qgsprocessingutils.cpp:521
QgsProcessingParameterTinInputLayers::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: qgsprocessingparametertininputlayers.cpp:88
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsProcessingParameterTinInputLayers::clone
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
Definition: qgsprocessingparametertininputlayers.cpp:23
QgsProcessingParameterTinInputLayers::checkValueIsAcceptable
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
Definition: qgsprocessingparametertininputlayers.cpp:33
QgsProcessingUtils::mapLayerFromString
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
Definition: qgsprocessingutils.cpp:316
QgsProcessing::PythonOutputType
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:59
QgsProcessingParameterTinInputLayers::typeName
static QString typeName()
Returns the type name for the parameter class.
Definition: qgsprocessingparametertininputlayers.h:65
QgsMapLayer::type
QgsMapLayerType type
Definition: qgsmaplayer.h:90