QGIS API Documentation 3.99.0-Master (09f76ad7019)
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 ) ||
55 !layerMap.contains( u"type"_s ) ||
56 !layerMap.contains( u"attributeIndex"_s ) )
57 return false;
58
59 if ( !context )
60 continue; // when called without context, we will skip checking whether the layer can be resolved
61
62 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( u"source"_s ).toString(), *context );
63 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector )
64 return false;
65
66 QgsVectorLayer *vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
67
68 if ( layerMap.value( u"attributeIndex"_s ).toInt() >= vectorLayer->fields().count() )
69 return false;
70 }
71
72 return true;
73}
74
76{
77 Q_UNUSED( context );
78 QStringList parts;
79 const QVariantList variantLayers = value.toList();
80 for ( const QVariant &variantLayer : variantLayers )
81 {
82 const QVariantMap layerMap = variantLayer.toMap();
83 QStringList layerDefParts;
84 layerDefParts << u"'source': "_s + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( u"source"_s ) );
85 layerDefParts << u"'type': "_s + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( u"type"_s ) );
86 layerDefParts << u"'attributeIndex': "_s + QgsProcessingUtils::variantToPythonLiteral( layerMap.value( u"attributeIndex"_s ) );
87 const QString layerDef = u"{%1}"_s.arg( layerDefParts.join( ',' ) );
88 parts.append( layerDef );
89 }
90 return parts.join( ',' ).prepend( '[' ).append( ']' );
91}
92
93QString QgsProcessingParameterTinInputLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
94{
96}
97
99{
101}
102
104{
105 switch ( outputType )
106 {
108 {
109 QString code = u"QgsProcessingParameterTinInputLayers('%1', %2)"_s
111 return code;
112 }
113 }
114 return QString();
115}
@ Vector
Vector layer.
Definition qgis.h:194
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.