QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgsprocessingparameterdxflayers.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprocessingparameterdxflayers.cpp
3 ---------------------
4 Date : September 2020
5 Copyright : (C) 2020 by Alexander Bruy
6 Email : alexander dot bruy 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
20QgsProcessingParameterDxfLayers::QgsProcessingParameterDxfLayers( const QString &name, const QString &description )
21 : QgsProcessingParameterDefinition( name, description, QVariant(), false )
22{
23}
24
26{
27 return new QgsProcessingParameterDxfLayers( *this );
28}
29
31{
32 return typeName();
33}
34
36{
37 if ( !input.isValid() )
39
40 QgsMapLayer *mapLayer = nullptr;
41 QgsVectorLayer *vectorLayer = input.value<QgsVectorLayer *>();
42 if ( vectorLayer )
43 {
44 return vectorLayer->isSpatial();
45 }
46
47 if ( input.type() == QVariant::String )
48 {
49 if ( input.toString().isEmpty() )
51
52 if ( !context )
53 return true;
54
55 mapLayer = QgsProcessingUtils::mapLayerFromString( input.toString(), *context );
56 return mapLayer && ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() );
57 }
58 else if ( input.type() == QVariant::List )
59 {
60 if ( input.toList().isEmpty() )
62
63 const QVariantList layerList = input.toList();
64 for ( const QVariant &variantLayer : layerList )
65 {
66 vectorLayer = input.value<QgsVectorLayer *>();
67 if ( vectorLayer )
68 {
69 if ( vectorLayer->isSpatial() )
70 continue;
71 else
72 return false;
73 }
74
75 if ( variantLayer.type() == QVariant::String )
76 {
77 if ( !context )
78 return true;
79
80 mapLayer = QgsProcessingUtils::mapLayerFromString( variantLayer.toString(), *context );
81 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector || !mapLayer->isSpatial() )
82 return false;
83 }
84 else if ( variantLayer.type() == QVariant::Map )
85 {
86 const QVariantMap layerMap = variantLayer.toMap();
87
88 if ( !layerMap.contains( QStringLiteral( "layer" ) ) && !layerMap.contains( QStringLiteral( "attributeIndex" ) ) )
89 return false;
90
91 if ( !context )
92 return true;
93
94 mapLayer = QgsProcessingUtils::mapLayerFromString( layerMap.value( QStringLiteral( "layer" ) ).toString(), *context );
95 if ( !mapLayer || mapLayer->type() != Qgis::LayerType::Vector || !mapLayer->isSpatial() )
96 return false;
97
98 vectorLayer = static_cast<QgsVectorLayer *>( mapLayer );
99
100 if ( !vectorLayer )
101 return false;
102
103 if ( layerMap.value( QStringLiteral( "attributeIndex" ) ).toInt() >= vectorLayer->fields().count() )
104 return false;
105 }
106 else
107 {
108 return false;
109 }
110 }
111 return true;
112 }
113 else if ( input.type() == QVariant::StringList )
114 {
115 const auto constToStringList = input.toStringList();
116 if ( constToStringList.isEmpty() )
118
119 if ( !context )
120 return true;
121
122 for ( const QString &v : constToStringList )
123 {
124 mapLayer = QgsProcessingUtils::mapLayerFromString( v, *context );
125 if ( !mapLayer )
126 return false;
127
128 if ( mapLayer->type() == Qgis::LayerType::Vector && mapLayer->isSpatial() )
129 continue;
130 else
131 return false;
132 }
133 return true;
134 }
135
136 return false;
137}
138
140{
141 QStringList parts;
142 const QList<QgsDxfExport::DxfLayer> layers = parameterAsLayers( value, context );
143 for ( const QgsDxfExport::DxfLayer &layer : layers )
144 {
145 QStringList layerDefParts;
146 layerDefParts << QStringLiteral( "'layer': " ) + QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer.layer()->source() ) );
147 if ( layer.layerOutputAttributeIndex() >= -1 )
148 layerDefParts << QStringLiteral( "'attributeIndex': " ) + QgsProcessingUtils::variantToPythonLiteral( layer.layerOutputAttributeIndex() );
149
150 const QString layerDef = QStringLiteral( "{%1}" ).arg( layerDefParts.join( ',' ) );
151 parts << layerDef;
152 }
153 return parts.join( ',' ).prepend( '[' ).append( ']' );
154}
155
157{
158 switch ( outputType )
159 {
161 {
162 QString code = QStringLiteral( "QgsProcessingParameterDxfLayers('%1', %2)" )
164 return code;
165 }
166 }
167 return QString();
168}
169
170QString QgsProcessingParameterDxfLayers::valueAsString( const QVariant &value, QgsProcessingContext &context, bool &ok ) const
171{
173}
174
175QVariant QgsProcessingParameterDxfLayers::valueAsJsonObject( const QVariant &value, QgsProcessingContext &context ) const
176{
178}
179
180QList<QgsDxfExport::DxfLayer> QgsProcessingParameterDxfLayers::parameterAsLayers( const QVariant &layersVariant, QgsProcessingContext &context )
181{
182 QList<QgsDxfExport::DxfLayer> layers;
183
184 if ( QgsVectorLayer *layer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layersVariant ) ) )
185 {
186 layers << QgsDxfExport::DxfLayer( layer );
187 }
188
189 if ( layersVariant.type() == QVariant::String )
190 {
191 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layersVariant.toString(), context );
192 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
193 }
194 else if ( layersVariant.type() == QVariant::List )
195 {
196 const QVariantList layersVariantList = layersVariant.toList();
197 for ( const QVariant &layerItem : layersVariantList )
198 {
199 if ( layerItem.type() == QVariant::Map )
200 {
201 const QVariantMap layerVariantMap = layerItem.toMap();
202 layers << variantMapAsLayer( layerVariantMap, context );
203 }
204 else if ( layerItem.type() == QVariant::String )
205 {
206 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem.toString(), context );
207 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
208 }
209 }
210 }
211 else if ( layersVariant.type() == QVariant::StringList )
212 {
213 const auto layersStringList = layersVariant.toStringList();
214 for ( const QString &layerItem : layersStringList )
215 {
216 QgsMapLayer *mapLayer = QgsProcessingUtils::mapLayerFromString( layerItem, context );
217 layers << QgsDxfExport::DxfLayer( static_cast<QgsVectorLayer *>( mapLayer ) );
218 }
219 }
220
221 return layers;
222}
223
225{
226 const QVariant layerVariant = layerVariantMap[ QStringLiteral( "layer" ) ];
227
228 QgsVectorLayer *inputLayer = nullptr;
229 if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( qvariant_cast<QObject *>( layerVariant ) ) ) )
230 {
231 // good
232 }
233 else if ( ( inputLayer = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( layerVariant.toString(), context ) ) ) )
234 {
235 // good
236 }
237 else
238 {
239 // bad
240 }
241
242 QgsDxfExport::DxfLayer dxfLayer( inputLayer, layerVariantMap[ QStringLiteral( "attributeIndex" ) ].toInt() );
243 return dxfLayer;
244}
245
247{
248 QVariantMap vm;
249 if ( !layer.layer() )
250 return vm;
251
252 vm[ QStringLiteral( "layer" )] = layer.layer()->id();
253 vm[ QStringLiteral( "attributeIndex" ) ] = layer.layerOutputAttributeIndex();
254 return vm;
255}
@ Vector
Vector layer.
@ Optional
Parameter is optional.
int count() const
Returns number of items.
Definition: qgsfields.cpp:133
Base class for all map layer types.
Definition: qgsmaplayer.h:75
virtual bool isSpatial() const
Returns true if the layer is considered a spatial layer, ie it has some form of geometry associated w...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
Qgis::LayerType type
Definition: qgsmaplayer.h:82
Contains information about the context in which a processing algorithm is executed.
Base class for the definition of processing parameters.
QString valueAsStringPrivate(const QVariant &value, QgsProcessingContext &context, bool &ok, ValueAsStringFlags flags) const
Internal method for evaluating values as string.
Qgis::ProcessingParameterFlags mFlags
Parameter flags.
@ AllowMapLayerValues
Enable map layer value handling.
QString description() const
Returns the description for the parameter.
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.
static QVariantMap layerAsVariantMap(const QgsDxfExport::DxfLayer &layer)
Converts a single input layer to QVariant representation (a QVariantMap)
QgsProcessingParameterDefinition * clone() const override
Creates a clone of the parameter definition.
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...
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...
QgsProcessingParameterDxfLayers(const QString &name, const QString &description=QString())
Constructor for QgsProcessingParameterDxfLayers.
QString type() const override
Unique parameter type name.
bool checkValueIsAcceptable(const QVariant &input, QgsProcessingContext *context=nullptr) const override
Checks whether the specified input value is acceptable for the parameter.
static QgsDxfExport::DxfLayer variantMapAsLayer(const QVariantMap &layerVariantMap, QgsProcessingContext &context)
Converts a QVariant value (a QVariantMap) to a single input layer.
static QString typeName()
Returns the type name for the parameter class.
QString valueAsString(const QVariant &value, QgsProcessingContext &context, bool &ok) const override
Returns a string version of the parameter input value (if possible).
static QList< QgsDxfExport::DxfLayer > parameterAsLayers(const QVariant &layersVariant, QgsProcessingContext &context)
Converts a QVariant value (a QVariantList) to a list of input layers.
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.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static QString normalizeLayerSource(const QString &source)
Normalizes a layer source string for safe comparison across different operating system environments.
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.
Definition: qgsprocessing.h:48
@ PythonQgsProcessingAlgorithmSubclass
Full Python QgsProcessingAlgorithm subclass.
Represents a vector layer which manages a vector based data sets.
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Layers and optional attribute index to split into multiple layers using attribute value as layer name...
Definition: qgsdxfexport.h:75
QgsVectorLayer * layer() const
Returns the layer.
Definition: qgsdxfexport.h:84
int layerOutputAttributeIndex() const
Returns the attribute index used to split into multiple layers.
Definition: qgsdxfexport.h:91