QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
20#include <QString>
21
22using namespace Qt::StringLiterals;
23
27
32
34{
35 return typeName();
36}
37
39{
40 if ( input.userType() != QMetaType::Type::QVariantList )
41 return false;
42
43 const QVariantList variantLayers = input.toList();
44
45 if ( variantLayers.isEmpty() )
46 return false;
47
48 for ( const QVariant &variantLayer : variantLayers )
49 {
50 if ( variantLayer.userType() != QMetaType::Type::QVariantMap )
51 return false;
52 const QVariantMap layerMap = variantLayer.toMap();
53
54 if ( !layerMap.contains( u"source"_s ) || !layerMap.contains( u"type"_s ) || !layerMap.contains( u"attributeIndex"_s ) )
55 return false;
56
57 if ( !context )
58 continue; // when called without context, we will skip checking whether the layer can be resolved
59
60 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( u"source"_s ).toString(), *context );
61 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector )
62 return false;
63
64 QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
65
66 if ( layerMap.value( u"attributeIndex"_s ).toInt() >= vectorLayer->fields().count() )
67 return false;
68 }
69
70 return true;
71}
72
74{
75 Q_UNUSED( context );
76 QStringList parts;
77 const QVariantList variantLayers = value.toList();
78 for ( const QVariant &variantLayer : variantLayers )
79 {
80 const QVariantMap layerMap = variantLayer.toMap();
81 QStringList layerDefParts;
82 layerDefParts << u"'source': "_s + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( u"source"_s ) );
83 layerDefParts << u"'type': "_s + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( u"type"_s ) );
84 layerDefParts << u"'attributeIndex': "_s + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( u"attributeIndex"_s ) );
85 const QString layerDef = u"{%1}"_s.arg( layerDefParts.join( ',' ) );
86 parts.append( layerDef );
87 }
88 return parts.join( ',' ).prepend( '[' ).append( ']' );
89}
90
91QString QgsProcessingParameterTinInputLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
92{
94}
95
100
102{
103 switch ( outputType )
104 {
106 {
107 QString code = u"QgsProcessingParameterTinInputLayers('%1', %2)"_s.arg( name(), QgsProcessingUtils::stringToPythonLiteral( description() ) );
108 return code;
109 }
110 }
111 return QString();
112}
@ Vector
Vector layer.
Definition qgis.h:207
int count
Definition qgsfields.h:50
Base class for all map layer types.
Definition qgsmaplayer.h:83
Qgis::LayerType type
Definition qgsmaplayer.h:93
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.