QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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  const 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)" )
96  return code;
97  }
98  }
99  return QString();
100 }
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
Base class for all map layer types.
Definition: qgsmaplayer.h:73
QgsMapLayerType type
Definition: qgsmaplayer.h:80
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
QString description() const
Returns the description for the parameter.
QString name() const
Returns the name of the parameter.
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...
QString type() const override
Unique parameter type name.
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
QgsProcessingParameterTinInputLayers(const QString &name, const QString &description=QString())
Constructor.
static QString typeName()
Returns the type name for the parameter class.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
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...
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString variantToPythonLiteral(const QVariant &value)
Converts a variant to a Python literal.
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.
PythonOutputType
Available Python output types.
Definition: qgsprocessing.h:63
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Definition: qgsprocessing.h:64
Represents a vector layer which manages a vector based data sets.
QgsFields fields() const FINAL
Returns the list of fields of this layer.
@ VectorLayer
Vector layer.