QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
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
18#include "qgsvectorlayer.h"
19
23
28
30{
31 return typeName();
32}
33
35{
36 if ( input.userType() != QMetaType::Type::QVariantList )
37 return false;
38
39 const QVariantList variantLayers = input.toList();
40
41 if ( variantLayers.isEmpty() )
42 return false;
43
44 for ( const QVariant &variantLayer : variantLayers )
45 {
46 if ( variantLayer.userType() != QMetaType::Type::QVariantMap )
47 return false;
48 const QVariantMap layerMap = variantLayer.toMap();
49
50 if ( !layerMap.contains( QStringLiteral( "source" ) ) ||
51 !layerMap.contains( QStringLiteral( "type" ) ) ||
52 !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
53 return false;
54
55 if ( !context )
56 continue; // when called without context, we will skip checking whether the layer can be resolved
57
58 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "source" ) ).toString(), *context );
59 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector )
60 return false;
61
62 QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
63
64 if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
65 return false;
66 }
67
68 return true;
69}
70
72{
73 Q_UNUSED( context );
74 QStringList parts;
75 const QVariantList variantLayers = value.toList();
76 for ( const QVariant &variantLayer : variantLayers )
77 {
78 const QVariantMap layerMap = variantLayer.toMap();
79 QStringList layerDefParts;
80 layerDefParts << QStringLiteral( "'source': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "source" ) ) );
81 layerDefParts << QStringLiteral( "'type': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "type" ) ) );
82 layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( QStringLiteral( "attributeIndex" ) ) );
83 const QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
84 parts.append( layerDef );
85 }
86 return parts.join( ',' ).prepend( '[' ).append( ']' );
87}
88
89QString QgsProcessingParameterTinInputLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
90{
92}
93
98
100{
101 switch ( outputType )
102 {
104 {
105 QString code = QStringLiteral( "QgsProcessingParameterTinInputLayers('%1', %2)" )
107 return code;
108 }
109 }
110 return QString();
111}
@ Vector
Vector layer.
Definition qgis.h:191
int count
Definition qgsfields.h:50
Base class for all map layer types.
Definition qgsmaplayer.h:80
Qgis::LayerType type
Definition qgsmaplayer.h:90
Contains information about the context in which a processing algorithm is executed.
QString valueAsStringPrivate(const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags) const
Internal method for evaluating values as string.
QString description() const
Returns the description for the parameter.
QgsProcessingParameterDefinition(const QString &name, const QString &description=QString(), const QVariant &defaultValue=QVariant(), bool optional=false, const QString &help=QString())
Constructor for QgsProcessingParameterDefinition.
QString name() const
Returns the name of the parameter.
QVariant valueAsJsonObjectPrivate(const QVariant &value, QgsProcessingContext &context, ValueAsStringFlags flags) const
Internal method for evaluating values as JSON objects.
QString asPythonString(QgsProcessing::PythonOutputType outputType=QgsProcessing::PythonOutputType::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.
QVariant valueAsJsonObject(const QVariant &value, QgsProcessingContext &context) const override
Returns a version of the parameter input value, which is suitable for use in a JSON object.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
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, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
PythonOutputType
Available Python output types.
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Represents a vector layer which manages a vector based dataset.